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