/*
- * bitops.h
+ * bitops.h - inline functions for bit manipulation
*
- * Inline functions for bit manipulation.
+ * The following copying information applies to this specific source code file:
*
- * The author dedicates this file to the public domain.
- * You can do whatever you want with this file.
+ * Written in 2014-2016 by Eric Biggers <ebiggers3@gmail.com>
+ *
+ * To the extent possible under law, the author(s) have dedicated all copyright
+ * and related and neighboring rights to this software to the public domain
+ * worldwide via the Creative Commons Zero 1.0 Universal Public Domain
+ * Dedication (the "CC0").
+ *
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the CC0 for more details.
+ *
+ * You should have received a copy of the CC0 along with this software; if not
+ * see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#ifndef _WIMLIB_BITOPS_H
static inline unsigned
flsw(machine_word_t v)
{
- BUILD_BUG_ON(WORDSIZE != 4 && WORDSIZE != 8);
- if (WORDSIZE == 4)
+ STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
+ if (WORDBITS == 32)
return fls32(v);
else
return fls64(v);
static inline unsigned
ffsw(machine_word_t v)
{
- BUILD_BUG_ON(WORDSIZE != 4 && WORDSIZE != 8);
- if (WORDSIZE == 4)
+ STATIC_ASSERT(WORDBITS == 32 || WORDBITS == 64);
+ if (WORDBITS == 32)
return ffs32(v);
else
return ffs64(v);
}
+/* Return the log base 2 of 'n', rounded up to the nearest integer. */
+static inline unsigned
+ilog2_ceil(size_t n)
+{
+ if (n <= 1)
+ return 0;
+ return 1 + flsw(n - 1);
+}
+
+/* Round 'n' up to the nearest power of 2 */
+static inline size_t
+roundup_pow_of_2(size_t n)
+{
+ return (size_t)1 << ilog2_ceil(n);
+}
+
#endif /* _WIMLIB_BITOPS_H */