Replace hash_bits, hash_size and hash_mask with defines.

This commit is contained in:
Hans Kristian Rosbach 2020-08-21 15:53:30 +02:00 committed by Hans Kristian Rosbach
parent 18ecfceb65
commit e7bb6db09a
9 changed files with 21 additions and 35 deletions

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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);

View File

@ -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.

View File

@ -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

View File

@ -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)) {

View File

@ -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;