From 2c7fbfba2f19f87c7920a18ab6a69fa9675bd56a Mon Sep 17 00:00:00 2001 From: Nathan Moinvaziri Date: Fri, 5 Dec 2025 17:52:47 -0800 Subject: [PATCH] Compute w_mask rather than storing it in the deflate_state structure Co-authored-by: Brian Pane --- deflate.c | 1 - deflate.h | 4 +++- insert_string_tpl.h | 6 +++--- match_tpl.h | 2 +- test/benchmarks/benchmark_insert_string.cc | 1 - 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/deflate.c b/deflate.c index 19a9ddf5b..1002fa6eb 100644 --- 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 */ diff --git a/deflate.h b/deflate.h index d91b35fc2..9cd0fe8f6 100644 --- 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 */ diff --git a/insert_string_tpl.h b/insert_string_tpl.h index 04a9b0a6b..e507fb591 100644 --- a/insert_string_tpl.h +++ b/insert_string_tpl.h @@ -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; diff --git a/match_tpl.h b/match_tpl.h index 94de125e4..ffc22433b 100644 --- a/match_tpl.h +++ b/match_tpl.h @@ -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; diff --git a/test/benchmarks/benchmark_insert_string.cc b/test/benchmarks/benchmark_insert_string.cc index fb0f99e8e..7a68fb519 100644 --- a/test/benchmarks/benchmark_insert_string.cc +++ b/test/benchmarks/benchmark_insert_string.cc @@ -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 -- 2.47.3