math.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include "../types.h"
  3. #include "div64.h"
  4. /*
  5. * This looks more complex than it should be. But we need to
  6. * get the type for the ~ right in round_down (it needs to be
  7. * as wide as the result!), and we want to evaluate the macro
  8. * arguments just once each.
  9. */
  10. #define __round_mask(x, y) ((__typeof__(x))((y)-1))
  11. /**
  12. * round_up - round up to next specified power of 2
  13. * @x: the value to round
  14. * @y: multiple to round up to (must be a power of 2)
  15. *
  16. * Rounds @x up to next multiple of @y (which must be a power of 2).
  17. * To perform arbitrary rounding up, use roundup() below.
  18. */
  19. #define round_up(x, y) ((((x)-1) | __round_mask(x, y)) + 1)
  20. /**
  21. * round_down - round down to next specified power of 2
  22. * @x: the value to round
  23. * @y: multiple to round down to (must be a power of 2)
  24. *
  25. * Rounds @x down to next multiple of @y (which must be a power of 2).
  26. * To perform arbitrary rounding down, use rounddown() below.
  27. */
  28. #define round_down(x, y) ((x) & ~__round_mask(x, y))
  29. #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
  30. #define DIV_ROUND_DOWN_ULL(ll, d) \
  31. ({ \
  32. unsigned long long _tmp = (ll); \
  33. do_div(_tmp, d); \
  34. _tmp; \
  35. })
  36. #define DIV_ROUND_UP_ULL(ll, d) \
  37. DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d)-1, (d))
  38. #if BITS_PER_LONG == 32
  39. #define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP_ULL(ll, d)
  40. #else
  41. #define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP(ll, d)
  42. #endif
  43. /**
  44. * roundup - round up to the next specified multiple
  45. * @x: the value to up
  46. * @y: multiple to round up to
  47. *
  48. * Rounds @x up to next multiple of @y. If @y will always be a power
  49. * of 2, consider using the faster round_up().
  50. */
  51. #define roundup(x, y) \
  52. ({ \
  53. typeof(y) __y = y; \
  54. (((x) + (__y - 1)) / __y) * __y; \
  55. })
  56. /**
  57. * rounddown - round down to next specified multiple
  58. * @x: the value to round
  59. * @y: multiple to round down to
  60. *
  61. * Rounds @x down to next multiple of @y. If @y will always be a power
  62. * of 2, consider using the faster round_down().
  63. */
  64. #define rounddown(x, y) \
  65. ({ \
  66. typeof(x) __x = (x); \
  67. __x - (__x % (y)); \
  68. })
  69. /*
  70. * Divide positive or negative dividend by positive or negative divisor
  71. * and round to closest integer. Result is undefined for negative
  72. * divisors if the dividend variable type is unsigned and for negative
  73. * dividends if the divisor variable type is unsigned.
  74. */
  75. #define DIV_ROUND_CLOSEST(x, divisor) \
  76. ({ \
  77. typeof(x) __x = x; \
  78. typeof(divisor) __d = divisor; \
  79. (((typeof(x))-1) > 0 || ((typeof(divisor))-1) > 0 || \
  80. (((__x) > 0) == ((__d) > 0))) ? \
  81. (((__x) + ((__d) / 2)) / (__d)) : \
  82. (((__x) - ((__d) / 2)) / (__d)); \
  83. })
  84. /*
  85. * Same as above but for u64 dividends. divisor must be a 32-bit
  86. * number.
  87. */
  88. #define DIV_ROUND_CLOSEST_ULL(x, divisor) \
  89. ({ \
  90. typeof(divisor) __d = divisor; \
  91. unsigned long long _tmp = (x) + (__d) / 2; \
  92. do_div(_tmp, __d); \
  93. _tmp; \
  94. })