Utils.h 696 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, 1, 2, 2, 3, 2, 3, 3, 4};
  11. int sum = 0;
  12. for(int i = 0; i < static_cast<int>(sizeof(T) * 8); i += 4) {
  13. sum += map[(t >> i) & 0xF];
  14. }
  15. return sum;
  16. }
  17. constexpr int roundUpLog2(int i) {
  18. if(i <= 0) {
  19. return 0;
  20. }
  21. int c = 1;
  22. while(((i - 1) >> c) > 0) {
  23. c++;
  24. }
  25. return c;
  26. }
  27. }
  28. #endif