1 /* $NetBSD: bitops.h,v 1.7 2010/03/21 14:28:15 christos Exp $ */
4 * Copyright (c) 2007, 2010 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas and Joerg Sonnenberger.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #ifndef _SYS_BITOPS_H_
32 #define _SYS_BITOPS_H_
34 #include <sys/stdint.h>
37 * Find First Set functions
40 static __inline
int __unused
49 if ((_n
& 0x0000FFFFU
) == 0) {
53 if ((_n
& 0x000000FFU
) == 0) {
57 if ((_n
& 0x0000000FU
) == 0) {
61 if ((_n
& 0x00000003U
) == 0) {
65 if ((_n
& 0x00000001U
) == 0) {
74 static __inline
int __unused
83 if ((_n
& 0x00000000FFFFFFFFULL
) == 0) {
87 if ((_n
& 0x000000000000FFFFULL
) == 0) {
91 if ((_n
& 0x00000000000000FFULL
) == 0) {
95 if ((_n
& 0x000000000000000FULL
) == 0) {
99 if ((_n
& 0x0000000000000003ULL
) == 0) {
103 if ((_n
& 0x0000000000000001ULL
) == 0) {
112 * Find Last Set functions
115 static __inline
int __unused
124 if ((_n
& 0xFFFF0000U
) == 0) {
128 if ((_n
& 0xFF000000U
) == 0) {
132 if ((_n
& 0xF0000000U
) == 0) {
136 if ((_n
& 0xC0000000U
) == 0) {
140 if ((_n
& 0x80000000U
) == 0) {
149 static __inline
int __unused
158 if ((_n
& 0xFFFFFFFF00000000ULL
) == 0) {
162 if ((_n
& 0xFFFF000000000000ULL
) == 0) {
166 if ((_n
& 0xFF00000000000000ULL
) == 0) {
170 if ((_n
& 0xF000000000000000ULL
) == 0) {
174 if ((_n
& 0xC000000000000000ULL
) == 0) {
178 if ((_n
& 0x8000000000000000ULL
) == 0) {
187 * Integer logarithm, returns -1 on error. Inspired by the linux
188 * version written by David Howells.
190 #define _ilog2_helper(_n, _x) ((_n) & (1ULL << (_x))) ? _x :
193 __builtin_constant_p(_n) ? ( \
194 _ilog2_helper(_n, 63) \
195 _ilog2_helper(_n, 62) \
196 _ilog2_helper(_n, 61) \
197 _ilog2_helper(_n, 60) \
198 _ilog2_helper(_n, 59) \
199 _ilog2_helper(_n, 58) \
200 _ilog2_helper(_n, 57) \
201 _ilog2_helper(_n, 56) \
202 _ilog2_helper(_n, 55) \
203 _ilog2_helper(_n, 54) \
204 _ilog2_helper(_n, 53) \
205 _ilog2_helper(_n, 52) \
206 _ilog2_helper(_n, 51) \
207 _ilog2_helper(_n, 50) \
208 _ilog2_helper(_n, 49) \
209 _ilog2_helper(_n, 48) \
210 _ilog2_helper(_n, 47) \
211 _ilog2_helper(_n, 46) \
212 _ilog2_helper(_n, 45) \
213 _ilog2_helper(_n, 44) \
214 _ilog2_helper(_n, 43) \
215 _ilog2_helper(_n, 42) \
216 _ilog2_helper(_n, 41) \
217 _ilog2_helper(_n, 40) \
218 _ilog2_helper(_n, 39) \
219 _ilog2_helper(_n, 38) \
220 _ilog2_helper(_n, 37) \
221 _ilog2_helper(_n, 36) \
222 _ilog2_helper(_n, 35) \
223 _ilog2_helper(_n, 34) \
224 _ilog2_helper(_n, 33) \
225 _ilog2_helper(_n, 32) \
226 _ilog2_helper(_n, 31) \
227 _ilog2_helper(_n, 30) \
228 _ilog2_helper(_n, 29) \
229 _ilog2_helper(_n, 28) \
230 _ilog2_helper(_n, 27) \
231 _ilog2_helper(_n, 26) \
232 _ilog2_helper(_n, 25) \
233 _ilog2_helper(_n, 24) \
234 _ilog2_helper(_n, 23) \
235 _ilog2_helper(_n, 22) \
236 _ilog2_helper(_n, 21) \
237 _ilog2_helper(_n, 20) \
238 _ilog2_helper(_n, 19) \
239 _ilog2_helper(_n, 18) \
240 _ilog2_helper(_n, 17) \
241 _ilog2_helper(_n, 16) \
242 _ilog2_helper(_n, 15) \
243 _ilog2_helper(_n, 14) \
244 _ilog2_helper(_n, 13) \
245 _ilog2_helper(_n, 12) \
246 _ilog2_helper(_n, 11) \
247 _ilog2_helper(_n, 10) \
248 _ilog2_helper(_n, 9) \
249 _ilog2_helper(_n, 8) \
250 _ilog2_helper(_n, 7) \
251 _ilog2_helper(_n, 6) \
252 _ilog2_helper(_n, 5) \
253 _ilog2_helper(_n, 4) \
254 _ilog2_helper(_n, 3) \
255 _ilog2_helper(_n, 2) \
256 _ilog2_helper(_n, 1) \
257 _ilog2_helper(_n, 0) \
258 -1) : ((sizeof(_n) >= 4 ? fls64(_n) : fls32(_n)) - 1) \
262 fast_divide32_prepare(uint32_t _div
, uint32_t * __restrict _m
,
263 uint8_t *__restrict _s1
, uint8_t *__restrict _s2
)
268 _l
= fls32(_div
- 1);
269 _mt
= 0x100000000ULL
* ((1ULL << _l
) - _div
);
270 *_m
= (uint32_t)(_mt
/ _div
+ 1);
271 *_s1
= (_l
> 1) ? 1 : _l
;
272 *_s2
= (_l
== 0) ? 0 : _l
- 1;
276 static __inline
uint32_t
277 fast_divide32(uint32_t _v
, uint32_t _div
, uint32_t _m
, uint8_t _s1
,
282 _t
= (uint32_t)(((uint64_t)_v
* _m
) >> 32);
283 return (_t
+ ((_v
- _t
) >> _s1
)) >> _s2
;
286 static __inline
uint32_t
287 fast_remainder32(uint32_t _v
, uint32_t _div
, uint32_t _m
, uint8_t _s1
,
291 return _v
- _div
* fast_divide32(_v
, _div
, _m
, _s1
, _s2
);
294 #endif /* _SYS_BITOPS_H_ */