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
|
/* $NetBSD: bitops.h,v 1.15 2021/09/12 15:22:05 rillig Exp $ */
/*-
* Copyright (c) 2007, 2010 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Christos Zoulas and Joerg Sonnenberger.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _SYS_BITOPS_H_
#define _SYS_BITOPS_H_
#include <sys/stdint.h>
/*
* Find First Set functions
*/
#ifndef ffs32
static __inline int __unused
ffs32(uint32_t _n)
{
int _v;
if (!_n)
return 0;
_v = 1;
if ((_n & 0x0000FFFFU) == 0) {
_n >>= 16;
_v += 16;
}
if ((_n & 0x000000FFU) == 0) {
_n >>= 8;
_v += 8;
}
if ((_n & 0x0000000FU) == 0) {
_n >>= 4;
_v += 4;
}
if ((_n & 0x00000003U) == 0) {
_n >>= 2;
_v += 2;
}
if ((_n & 0x00000001U) == 0) {
_n >>= 1;
_v += 1;
}
return _v;
}
#endif
#ifndef ffs64
static __inline int __unused
ffs64(uint64_t _n)
{
int _v;
if (!_n)
return 0;
_v = 1;
if ((_n & 0x00000000FFFFFFFFULL) == 0) {
_n >>= 32;
_v += 32;
}
if ((_n & 0x000000000000FFFFULL) == 0) {
_n >>= 16;
_v += 16;
}
if ((_n & 0x00000000000000FFULL) == 0) {
_n >>= 8;
_v += 8;
}
if ((_n & 0x000000000000000FULL) == 0) {
_n >>= 4;
_v += 4;
}
if ((_n & 0x0000000000000003ULL) == 0) {
_n >>= 2;
_v += 2;
}
if ((_n & 0x0000000000000001ULL) == 0) {
_n >>= 1;
_v += 1;
}
return _v;
}
#endif
/*
* Find Last Set functions
*/
#ifndef fls32
static __inline int __unused
fls32(uint32_t _n)
{
int _v;
if (!_n)
return 0;
_v = 32;
if ((_n & 0xFFFF0000U) == 0) {
_n <<= 16;
_v -= 16;
}
if ((_n & 0xFF000000U) == 0) {
_n <<= 8;
_v -= 8;
}
if ((_n & 0xF0000000U) == 0) {
_n <<= 4;
_v -= 4;
}
if ((_n & 0xC0000000U) == 0) {
_n <<= 2;
_v -= 2;
}
if ((_n & 0x80000000U) == 0) {
_n <<= 1;
_v -= 1;
}
return _v;
}
#endif
#ifndef fls64
static __inline int __unused
fls64(uint64_t _n)
{
int _v;
if (!_n)
return 0;
_v = 64;
if ((_n & 0xFFFFFFFF00000000ULL) == 0) {
_n <<= 32;
_v -= 32;
}
if ((_n & 0xFFFF000000000000ULL) == 0) {
_n <<= 16;
_v -= 16;
}
if ((_n & 0xFF00000000000000ULL) == 0) {
_n <<= 8;
_v -= 8;
}
if ((_n & 0xF000000000000000ULL) == 0) {
_n <<= 4;
_v -= 4;
}
if ((_n & 0xC000000000000000ULL) == 0) {
_n <<= 2;
_v -= 2;
}
if ((_n & 0x8000000000000000ULL) == 0) {
_n <<= 1;
_v -= 1;
}
return _v;
}
#endif
/*
* Integer logarithm, returns -1 on error. Inspired by the linux
* version written by David Howells.
*/
#define _ilog2_helper(_n, _x) ((_n) & (1ULL << (_x))) ? _x :
#define _ilog2_const(_n) ( \
_ilog2_helper(_n, 63) \
_ilog2_helper(_n, 62) \
_ilog2_helper(_n, 61) \
_ilog2_helper(_n, 60) \
_ilog2_helper(_n, 59) \
_ilog2_helper(_n, 58) \
_ilog2_helper(_n, 57) \
_ilog2_helper(_n, 56) \
_ilog2_helper(_n, 55) \
_ilog2_helper(_n, 54) \
_ilog2_helper(_n, 53) \
_ilog2_helper(_n, 52) \
_ilog2_helper(_n, 51) \
_ilog2_helper(_n, 50) \
_ilog2_helper(_n, 49) \
_ilog2_helper(_n, 48) \
_ilog2_helper(_n, 47) \
_ilog2_helper(_n, 46) \
_ilog2_helper(_n, 45) \
_ilog2_helper(_n, 44) \
_ilog2_helper(_n, 43) \
_ilog2_helper(_n, 42) \
_ilog2_helper(_n, 41) \
_ilog2_helper(_n, 40) \
_ilog2_helper(_n, 39) \
_ilog2_helper(_n, 38) \
_ilog2_helper(_n, 37) \
_ilog2_helper(_n, 36) \
_ilog2_helper(_n, 35) \
_ilog2_helper(_n, 34) \
_ilog2_helper(_n, 33) \
_ilog2_helper(_n, 32) \
_ilog2_helper(_n, 31) \
_ilog2_helper(_n, 30) \
_ilog2_helper(_n, 29) \
_ilog2_helper(_n, 28) \
_ilog2_helper(_n, 27) \
_ilog2_helper(_n, 26) \
_ilog2_helper(_n, 25) \
_ilog2_helper(_n, 24) \
_ilog2_helper(_n, 23) \
_ilog2_helper(_n, 22) \
_ilog2_helper(_n, 21) \
_ilog2_helper(_n, 20) \
_ilog2_helper(_n, 19) \
_ilog2_helper(_n, 18) \
_ilog2_helper(_n, 17) \
_ilog2_helper(_n, 16) \
_ilog2_helper(_n, 15) \
_ilog2_helper(_n, 14) \
_ilog2_helper(_n, 13) \
_ilog2_helper(_n, 12) \
_ilog2_helper(_n, 11) \
_ilog2_helper(_n, 10) \
_ilog2_helper(_n, 9) \
_ilog2_helper(_n, 8) \
_ilog2_helper(_n, 7) \
_ilog2_helper(_n, 6) \
_ilog2_helper(_n, 5) \
_ilog2_helper(_n, 4) \
_ilog2_helper(_n, 3) \
_ilog2_helper(_n, 2) \
_ilog2_helper(_n, 1) \
_ilog2_helper(_n, 0) \
-1)
#define ilog2(_n) \
( \
__builtin_constant_p(_n) ? _ilog2_const(_n) : \
((sizeof(_n) > 4 ? fls64(_n) : fls32(_n)) - 1) \
)
static __inline void
fast_divide32_prepare(uint32_t _div, uint32_t * __restrict _m,
uint8_t *__restrict _s1, uint8_t *__restrict _s2)
{
uint64_t _mt;
int _l;
_l = fls32(_div - 1);
_mt = (uint64_t)(0x100000000ULL * ((1ULL << _l) - _div));
*_m = (uint32_t)(_mt / _div + 1);
*_s1 = (_l > 1) ? 1U : (uint8_t)_l;
*_s2 = (_l == 0) ? 0 : (uint8_t)(_l - 1);
}
/* ARGSUSED */
static __inline uint32_t
fast_divide32(uint32_t _v, uint32_t _div __unused, uint32_t _m, uint8_t _s1,
uint8_t _s2)
{
uint32_t _t;
_t = (uint32_t)(((uint64_t)_v * _m) >> 32);
return (_t + ((_v - _t) >> _s1)) >> _s2;
}
static __inline uint32_t
fast_remainder32(uint32_t _v, uint32_t _div, uint32_t _m, uint8_t _s1,
uint8_t _s2)
{
return _v - _div * fast_divide32(_v, _div, _m, _s1, _s2);
}
#define __BITMAP_TYPE(__s, __t, __n) struct __s { \
__t _b[__BITMAP_SIZE(__t, __n)]; \
}
#define __BITMAP_BITS(__t) (sizeof(__t) * NBBY)
#define __BITMAP_SHIFT(__t) (ilog2(__BITMAP_BITS(__t)))
#define __BITMAP_MASK(__t) (__BITMAP_BITS(__t) - 1)
#define __BITMAP_SIZE(__t, __n) \
(((__n) + (__BITMAP_BITS(__t) - 1)) / __BITMAP_BITS(__t))
#define __BITMAP_BIT(__n, __v) \
((__typeof__((__v)->_b[0]))1 << ((__n) & __BITMAP_MASK(*(__v)->_b)))
#define __BITMAP_WORD(__n, __v) \
((__n) >> __BITMAP_SHIFT(*(__v)->_b))
#define __BITMAP_SET(__n, __v) \
((__v)->_b[__BITMAP_WORD(__n, __v)] |= __BITMAP_BIT(__n, __v))
#define __BITMAP_CLR(__n, __v) \
((__v)->_b[__BITMAP_WORD(__n, __v)] &= ~__BITMAP_BIT(__n, __v))
#define __BITMAP_ISSET(__n, __v) \
((__v)->_b[__BITMAP_WORD(__n, __v)] & __BITMAP_BIT(__n, __v))
#if __GNUC_PREREQ__(2, 95)
#define __BITMAP_ZERO(__v) \
(void)__builtin_memset((__v), 0, sizeof(*__v))
#else
#define __BITMAP_ZERO(__v) do { \
size_t __i; \
for (__i = 0; __i < __arraycount((__v)->_b); __i++) \
(__v)->_b[__i] = 0; \
} while (/* CONSTCOND */ 0)
#endif /* GCC 2.95 */
#endif /* _SYS_BITOPS_H_ */
|