]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Use __builtin_bitreverse16 in inflate_table
authorNathan Moinvaziri <nathan@nathanm.com>
Sun, 7 Dec 2025 07:56:21 +0000 (23:56 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Sun, 14 Dec 2025 14:57:33 +0000 (15:57 +0100)
https://github.com/dougallj/zlib-dougallj/commit/f23fa25aa168ef782bab5e7cd6f9df50d7bb5eb2

inftrees.c

index 5234fe7ae0cdd51e33b3953bc888b2dece57a1f7..c7ea1208103e086c2d8336248f60e8958873a437 100644 (file)
@@ -6,6 +6,7 @@
 #include "zbuild.h"
 #include "zutil.h"
 #include "inftrees.h"
+#include "fallback_builtins.h"
 
 const char PREFIX(inflate_copyright)[] = " inflate 1.3.1 Copyright 1995-2024 Mark Adler ";
 /*
@@ -37,6 +38,7 @@ int Z_INTERNAL zng_inflate_table(codetype type, uint16_t *lens, unsigned codes,
     unsigned drop;              /* code bits to drop for sub-table */
     int left;                   /* number of prefix codes available */
     unsigned used;              /* code entries in table used */
+    uint16_t rhuff;             /* Reversed huffman code */
     unsigned huff;              /* Huffman code */
     unsigned incr;              /* for incrementing code, index */
     unsigned fill;              /* index for replicating entries */
@@ -187,6 +189,7 @@ int Z_INTERNAL zng_inflate_table(codetype type, uint16_t *lens, unsigned codes,
     }
 
     /* initialize state for loop */
+    rhuff = 0;                  /* starting code, reversed */
     huff = 0;                   /* starting code */
     sym = 0;                    /* starting code symbol */
     len = min;                  /* starting code length */
@@ -227,15 +230,8 @@ int Z_INTERNAL zng_inflate_table(codetype type, uint16_t *lens, unsigned codes,
         } while (fill != 0);
 
         /* backwards increment the len-bit code huff */
-        incr = 1U << (len - 1);
-        while (huff & incr)
-            incr >>= 1;
-        if (incr != 0) {
-            huff &= incr - 1;
-            huff += incr;
-        } else {
-            huff = 0;
-        }
+        rhuff += (0x8000u >> (len - 1));
+        huff = __builtin_bitreverse16(rhuff);
 
         /* go to next symbol, update count, len */
         sym++;