From e7bb6db09a183807a8f94a8bdcf156a765402d9f Mon Sep 17 00:00:00 2001 From: Hans Kristian Rosbach Date: Fri, 21 Aug 2020 15:53:30 +0200 Subject: [PATCH] Replace hash_bits, hash_size and hash_mask with defines. --- arch/arm/slide_neon.c | 2 +- arch/power/slide_hash_power8.c | 2 +- arch/x86/slide_avx.c | 2 +- arch/x86/slide_sse.c | 2 +- deflate.c | 28 ++++++++-------------------- deflate.h | 8 ++++---- insert_string.c | 2 +- insert_string_tpl.h | 5 ++--- match_tpl.h | 5 ++--- 9 files changed, 21 insertions(+), 35 deletions(-) diff --git a/arch/arm/slide_neon.c b/arch/arm/slide_neon.c index b5b4fdf0..e5bea64f 100644 --- a/arch/arm/slide_neon.c +++ b/arch/arm/slide_neon.c @@ -46,7 +46,7 @@ static inline void slide_hash_chain(Pos *table, unsigned int entries, uint16_t w ZLIB_INTERNAL void slide_hash_neon(deflate_state *s) { unsigned int wsize = s->w_size; - slide_hash_chain(s->head, s->hash_size, wsize); + slide_hash_chain(s->head, HASH_SIZE, wsize); slide_hash_chain(s->prev, wsize, wsize); } #endif diff --git a/arch/power/slide_hash_power8.c b/arch/power/slide_hash_power8.c index b869307a..8bd79dfa 100644 --- a/arch/power/slide_hash_power8.c +++ b/arch/power/slide_hash_power8.c @@ -48,7 +48,7 @@ void ZLIB_INTERNAL slide_hash_power8(deflate_state *s) { unsigned int n; Pos *p; - n = s->hash_size; + n = HASH_SIZE; p = &s->head[n]; slide_hash_power8_loop(s,n,p); diff --git a/arch/x86/slide_avx.c b/arch/x86/slide_avx.c index 6083dc7d..42ba9121 100644 --- a/arch/x86/slide_avx.c +++ b/arch/x86/slide_avx.c @@ -20,7 +20,7 @@ ZLIB_INTERNAL void slide_hash_avx2(deflate_state *s) { uint16_t wsize = (uint16_t)s->w_size; const __m256i zmm_wsize = _mm256_set1_epi16(wsize); - n = s->hash_size; + n = HASH_SIZE; p = &s->head[n] - 16; do { __m256i value, result; diff --git a/arch/x86/slide_sse.c b/arch/x86/slide_sse.c index f57a7d33..7ae79a82 100644 --- a/arch/x86/slide_sse.c +++ b/arch/x86/slide_sse.c @@ -19,7 +19,7 @@ ZLIB_INTERNAL void slide_hash_sse2(deflate_state *s) { uint16_t wsize = (uint16_t)s->w_size; const __m128i xmm_wsize = _mm_set1_epi16(wsize); - n = s->hash_size; + n = HASH_SIZE; p = &s->head[n] - 8; do { __m128i value, result; diff --git a/deflate.c b/deflate.c index 2a7fd1e4..a7d9a5a0 100644 --- a/deflate.c +++ b/deflate.c @@ -180,12 +180,10 @@ static const config configuration_table[10] = { /* =========================================================================== - * Initialize the hash table (avoiding 64K overflow for 16 bit systems). - * prev[] will be initialized on the fly. + * Initialize the hash table. prev[] will be initialized on the fly. */ #define CLEAR_HASH(s) do { \ - s->head[s->hash_size - 1] = NIL; \ - memset((unsigned char *)s->head, 0, (unsigned)(s->hash_size - 1) * sizeof(*s->head)); \ + memset((unsigned char *)s->head, 0, HASH_SIZE * sizeof(*s->head)); \ } while (0) /* =========================================================================== @@ -194,11 +192,11 @@ static const config configuration_table[10] = { * keep the hash table consistent if we switch back to level > 0 later. */ ZLIB_INTERNAL void slide_hash_c(deflate_state *s) { - unsigned n; Pos *p; + unsigned n; unsigned int wsize = s->w_size; - n = s->hash_size; + n = HASH_SIZE; p = &s->head[n]; #ifdef NOT_TWEAK_COMPILER do { @@ -322,16 +320,6 @@ int32_t ZEXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int3 s->w_size = 1 << s->w_bits; s->w_mask = s->w_size - 1; -#ifdef X86_SSE42_CRC_HASH - if (x86_cpu_has_sse42) - s->hash_bits = (unsigned int)15; - else -#endif - s->hash_bits = (unsigned int)memLevel + 7; - - s->hash_size = 1 << s->hash_bits; - s->hash_mask = s->hash_size - 1; - #ifdef X86_PCLMULQDQ_CRC window_padding = 8; #endif @@ -339,7 +327,7 @@ int32_t ZEXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int32_t level, int3 s->window = (unsigned char *) ZALLOC_WINDOW(strm, s->w_size + window_padding, 2*sizeof(unsigned char)); s->prev = (Pos *) ZALLOC(strm, s->w_size, sizeof(Pos)); memset(s->prev, 0, s->w_size * sizeof(Pos)); - s->head = (Pos *) ZALLOC(strm, s->hash_size, sizeof(Pos)); + s->head = (Pos *) ZALLOC(strm, HASH_SIZE, sizeof(Pos)); s->high_water = 0; /* nothing written to s->window yet */ @@ -738,7 +726,7 @@ unsigned long ZEXPORT PREFIX(deflateBound)(PREFIX3(stream) *strm, unsigned long /* if not default parameters, return conservative bound */ if (DEFLATE_NEED_CONSERVATIVE_BOUND(strm) || /* hook for IBM Z DFLTCC */ - s->w_bits != 15 || s->hash_bits != 8 + 7) + s->w_bits != 15 || HASH_BITS < 15) return complen + wraplen; /* default settings: return tight bound for that case */ @@ -1133,7 +1121,7 @@ int32_t ZEXPORT PREFIX(deflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *sour ds->window = (unsigned char *) ZALLOC_WINDOW(dest, ds->w_size + window_padding, 2*sizeof(unsigned char)); ds->prev = (Pos *) ZALLOC(dest, ds->w_size, sizeof(Pos)); - ds->head = (Pos *) ZALLOC(dest, ds->hash_size, sizeof(Pos)); + ds->head = (Pos *) ZALLOC(dest, HASH_SIZE, sizeof(Pos)); ds->pending_buf = (unsigned char *) ZALLOC(dest, ds->lit_bufsize, 4); if (ds->window == NULL || ds->prev == NULL || ds->head == NULL || ds->pending_buf == NULL) { @@ -1143,7 +1131,7 @@ int32_t ZEXPORT PREFIX(deflateCopy)(PREFIX3(stream) *dest, PREFIX3(stream) *sour memcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(unsigned char)); memcpy((void *)ds->prev, (void *)ss->prev, ds->w_size * sizeof(Pos)); - memcpy((void *)ds->head, (void *)ss->head, ds->hash_size * sizeof(Pos)); + memcpy((void *)ds->head, (void *)ss->head, HASH_SIZE * sizeof(Pos)); memcpy(ds->pending_buf, ss->pending_buf, (unsigned int)ds->pending_buf_size); ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf); diff --git a/deflate.h b/deflate.h index 93f7b5b7..f9db02bb 100644 --- a/deflate.h +++ b/deflate.h @@ -68,6 +68,10 @@ #define FINISH_STATE 666 /* stream complete */ /* Stream status */ +#define HASH_BITS 15 /* log2(HASH_SIZE) */ +#define HASH_SIZE 32768 /* number of elements in hash table */ +#define HASH_MASK 0x7FFF /* HASH_SIZE-1 */ + /* Data structure describing a single value and its code string. */ typedef struct ct_data_s { @@ -146,10 +150,6 @@ typedef struct internal_state { Pos *head; /* Heads of the hash chains or NIL. */ - unsigned int hash_size; /* number of elements in hash table */ - unsigned int hash_bits; /* log2(hash_size) */ - unsigned int hash_mask; /* hash_size-1 */ - long block_start; /* Window position at the beginning of the current output block. Gets * negative when the window is moved backwards. diff --git a/insert_string.c b/insert_string.c index 2bf4d716..72336681 100644 --- a/insert_string.c +++ b/insert_string.c @@ -15,7 +15,7 @@ * previous key instead of complete recalculation each time. */ #define UPDATE_HASH(s, h, val) \ - h = ((val * 2654435761U) >> (32 - s->hash_bits)); + h = ((val * 2654435761U) >> (32 - HASH_BITS)); #define INSERT_STRING insert_string_c #define QUICK_INSERT_STRING quick_insert_string_c diff --git a/insert_string_tpl.h b/insert_string_tpl.h index d667231c..17a42b2d 100644 --- a/insert_string_tpl.h +++ b/insert_string_tpl.h @@ -42,7 +42,7 @@ ZLIB_INTERNAL Pos QUICK_INSERT_STRING(deflate_state *const s, const uint32_t str #endif UPDATE_HASH(s, h, val); - hm = h & s->hash_mask; + hm = h & HASH_MASK; head = s->head[hm]; if (LIKELY(head != str)) { @@ -63,7 +63,6 @@ ZLIB_INTERNAL Pos QUICK_INSERT_STRING(deflate_state *const s, const uint32_t str ZLIB_INTERNAL void INSERT_STRING(deflate_state *const s, const uint32_t str, uint32_t count) { uint8_t *strstart = s->window + str; uint8_t *strend = strstart + count - 1; /* last position */ - uint32_t hash_mask = s->hash_mask; for (Pos idx = str; strstart <= strend; idx++, strstart++) { uint32_t val, hm, h = 0; @@ -78,7 +77,7 @@ ZLIB_INTERNAL void INSERT_STRING(deflate_state *const s, const uint32_t str, uin #endif UPDATE_HASH(s, h, val); - hm = h & hash_mask; + hm = h & HASH_MASK; Pos head = s->head[hm]; if (LIKELY(head != idx)) { diff --git a/match_tpl.h b/match_tpl.h index 49ee292b..b853b2a3 100644 --- a/match_tpl.h +++ b/match_tpl.h @@ -53,10 +53,9 @@ ZLIB_INTERNAL int32_t LONGEST_MATCH(deflate_state *const s, Pos cur_match) { return best_len; /* - * The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple - * of 16. It is easy to get rid of this optimization if necessary. + * The code is optimized for MAX_MATCH-2 multiple of 16. */ - Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever"); + Assert(MAX_MATCH == 258, "Code too clever"); best_len = s->prev_length ? s->prev_length : 1; -- 2.47.2