libosmocore  1.9.0.142-9cce.202401252026
Osmocom core library
log2.h
Go to the documentation of this file.
1 /* 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
9 #include <stdint.h>
10 
11 /* from linux/asm-generic/bitops/{fls,fls64}.h - could later be enhanced
12  * with architecture specific optimized versions */
13 
21 static inline __attribute__((always_inline)) int fls(unsigned int x)
22 {
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 
61 static inline __attribute__((always_inline)) int fls64(uint64_t x)
62 {
63  uint32_t h = x >> 32;
64  if (h)
65  return fls(h) + 32;
66  return fls(x);
67 }
68 
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
76 static inline __attribute__((const))
77 int __ilog2_u32(uint32_t n)
78 {
79  return fls(n) - 1;
80 }
81 #endif
82 
83 #ifndef CONFIG_ARCH_HAS_ILOG2_U64
84 static inline __attribute__((const))
85 int __ilog2_u64(uint64_t n)
86 {
87  return fls64(n) - 1;
88 }
89 #endif
90 
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 
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  )
write Write running configuration to or terminal n Write configuration to the copy running config startup Copy configuration n Copy running config to n Copy running config to startup write Write running configuration to or terminal n Write to terminal n
struct gad_raw_head h
static __attribute__((always_inline)) int fls(unsigned int x)
fls - find last (most-significant) bit set @x: the word to search
Definition: log2.h:21