1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MATH_H
3 #define _LINUX_MATH_H
4
5 #include <asm/div64.h>
6 #include <uapi/linux/kernel.h>
7
8 /*
9 * This looks more complex than it should be. But we need to
10 * get the type for the ~ right in round_down (it needs to be
11 * as wide as the result!), and we want to evaluate the macro
12 * arguments just once each.
13 */
14 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
15
16 /**
17 * round_up - round up to next specified power of 2
18 * @x: the value to round
19 * @y: multiple to round up to (must be a power of 2)
20 *
21 * Rounds @x up to next multiple of @y (which must be a power of 2).
22 * To perform arbitrary rounding up, use roundup() below.
23 */
24 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
25
26 /**
27 * round_down - round down to next specified power of 2
28 * @x: the value to round
29 * @y: multiple to round down to (must be a power of 2)
30 *
31 * Rounds @x down to next multiple of @y (which must be a power of 2).
32 * To perform arbitrary rounding down, use rounddown() below.
33 */
34 #define round_down(x, y) ((x) & ~__round_mask(x, y))
35
36 #define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
37
38 #define DIV_ROUND_DOWN_ULL(ll, d) \
39 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
40
41 #define DIV_ROUND_UP_ULL(ll, d) \
42 DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
43
44 #if BITS_PER_LONG == 32
45 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
46 #else
47 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
48 #endif
49
50 /**
51 * roundup - round up to the next specified multiple
52 * @x: the value to up
53 * @y: multiple to round up to
54 *
55 * Rounds @x up to next multiple of @y. If @y will always be a power
56 * of 2, consider using the faster round_up().
57 */
58 #define roundup(x, y) ( \
59 { \
60 typeof(y) __y = y; \
61 (((x) + (__y - 1)) / __y) * __y; \
62 } \
63 )
64 /**
65 * rounddown - round down to next specified multiple
66 * @x: the value to round
67 * @y: multiple to round down to
68 *
69 * Rounds @x down to next multiple of @y. If @y will always be a power
70 * of 2, consider using the faster round_down().
71 */
72 #define rounddown(x, y) ( \
73 { \
74 typeof(x) __x = (x); \
75 __x - (__x % (y)); \
76 } \
77 )
78
79 /*
80 * Divide positive or negative dividend by positive or negative divisor
81 * and round to closest integer. Result is undefined for negative
82 * divisors if the dividend variable type is unsigned and for negative
83 * dividends if the divisor variable type is unsigned.
84 */
85 #define DIV_ROUND_CLOSEST(x, divisor)( \
86 { \
87 typeof(x) __x = x; \
88 typeof(divisor) __d = divisor; \
89 (((typeof(x))-1) > 0 || \
90 ((typeof(divisor))-1) > 0 || \
91 (((__x) > 0) == ((__d) > 0))) ? \
92 (((__x) + ((__d) / 2)) / (__d)) : \
93 (((__x) - ((__d) / 2)) / (__d)); \
94 } \
95 )
96 /*
97 * Same as above but for u64 dividends. divisor must be a 32-bit
98 * number.
99 */
100 #define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
101 { \
102 typeof(divisor) __d = divisor; \
103 unsigned long long _tmp = (x) + (__d) / 2; \
104 do_div(_tmp, __d); \
105 _tmp; \
106 } \
107 )
108
109 /*
110 * Multiplies an integer by a fraction, while avoiding unnecessary
111 * overflow or loss of precision.
112 */
113 #define mult_frac(x, numer, denom)( \
114 { \
115 typeof(x) quot = (x) / (denom); \
116 typeof(x) rem = (x) % (denom); \
117 (quot * (numer)) + ((rem * (numer)) / (denom)); \
118 } \
119 )
120
121 #define sector_div(a, b) do_div(a, b)
122
123 /**
124 * abs - return absolute value of an argument
125 * @x: the value. If it is unsigned type, it is converted to signed type first.
126 * char is treated as if it was signed (regardless of whether it really is)
127 * but the macro's return type is preserved as char.
128 *
129 * Return: an absolute value of x.
130 */
131 #define abs(x) __abs_choose_expr(x, long long, \
132 __abs_choose_expr(x, long, \
133 __abs_choose_expr(x, int, \
134 __abs_choose_expr(x, short, \
135 __abs_choose_expr(x, char, \
136 __builtin_choose_expr( \
137 __builtin_types_compatible_p(typeof(x), char), \
138 (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
139 ((void)0)))))))
140
141 #define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
142 __builtin_types_compatible_p(typeof(x), signed type) || \
143 __builtin_types_compatible_p(typeof(x), unsigned type), \
144 ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
145
146 /**
147 * reciprocal_scale - "scale" a value into range [0, ep_ro)
148 * @val: value
149 * @ep_ro: right open interval endpoint
150 *
151 * Perform a "reciprocal multiplication" in order to "scale" a value into
152 * range [0, @ep_ro), where the upper interval endpoint is right-open.
153 * This is useful, e.g. for accessing a index of an array containing
154 * @ep_ro elements, for example. Think of it as sort of modulus, only that
155 * the result isn't that of modulo. ;) Note that if initial input is a
156 * small value, then result will return 0.
157 *
158 * Return: a result based on @val in interval [0, @ep_ro).
159 */
reciprocal_scale(u32 val,u32 ep_ro)160 static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
161 {
162 return (u32)(((u64) val * ep_ro) >> 32);
163 }
164
165 u64 int_pow(u64 base, unsigned int exp);
166 unsigned long int_sqrt(unsigned long);
167
168 #if BITS_PER_LONG < 64
169 u32 int_sqrt64(u64 x);
170 #else
int_sqrt64(u64 x)171 static inline u32 int_sqrt64(u64 x)
172 {
173 return (u32)int_sqrt(x);
174 }
175 #endif
176
177 #endif /* _LINUX_MATH_H */
178