2 * Copyright 2013 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Paweł Dziepak, pdziepak@quarnos.org
8 #ifndef KERNEL_UTIL_BITUTIL_H
9 #define KERNEL_UTIL_BITUTIL_H
12 #include <SupportDefs.h>
15 // http://graphics.stanford.edu/~seander/bithacks.html
17 next_power_of_2(uint32 v
)
31 // http://graphics.stanford.edu/~seander/bithacks.html
33 count_set_bits(uint32 v
)
35 v
= v
- ((v
>> 1) & 0x55555555);
36 v
= (v
& 0x33333333) + ((v
>> 2) & 0x33333333);
37 return (((v
+ (v
>> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
44 static const int MultiplyDeBruijnBitPosition
[32] = {
45 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
46 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
55 return MultiplyDeBruijnBitPosition
[(uint32
)(v
* 0x07C4ACDDU
) >> 27];
59 #endif // KERNEL_UTIL_BITUTIL_H