]> wimlib.net Git - wimlib/blob - include/wimlib/bitops.h
7cca2f22debb0cff8bd4aa096df2a6ba2e8e6c64
[wimlib] / include / wimlib / bitops.h
1 /*
2  * bitops.h - inline functions for bit manipulation
3  *
4  * Copyright 2022 Eric Biggers
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use,
10  * copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following
13  * conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #ifndef _WIMLIB_BITOPS_H
29 #define _WIMLIB_BITOPS_H
30
31 #include "wimlib/compiler.h"
32 #include "wimlib/types.h"
33
34 /*
35  * Bit Scan Reverse (BSR) - find the 0-based index (relative to the least
36  * significant bit) of the *most* significant 1 bit in the input value.  The
37  * input value must be nonzero!
38  */
39
40 static forceinline unsigned
41 bsr32(u32 v)
42 {
43 #ifdef compiler_bsr32
44         return compiler_bsr32(v);
45 #else
46         unsigned bit = 0;
47         while ((v >>= 1) != 0)
48                 bit++;
49         return bit;
50 #endif
51 }
52
53 static forceinline unsigned
54 bsr64(u64 v)
55 {
56 #ifdef compiler_bsr64
57         return compiler_bsr64(v);
58 #else
59         unsigned bit = 0;
60         while ((v >>= 1) != 0)
61                 bit++;
62         return bit;
63 #endif
64 }
65
66 static forceinline unsigned
67 bsrw(machine_word_t v)
68 {
69         STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
70         if (WORDBITS == 32)
71                 return bsr32(v);
72         else
73                 return bsr64(v);
74 }
75
76 /*
77  * Bit Scan Forward (BSF) - find the 0-based index (relative to the least
78  * significant bit) of the *least* significant 1 bit in the input value.  The
79  * input value must be nonzero!
80  */
81
82 static forceinline unsigned
83 bsf32(u32 v)
84 {
85 #ifdef compiler_bsf32
86         return compiler_bsf32(v);
87 #else
88         unsigned bit;
89         for (bit = 0; !(v & 1); bit++, v >>= 1)
90                 ;
91         return bit;
92 #endif
93 }
94
95 static forceinline unsigned
96 bsf64(u64 v)
97 {
98 #ifdef compiler_bsf64
99         return compiler_bsf64(v);
100 #else
101         unsigned bit;
102         for (bit = 0; !(v & 1); bit++, v >>= 1)
103                 ;
104         return bit;
105 #endif
106 }
107
108 static forceinline unsigned
109 bsfw(machine_word_t v)
110 {
111         STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
112         if (WORDBITS == 32)
113                 return bsf32(v);
114         else
115                 return bsf64(v);
116 }
117
118 /* Return the log base 2 of 'n', rounded up to the nearest integer. */
119 static forceinline unsigned
120 ilog2_ceil(size_t n)
121 {
122         if (n <= 1)
123                 return 0;
124         return 1 + bsrw(n - 1);
125 }
126
127 /* Round 'n' up to the nearest power of 2 */
128 static forceinline size_t
129 roundup_pow_of_2(size_t n)
130 {
131         return (size_t)1 << ilog2_ceil(n);
132 }
133
134 #endif /* _WIMLIB_BITOPS_H */