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 #define BIT10 0x00000400
70 #define BIT11 0x00000800
71 #define BIT12 0x00001000
72 #define BIT13 0x00002000
73 #define BIT14 0x00004000
74 #define BIT15 0x00008000
75 #endif
76 #ifndef BIT16
77 #define BIT16 0x00010000
78 #define BIT17 0x00020000
79 #define BIT18 0x00040000
80 #define BIT19 0x00080000
81 #define BIT20 0x00100000
82 #define BIT21 0x00200000
83 #define BIT22 0x00400000
84 #define BIT23 0x00800000
85 #define BIT24 0x01000000
86 #define BIT25 0x02000000
87 #define BIT26 0x04000000
88 #define BIT27 0x08000000
89 #define BIT28 0x10000000
90 #define BIT29 0x20000000
91 #define BIT30 0x40000000
92 #define BIT31 0x80000000
93 #endif
94 
96 #define ARCH_32_BIT (__INT_MAX__ == 2147483647)
105 unsigned bitarithm_msb(unsigned v);
106 
114 static inline unsigned bitarithm_lsb(unsigned v);
115 
122 unsigned bitarithm_bits_set(unsigned v);
123 
130 #if ARCH_32_BIT
131 static inline uint8_t bitarithm_bits_set_u32(uint32_t v)
132 {
133  return bitarithm_bits_set(v);
134 }
135 #else
136 uint8_t bitarithm_bits_set_u32(uint32_t v);
137 #endif
138 
139 /* implementations */
140 
141 static inline unsigned bitarithm_lsb(unsigned v)
142 #if defined(BITARITHM_LSB_BUILTIN)
143 {
144  return __builtin_ffs(v) - 1;
145 }
146 #elif defined(BITARITHM_LSB_LOOKUP)
147 {
148 /* Source: http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
149  extern const uint8_t MultiplyDeBruijnBitPosition[32];
150  return MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
151 }
152 #else
153 {
154 /* Source: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious */
155  unsigned r = 0;
156 
157  while ((v & 0x01) == 0) {
158  v >>= 1;
159  r++;
160  };
161 
162  return r;
163 }
164 #endif
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif /* BITARITHM_H */
171 
uint8_t bitarithm_bits_set_u32(uint32_t v)
Returns the (uint32_t version) number of bits set in a value.
static unsigned bitarithm_lsb(unsigned v)
Returns the number of the lowest &#39;1&#39; bit in a value.
Definition: bitarithm.h:141
unsigned bitarithm_bits_set(unsigned v)
Returns the number of bits set in a value.