Utils.h 735 B

123456789101112131415161718192021222324252627282930313233
  1. #ifndef UTILS_H
  2. #define UTILS_H
  3. namespace Utils {
  4. template<typename T>
  5. T interpolate(const T& a, const T& b, float f) {
  6. return a * (1.0f - f) + b * f;
  7. }
  8. template<typename T>
  9. int popCount(const T& t) {
  10. static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3,
  11. 1, 2, 2, 3, 2, 3, 3, 4};
  12. int sum = 0;
  13. for(int i = 0; i < static_cast<int>(sizeof(T) * 8); i += 4) {
  14. sum += map[(t >> i) & 0xF];
  15. }
  16. return sum;
  17. }
  18. constexpr int roundUpLog2(int i) {
  19. if(i <= 0) {
  20. return 0;
  21. }
  22. int c = 1;
  23. while(((i - 1) >> c) > 0) {
  24. c++;
  25. }
  26. return c;
  27. }
  28. }
  29. #endif