X-Git-Url: https://wimlib.net/git/?p=wimlib;a=blobdiff_plain;f=include%2Fwimlib%2Fbitops.h;h=d964374d63e0def7112e83ea5532d7d1d7f91832;hp=ed8b16cdd2410b1d5649081bc3758938d27dd074;hb=144bba695a7a57cdd179dcfd1a916264c8bd4cde;hpb=32158cb5b4df58eb71a1986762e5aaf12bce9d30 diff --git a/include/wimlib/bitops.h b/include/wimlib/bitops.h index ed8b16cd..d964374d 100644 --- a/include/wimlib/bitops.h +++ b/include/wimlib/bitops.h @@ -100,14 +100,20 @@ ffsw(machine_word_t v) return ffs64(v); } -/* Round up to nearest power of 2 */ +/* 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) { - if (n <= 1) - return 1; - return (size_t)1 << (1 + flsw(n - 1)); + return (size_t)1 << ilog2_ceil(n); } #endif /* _WIMLIB_BITOPS_H */