1218885Sdim/* SPDX-License-Identifier: GPL-2.0-or-later */
2218885Sdim/* Integer base 2 logarithm calculation
3218885Sdim *
4218885Sdim * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
5218885Sdim * Written by David Howells (dhowells@redhat.com)
6218885Sdim */
7218885Sdim
8218885Sdim#ifndef _LINUX_LOG2_H
9218885Sdim#define _LINUX_LOG2_H
10218885Sdim
11218885Sdim#include <linux/types.h>
12218885Sdim#include <linux/bitops.h>
13218885Sdim
14218885Sdim/*
15218885Sdim * non-constant log of base 2 calculators
16218885Sdim * - the arch may override these in asm/bitops.h if they can be implemented
17218885Sdim *   more efficiently than using fls() and fls64()
18218885Sdim * - the arch is not required to handle n==0 if implementing the fallback
19218885Sdim */
20218885Sdim#ifndef CONFIG_ARCH_HAS_ILOG2_U32
21218885Sdimstatic __always_inline __attribute__((const))
22218885Sdimint __ilog2_u32(u32 n)
23218885Sdim{
24218885Sdim	return fls(n) - 1;
25218885Sdim}
26218885Sdim#endif
27218885Sdim
28218885Sdim#ifndef CONFIG_ARCH_HAS_ILOG2_U64
29218885Sdimstatic __always_inline __attribute__((const))
30218885Sdimint __ilog2_u64(u64 n)
31218885Sdim{
32218885Sdim	return fls64(n) - 1;
33218885Sdim}
34218885Sdim#endif
35218885Sdim
36218885Sdim/**
37218885Sdim * is_power_of_2() - check if a value is a power of two
38218885Sdim * @n: the value to check
39218885Sdim *
40218885Sdim * Determine whether some value is a power of two, where zero is
41218885Sdim * *not* considered a power of two.
42218885Sdim * Return: true if @n is a power of 2, otherwise false.
43218885Sdim */
44218885Sdimstatic inline __attribute__((const))
45218885Sdimbool is_power_of_2(unsigned long n)
46218885Sdim{
47218885Sdim	return (n != 0 && ((n & (n - 1)) == 0));
48218885Sdim}
49218885Sdim
50218885Sdim/**
51218885Sdim * __roundup_pow_of_two() - round up to nearest power of two
52218885Sdim * @n: value to round up
53218885Sdim */
54218885Sdimstatic inline __attribute__((const))
55218885Sdimunsigned long __roundup_pow_of_two(unsigned long n)
56218885Sdim{
57218885Sdim	return 1UL << fls_long(n - 1);
58218885Sdim}
59218885Sdim
60218885Sdim/**
61218885Sdim * __rounddown_pow_of_two() - round down to nearest power of two
62218885Sdim * @n: value to round down
63218885Sdim */
64218885Sdimstatic inline __attribute__((const))
65218885Sdimunsigned long __rounddown_pow_of_two(unsigned long n)
66218885Sdim{
67218885Sdim	return 1UL << (fls_long(n) - 1);
68218885Sdim}
69218885Sdim
70218885Sdim/**
71218885Sdim * const_ilog2 - log base 2 of 32-bit or a 64-bit constant unsigned value
72218885Sdim * @n: parameter
73218885Sdim *
74218885Sdim * Use this where sparse expects a true constant expression, e.g. for array
75218885Sdim * indices.
76218885Sdim */
77218885Sdim#define const_ilog2(n)				\
78218885Sdim(						\
79218885Sdim	__builtin_constant_p(n) ? (		\
80218885Sdim		(n) < 2 ? 0 :			\
81218885Sdim		(n) & (1ULL << 63) ? 63 :	\
82218885Sdim		(n) & (1ULL << 62) ? 62 :	\
83218885Sdim		(n) & (1ULL << 61) ? 61 :	\
84218885Sdim		(n) & (1ULL << 60) ? 60 :	\
85218885Sdim		(n) & (1ULL << 59) ? 59 :	\
86218885Sdim		(n) & (1ULL << 58) ? 58 :	\
87218885Sdim		(n) & (1ULL << 57) ? 57 :	\
88218885Sdim		(n) & (1ULL << 56) ? 56 :	\
89218885Sdim		(n) & (1ULL << 55) ? 55 :	\
90218885Sdim		(n) & (1ULL << 54) ? 54 :	\
91218885Sdim		(n) & (1ULL << 53) ? 53 :	\
92218885Sdim		(n) & (1ULL << 52) ? 52 :	\
93218885Sdim		(n) & (1ULL << 51) ? 51 :	\
94218885Sdim		(n) & (1ULL << 50) ? 50 :	\
95218885Sdim		(n) & (1ULL << 49) ? 49 :	\
96218885Sdim		(n) & (1ULL << 48) ? 48 :	\
97218885Sdim		(n) & (1ULL << 47) ? 47 :	\
98218885Sdim		(n) & (1ULL << 46) ? 46 :	\
99218885Sdim		(n) & (1ULL << 45) ? 45 :	\
100218885Sdim		(n) & (1ULL << 44) ? 44 :	\
101218885Sdim		(n) & (1ULL << 43) ? 43 :	\
102218885Sdim		(n) & (1ULL << 42) ? 42 :	\
103218885Sdim		(n) & (1ULL << 41) ? 41 :	\
104218885Sdim		(n) & (1ULL << 40) ? 40 :	\
105218885Sdim		(n) & (1ULL << 39) ? 39 :	\
106218885Sdim		(n) & (1ULL << 38) ? 38 :	\
107218885Sdim		(n) & (1ULL << 37) ? 37 :	\
108218885Sdim		(n) & (1ULL << 36) ? 36 :	\
109218885Sdim		(n) & (1ULL << 35) ? 35 :	\
110218885Sdim		(n) & (1ULL << 34) ? 34 :	\
111218885Sdim		(n) & (1ULL << 33) ? 33 :	\
112218885Sdim		(n) & (1ULL << 32) ? 32 :	\
113218885Sdim		(n) & (1ULL << 31) ? 31 :	\
114218885Sdim		(n) & (1ULL << 30) ? 30 :	\
115218885Sdim		(n) & (1ULL << 29) ? 29 :	\
116218885Sdim		(n) & (1ULL << 28) ? 28 :	\
117218885Sdim		(n) & (1ULL << 27) ? 27 :	\
118218885Sdim		(n) & (1ULL << 26) ? 26 :	\
119218885Sdim		(n) & (1ULL << 25) ? 25 :	\
120218885Sdim		(n) & (1ULL << 24) ? 24 :	\
121218885Sdim		(n) & (1ULL << 23) ? 23 :	\
122218885Sdim		(n) & (1ULL << 22) ? 22 :	\
123218885Sdim		(n) & (1ULL << 21) ? 21 :	\
124218885Sdim		(n) & (1ULL << 20) ? 20 :	\
125218885Sdim		(n) & (1ULL << 19) ? 19 :	\
126218885Sdim		(n) & (1ULL << 18) ? 18 :	\
127218885Sdim		(n) & (1ULL << 17) ? 17 :	\
128218885Sdim		(n) & (1ULL << 16) ? 16 :	\
129218885Sdim		(n) & (1ULL << 15) ? 15 :	\
130218885Sdim		(n) & (1ULL << 14) ? 14 :	\
131218885Sdim		(n) & (1ULL << 13) ? 13 :	\
132218885Sdim		(n) & (1ULL << 12) ? 12 :	\
133218885Sdim		(n) & (1ULL << 11) ? 11 :	\
134218885Sdim		(n) & (1ULL << 10) ? 10 :	\
135218885Sdim		(n) & (1ULL <<  9) ?  9 :	\
136218885Sdim		(n) & (1ULL <<  8) ?  8 :	\
137218885Sdim		(n) & (1ULL <<  7) ?  7 :	\
138218885Sdim		(n) & (1ULL <<  6) ?  6 :	\
139218885Sdim		(n) & (1ULL <<  5) ?  5 :	\
140218885Sdim		(n) & (1ULL <<  4) ?  4 :	\
141218885Sdim		(n) & (1ULL <<  3) ?  3 :	\
142218885Sdim		(n) & (1ULL <<  2) ?  2 :	\
143		1) :				\
144	-1)
145
146/**
147 * ilog2 - log base 2 of 32-bit or a 64-bit unsigned value
148 * @n: parameter
149 *
150 * constant-capable log of base 2 calculation
151 * - this can be used to initialise global variables from constant data, hence
152 * the massive ternary operator construction
153 *
154 * selects the appropriately-sized optimised version depending on sizeof(n)
155 */
156#define ilog2(n) \
157( \
158	__builtin_constant_p(n) ?	\
159	((n) < 2 ? 0 :			\
160	 63 - __builtin_clzll(n)) :	\
161	(sizeof(n) <= 4) ?		\
162	__ilog2_u32(n) :		\
163	__ilog2_u64(n)			\
164 )
165
166/**
167 * roundup_pow_of_two - round the given value up to nearest power of two
168 * @n: parameter
169 *
170 * round the given value up to the nearest power of two
171 * - the result is undefined when n == 0
172 * - this can be used to initialise global variables from constant data
173 */
174#define roundup_pow_of_two(n)			\
175(						\
176	__builtin_constant_p(n) ? (		\
177		((n) == 1) ? 1 :		\
178		(1UL << (ilog2((n) - 1) + 1))	\
179				   ) :		\
180	__roundup_pow_of_two(n)			\
181 )
182
183/**
184 * rounddown_pow_of_two - round the given value down to nearest power of two
185 * @n: parameter
186 *
187 * round the given value down to the nearest power of two
188 * - the result is undefined when n == 0
189 * - this can be used to initialise global variables from constant data
190 */
191#define rounddown_pow_of_two(n)			\
192(						\
193	__builtin_constant_p(n) ? (		\
194		(1UL << ilog2(n))) :		\
195	__rounddown_pow_of_two(n)		\
196 )
197
198static inline __attribute_const__
199int __order_base_2(unsigned long n)
200{
201	return n > 1 ? ilog2(n - 1) + 1 : 0;
202}
203
204/**
205 * order_base_2 - calculate the (rounded up) base 2 order of the argument
206 * @n: parameter
207 *
208 * The first few values calculated by this routine:
209 *  ob2(0) = 0
210 *  ob2(1) = 0
211 *  ob2(2) = 1
212 *  ob2(3) = 2
213 *  ob2(4) = 2
214 *  ob2(5) = 3
215 *  ... and so on.
216 */
217#define order_base_2(n)				\
218(						\
219	__builtin_constant_p(n) ? (		\
220		((n) == 0 || (n) == 1) ? 0 :	\
221		ilog2((n) - 1) + 1) :		\
222	__order_base_2(n)			\
223)
224
225static inline __attribute__((const))
226int __bits_per(unsigned long n)
227{
228	if (n < 2)
229		return 1;
230	if (is_power_of_2(n))
231		return order_base_2(n) + 1;
232	return order_base_2(n);
233}
234
235/**
236 * bits_per - calculate the number of bits required for the argument
237 * @n: parameter
238 *
239 * This is constant-capable and can be used for compile time
240 * initializations, e.g bitfields.
241 *
242 * The first few values calculated by this routine:
243 * bf(0) = 1
244 * bf(1) = 1
245 * bf(2) = 2
246 * bf(3) = 2
247 * bf(4) = 3
248 * ... and so on.
249 */
250#define bits_per(n)				\
251(						\
252	__builtin_constant_p(n) ? (		\
253		((n) == 0 || (n) == 1)		\
254			? 1 : ilog2(n) + 1	\
255	) :					\
256	__bits_per(n)				\
257)
258#endif /* _LINUX_LOG2_H */
259