From: Nathan Moinvaziri Date: Fri, 1 May 2020 04:05:05 +0000 (-0700) Subject: Simplify generic hash function using knuth's multiplicative hash. X-Git-Tag: 1.9.9-b1~292 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=07207681ed29d6cc7528df9d5cf8f587c6199a1c;p=thirdparty%2Fzlib-ng.git Simplify generic hash function using knuth's multiplicative hash. --- diff --git a/deflate.c b/deflate.c index 939f47ed0..6825f3efb 100644 --- a/deflate.c +++ b/deflate.c @@ -329,9 +329,6 @@ int ZEXPORT PREFIX(deflateInit2_)(PREFIX3(stream) *strm, int level, int method, s->hash_size = 1 << s->hash_bits; s->hash_mask = s->hash_size - 1; -#if !defined(__x86_64__) && !defined(_M_X64) && !defined(__i386) && !defined(_M_IX86) - s->hash_shift = ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH); -#endif #ifdef X86_PCLMULQDQ_CRC window_padding = 8; @@ -1204,7 +1201,6 @@ static void lm_init(deflate_state *s) { s->match_length = s->prev_length = MIN_MATCH-1; s->match_available = 0; s->match_start = 0; - s->ins_h = 0; } #ifdef ZLIB_DEBUG @@ -1290,7 +1286,6 @@ void ZLIB_INTERNAL fill_window(deflate_state *s) { /* Initialize the hash value now that we have some input: */ if (s->lookahead + s->insert >= MIN_MATCH) { unsigned int str = s->strstart - s->insert; - s->ins_h = s->window[str]; if (str >= 1) functable.quick_insert_string(s, str + 2 - MIN_MATCH); #if MIN_MATCH != 3 diff --git a/deflate.h b/deflate.h index 00a448cf6..4d1f21885 100644 --- a/deflate.h +++ b/deflate.h @@ -147,20 +147,10 @@ typedef struct internal_state { Pos *head; /* Heads of the hash chains or NIL. */ - unsigned int ins_h; /* hash index of string to be inserted */ unsigned int hash_size; /* number of elements in hash table */ unsigned int hash_bits; /* log2(hash_size) */ unsigned int hash_mask; /* hash_size-1 */ -#if !defined(__x86_64__) && !defined(_M_X64) && !defined(__i386) && !defined(_M_IX86) - unsigned int hash_shift; -#endif - /* Number of bits by which ins_h must be shifted at each input - * step. It must be such that after MIN_MATCH steps, the oldest - * byte no longer takes part in the hash key, that is: - * hash_shift * MIN_MATCH >= hash_bits - */ - long block_start; /* Window position at the beginning of the current output block. Gets * negative when the window is moved backwards. diff --git a/deflate_fast.c b/deflate_fast.c index 12244fe73..191064536 100644 --- a/deflate_fast.c +++ b/deflate_fast.c @@ -74,7 +74,6 @@ ZLIB_INTERNAL block_state deflate_fast(deflate_state *s, int flush) { } else { s->strstart += s->match_length; s->match_length = 0; - s->ins_h = s->window[s->strstart]; #if MIN_MATCH != 3 functable.insert_string(s, s->strstart + 2 - MIN_MATCH, MIN_MATCH - 2); #else diff --git a/deflate_medium.c b/deflate_medium.c index f58e49982..81b3ad78e 100644 --- a/deflate_medium.c +++ b/deflate_medium.c @@ -85,7 +85,6 @@ static void insert_match(deflate_state *s, struct match match) { } else { match.strstart += match.match_length; match.match_length = 0; - s->ins_h = s->window[match.strstart]; if (match.strstart >= (MIN_MATCH - 2)) #if MIN_MATCH != 3 functable.insert_string(s, match.strstart + 2 - MIN_MATCH, MIN_MATCH - 2); diff --git a/insert_string.c b/insert_string.c index e9efd517f..774d18ada 100644 --- a/insert_string.c +++ b/insert_string.c @@ -15,16 +15,8 @@ * previous key instead of complete recalculation each time. */ -#if defined(__x86_64__) || defined(_M_X64) || defined(__i386) || defined(_M_IX86) -# define UPDATE_HASH(s, h, val) \ - h = (3483 * ((val) & 0xff) +\ - 23081 * (((val) >> 8) & 0xff) +\ - 6954 * (((val) >> 16) & 0xff) +\ - 20947 * (((val) >> 24) & 0xff)); -#else -# define UPDATE_HASH(s, h, val)\ - h = (s->ins_h = ((s->ins_h << s->hash_shift) ^ ((val) >> ((MIN_MATCH - 1) * 8))) & s->hash_mask) -#endif +#define UPDATE_HASH(s, h, val) \ + h = ((val * 2654435761U) >> (32 - s->hash_bits)); #define INSERT_STRING insert_string_c #define QUICK_INSERT_STRING quick_insert_string_c