From: Eric Biggers Date: Wed, 22 Jun 2016 01:02:00 +0000 (-0500) Subject: Introduce ilog2_ceil() X-Git-Tag: v1.10.0~37 X-Git-Url: https://wimlib.net/git/?p=wimlib;a=commitdiff_plain;h=144bba695a7a57cdd179dcfd1a916264c8bd4cde;hp=32158cb5b4df58eb71a1986762e5aaf12bce9d30 Introduce ilog2_ceil() --- 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 */ diff --git a/src/lzx_common.c b/src/lzx_common.c index f5c8f89a..7925f1f1 100644 --- a/src/lzx_common.c +++ b/src/lzx_common.c @@ -3,7 +3,7 @@ */ /* - * Copyright (C) 2012, 2013, 2014, 2015 Eric Biggers + * Copyright (C) 2012-2016 Eric Biggers * * This file is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free @@ -76,17 +76,10 @@ const u8 lzx_extra_offset_bits[LZX_MAX_OFFSET_SLOTS] = { unsigned lzx_get_window_order(size_t max_bufsize) { - unsigned order; - if (max_bufsize == 0 || max_bufsize > LZX_MAX_WINDOW_SIZE) return 0; - order = fls32(max_bufsize); - - if (((u32)1 << order) != max_bufsize) - order++; - - return max(order, LZX_MIN_WINDOW_ORDER); + return max(ilog2_ceil(max_bufsize), LZX_MIN_WINDOW_ORDER); } /* Given a valid LZX window order, return the number of symbols that will exist