hmath.h 525 B

12345678910111213141516171819202122232425
  1. #ifndef HV_MATH_H_
  2. #define HV_MATH_H_
  3. #include <math.h>
  4. static inline unsigned long floor2e(unsigned long num) {
  5. unsigned long n = num;
  6. int e = 0;
  7. while (n>>=1) ++e;
  8. unsigned long ret = 1;
  9. while (e--) ret<<=1;
  10. return ret;
  11. }
  12. static inline unsigned long ceil2e(unsigned long num) {
  13. // 2**0 = 1
  14. if (num == 0 || num == 1) return 1;
  15. unsigned long n = num - 1;
  16. int e = 1;
  17. while (n>>=1) ++e;
  18. unsigned long ret = 1;
  19. while (e--) ret<<=1;
  20. return ret;
  21. }
  22. #endif // HV_MATH_H_