]> wimlib.net Git - wimlib/commitdiff
Introduce ilog2_ceil()
authorEric Biggers <ebiggers3@gmail.com>
Wed, 22 Jun 2016 01:02:00 +0000 (20:02 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Sat, 2 Jul 2016 15:04:13 +0000 (10:04 -0500)
include/wimlib/bitops.h
src/lzx_common.c

index ed8b16cdd2410b1d5649081bc3758938d27dd074..d964374d63e0def7112e83ea5532d7d1d7f91832 100644 (file)
@@ -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 */
index f5c8f89a7a0471202c752efd384a6f8c811eadb9..7925f1f197b6803ce31bca7fed7564302f6220e1 100644 (file)
@@ -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