]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Compute w_mask rather than storing it in the deflate_state structure
authorNathan Moinvaziri <nathan@nathanm.com>
Sat, 6 Dec 2025 01:52:47 +0000 (17:52 -0800)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Tue, 9 Dec 2025 14:23:33 +0000 (15:23 +0100)
Co-authored-by: Brian Pane <brianp@brianp.net>
deflate.c
deflate.h
insert_string_tpl.h
match_tpl.h
test/benchmarks/benchmark_insert_string.cc

index 19a9ddf5b91eaba88c0cc0d0dc05efb264615a87..1002fa6eba2a7f2f8d26406c89dd8f171fb6376e 100644 (file)
--- a/deflate.c
+++ b/deflate.c
@@ -304,7 +304,6 @@ int32_t ZNG_CONDEXPORT PREFIX(deflateInit2)(PREFIX3(stream) *strm, int32_t level
     s->gzhead = NULL;
     s->w_bits = (unsigned int)windowBits;
     s->w_size = 1 << s->w_bits;
-    s->w_mask = s->w_size - 1;
 
     s->high_water = 0;      /* nothing written to s->window yet */
 
index d91b35fc295e5197ec7f0fc9bc79d571ae311d10..9cd0fe8f66a5200cd0efd21c2e202d1676485e8b 100644 (file)
--- a/deflate.h
+++ b/deflate.h
@@ -156,7 +156,6 @@ struct ALIGNED_(64) internal_state {
 
     unsigned int  w_size;            /* LZ77 window size (32K by default) */
     unsigned int  w_bits;            /* log2(w_size)  (8..16) */
-    unsigned int  w_mask;            /* w_size - 1 */
     unsigned int  lookahead;         /* number of valid bytes ahead in window */
 
     unsigned int high_water;
@@ -404,6 +403,9 @@ static inline void put_uint64(deflate_state *s, uint64_t lld) {
  * distances are limited to MAX_DIST instead of WSIZE.
  */
 
+#define W_MASK(s)  ((s)->w_size - 1)
+/* Window mask: w_size is always a power of 2, so w_mask = w_size - 1 */
+
 #define WIN_INIT STD_MAX_MATCH
 /* Number of bytes after end of data in window to initialize in order to avoid
    memory checker errors from longest match routines */
index 04a9b0a6b680eec8162077f5d4801e5966f15bcb..e507fb59175eff0c76a73e1a947ebc7c27108760 100644 (file)
@@ -56,7 +56,7 @@ Z_FORCEINLINE static Pos QUICK_INSERT_VALUE(deflate_state *const s, uint32_t str
 
     head = s->head[hm];
     if (LIKELY(head != str)) {
-        s->prev[str & s->w_mask] = head;
+        s->prev[str & W_MASK(s)] = head;
         s->head[hm] = (Pos)str;
     }
     return head;
@@ -80,7 +80,7 @@ Z_FORCEINLINE static Pos QUICK_INSERT_STRING(deflate_state *const s, uint32_t st
 
     head = s->head[hm];
     if (LIKELY(head != str)) {
-        s->prev[str & s->w_mask] = head;
+        s->prev[str & W_MASK(s)] = head;
         s->head[hm] = (Pos)str;
     }
     return head;
@@ -101,7 +101,7 @@ Z_FORCEINLINE static void INSERT_STRING(deflate_state *const s, uint32_t str, ui
     /* Local pointers to avoid indirection */
     Pos *headp = s->head;
     Pos *prevp = s->prev;
-    const unsigned int w_mask = s->w_mask;
+    const unsigned int w_mask = W_MASK(s);
 
     for (Pos idx = (Pos)str; strstart < strend; idx++, strstart++) {
         uint32_t val, hm;
index 94de125e4bfe80de0889142af27b54a4b2ada615..ffc22433bd1b819fde7099fa885631ef4a948640 100644 (file)
@@ -25,7 +25,7 @@
  */
 Z_INTERNAL uint32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) {
     unsigned int strstart = s->strstart;
-    const unsigned wmask = s->w_mask;
+    const unsigned wmask = W_MASK(s);
     unsigned char *window = s->window;
     unsigned char *scan = window + strstart;
     Z_REGISTER unsigned char *mbase_start = window;
index fb0f99e8e7913caa41374980249fd5fa81c683a8..7a68fb51929eda325513d03a27936b07a50540c9 100644 (file)
@@ -35,7 +35,6 @@ public:
         // Set up window parameters
         s->w_size = MAX_WSIZE;
         s->w_bits = 15;
-        s->w_mask = MAX_WSIZE - 1;
         s->window_size = TEST_WINDOW_SIZE;
 
         // Allocate window