forked from kamailio/kamailio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_scan.h
422 lines (351 loc) · 9.73 KB
/
bit_scan.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* Copyright (C) 2007 iptelorg GmbH
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*!
* \file
* \brief Kamailio core :: bit scan operations
*
* Copyright (C) 2007 iptelorg GmbH
* \ingroup core
* Module: \ref core
*
* bit scan operations
*
* - int bit_scan_forward(unsigned long v) - returns the index of the first
* set bit (undefined value if v==0)
* - int bit_scan_forward32(unsigned int v) - returns the index of the first
* set bit (undefined value if v==0)
* - int bit_scan_forward64(long long v) - returns the index of the first
* set bit (undefined value if v==0)
* - int bit_scan_reverse(unsigned long v) - returns the index of the last
* set bit (undefined value if v==0)
* - int bit_scan_reverse32(unsigned int v) - returns the index of the last
* set bit (undefined value if v==0)
* - int bit_scan_reverse64(long long v) - returns the index of the last
* set bit (undefined value if v==0)
*
* Config defines: CC_GCC_LIKE_ASM - the compiler support gcc style
* inline asm,
* __CPU_x86, __CPU_x86_64,
* ULONG_MAX (limits.h)
*/
/*
* History:
* --------
* 2007-06-23 created by andrei
*/
#ifndef _bit_scan_h
#define _bit_scan_h
#include <limits.h>
/*! \brief fix __CPU_i386 -> __CPU_x86 */
#if defined __CPU_i386 && ! defined __CPU_x86
#define __CPU_x86
#endif
#ifdef CC_GCC_LIKE_ASM
#if defined __CPU_x86 || defined __CPU_x86_64
#define BIT_SCAN_ASM
#endif
#endif
/*! \brief set default bitscan versions, depending on the architecture
* In general the order is asm, debruijn, br, slow for bit_scan_forward
* and asm, br, slow, debruijn for bit_scan_reverse. */
#ifdef BIT_SCAN_ASM
/* have asm => use it */
#define bit_scan_forward32(i) bit_scan_forward_asm32(i)
#define bit_scan_forward64(i) bit_scan_forward_asm64(i)
#define bit_scan_reverse32(i) bit_scan_reverse_asm32(i)
#define bit_scan_reverse64(i) bit_scan_reverse_asm64(i)
#elif defined __CPU_x86 || defined __CPU_x86_64
/* no asm (e.g. no CC_GCC_LIKE_ASM) => debruijn for bit_scan_forward and
* br for bit_scan_reverse */
/* make sure debruijn an branch version are enabled */
#ifndef BIT_SCAN_DEBRUIJN
#define BIT_SCAN_DEBRUIJN
#endif
#ifndef BIT_SCAN_BRANCH
#define BIT_SCAN_BRANCH
#endif
#define bit_scan_forward32(i) bit_scan_forward_debruijn32(i)
#define bit_scan_forward64(i) bit_scan_forward_debruijn64(i)
#define bit_scan_reverse32(i) bit_scan_reverse_br32(i)
#define bit_scan_reverse64(i) bit_scan_reverse_br64(i)
#elif defined __CPU_sparc64
/* no asm yet => use branch for everything in 64 bit mode
* and debruijn + branch in 32 bit mode
* (in 64bit mode the branch method is slightly faster then debruijn,
* however note that in 32 bit mode the roles are reversed for _forward)*/
#ifndef BIT_SCAN_BRANCH
#define BIT_SCAN_BRANCH
#endif
#define bit_scan_reverse32(i) bit_scan_reverse_br32(i)
#define bit_scan_reverse64(i) bit_scan_reverse_br64(i)
#ifdef LP64
#define bit_scan_forward32(i) bit_scan_forward_br32(i)
#define bit_scan_forward64(i) bit_scan_forward_br64(i)
#else /* LP64 */
#ifndef BIT_SCAN_DEBRUIJN
#define BIT_SCAN_DEBRUIJN
#endif
#define bit_scan_forward32(i) bit_scan_forward_debruijn32(i)
#define bit_scan_forward64(i) bit_scan_forward_debruijn64(i)
#endif /* LP64 */
#else /* __CPU_XXX */
/* default - like x86 no asm */
/* make sure debruijn an branch version are enabled */
#ifndef BIT_SCAN_DEBRUIJN
#define BIT_SCAN_DEBRUIJN
#endif
#ifndef BIT_SCAN_BRANCH
#define BIT_SCAN_BRANCH
#endif
#define bit_scan_forward32(i) bit_scan_forward_debruijn32(i)
#define bit_scan_forward64(i) bit_scan_forward_debruijn64(i)
#define bit_scan_reverse32(i) bit_scan_reverse_br32(i)
#define bit_scan_reverse64(i) bit_scan_reverse_br64(i)
#endif /* __CPU_XXX */
/*! \brief try to use the right version for bit_scan_forward(unisgned long l)
*/
#if (defined (ULONG_MAX) && ULONG_MAX > 4294967295) || defined LP64
/*! \brief long is 64 bits */
#define bit_scan_forward(l) bit_scan_forward64((unsigned long long)(l))
#define bit_scan_reverse(l) bit_scan_reverse64((unsigned long long)(l))
#else
/*! \brief long is 32 bits */
#define bit_scan_forward(l) bit_scan_forward32((l))
#define bit_scan_reverse(l) bit_scan_reverse32((l))
#endif
#ifdef BIT_SCAN_DEBRUIJN
/*! \brief use a de Bruijn sequence to get the index of the set bit for a number
* of the form 2^k (DEBRUIJN_HASH32() and DEBRUIJN_HASH64()).
* bit_scan_forward & bit_scan_reverse would need first to convert
* the argument to 2^k (where k is the first set bit or last set bit index)-
* For bit_scan_forward this can be done very fast using x & (-x).
* For more info about this method see:
* http://citeseer.ist.psu.edu/leiserson98using.html
* ("Using de Bruijn Sequences to Index a 1 in a Computer Word")
*/
extern unsigned char _debruijn_hash32[32]; /* see bit_scan.c */
extern unsigned char _debruijn_hash64[64]; /* see bit_scan.c */
#define DEBRUIJN_CT32 0x04653ADFU
#define DEBRUIJN_CT64 0x0218A392CD3D5DBFULL
#define DEBRUIJN_HASH32(x)\
(((x)*DEBRUIJN_CT32)>>(sizeof(x)*8-5))
#define DEBRUIJN_HASH64(x)\
(((x)*DEBRUIJN_CT64)>>(sizeof(x)*8-6))
#define bit_scan_forward_debruijn32(x) \
( _debruijn_hash32[DEBRUIJN_HASH32((x) & (-(x)))])
#define bit_scan_forward_debruijn64(x) \
( _debruijn_hash64[DEBRUIJN_HASH64((x) & (-(x)))])
static inline int bit_scan_reverse_debruijn32(unsigned int v)
{
unsigned int last;
do{
last=v;
v=v&(v-1);
}while(v); /* => last is 2^k */
return _debruijn_hash32[DEBRUIJN_HASH32(last)];
}
static inline int bit_scan_reverse_debruijn64(unsigned long long v)
{
unsigned long long last;
do{
last=v;
v=v&(v-1);
}while(v); /* => last is 2^k */
return _debruijn_hash64[DEBRUIJN_HASH64(last)];
}
#endif /* BIT_SCAN_DEBRUIJN */
#ifdef BIT_SCAN_SLOW
/* only for reference purposes (testing the other versions against it) */
static inline int bit_scan_forward_slow32(unsigned int v)
{
int r;
for(r=0; r<(sizeof(v)*8); r++, v>>=1)
if (v&1) return r;
return 0;
}
static inline int bit_scan_reverse_slow32(unsigned int v)
{
int r;
for(r=sizeof(v)*8-1; r>0; r--, v<<=1)
if (v& (1UL<<(sizeof(v)*8-1))) return r;
return 0;
}
static inline int bit_scan_forward_slow64(unsigned long long v)
{
int r;
for(r=0; r<(sizeof(v)*8); r++, v>>=1)
if (v&1ULL) return r;
return 0;
}
static inline int bit_scan_reverse_slow64(unsigned long long v)
{
int r;
for(r=sizeof(v)*8-1; r>0; r--, v<<=1)
if (v& (1ULL<<(sizeof(v)*8-1))) return r;
return 0;
}
#endif /* BIT_SCAN_SLOW */
#ifdef BIT_SCAN_BRANCH
static inline int bit_scan_forward_br32(unsigned int v)
{
int b;
b=0;
if (v&0x01)
return 0;
if (!(v & 0xffff)){
b+=16;
v>>=16;
}
if (!(v&0xff)){
b+=8;
v>>=8;
}
if (!(v&0x0f)){
b+=4;
v>>=4;
}
if (!(v&0x03)){
b+=2;
v>>=2;
}
b+= !(v&0x01);
return b;
}
static inline int bit_scan_reverse_br32(unsigned int v)
{
int b;
b=0;
if (v & 0xffff0000){
b+=16;
v>>=16;
}
if (v&0xff00){
b+=8;
v>>=8;
}
if (v&0xf0){
b+=4;
v>>=4;
}
if (v&0x0c){
b+=2;
v>>=2;
}
b+= !!(v&0x02);
return b;
}
static inline int bit_scan_forward_br64(unsigned long long v)
{
int b;
b=0;
if (v&0x01ULL)
return 0;
if (!(v & 0xffffffffULL)){
b+=32;
v>>=32;
}
if (!(v & 0xffffULL)){
b+=16;
v>>=16;
}
if (!(v&0xffULL)){
b+=8;
v>>=8;
}
if (!(v&0x0fULL)){
b+=4;
v>>=4;
}
if (!(v&0x03ULL)){
b+=2;
v>>=2;
}
b+= !(v&0x01ULL);
return b;
}
static inline int bit_scan_reverse_br64(unsigned long long v)
{
int b;
b=0;
if (v & 0xffffffff00000000ULL){
b+=32;
v>>=32;
}
if (v & 0xffff0000ULL){
b+=16;
v>>=16;
}
if (v&0xff00ULL){
b+=8;
v>>=8;
}
if (v&0xf0ULL){
b+=4;
v>>=4;
}
if (v&0x0cULL){
b+=2;
v>>=2;
}
b+= !!(v&0x02ULL);
return b;
}
#endif /* BIT_SCAN_BRANCH */
#ifdef BIT_SCAN_ASM
#if defined __CPU_x86 || defined __CPU_x86_64
#define HAS_BIT_SCAN_ASM
static inline int bit_scan_forward_asm32(unsigned int v)
{
int r;
asm volatile(" bsfl %1, %0": "=r"(r): "rm"(v) );
return r;
}
static inline int bit_scan_reverse_asm32(unsigned int v)
{
int r;
asm volatile(" bsrl %1, %0": "=r"(r): "rm"(v) );
return r;
}
#ifdef __CPU_x86_64
static inline int bit_scan_forward_asm64(unsigned long long v)
{
long r;
asm volatile(" bsfq %1, %0": "=r"(r): "rm"(v) );
return r;
}
static inline int bit_scan_reverse_asm64(unsigned long long v)
{
long r;
asm volatile(" bsrq %1, %0": "=r"(r): "rm"(v) );
return r;
}
#else
static inline int bit_scan_forward_asm64(unsigned long long v)
{
if ((unsigned int)v)
return bit_scan_forward_asm32((unsigned int)v);
return 32+bit_scan_forward_asm32(*(((unsigned int*)(void*)&v)+1));
}
static inline int bit_scan_reverse_asm64(unsigned long long v)
{
if (v & 0xffffffff00000000ULL)
return 32+bit_scan_reverse_asm32(*(((unsigned int*)(void*)&v)+1));
return bit_scan_reverse_asm32((unsigned int)v);
}
#endif /* __CPU_x86_64 */
#endif /* __CPU_x86 || __CPU_x86_64 */
#endif /* BIT_SCAN_ASM */
#endif