math.h 764 B

12345678910111213141516171819202122232425
  1. #ifndef _TOOLS_MATH_H
  2. #define _TOOLS_MATH_H
  3. /*
  4. * This looks more complex than it should be. But we need to
  5. * get the type for the ~ right in round_down (it needs to be
  6. * as wide as the result!), and we want to evaluate the macro
  7. * arguments just once each.
  8. */
  9. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  10. #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
  11. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  12. #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
  13. #ifndef roundup
  14. #define roundup(x, y) ( \
  15. { \
  16. const typeof(y) __y = y; \
  17. (((x) + (__y - 1)) / __y) * __y; \
  18. } \
  19. )
  20. #endif
  21. #endif