Math.h 886 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifndef COREMATH_H
  2. #define COREMATH_H
  3. #include <cmath>
  4. #ifndef M_PI
  5. #define M_PI 3.14159265358979323846
  6. #endif
  7. #ifndef _GNU_SOURCE
  8. void sincosf(float a, float* s, float* c);
  9. #endif
  10. namespace Math {
  11. template<typename T>
  12. T interpolate(const T& a, const T& b, float f) {
  13. return a * (1.0f - f) + b * f;
  14. }
  15. template<typename T>
  16. int popCount(const T& t) {
  17. static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3,
  18. 1, 2, 2, 3, 2, 3, 3, 4};
  19. int sum = 0;
  20. for(int i = 0; i < static_cast<int>(sizeof(T) * 8); i += 4) {
  21. sum += map[(t >> i) & 0xF];
  22. }
  23. return sum;
  24. }
  25. constexpr int roundUpLog2(int i) {
  26. if(i <= 0) {
  27. return 0;
  28. }
  29. int c = 1;
  30. while(((i - 1) >> c) > 0) {
  31. c++;
  32. }
  33. return c;
  34. }
  35. }
  36. #endif