]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/math.h
3cf6726d03848fc83675bf0ac0bc2cd58d71b705
[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 /*
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.
10  */
11 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
12
13 /**
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)
17  *
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.
20  */
21 #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
22
23 /**
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)
27  *
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.
30  */
31 #define round_down(x, y) ((x) & ~__round_mask(x, y))
32
33 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
34
35 #define DIV_ROUND_DOWN_ULL(ll, d) \
36         ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
37
38 #define DIV_ROUND_UP_ULL(ll, d) \
39         DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
40
41 #if BITS_PER_LONG == 32
42 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
43 #else
44 # define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
45 #endif
46
47 /**
48  * roundup - round up to the next specified multiple
49  * @x: the value to up
50  * @y: multiple to round up to
51  *
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().
54  */
55 #define roundup(x, y) (                                 \
56 {                                                       \
57         typeof(y) __y = y;                              \
58         (((x) + (__y - 1)) / __y) * __y;                \
59 }                                                       \
60 )
61 /**
62  * rounddown - round down to next specified multiple
63  * @x: the value to round
64  * @y: multiple to round down to
65  *
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().
68  */
69 #define rounddown(x, y) (                               \
70 {                                                       \
71         typeof(x) __x = (x);                            \
72         __x - (__x % (y));                              \
73 }                                                       \
74 )
75
76 /*
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.
81  */
82 #define DIV_ROUND_CLOSEST(x, divisor)(                  \
83 {                                                       \
84         typeof(x) __x = x;                              \
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));        \
91 }                                                       \
92 )
93 /*
94  * Same as above but for u64 dividends. divisor must be a 32-bit
95  * number.
96  */
97 #define DIV_ROUND_CLOSEST_ULL(x, divisor)(              \
98 {                                                       \
99         typeof(divisor) __d = divisor;                  \
100         unsigned long long _tmp = (x) + (__d) / 2;      \
101         do_div(_tmp, __d);                              \
102         _tmp;                                           \
103 }                                                       \
104 )
105
106 /*
107  * Multiplies an integer by a fraction, while avoiding unnecessary
108  * overflow or loss of precision.
109  */
110 #define mult_frac(x, numer, denom)(                     \
111 {                                                       \
112         typeof(x) quot = (x) / (denom);                 \
113         typeof(x) rem  = (x) % (denom);                 \
114         (quot * (numer)) + ((rem * (numer)) / (denom)); \
115 }                                                       \
116 )
117
118 #define sector_div(a, b) do_div(a, b)
119
120 /**
121  * reciprocal_scale - "scale" a value into range [0, ep_ro)
122  * @val: value
123  * @ep_ro: right open interval endpoint
124  *
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.
131  *
132  * Return: a result based on @val in interval [0, @ep_ro).
133  */
134 static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
135 {
136         return (u32)(((u64) val * ep_ro) >> 32);
137 }
138
139 u64 int_pow(u64 base, unsigned int exp);
140 unsigned long int_sqrt(unsigned long);
141
142 #if BITS_PER_LONG < 64
143 u32 int_sqrt64(u64 x);
144 #else
145 static inline u32 int_sqrt64(u64 x)
146 {
147         return (u32)int_sqrt(x);
148 }
149 #endif
150
151 #endif  /* _LINUX_MATH_H */