Loading...
Searching...
No Matches
bitarithm.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Kaspar Schleiser <kaspar@schleiser.de>
3 * SPDX-FileCopyrightText: 2014 Freie Universität Berlin
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7#pragma once
8
19
20#include <stdint.h>
21
22#include "cpu_conf.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
37#define SETBIT(val, bit) val |= (bit)
38
48#define CLRBIT(val, bit) val &= (~(bit))
49
54#ifndef BIT0
55#define BIT0 0x00000001
56#define BIT1 0x00000002
57#define BIT2 0x00000004
58#define BIT3 0x00000008
59#define BIT4 0x00000010
60#define BIT5 0x00000020
61#define BIT6 0x00000040
62#define BIT7 0x00000080
63#define BIT8 0x00000100
64#define BIT9 0x00000200
65#endif
66#ifndef BIT10
67#define BIT10 0x00000400
68#define BIT11 0x00000800
69#define BIT12 0x00001000
70#define BIT13 0x00002000
71#define BIT14 0x00004000
72#define BIT15 0x00008000
73#endif
74#ifndef BIT16
75#define BIT16 0x00010000
76#define BIT17 0x00020000
77#define BIT18 0x00040000
78#define BIT19 0x00080000
79#define BIT20 0x00100000
80#define BIT21 0x00200000
81#define BIT22 0x00400000
82#define BIT23 0x00800000
83#define BIT24 0x01000000
84#define BIT25 0x02000000
85#define BIT26 0x04000000
86#define BIT27 0x08000000
87#define BIT28 0x10000000
88#define BIT29 0x20000000
89#define BIT30 0x40000000
90#define BIT31 0x80000000
91#endif
93
94#define ARCH_32_BIT (__INT_MAX__ == 2147483647)
95
103static inline unsigned bitarithm_msb(unsigned v);
104
112static inline unsigned bitarithm_lsb(unsigned v);
113
120unsigned bitarithm_bits_set(unsigned v);
121
128#if ARCH_32_BIT
129static inline uint8_t bitarithm_bits_set_u32(uint32_t v)
130{
131 return bitarithm_bits_set(v);
132}
133#else
134uint8_t bitarithm_bits_set_u32(uint32_t v);
135#endif
136
146
147/* implementations */
148
149static inline unsigned bitarithm_msb(unsigned v)
150{
151#if defined(BITARITHM_HAS_CLZ)
152 return 8 * sizeof(v) - __builtin_clz(v) - 1;
153#elif ARCH_32_BIT
155#else
156 unsigned r = 0;
157 while (v >>= 1) { /* unroll for more speed... */
158 ++r;
159 }
160 return r;
161#endif
162}
163
172static inline uint8_t bitarithm_clzb(uint8_t x)
173{
174#if defined(BITARITHM_HAS_CLZ)
175 /* clz operates on `unsigned int`, so `x` will be promoted to the size
176 of an `unsigned int` */
177 return __builtin_clz(x) - 8 * (sizeof(unsigned) - 1);
178#else
179 uint8_t l = 0;
180 while (!(x & 0x80)) {
181 ++l;
182 x <<= 1;
183 }
184 return l;
185#endif
186}
187
200extern const uint8_t bitarithm_MultiplyDeBruijnBitPosition[32];
201
202static inline unsigned bitarithm_lsb(unsigned v)
203#if defined(BITARITHM_LSB_BUILTIN)
204{
205 return __builtin_ffs(v) - 1;
206}
207#elif defined(BITARITHM_LSB_LOOKUP)
208{
209/* Source: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
210 /* cppcheck-suppress oppositeExpression
211 * (reason: `x & -x` is a bit twiddling idiom to extract the LSB; the
212 * check treats opposite arguments as indicator for poor copy-pasting
213 * as e.g. `x + -x` or `x & ~x` don't make sense. ) */
214 return bitarithm_MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >>
215 27];
216}
217#else
218{
219/* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious */
220 unsigned r = 0;
221
222 while ((v & 0x01) == 0) {
223 v >>= 1;
224 r++;
225 }
226
227 return r;
228}
229#endif
230
250static inline unsigned bitarithm_test_and_clear(unsigned state, uint8_t *index)
251{
252#if defined(BITARITHM_HAS_CLZ)
253 *index = 8 * sizeof(state) - __builtin_clz(state) - 1;
254 return state & ~(1 << *index);
255#elif defined(BITARITHM_LSB_LOOKUP)
256 /* Source: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
257 /* cppcheck-suppress oppositeExpression
258 * (reason: `x & -x` is a bit twiddling idiom to extract the LSB; the
259 * check treats opposite arguments as indicator for poor copy-pasting
260 * as e.g. `x + -x` or `x & ~x` don't make sense. ) */
261 uint32_t least_bit = state & -state;
262 *index = bitarithm_MultiplyDeBruijnBitPosition[(least_bit * 0x077CB531U) >> 27];
263 return state & ~least_bit;
264#else
265 while ((state & 1) == 0) {
266 *index += 1;
267 state >>= 1;
268 }
269 return state & ~1;
270#endif
271}
272
273#ifdef __cplusplus
274}
275#endif
276
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:149
static unsigned bitarithm_lsb(unsigned v)
Returns the number of the lowest '1' bit in a value.
Definition bitarithm.h:202
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:250
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:172