hmath.h 531 B

12345678910111213141516171819202122232425
  1. #ifndef HW_MATH_H_
  2. #define HW_MATH_H_
  3. #include <math.h>
  4. static inline unsigned long floor2e(unsigned long num) {
  5. unsigned long n = num;
  6. int e = 1;
  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) return 1;
  15. unsigned long n = num;
  16. int e = 1;
  17. while (n>>=1) ++e;
  18. unsigned long ret = 1;
  19. while (--e) ret<<=1;
  20. return ret == num ? ret : ret<<1;
  21. }
  22. #endif // HW_MATH_H_