utils.h 757 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #ifndef UTILS_H
  2. #define UTILS_H
  3. #include "fixed_types.h"
  4. #include <assert.h>
  5. #include <sstream>
  6. #include <iostream>
  7. String myDecStr(UInt64 v, UInt32 w);
  8. #define safeFDiv(x) (x ? (double) x : 1.0)
  9. // Checks if n is a power of 2.
  10. // returns true if n is power of 2
  11. bool isPower2(UInt32 n);
  12. // Computes floor(log2(n))
  13. // Works by finding position of MSB set.
  14. // returns -1 if n == 0.
  15. SInt32 floorLog2(UInt32 n);
  16. // Computes floor(log2(n))
  17. // Works by finding position of MSB set.
  18. // returns -1 if n == 0.
  19. SInt32 ceilLog2(UInt32 n);
  20. // Max and Min functions
  21. template <class T>
  22. T getMin(T v1, T v2)
  23. {
  24. return (v1 < v2) ? v1 : v2;
  25. }
  26. template <class T>
  27. T getMax(T v1, T v2)
  28. {
  29. return (v1 > v2) ? v1 : v2;
  30. }
  31. #endif
  32. UInt64 countBits(UInt64 n);