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