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 
10 #pragma once
11 
23 #include <stdint.h>
24 
25 #include "cpu_conf.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
40 #define SETBIT(val, bit) val |= (bit)
41 
51 #define CLRBIT(val, bit) val &= (~(bit))
52 
57 #ifndef BIT0
58 #define BIT0 0x00000001
59 #define BIT1 0x00000002
60 #define BIT2 0x00000004
61 #define BIT3 0x00000008
62 #define BIT4 0x00000010
63 #define BIT5 0x00000020
64 #define BIT6 0x00000040
65 #define BIT7 0x00000080
66 #define BIT8 0x00000100
67 #define BIT9 0x00000200
68 #endif
69 #ifndef BIT10
70 #define BIT10 0x00000400
71 #define BIT11 0x00000800
72 #define BIT12 0x00001000
73 #define BIT13 0x00002000
74 #define BIT14 0x00004000
75 #define BIT15 0x00008000
76 #endif
77 #ifndef BIT16
78 #define BIT16 0x00010000
79 #define BIT17 0x00020000
80 #define BIT18 0x00040000
81 #define BIT19 0x00080000
82 #define BIT20 0x00100000
83 #define BIT21 0x00200000
84 #define BIT22 0x00400000
85 #define BIT23 0x00800000
86 #define BIT24 0x01000000
87 #define BIT25 0x02000000
88 #define BIT26 0x04000000
89 #define BIT27 0x08000000
90 #define BIT28 0x10000000
91 #define BIT29 0x20000000
92 #define BIT30 0x40000000
93 #define BIT31 0x80000000
94 #endif
97 #define ARCH_32_BIT (__INT_MAX__ == 2147483647)
106 static inline unsigned bitarithm_msb(unsigned v);
107 
115 static inline unsigned bitarithm_lsb(unsigned v);
116 
123 unsigned bitarithm_bits_set(unsigned v);
124 
131 #if ARCH_32_BIT
132 static inline uint8_t bitarithm_bits_set_u32(uint32_t v)
133 {
134  return bitarithm_bits_set(v);
135 }
136 #else
137 uint8_t bitarithm_bits_set_u32(uint32_t v);
138 #endif
139 
148 unsigned bitarith_msb_32bit_no_native_clz(unsigned v);
149 
150 /* implementations */
151 
152 static inline unsigned bitarithm_msb(unsigned v)
153 {
154 #if defined(BITARITHM_HAS_CLZ)
155  return 8 * sizeof(v) - __builtin_clz(v) - 1;
156 #elif ARCH_32_BIT
158 #else
159  unsigned r = 0;
160  while (v >>= 1) { /* unroll for more speed... */
161  ++r;
162  }
163  return r;
164 #endif
165 }
166 
175 static inline uint8_t bitarithm_clzb(uint8_t x)
176 {
177 #if defined(BITARITHM_HAS_CLZ)
178  /* clz operates on `unsigned int`, so `x` will be promoted to the size
179  of an `unsigned int` */
180  return __builtin_clz(x) - 8 * (sizeof(unsigned) - 1);
181 #else
182  uint8_t l = 0;
183  while (!(x & 0x80)) {
184  ++l;
185  x <<= 1;
186  }
187  return l;
188 #endif
189 }
190 
203 extern const uint8_t bitarithm_MultiplyDeBruijnBitPosition[32];
204 
205 static inline unsigned bitarithm_lsb(unsigned v)
206 #if defined(BITARITHM_LSB_BUILTIN)
207 {
208  return __builtin_ffs(v) - 1;
209 }
210 #elif defined(BITARITHM_LSB_LOOKUP)
211 {
212 /* Source: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
213  /* cppcheck-suppress oppositeExpression
214  * (reason: `x & -x` is a bit twiddling idiom to extract the LSB; the
215  * check treats opposite arguments as indicator for poor copy-pasting
216  * as e.g. `x + -x` or `x & ~x` don't make sense. ) */
217  return bitarithm_MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >>
218  27];
219 }
220 #else
221 {
222 /* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious */
223  unsigned r = 0;
224 
225  while ((v & 0x01) == 0) {
226  v >>= 1;
227  r++;
228  }
229 
230  return r;
231 }
232 #endif
233 
253 static inline unsigned bitarithm_test_and_clear(unsigned state, uint8_t *index)
254 {
255 #if defined(BITARITHM_HAS_CLZ)
256  *index = 8 * sizeof(state) - __builtin_clz(state) - 1;
257  return state & ~(1 << *index);
258 #elif defined(BITARITHM_LSB_LOOKUP)
259  /* Source: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
260  /* cppcheck-suppress oppositeExpression
261  * (reason: `x & -x` is a bit twiddling idiom to extract the LSB; the
262  * check treats opposite arguments as indicator for poor copy-pasting
263  * as e.g. `x + -x` or `x & ~x` don't make sense. ) */
264  uint32_t least_bit = state & -state;
265  *index = bitarithm_MultiplyDeBruijnBitPosition[(least_bit * 0x077CB531U) >> 27];
266  return state & ~least_bit;
267 #else
268  while ((state & 1) == 0) {
269  *index += 1;
270  state >>= 1;
271  }
272  return state & ~1;
273 #endif
274 }
275 
276 #ifdef __cplusplus
277 }
278 #endif
279 
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:152
static unsigned bitarithm_lsb(unsigned v)
Returns the number of the lowest '1' bit in a value.
Definition: bitarithm.h:205
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:253
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:175