-
Notifications
You must be signed in to change notification settings - Fork 3
/
barith.h
401 lines (320 loc) · 9.8 KB
/
barith.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* barith.h -- binary arithmetic */
#ifndef UTILS_BARITH_H
#define UTILS_BARITH_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <limits.h>
#include <stdint.h>
/* ----------------------------------------------------------------------- */
/* The inline arrangement here is taken from "Inline Functions In C"
* http://www.greenend.org.uk/rjk/tech/inline.html
*
* If BARITH_INLINE is defined then we'll generate inline functions,
* otherwise we'll generate ordinary functions.
*
* We generate inline functions, but we also need functions declared for when
* the inline form is not chosen by the compiler.
*/
#ifndef BARITH_INLINE
# if __GNUC__ && !__GNUC_STDC_INLINE__
# define BARITH_INLINE extern __inline__
# else
# define BARITH_INLINE __inline__
# endif
#endif
/* Test to see if we can use __builtin_popcount, __builtin_clz and
* __builtin_ctz. */
#if defined(__GNUC__) && __GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
#define BARITH_HAVE_GCC_BUILTINS
#endif
/* ----------------------------------------------------------------------- */
/* Returns the least significant set bit.
* Graphics Gems II: 'Bit Picking', p366.
*/
#define LSB(x) ((x) & (0 - (x))) /* 0-x rather than -x to avoid a warning */
/* x &= x - 1 clears the LSB */
/* Returns whether the argument is a power of 2. */
#define ISPOWER2(x) (((x) & ((x) - 1)) == 0)
/* Decode a "zero means 2^N" field. */
#define ZEROIS2N(x, n) ((((x) - 1) & ((1 << (n)) - 1)) + 1)
/* ----------------------------------------------------------------------- */
#ifndef BARITH_HAVE_GCC_BUILTINS
/* Returns the number of bits set in the argument. */
BARITH_INLINE int countbits_32(uint32_t);
BARITH_INLINE int countbits_64(uint64_t);
/* Returns the count of leading zeros of the argument. */
BARITH_INLINE int clz_32(uint32_t);
BARITH_INLINE int clz_64(uint64_t);
/* Count leading zeroes for bytes. */
BARITH_INLINE int clz8(unsigned int);
/* Returns the count of trailing zeros of the argument. */
BARITH_INLINE int ctz_32(uint32_t);
BARITH_INLINE int ctz_64(uint64_t);
#else
#define countbits_32(x) __builtin_popcount(x)
#define countbits_64(x) __builtin_popcountll(x)
#define clz_32(x) __builtin_clz(x)
#define clz_64(x) __builtin_clzll(x)
#define clz8(byte) (byte ? __builtin_clz(byte) - 24 : 8)
#define ctz_32(x) __builtin_ctz(x)
#define ctz_64(x) __builtin_ctzll(x)
#endif
/* Returns the most significant set bit. */
BARITH_INLINE uint32_t msb_32(uint32_t);
BARITH_INLINE uint64_t msb_64(uint64_t);
/* Returns the power of two less than or equal to the argument. */
BARITH_INLINE uint32_t power2le_32(uint32_t);
BARITH_INLINE uint64_t power2le_64(uint64_t);
/* Returns the power of two greater than the argument. */
BARITH_INLINE uint32_t power2gt_32(uint32_t);
BARITH_INLINE uint64_t power2gt_64(uint64_t);
/* Returns the floor log2 of the argument. */
BARITH_INLINE uint32_t floorlog2_32(uint32_t);
BARITH_INLINE uint64_t floorlog2_64(uint64_t);
/* Returns the ceiling log2 of the argument. */
BARITH_INLINE uint32_t ceillog2_32(uint32_t);
BARITH_INLINE uint64_t ceillog2_64(uint64_t);
/* Returns the argument reversed bitwise. */
BARITH_INLINE uint32_t reversebits_32(uint32_t);
BARITH_INLINE uint64_t reversebits_64(uint64_t);
/* ----------------------------------------------------------------------- */
/* Spread the most significant set bit downwards so it fills all lower bits.
*/
#define SPREADMSB_32(x) \
x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16
#define SPREADMSB_64(x) \
x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x |= x >> 32
#ifndef BARITH_HAVE_GCC_BUILTINS
/* Returns the number of bits set in the argument. */
/* See http://en.wikipedia.org/wiki/Hamming_weight */
BARITH_INLINE int countbits_32(uint32_t x)
{
x -= (x >> 1) & 0x55555555;
x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
x = ((x >> 4) + x) & 0x0f0f0f0f;
x += x >> 8;
x += x >> 16;
return x & 0x3f;
}
BARITH_INLINE int countbits_64(uint32_t x)
{
x -= (x >> 1) & 0x5555555555555555;
x = ((x >> 2) & 0x3333333333333333) + (x & 0x3333333333333333);
x = ((x >> 4) + x) & 0x0f0f0f0f0f0f0f0f;
x += x >> 8;
x += x >> 16;
x += x >> 32;
return x & 0x7f;
}
#endif
#ifndef BARITH_HAVE_GCC_BUILTINS
/* Returns the count of leading zeros of the argument. */
/* See http://en.wikipedia.org/wiki/Find_first_set */
BARITH_INLINE int clz_32(uint32_t x)
{
#ifdef UNUSED_ALTERNATIVE
SPREADMSB_32(x);
return 32 - countbits(x);
#else
/* Source:
*
* From: Richard Woodruff <richardw@microware.com>
* Newsgroups: comp.sys.arm
* Subject: Re: Finding highest/lowest set bit
* Date: 28 Oct 2000 01:01:26 GMT
*/
static const unsigned char tab[32] =
{
0, 31, 9, 30, 3, 8, 18, 29,
2, 5, 7, 14, 12, 17, 22, 28,
1, 10, 4, 19, 6, 15, 13, 23,
11, 20, 16, 24, 21, 25, 26, 27
};
if (x == 0)
return 32;
SPREADMSB_32(x);
return tab[(0x07dcd629 * (x + 1)) >> 27];
#endif
}
BARITH_INLINE int clz_64(uint64_t x)
{
SPREADMSB_64(x);
return 64 - countbits(x);
}
#endif
#ifndef BARITH_HAVE_GCC_BUILTINS
/* Count leading zeroes for bytes. */
BARITH_INLINE int clz8(unsigned int byte)
{
static const unsigned char counts[32] =
{
8, 7, 6, 6, 5, 5, 5, 5,
4, 4, 4, 4, 4, 4, 4, 4,
3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3,
};
if (byte & 0x80) return 0;
if (byte & 0x40) return 1;
if (byte & 0x20) return 2;
return counts[byte];
}
#endif
#ifndef BARITH_HAVE_GCC_BUILTINS
/* Returns the count of trailing zeros of the argument. */
BARITH_INLINE int ctz_32(uint32_t x)
{
#ifdef UNUSED_ALTERNATIVE
return countbits(LSB(x) - 1);
#else
static const unsigned char tab[32] =
{
31, 0, 22, 1, 28, 23, 13, 2,
29, 26, 24, 17, 19, 14, 9, 3,
30, 21, 27, 12, 25, 16, 18, 8,
20, 11, 15, 7, 10, 6, 5, 4
};
x = LSB(x);
if (x == 0)
return 32;
return tab[(0x0fb9ac52 * x) >> 27];
#endif
}
BARITH_INLINE int ctz_64(uint64_t x)
{
return countbits(LSB(x) - 1);
}
#endif
/* Returns the most significant set bit. */
BARITH_INLINE uint32_t msb_32(uint32_t x)
{
SPREADMSB_32(x);
return x & ~(x >> 1);
}
BARITH_INLINE uint64_t msb_64(uint64_t x)
{
SPREADMSB_64(x);
return x & ~(x >> 1);
}
/* Returns the power of two less than or equal to the argument. */
BARITH_INLINE uint32_t power2le_32(uint32_t x)
{
SPREADMSB_32(x);
return (x >> 1) + 1;
}
BARITH_INLINE uint64_t power2le_64(uint64_t x)
{
SPREADMSB_64(x);
return (x >> 1) + 1;
}
/* Returns the power of two greater than the argument. */
BARITH_INLINE uint32_t power2gt_32(uint32_t x)
{
SPREADMSB_32(x);
return x + 1;
}
BARITH_INLINE uint64_t power2gt_64(uint64_t x)
{
SPREADMSB_64(x);
return x + 1;
}
/* Returns the floor log2 of the argument. */
BARITH_INLINE uint32_t floorlog2_32(uint32_t x)
{
SPREADMSB_32(x);
return countbits_32(x >> 1);
}
BARITH_INLINE uint64_t floorlog2_64(uint64_t x)
{
SPREADMSB_64(x);
return countbits_64(x >> 1);
}
/* Returns the ceiling log2 of the argument. */
BARITH_INLINE uint32_t ceillog2_32(uint32_t x)
{
uint32_t y;
y = !ISPOWER2(x); /* 1 if x is not a power of two, 0 otherwise */
SPREADMSB_32(x);
return countbits_32(x >> 1) + y;
}
BARITH_INLINE uint64_t ceillog2_64(uint64_t x)
{
uint64_t y;
y = !ISPOWER2(x); /* 1 if x is not a power of two, 0 otherwise */
SPREADMSB_64(x);
return countbits_64(x >> 1) + y;
}
/* Returns the argument reversed bitwise. */
BARITH_INLINE uint32_t reversebits_32(uint32_t x)
{
uint32_t y;
y = 0x55555555; x = ((x >> 1) & y) | ((x & y) << 1);
y = 0x33333333; x = ((x >> 2) & y) | ((x & y) << 2);
y = 0x0f0f0f0f; x = ((x >> 4) & y) | ((x & y) << 4);
y = 0x00ff00ff; x = ((x >> 8) & y) | ((x & y) << 8);
return (x >> 16) | (x << 16);
}
BARITH_INLINE uint64_t reversebits_64(uint64_t x)
{
uint64_t y;
y = 0x5555555555555555; x = ((x >> 1) & y) | ((x & y) << 1);
y = 0x3333333333333333; x = ((x >> 2) & y) | ((x & y) << 2);
y = 0x0f0f0f0f0f0f0f0f; x = ((x >> 4) & y) | ((x & y) << 4);
y = 0x00ff00ff00ff00ff; x = ((x >> 8) & y) | ((x & y) << 8);
y = 0x0000ffff0000ffff; x = ((x >> 16) & y) | ((x & y) << 16);
return (x >> 32) | (x << 32);
}
/* ----------------------------------------------------------------------- */
/* */
#if UINT_MAX == 4294967295U
#define countbits countbits_32
#define clz clz_32
#define ctz ctz_32
#define msb msb_32
#define power2le power2le_32
#define power2gt power2gt_32
#define floorlog2 floorlog2_32
#define ceillog2 ceillog2_32
#define reversebits reversebits_32
#elif UINT_MAX == 18446744073709551615ULL
#define countbits countbits_64
#define clz clz_64
#define ctz ctz_64
#define msb msb_64
#define power2le power2le_64
#define power2gt power2gt_64
#define floorlog2 floorlog2_64
#define ceillog2 ceillog2_64
#define reversebits reversebits_64
#else
#error Unable to determine size of int.
#endif
#if SIZE_MAX == 4294967295U
#define countbits_size_t countbits_32
#define clz_size_t clz_32
#define ctz_size_t ctz_32
#define msb_size_t msb_32
#define power2le_size_t power2le_32
#define power2gt_size_t power2gt_32
#define floorlog2_size_t floorlog2_32
#define ceillog2_size_t ceillog2_32
#define reversebits_size_t reversebits_32
#elif SIZE_MAX == 18446744073709551615ULL
#define countbits_size_t countbits_64
#define clz_size_t clz_64
#define ctz_size_t ctz_64
#define msb_size_t msb_64
#define power2le_size_t power2le_64
#define power2gt_size_t power2gt_64
#define floorlog2_size_t floorlog2_64
#define ceillog2_size_t ceillog2_64
#define reversebits_size_t reversebits_64
#else
#error Unable to determine size of size_t.
#endif
// TODO: Add variants for other types here.
#ifdef __cplusplus
}
#endif
#endif /* UTILS_BARITH_H */