]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Use uint32_t for hash_head in update_hash/insert_string
authorHans Kristian Rosbach <hk-git@circlestorm.org>
Thu, 11 Dec 2025 19:34:05 +0000 (20:34 +0100)
committerHans Kristian Rosbach <hk-github@circlestorm.org>
Mon, 22 Dec 2025 21:58:06 +0000 (22:58 +0100)
deflate.h
deflate_fast.c
deflate_medium.c
deflate_p.h
deflate_quick.c
deflate_rle.c
deflate_slow.c
insert_string_tpl.h
test/benchmarks/benchmark_insert_string.cc

index dce51dd489073f0b90eae46eb465863326164960..73d44ed8c0077758263c4af16270a60f37ce743e 100644 (file)
--- a/deflate.h
+++ b/deflate.h
@@ -196,7 +196,7 @@ struct ALIGNED_(64) internal_state {
      */
 
     unsigned int match_length;       /* length of best match */
-    Pos          prev_match;         /* previous match */
+    uint32_t     prev_match;         /* previous match */
     int          match_available;    /* set if previous match exists */
     unsigned int strstart;           /* start of string to insert */
     unsigned int match_start;        /* start of matching string */
index b13a31d727caae31a4de0b29225ea532ccc1fce9..1f3909c77a1a6872231ce375fcd94fa571ba4fa9 100644 (file)
@@ -49,7 +49,7 @@ Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
 #else
             uint32_t str_val = ZSWAP32(zng_memread_4(window + s->strstart));
 #endif
-            Pos hash_head = quick_insert_value(s, s->strstart, str_val);
+            uint32_t hash_head = quick_insert_value(s, s->strstart, str_val);
             int64_t dist = (int64_t)s->strstart - hash_head;
             lc = (uint8_t)str_val;
 
@@ -71,7 +71,7 @@ Z_INTERNAL block_state deflate_fast(deflate_state *s, int flush) {
         if (match_len >= WANT_MIN_MATCH) {
             Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
             Assert(s->match_start <= UINT16_MAX, "match_start should fit in uint16_t");
-            check_match(s, (Pos)s->strstart, (Pos)s->match_start, match_len);
+            check_match(s, s->strstart, s->match_start, match_len);
 
             bflush = zng_tr_tally_dist(s, s->strstart - s->match_start, match_len - STD_MIN_MATCH);
 
index 2f5893c872d7af97c2e1a00c915dda8397451d7e..3849beeb996a5446fad3faf13747a5838329ebf2 100644 (file)
@@ -173,7 +173,7 @@ Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
     memset(&next_match, 0, sizeof(struct match));
 
     for (;;) {
-        Pos hash_head = 0;    /* head of the hash chain */
+        uint32_t hash_head = 0;    /* head of the hash chain */
         int bflush = 0;       /* set if current block must be flushed */
         int64_t dist;
 
@@ -219,7 +219,7 @@ Z_INTERNAL block_state deflate_medium(deflate_state *s, int flush) {
                  * of window index 0 (in particular we have to avoid a match
                  * of the string with itself at the start of the input file).
                  */
-                current_match.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, (uint32_t)hash_head);
+                current_match.match_length = (uint16_t)FUNCTABLE_CALL(longest_match)(s, hash_head);
                 current_match.match_start = (uint16_t)s->match_start;
                 if (UNLIKELY(current_match.match_length < WANT_MIN_MATCH))
                     current_match.match_length = 1;
index f9c635b1ee14e1da278e9b5021b82613cb59c353..81b715bc5d715ac5ada41f2faea48f047e5da8d6 100644 (file)
@@ -18,7 +18,7 @@
 /* ===========================================================================
  * Check that the match at match_start is indeed a match.
  */
-static inline void check_match(deflate_state *s, Pos start, Pos match, int length) {
+static inline void check_match(deflate_state *s, uint32_t start, uint32_t match, int length) {
     /* check that the match length is valid*/
     if (length < STD_MIN_MATCH || length > STD_MAX_MATCH) {
         fprintf(stderr, " start %u, match %u, length %d\n", start, match, length);
index 940b93b7fadec9efb4f4dacb0a5007badee07d4b..a191070d210f56c903bba379bf04dc3ad75b653e 100644 (file)
@@ -93,7 +93,7 @@ Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
 #else
             uint32_t str_val = ZSWAP32(zng_memread_4(window + s->strstart));
 #endif
-            Pos hash_head = quick_insert_value(s, s->strstart, str_val);
+            uint32_t hash_head = quick_insert_value(s, s->strstart, str_val);
             int64_t dist = (int64_t)s->strstart - hash_head;
             lc = (uint8_t)str_val;
 
@@ -115,7 +115,7 @@ Z_INTERNAL block_state deflate_quick(deflate_state *s, int flush) {
 
                         Assert(match_len <= STD_MAX_MATCH, "match too long");
                         Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
-                        check_match(s, (Pos)s->strstart, hash_head, match_len);
+                        check_match(s, s->strstart, hash_head, match_len);
 
                         zng_tr_emit_dist(s, static_ltree, static_dtree, match_len - STD_MIN_MATCH, (uint32_t)dist);
                         s->lookahead -= match_len;
index 9e39810483459931772c969455ff2c1c8c572463..e468bc6b6ee29b336eb98a51f778d7de107dc090 100644 (file)
@@ -57,7 +57,7 @@ Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
         /* Emit match if have run of STD_MIN_MATCH or longer, else emit literal */
         if (match_len >= STD_MIN_MATCH) {
             Assert(s->strstart <= UINT16_MAX, "strstart should fit in uint16_t");
-            check_match(s, (Pos)s->strstart, (Pos)(s->strstart - 1), match_len);
+            check_match(s, s->strstart, s->strstart - 1, match_len);
 
             bflush = zng_tr_tally_dist(s, 1, match_len - STD_MIN_MATCH);
 
index 0567f7148bf7d2d8b21df8758e11de681e4e2568..96274e04699fa0ec18cbd07e878fa583c6aa2f4b 100644 (file)
@@ -49,7 +49,7 @@ Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
         /* Insert the string window[strstart .. strstart+2] in the
          * dictionary, and set hash_head to the head of the hash chain:
          */
-        Pos hash_head = 0;
+        uint32_t hash_head = 0;
         if (LIKELY(s->lookahead >= WANT_MIN_MATCH)) {
             if (level >= 9)
                 hash_head = quick_insert_string_roll(s, s->strstart);
@@ -59,7 +59,7 @@ Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
 
         /* Find the longest match, discarding those <= prev_length.
          */
-        s->prev_match = (Pos)s->match_start;
+        s->prev_match = s->match_start;
         uint32_t match_len = STD_MIN_MATCH - 1;
         int64_t dist = (int64_t)s->strstart - hash_head;
 
@@ -68,7 +68,7 @@ Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
              * of window index 0 (in particular we have to avoid a match
              * of the string with itself at the start of the input file).
              */
-            match_len = longest_match(s, (uint32_t)hash_head);
+            match_len = longest_match(s, hash_head);
             /* longest_match() sets match_start */
 
             if (match_len <= 5 && (s->strategy == Z_FILTERED)) {
@@ -86,7 +86,7 @@ Z_INTERNAL block_state deflate_slow(deflate_state *s, int flush) {
             /* Do not insert strings in hash table beyond this. */
 
             Assert((s->strstart-1) <= UINT16_MAX, "strstart-1 should fit in uint16_t");
-            check_match(s, (Pos)(s->strstart - 1), s->prev_match, s->prev_length);
+            check_match(s, s->strstart - 1, s->prev_match, s->prev_length);
 
             bflush = zng_tr_tally_dist(s, s->strstart -1 - s->prev_match, s->prev_length - STD_MIN_MATCH);
 
index e507fb59175eff0c76a73e1a947ebc7c27108760..808405486415533d44855d96642a701656b209ca 100644 (file)
@@ -45,9 +45,8 @@ Z_FORCEINLINE static uint32_t UPDATE_HASH(uint32_t h, uint32_t val) {
  * to the previous head of the hash chain (the most recent string with same hash key).
  * Return the previous length of the hash chain.
  */
-Z_FORCEINLINE static Pos QUICK_INSERT_VALUE(deflate_state *const s, uint32_t str, uint32_t val) {
-    uint32_t hm;
-    Pos head;
+Z_FORCEINLINE static uint32_t QUICK_INSERT_VALUE(deflate_state *const s, uint32_t str, uint32_t val) {
+    uint32_t hm, head;
 
     HASH_CALC_VAR_INIT;
     HASH_CALC(HASH_CALC_VAR, val);
@@ -56,7 +55,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 & W_MASK(s)] = head;
+        s->prev[str & W_MASK(s)] = (Pos)head;
         s->head[hm] = (Pos)str;
     }
     return head;
@@ -67,10 +66,9 @@ Z_FORCEINLINE static Pos QUICK_INSERT_VALUE(deflate_state *const s, uint32_t str
  * of the hash chain (the most recent string with same hash key). Return
  * the previous length of the hash chain.
  */
-Z_FORCEINLINE static Pos QUICK_INSERT_STRING(deflate_state *const s, uint32_t str) {
+Z_FORCEINLINE static uint32_t QUICK_INSERT_STRING(deflate_state *const s, uint32_t str) {
     uint8_t *strstart = s->window + str + HASH_CALC_OFFSET;
-    uint32_t val, hm;
-    Pos head;
+    uint32_t val, hm, head;
 
     HASH_CALC_VAR_INIT;
     HASH_CALC_READ;
@@ -80,7 +78,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 & W_MASK(s)] = head;
+        s->prev[str & W_MASK(s)] = (Pos)head;
         s->head[hm] = (Pos)str;
     }
     return head;
@@ -103,8 +101,8 @@ Z_FORCEINLINE static void INSERT_STRING(deflate_state *const s, uint32_t str, ui
     Pos *prevp = s->prev;
     const unsigned int w_mask = W_MASK(s);
 
-    for (Pos idx = (Pos)str; strstart < strend; idx++, strstart++) {
-        uint32_t val, hm;
+    for (uint32_t idx = str; strstart < strend; idx++, strstart++) {
+        uint32_t val, hm, head;
 
         HASH_CALC_VAR_INIT;
         HASH_CALC_READ;
@@ -112,10 +110,10 @@ Z_FORCEINLINE static void INSERT_STRING(deflate_state *const s, uint32_t str, ui
         HASH_CALC_VAR &= HASH_CALC_MASK;
         hm = HASH_CALC_VAR;
 
-        Pos head = headp[hm];
+        head = headp[hm];
         if (LIKELY(head != idx)) {
-            prevp[idx & w_mask] = head;
-            headp[hm] = idx;
+            prevp[idx & w_mask] = (Pos)head;
+            headp[hm] = (Pos)idx;
         }
     }
 }
index b28e13ba7bcd3d63c1d90c4e6f824cee012c8d21..4bff1fda209bb13720929c405e229d279a00da10 100644 (file)
@@ -20,7 +20,7 @@ extern "C" {
 #define MAX_WSIZE 32768
 #define TEST_WINDOW_SIZE (MAX_WSIZE * 2)
 
-typedef Pos (* quick_insert_string_cb)(deflate_state *const s, uint32_t str);
+typedef uint32_t (* quick_insert_string_cb)(deflate_state *const s, uint32_t str);
 
 // Base class with common setup/teardown for both insert_string benchmarks
 class insert_string_base: public benchmark::Fixture {
@@ -141,7 +141,7 @@ public:
 
             // Benchmark quick_insert_string (single insertions)
             for (uint32_t i = 0; i < count; i++) {
-                Pos result = quick_insert_func(s, start_pos + i);
+                uint32_t result = quick_insert_func(s, start_pos + i);
                 benchmark::DoNotOptimize(result);
             }
         }