]> git.sesse.net Git - bcachefs-tools-debian/blob - include/linux/log2.h
Update bcachefs sources to 9abf628c70 bcachefs: Fix a spurious error in fsck
[bcachefs-tools-debian] / include / linux / log2.h
1 /* Integer base 2 logarithm calculation
2  *
3  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  */
11
12 #ifndef _TOOLS_LINUX_LOG2_H
13 #define _TOOLS_LINUX_LOG2_H
14
15 #include <limits.h>
16 #ifndef PAGE_SHIFT
17 #define PAGE_SHIFT ilog2(PAGE_SIZE)
18 #endif
19
20 #include <linux/bitops.h>
21 #include <linux/compiler.h>
22
23 /*
24  * deal with unrepresentable constant logarithms
25  */
26 extern __attribute__((const, noreturn))
27 int ____ilog2_NaN(void);
28
29 /*
30  * non-constant log of base 2 calculators
31  * - the arch may override these in asm/bitops.h if they can be implemented
32  *   more efficiently than using fls() and fls64()
33  * - the arch is not required to handle n==0 if implementing the fallback
34  */
35 static inline __attribute__((const))
36 int __ilog2_u32(u32 n)
37 {
38         return fls(n) - 1;
39 }
40
41 static inline __attribute__((const))
42 int __ilog2_u64(u64 n)
43 {
44         return fls64(n) - 1;
45 }
46
47 /*
48  *  Determine whether some value is a power of two, where zero is
49  * *not* considered a power of two.
50  */
51
52 static inline __attribute__((const))
53 bool is_power_of_2(unsigned long n)
54 {
55         return (n != 0 && ((n & (n - 1)) == 0));
56 }
57
58 /*
59  * round up to nearest power of two
60  */
61 static inline __attribute__((const))
62 unsigned long __roundup_pow_of_two(unsigned long n)
63 {
64         return 1UL << fls_long(n - 1);
65 }
66
67 /*
68  * round down to nearest power of two
69  */
70 static inline __attribute__((const))
71 unsigned long __rounddown_pow_of_two(unsigned long n)
72 {
73         return 1UL << (fls_long(n) - 1);
74 }
75
76 /**
77  * ilog2 - log of base 2 of 32-bit or a 64-bit unsigned value
78  * @n - parameter
79  *
80  * constant-capable log of base 2 calculation
81  * - this can be used to initialise global variables from constant data, hence
82  *   the massive ternary operator construction
83  *
84  * selects the appropriately-sized optimised version depending on sizeof(n)
85  */
86 #define ilog2(n)                                \
87 (                                               \
88         __builtin_constant_p(n) ? (             \
89                 (n) < 1 ? ____ilog2_NaN() :     \
90                 (n) & (1ULL << 63) ? 63 :       \
91                 (n) & (1ULL << 62) ? 62 :       \
92                 (n) & (1ULL << 61) ? 61 :       \
93                 (n) & (1ULL << 60) ? 60 :       \
94                 (n) & (1ULL << 59) ? 59 :       \
95                 (n) & (1ULL << 58) ? 58 :       \
96                 (n) & (1ULL << 57) ? 57 :       \
97                 (n) & (1ULL << 56) ? 56 :       \
98                 (n) & (1ULL << 55) ? 55 :       \
99                 (n) & (1ULL << 54) ? 54 :       \
100                 (n) & (1ULL << 53) ? 53 :       \
101                 (n) & (1ULL << 52) ? 52 :       \
102                 (n) & (1ULL << 51) ? 51 :       \
103                 (n) & (1ULL << 50) ? 50 :       \
104                 (n) & (1ULL << 49) ? 49 :       \
105                 (n) & (1ULL << 48) ? 48 :       \
106                 (n) & (1ULL << 47) ? 47 :       \
107                 (n) & (1ULL << 46) ? 46 :       \
108                 (n) & (1ULL << 45) ? 45 :       \
109                 (n) & (1ULL << 44) ? 44 :       \
110                 (n) & (1ULL << 43) ? 43 :       \
111                 (n) & (1ULL << 42) ? 42 :       \
112                 (n) & (1ULL << 41) ? 41 :       \
113                 (n) & (1ULL << 40) ? 40 :       \
114                 (n) & (1ULL << 39) ? 39 :       \
115                 (n) & (1ULL << 38) ? 38 :       \
116                 (n) & (1ULL << 37) ? 37 :       \
117                 (n) & (1ULL << 36) ? 36 :       \
118                 (n) & (1ULL << 35) ? 35 :       \
119                 (n) & (1ULL << 34) ? 34 :       \
120                 (n) & (1ULL << 33) ? 33 :       \
121                 (n) & (1ULL << 32) ? 32 :       \
122                 (n) & (1ULL << 31) ? 31 :       \
123                 (n) & (1ULL << 30) ? 30 :       \
124                 (n) & (1ULL << 29) ? 29 :       \
125                 (n) & (1ULL << 28) ? 28 :       \
126                 (n) & (1ULL << 27) ? 27 :       \
127                 (n) & (1ULL << 26) ? 26 :       \
128                 (n) & (1ULL << 25) ? 25 :       \
129                 (n) & (1ULL << 24) ? 24 :       \
130                 (n) & (1ULL << 23) ? 23 :       \
131                 (n) & (1ULL << 22) ? 22 :       \
132                 (n) & (1ULL << 21) ? 21 :       \
133                 (n) & (1ULL << 20) ? 20 :       \
134                 (n) & (1ULL << 19) ? 19 :       \
135                 (n) & (1ULL << 18) ? 18 :       \
136                 (n) & (1ULL << 17) ? 17 :       \
137                 (n) & (1ULL << 16) ? 16 :       \
138                 (n) & (1ULL << 15) ? 15 :       \
139                 (n) & (1ULL << 14) ? 14 :       \
140                 (n) & (1ULL << 13) ? 13 :       \
141                 (n) & (1ULL << 12) ? 12 :       \
142                 (n) & (1ULL << 11) ? 11 :       \
143                 (n) & (1ULL << 10) ? 10 :       \
144                 (n) & (1ULL <<  9) ?  9 :       \
145                 (n) & (1ULL <<  8) ?  8 :       \
146                 (n) & (1ULL <<  7) ?  7 :       \
147                 (n) & (1ULL <<  6) ?  6 :       \
148                 (n) & (1ULL <<  5) ?  5 :       \
149                 (n) & (1ULL <<  4) ?  4 :       \
150                 (n) & (1ULL <<  3) ?  3 :       \
151                 (n) & (1ULL <<  2) ?  2 :       \
152                 (n) & (1ULL <<  1) ?  1 :       \
153                 (n) & (1ULL <<  0) ?  0 :       \
154                 ____ilog2_NaN()                 \
155                                    ) :          \
156         (sizeof(n) <= 4) ?                      \
157         __ilog2_u32(n) :                        \
158         __ilog2_u64(n)                          \
159  )
160
161 /**
162  * roundup_pow_of_two - round the given value up to nearest power of two
163  * @n - parameter
164  *
165  * round the given value up to the nearest power of two
166  * - the result is undefined when n == 0
167  * - this can be used to initialise global variables from constant data
168  */
169 #define roundup_pow_of_two(n)                   \
170 (                                               \
171         __builtin_constant_p(n) ? (             \
172                 (n == 1) ? 1 :                  \
173                 (1UL << (ilog2((n) - 1) + 1))   \
174                                    ) :          \
175         __roundup_pow_of_two(n)                 \
176  )
177
178 /**
179  * rounddown_pow_of_two - round the given value down to nearest power of two
180  * @n - parameter
181  *
182  * round the given value down to the nearest power of two
183  * - the result is undefined when n == 0
184  * - this can be used to initialise global variables from constant data
185  */
186 #define rounddown_pow_of_two(n)                 \
187 (                                               \
188         __builtin_constant_p(n) ? (             \
189                 (1UL << ilog2(n))) :            \
190         __rounddown_pow_of_two(n)               \
191  )
192
193 static inline __attribute__((const))
194 int __get_order(unsigned long size)
195 {
196         int order;
197
198         size--;
199         size >>= PAGE_SHIFT;
200 #if BITS_PER_LONG == 32
201         order = fls(size);
202 #else
203         order = fls64(size);
204 #endif
205         return order;
206 }
207
208 #define get_order(n)                                            \
209 (                                                               \
210         __builtin_constant_p(n) ? (                             \
211                 ((n) == 0UL) ? BITS_PER_LONG - PAGE_SHIFT :     \
212                 (((n) < (1UL << PAGE_SHIFT)) ? 0 :              \
213                  ilog2((n) - 1) - PAGE_SHIFT + 1)               \
214         ) :                                                     \
215         __get_order(n)                                          \
216 )
217
218 #endif /* _TOOLS_LINUX_LOG2_H */