2 * bitops.h - inline functions for bit manipulation
4 * The following copying information applies to this specific source code file:
6 * Written in 2014-2016 by Eric Biggers <ebiggers3@gmail.com>
8 * To the extent possible under law, the author(s) have dedicated all copyright
9 * and related and neighboring rights to this software to the public domain
10 * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
11 * Dedication (the "CC0").
13 * This software is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
17 * You should have received a copy of the CC0 along with this software; if not
18 * see <http://creativecommons.org/publicdomain/zero/1.0/>.
21 #ifndef _WIMLIB_BITOPS_H
22 #define _WIMLIB_BITOPS_H
24 #include "wimlib/compiler.h"
25 #include "wimlib/types.h"
27 /* Find Last Set bit */
29 static inline unsigned
33 return compiler_fls32(v);
36 while ((v >>= 1) != 0)
42 static inline unsigned
46 return compiler_fls64(v);
49 while ((v >>= 1) != 0)
55 static inline unsigned
56 flsw(machine_word_t v)
58 STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
65 /* Find First Set bit */
67 static inline unsigned
71 return compiler_ffs32(v);
74 for (bit = 0; !(v & 1); bit++, v >>= 1)
80 static inline unsigned
84 return compiler_ffs64(v);
87 for (bit = 0; !(v & 1); bit++, v >>= 1)
93 static inline unsigned
94 ffsw(machine_word_t v)
96 STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
103 /* Return the log base 2 of 'n', rounded up to the nearest integer. */
104 static inline unsigned
109 return 1 + flsw(n - 1);
112 /* Round 'n' up to the nearest power of 2 */
114 roundup_pow_of_2(size_t n)
116 return (size_t)1 << ilog2_ceil(n);
119 #endif /* _WIMLIB_BITOPS_H */