•All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
bitarithm.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Kaspar Schleiser <kaspar@schleiser.de>
3  * 2014 Freie Universität Berlin
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
21 #ifndef BITARITHM_H
22 #define BITARITHM_H
23 
24 #include <stdint.h>
25 
26 #include "cpu_conf.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
41 #define SETBIT(val, bit) val |= (bit)
42 
52 #define CLRBIT(val, bit) val &= (~(bit))
53 
58 #ifndef BIT0
59 #define BIT0 0x00000001
60 #define BIT1 0x00000002
61 #define BIT2 0x00000004
62 #define BIT3 0x00000008
63 #define BIT4 0x00000010
64 #define BIT5 0x00000020
65 #define BIT6 0x00000040
66 #define BIT7 0x00000080
67 #define BIT8 0x00000100
68 #define BIT9 0x00000200
69 #endif
70 #ifndef BIT10
71 #define BIT10 0x00000400
72 #define BIT11 0x00000800
73 #define BIT12 0x00001000
74 #define BIT13 0x00002000
75 #define BIT14 0x00004000
76 #define BIT15 0x00008000
77 #endif
78 #ifndef BIT16
79 #define BIT16 0x00010000
80 #define BIT17 0x00020000
81 #define BIT18 0x00040000
82 #define BIT19 0x00080000
83 #define BIT20 0x00100000
84 #define BIT21 0x00200000
85 #define BIT22 0x00400000
86 #define BIT23 0x00800000
87 #define BIT24 0x01000000
88 #define BIT25 0x02000000
89 #define BIT26 0x04000000
90 #define BIT27 0x08000000
91 #define BIT28 0x10000000
92 #define BIT29 0x20000000
93 #define BIT30 0x40000000
94 #define BIT31 0x80000000
95 #endif
98 #define ARCH_32_BIT (__INT_MAX__ == 2147483647)
107 static inline unsigned bitarithm_msb(unsigned v);
108 
116 static inline unsigned bitarithm_lsb(unsigned v);
117 
124 unsigned bitarithm_bits_set(unsigned v);
125 
132 #if ARCH_32_BIT
133 static inline uint8_t bitarithm_bits_set_u32(uint32_t v)
134 {
135  return bitarithm_bits_set(v);
136 }
137 #else
138 uint8_t bitarithm_bits_set_u32(uint32_t v);
139 #endif
140 
149 unsigned bitarith_msb_32bit_no_native_clz(unsigned v);
150 
151 /* implementations */
152 
153 static inline unsigned bitarithm_msb(unsigned v)
154 {
155 #if defined(BITARITHM_HAS_CLZ)
156  return 8 * sizeof(v) - __builtin_clz(v) - 1;
157 #elif ARCH_32_BIT
159 #else
160  unsigned r = 0;
161  while (v >>= 1) { /* unroll for more speed... */
162  ++r;
163  }
164  return r;
165 #endif
166 }
167 
176 static inline uint8_t bitarithm_clzb(uint8_t x)
177 {
178 #if defined(BITARITHM_HAS_CLZ)
179  /* clz operates on `unsigned int`, so `x` will be promoted to the size
180  of an `unsigned int` */
181  return __builtin_clz(x) - 8 * (sizeof(unsigned) - 1);
182 #else
183  uint8_t l = 0;
184  while (!(x & 0x80)) {
185  ++l;
186  x <<= 1;
187  }
188  return l;
189 #endif
190 }
191 
204 extern const uint8_t bitarithm_MultiplyDeBruijnBitPosition[32];
205 
206 static inline unsigned bitarithm_lsb(unsigned v)
207 #if defined(BITARITHM_LSB_BUILTIN)
208 {
209  return __builtin_ffs(v) - 1;
210 }
211 #elif defined(BITARITHM_LSB_LOOKUP)
212 {
213 /* Source: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
214  /* cppcheck-suppress oppositeExpression
215  * (reason: `x & -x` is a bit twiddling idiom to extract the LSB; the
216  * check treats opposite arguments as indicator for poor copy-pasting
217  * as e.g. `x + -x` or `x & ~x` don't make sense. ) */
218  return bitarithm_MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >>
219  27];
220 }
221 #else
222 {
223 /* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious */
224  unsigned r = 0;
225 
226  while ((v & 0x01) == 0) {
227  v >>= 1;
228  r++;
229  }
230 
231  return r;
232 }
233 #endif
234 
254 static inline unsigned bitarithm_test_and_clear(unsigned state, uint8_t *index)
255 {
256 #if defined(BITARITHM_HAS_CLZ)
257  *index = 8 * sizeof(state) - __builtin_clz(state) - 1;
258  return state & ~(1 << *index);
259 #elif defined(BITARITHM_LSB_LOOKUP)
260  /* Source: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
261  /* cppcheck-suppress oppositeExpression
262  * (reason: `x & -x` is a bit twiddling idiom to extract the LSB; the
263  * check treats opposite arguments as indicator for poor copy-pasting
264  * as e.g. `x + -x` or `x & ~x` don't make sense. ) */
265  uint32_t least_bit = state & -state;
266  *index = bitarithm_MultiplyDeBruijnBitPosition[(least_bit * 0x077CB531U) >> 27];
267  return state & ~least_bit;
268 #else
269  while ((state & 1) == 0) {
270  *index += 1;
271  state >>= 1;
272  }
273  return state & ~1;
274 #endif
275 }
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 
281 #endif /* BITARITHM_H */
uint8_t bitarithm_bits_set_u32(uint32_t v)
Returns the (uint32_t version) number of bits set in a value.
static unsigned bitarithm_msb(unsigned v)
Returns the number of the highest '1' bit in a value.
Definition: bitarithm.h:153
static unsigned bitarithm_lsb(unsigned v)
Returns the number of the lowest '1' bit in a value.
Definition: bitarithm.h:206
unsigned bitarithm_bits_set(unsigned v)
Returns the number of bits set in a value.
unsigned bitarith_msb_32bit_no_native_clz(unsigned v)
Returns the number of the highest '1' bit in a value.
static unsigned bitarithm_test_and_clear(unsigned state, uint8_t *index)
Used for iterating over the bits in state.
Definition: bitarithm.h:254
static uint8_t bitarithm_clzb(uint8_t x)
Returns the number of leading 0-bits in x, starting at the most significant bit position.
Definition: bitarithm.h:176