blob: 8c65768f554c80c2fce68f2e31ec495c6ff90676 [file] [log] [blame]
Harald Welte7101ca22020-12-04 21:44:44 +01001/* SPDX-License-Identifier: GPL-2.0-or-later */
2/* Integer base 2 logarithm calculation
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8#pragma once
Harald Weltec172d9f2020-12-06 15:13:09 +01009#include <stdint.h>
Harald Welte622cda32020-12-06 14:37:18 +010010
11/* from linux/asm-generic/bitops/{fls,fls64}.h - could later be enhanced
12 * with architecture specific optimized versions */
13
14/**
15 * fls - find last (most-significant) bit set
16 * @x: the word to search
17 *
18 * This is defined the same way as ffs.
19 * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32.
20 */
Harald Welte7fc88b32020-12-06 15:45:11 +010021static inline __attribute__((always_inline)) int fls(unsigned int x)
Harald Welte622cda32020-12-06 14:37:18 +010022{
23 int r = 32;
24
25 if (!x)
26 return 0;
27 if (!(x & 0xffff0000u)) {
28 x <<= 16;
29 r -= 16;
30 }
31 if (!(x & 0xff000000u)) {
32 x <<= 8;
33 r -= 8;
34 }
35 if (!(x & 0xf0000000u)) {
36 x <<= 4;
37 r -= 4;
38 }
39 if (!(x & 0xc0000000u)) {
40 x <<= 2;
41 r -= 2;
42 }
43 if (!(x & 0x80000000u)) {
44 x <<= 1;
45 r -= 1;
46 }
47 return r;
48}
49
50/**
51 * fls64 - find last set bit in a 64-bit word
52 * @x: the word to search
53 *
54 * This is defined in a similar way as the libc and compiler builtin
55 * ffsll, but returns the position of the most significant set bit.
56 *
57 * fls64(value) returns 0 if value is 0 or the position of the last
58 * set bit if value is nonzero. The last (most significant) bit is
59 * at position 64.
60 */
Harald Welte7fc88b32020-12-06 15:45:11 +010061static inline __attribute__((always_inline)) int fls64(uint64_t x)
Harald Welte622cda32020-12-06 14:37:18 +010062{
Harald Weltec172d9f2020-12-06 15:13:09 +010063 uint32_t h = x >> 32;
Harald Welte622cda32020-12-06 14:37:18 +010064 if (h)
65 return fls(h) + 32;
66 return fls(x);
67}
Harald Welte7101ca22020-12-04 21:44:44 +010068
69/*
70 * non-constant log of base 2 calculators
71 * - the arch may override these in asm/bitops.h if they can be implemented
72 * more efficiently than using fls() and fls64()
73 * - the arch is not required to handle n==0 if implementing the fallback
74 */
75#ifndef CONFIG_ARCH_HAS_ILOG2_U32
76static inline __attribute__((const))
77int __ilog2_u32(uint32_t n)
78{
79 return fls(n) - 1;
80}
81#endif
82
83#ifndef CONFIG_ARCH_HAS_ILOG2_U64
84static inline __attribute__((const))
85int __ilog2_u64(uint64_t n)
86{
87 return fls64(n) - 1;
88}
89#endif
90
91/**
92 * const_ilog2 - log base 2 of 32-bit or a 64-bit constant unsigned value
93 * @n: parameter
94 *
95 * Use this where sparse expects a true constant expression, e.g. for array
96 * indices.
97 */
98#define const_ilog2(n) \
99( \
100 __builtin_constant_p(n) ? ( \
101 (n) < 2 ? 0 : \
102 (n) & (1ULL << 63) ? 63 : \
103 (n) & (1ULL << 62) ? 62 : \
104 (n) & (1ULL << 61) ? 61 : \
105 (n) & (1ULL << 60) ? 60 : \
106 (n) & (1ULL << 59) ? 59 : \
107 (n) & (1ULL << 58) ? 58 : \
108 (n) & (1ULL << 57) ? 57 : \
109 (n) & (1ULL << 56) ? 56 : \
110 (n) & (1ULL << 55) ? 55 : \
111 (n) & (1ULL << 54) ? 54 : \
112 (n) & (1ULL << 53) ? 53 : \
113 (n) & (1ULL << 52) ? 52 : \
114 (n) & (1ULL << 51) ? 51 : \
115 (n) & (1ULL << 50) ? 50 : \
116 (n) & (1ULL << 49) ? 49 : \
117 (n) & (1ULL << 48) ? 48 : \
118 (n) & (1ULL << 47) ? 47 : \
119 (n) & (1ULL << 46) ? 46 : \
120 (n) & (1ULL << 45) ? 45 : \
121 (n) & (1ULL << 44) ? 44 : \
122 (n) & (1ULL << 43) ? 43 : \
123 (n) & (1ULL << 42) ? 42 : \
124 (n) & (1ULL << 41) ? 41 : \
125 (n) & (1ULL << 40) ? 40 : \
126 (n) & (1ULL << 39) ? 39 : \
127 (n) & (1ULL << 38) ? 38 : \
128 (n) & (1ULL << 37) ? 37 : \
129 (n) & (1ULL << 36) ? 36 : \
130 (n) & (1ULL << 35) ? 35 : \
131 (n) & (1ULL << 34) ? 34 : \
132 (n) & (1ULL << 33) ? 33 : \
133 (n) & (1ULL << 32) ? 32 : \
134 (n) & (1ULL << 31) ? 31 : \
135 (n) & (1ULL << 30) ? 30 : \
136 (n) & (1ULL << 29) ? 29 : \
137 (n) & (1ULL << 28) ? 28 : \
138 (n) & (1ULL << 27) ? 27 : \
139 (n) & (1ULL << 26) ? 26 : \
140 (n) & (1ULL << 25) ? 25 : \
141 (n) & (1ULL << 24) ? 24 : \
142 (n) & (1ULL << 23) ? 23 : \
143 (n) & (1ULL << 22) ? 22 : \
144 (n) & (1ULL << 21) ? 21 : \
145 (n) & (1ULL << 20) ? 20 : \
146 (n) & (1ULL << 19) ? 19 : \
147 (n) & (1ULL << 18) ? 18 : \
148 (n) & (1ULL << 17) ? 17 : \
149 (n) & (1ULL << 16) ? 16 : \
150 (n) & (1ULL << 15) ? 15 : \
151 (n) & (1ULL << 14) ? 14 : \
152 (n) & (1ULL << 13) ? 13 : \
153 (n) & (1ULL << 12) ? 12 : \
154 (n) & (1ULL << 11) ? 11 : \
155 (n) & (1ULL << 10) ? 10 : \
156 (n) & (1ULL << 9) ? 9 : \
157 (n) & (1ULL << 8) ? 8 : \
158 (n) & (1ULL << 7) ? 7 : \
159 (n) & (1ULL << 6) ? 6 : \
160 (n) & (1ULL << 5) ? 5 : \
161 (n) & (1ULL << 4) ? 4 : \
162 (n) & (1ULL << 3) ? 3 : \
163 (n) & (1ULL << 2) ? 2 : \
164 1) : \
165 -1)
166
167/**
168 * ilog2 - log base 2 of 32-bit or a 64-bit unsigned value
169 * @n: parameter
170 *
171 * constant-capable log of base 2 calculation
172 * - this can be used to initialise global variables from constant data, hence
173 * the massive ternary operator construction
174 *
175 * selects the appropriately-sized optimised version depending on sizeof(n)
176 */
177#define ilog2(n) \
178( \
179 __builtin_constant_p(n) ? \
180 const_ilog2(n) : \
181 (sizeof(n) <= 4) ? \
182 __ilog2_u32(n) : \
183 __ilog2_u64(n) \
184 )