1 /* SPDX-License-Identifier: GPL-2.0 */
6 * This looks more complex than it should be. But we need to
7 * get the type for the ~ right in round_down (it needs to be
8 * as wide as the result!), and we want to evaluate the macro
9 * arguments just once each.
11 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
14 * round_up - round up to next specified power of 2
15 * @x: the value to round
16 * @y: multiple to round up to (must be a power of 2)
18 * Rounds @x up to next multiple of @y (which must be a power of 2).
19 * To perform arbitrary rounding up, use roundup() below.
21 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
24 * round_down - round down to next specified power of 2
25 * @x: the value to round
26 * @y: multiple to round down to (must be a power of 2)
28 * Rounds @x down to next multiple of @y (which must be a power of 2).
29 * To perform arbitrary rounding down, use rounddown() below.
31 #define round_down(x, y) ((x) & ~__round_mask(x, y))
33 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
35 #define DIV_ROUND_DOWN_ULL(ll, d) \
36 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
38 #define DIV_ROUND_UP_ULL(ll, d) \
39 DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
41 #if BITS_PER_LONG == 32
42 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
44 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
48 * roundup - round up to the next specified multiple
50 * @y: multiple to round up to
52 * Rounds @x up to next multiple of @y. If @y will always be a power
53 * of 2, consider using the faster round_up().
55 #define roundup(x, y) ( \
58 (((x) + (__y - 1)) / __y) * __y; \
62 * rounddown - round down to next specified multiple
63 * @x: the value to round
64 * @y: multiple to round down to
66 * Rounds @x down to next multiple of @y. If @y will always be a power
67 * of 2, consider using the faster round_down().
69 #define rounddown(x, y) ( \
71 typeof(x) __x = (x); \
77 * Divide positive or negative dividend by positive or negative divisor
78 * and round to closest integer. Result is undefined for negative
79 * divisors if the dividend variable type is unsigned and for negative
80 * dividends if the divisor variable type is unsigned.
82 #define DIV_ROUND_CLOSEST(x, divisor)( \
85 typeof(divisor) __d = divisor; \
86 (((typeof(x))-1) > 0 || \
87 ((typeof(divisor))-1) > 0 || \
88 (((__x) > 0) == ((__d) > 0))) ? \
89 (((__x) + ((__d) / 2)) / (__d)) : \
90 (((__x) - ((__d) / 2)) / (__d)); \
94 * Same as above but for u64 dividends. divisor must be a 32-bit
97 #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
99 typeof(divisor) __d = divisor; \
100 unsigned long long _tmp = (x) + (__d) / 2; \
107 * Multiplies an integer by a fraction, while avoiding unnecessary
108 * overflow or loss of precision.
110 #define mult_frac(x, numer, denom)( \
112 typeof(x) quot = (x) / (denom); \
113 typeof(x) rem = (x) % (denom); \
114 (quot * (numer)) + ((rem * (numer)) / (denom)); \
118 #define sector_div(a, b) do_div(a, b)
121 * reciprocal_scale - "scale" a value into range [0, ep_ro)
123 * @ep_ro: right open interval endpoint
125 * Perform a "reciprocal multiplication" in order to "scale" a value into
126 * range [0, @ep_ro), where the upper interval endpoint is right-open.
127 * This is useful, e.g. for accessing a index of an array containing
128 * @ep_ro elements, for example. Think of it as sort of modulus, only that
129 * the result isn't that of modulo. ;) Note that if initial input is a
130 * small value, then result will return 0.
132 * Return: a result based on @val in interval [0, @ep_ro).
134 static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
136 return (u32)(((u64) val * ep_ro) >> 32);
139 u64 int_pow(u64 base, unsigned int exp);
140 unsigned long int_sqrt(unsigned long);
142 #if BITS_PER_LONG < 64
143 u32 int_sqrt64(u64 x);
145 static inline u32 int_sqrt64(u64 x)
147 return (u32)int_sqrt(x);
151 #endif /* _LINUX_MATH_H */