hmath.h 680 B

123456789101112131415161718192021222324252627282930313233
  1. #ifndef HV_MATH_H_
  2. #define HV_MATH_H_
  3. /*
  4. * @功能:此头文件补充了一些数学工具函数
  5. *
  6. */
  7. #include <math.h>
  8. // 向下取2的指数倍, floor2e(3) = 2
  9. static inline unsigned long floor2e(unsigned long num) {
  10. unsigned long n = num;
  11. int e = 0;
  12. while (n>>=1) ++e;
  13. unsigned long ret = 1;
  14. while (e--) ret<<=1;
  15. return ret;
  16. }
  17. // 向上取2的指数倍, floor2e(3) = 4
  18. static inline unsigned long ceil2e(unsigned long num) {
  19. // 2**0 = 1
  20. if (num == 0 || num == 1) return 1;
  21. unsigned long n = num - 1;
  22. int e = 1;
  23. while (n>>=1) ++e;
  24. unsigned long ret = 1;
  25. while (e--) ret<<=1;
  26. return ret;
  27. }
  28. #endif // HV_MATH_H_