10#ifndef BROTLI_ENC_HASH_H_
11#define BROTLI_ENC_HASH_H_
18#include "../common/constants.h"
19#include "../common/dictionary.h"
20#include "../common/platform.h"
21#include "compound_dictionary.h"
22#include "encoder_dict.h"
24#include "find_match_length.h"
25#include "matching_tag_mask.h"
28#include "static_dict.h"
30#if defined(__cplusplus) || defined(c_plusplus)
47 size_t dict_num_lookups;
48 size_t dict_num_matches;
62static const uint32_t kCutoffTransformsCount = 10;
65static const uint64_t kCutoffTransforms =
66 BROTLI_MAKE_UINT64_T(0x071B520A, 0xDA2D3200);
81static const uint32_t kHashMul32 = 0x1E35A7BD;
82static const uint64_t kHashMul64 =
83 BROTLI_MAKE_UINT64_T(0x1FE35A7Bu, 0xD3579BD3u);
85static BROTLI_INLINE uint32_t Hash14(
const uint8_t* data) {
86 uint32_t h = BROTLI_UNALIGNED_LOAD32LE(data) * kHashMul32;
89 return h >> (32 - 14);
92static BROTLI_INLINE
void PrepareDistanceCache(
93 int* BROTLI_RESTRICT distance_cache,
const int num_distances) {
94 if (num_distances > 4) {
95 int last_distance = distance_cache[0];
96 distance_cache[4] = last_distance - 1;
97 distance_cache[5] = last_distance + 1;
98 distance_cache[6] = last_distance - 2;
99 distance_cache[7] = last_distance + 2;
100 distance_cache[8] = last_distance - 3;
101 distance_cache[9] = last_distance + 3;
102 if (num_distances > 10) {
103 int next_last_distance = distance_cache[1];
104 distance_cache[10] = next_last_distance - 1;
105 distance_cache[11] = next_last_distance + 1;
106 distance_cache[12] = next_last_distance - 2;
107 distance_cache[13] = next_last_distance + 2;
108 distance_cache[14] = next_last_distance - 3;
109 distance_cache[15] = next_last_distance + 3;
114#define BROTLI_LITERAL_BYTE_SCORE 135
115#define BROTLI_DISTANCE_BIT_PENALTY 30
117#define BROTLI_SCORE_BASE (BROTLI_DISTANCE_BIT_PENALTY * 8 * sizeof(size_t))
135static BROTLI_INLINE score_t BackwardReferenceScore(
136 size_t copy_length,
size_t backward_reference_offset) {
137 return BROTLI_SCORE_BASE + BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length -
138 BROTLI_DISTANCE_BIT_PENALTY * Log2FloorNonZero(backward_reference_offset);
141static BROTLI_INLINE score_t BackwardReferenceScoreUsingLastDistance(
142 size_t copy_length) {
143 return BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length +
144 BROTLI_SCORE_BASE + 15;
147static BROTLI_INLINE score_t BackwardReferencePenaltyUsingLastDistance(
148 size_t distance_short_code) {
149 return (score_t)39 + ((0x1CA10 >> (distance_short_code & 0xE)) & 0xE);
152static BROTLI_INLINE
BROTLI_BOOL TestStaticDictionaryItem(
154 const uint8_t* data,
size_t max_length,
size_t max_backward,
160 offset = dictionary->words->offsets_by_length[len] + len * word_idx;
161 if (len > max_length) {
166 FindMatchLengthWithLimit(data, &dictionary->words->data[offset], len);
167 if (matchlen + dictionary->cutoffTransformsCount <= len || matchlen == 0) {
171 size_t cut = len - matchlen;
172 size_t transform_id = (cut << 2) +
173 (
size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F);
174 backward = max_backward + 1 + word_idx +
177 if (backward > max_distance) {
180 score = BackwardReferenceScore(matchlen, backward);
181 if (score < out->score) {
185 out->len_code_delta = (int)len - (
int)matchlen;
186 out->distance = backward;
191static BROTLI_INLINE
void SearchInStaticDictionary(
193 HasherCommon* common,
const uint8_t* data,
size_t max_length,
194 size_t max_backward,
size_t max_distance,
198 if (common->dict_num_matches < (common->dict_num_lookups >> 7)) {
201 key = Hash14(data) << 1;
202 for (i = 0; i < (shallow ? 1u : 2u); ++i, ++
key) {
203 common->dict_num_lookups++;
204 if (dictionary->hash_table_lengths[
key] != 0) {
205 BROTLI_BOOL item_matches = TestStaticDictionaryItem(
206 dictionary, dictionary->hash_table_lengths[
key],
207 dictionary->hash_table_words[
key], data,
208 max_length, max_backward, max_distance, out);
210 common->dict_num_matches++;
218 uint32_t length_and_code;
221static BROTLI_INLINE
void InitBackwardMatch(
BackwardMatch* self,
222 size_t dist,
size_t len) {
223 self->distance = (uint32_t)dist;
224 self->length_and_code = (uint32_t)(len << 5);
227static BROTLI_INLINE
void InitDictionaryBackwardMatch(
BackwardMatch* self,
228 size_t dist,
size_t len,
size_t len_code) {
229 self->distance = (uint32_t)dist;
230 self->length_and_code =
231 (uint32_t)((len << 5) | (len == len_code ? 0 : len_code));
234static BROTLI_INLINE
size_t BackwardMatchLength(
const BackwardMatch* self) {
235 return self->length_and_code >> 5;
238static BROTLI_INLINE
size_t BackwardMatchLengthCode(
const BackwardMatch* self) {
239 size_t code = self->length_and_code & 31;
240 return code ? code : BackwardMatchLength(self);
243#define EXPAND_CAT(a, b) CAT(a, b)
244#define CAT(a, b) a ## b
245#define FN(X) EXPAND_CAT(X, HASHER())
248#define BUCKET_BITS 17
249#define MAX_TREE_SEARCH_DEPTH 64
250#define MAX_TREE_COMP_LENGTH 128
251#include "hash_to_binary_tree_inc.h"
252#undef MAX_TREE_SEARCH_DEPTH
253#undef MAX_TREE_COMP_LENGTH
257#define MAX_NUM_MATCHES_H10 128
264#define BUCKET_BITS 16
265#define BUCKET_SWEEP_BITS 0
267#define USE_DICTIONARY 1
268#include "hash_longest_match_quickly_inc.h"
269#undef BUCKET_SWEEP_BITS
274#define BUCKET_SWEEP_BITS 1
275#define USE_DICTIONARY 0
276#include "hash_longest_match_quickly_inc.h"
278#undef BUCKET_SWEEP_BITS
283#define BUCKET_BITS 17
284#define BUCKET_SWEEP_BITS 2
285#define USE_DICTIONARY 1
286#include "hash_longest_match_quickly_inc.h"
289#undef BUCKET_SWEEP_BITS
294#include "hash_longest_match_inc.h"
298#include "hash_longest_match64_inc.h"
301#if defined(BROTLI_MAX_SIMD_QUALITY)
303#include "hash_longest_match_simd_inc.h"
307#include "hash_longest_match64_simd_inc.h"
311#define BUCKET_BITS 15
313#define NUM_LAST_DISTANCES_TO_CHECK 4
317#include "hash_forgetful_chain_inc.h"
319#undef NUM_LAST_DISTANCES_TO_CHECK
321#define NUM_LAST_DISTANCES_TO_CHECK 10
323#include "hash_forgetful_chain_inc.h"
325#undef NUM_LAST_DISTANCES_TO_CHECK
329#define NUM_LAST_DISTANCES_TO_CHECK 16
333#include "hash_forgetful_chain_inc.h"
335#undef NUM_LAST_DISTANCES_TO_CHECK
342#define BUCKET_BITS 20
343#define BUCKET_SWEEP_BITS 2
345#define USE_DICTIONARY 0
346#include "hash_longest_match_quickly_inc.h"
349#undef BUCKET_SWEEP_BITS
355#define HASHER() HROLLING_FAST
358#define NUMBUCKETS 16777216
359#define MASK ((NUMBUCKETS * 64) - 1)
360#include "hash_rolling_inc.h"
365#define HASHER() HROLLING
367#include "hash_rolling_inc.h"
376#define HASHER_B HROLLING_FAST
377#include "hash_composite_inc.h"
384#define HASHER_B HROLLING_FAST
385#include "hash_composite_inc.h"
392#define HASHER_B HROLLING
393#include "hash_composite_inc.h"
402#if defined(BROTLI_MAX_SIMD_QUALITY)
403#define FOR_SIMPLE_HASHERS(H) \
404 H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54) H(58) H(68)
406#define FOR_SIMPLE_HASHERS(H) \
407 H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54)
409#define FOR_COMPOSITE_HASHERS(H) H(35) H(55) H(65)
410#define FOR_GENERIC_HASHERS(H) FOR_SIMPLE_HASHERS(H) FOR_COMPOSITE_HASHERS(H)
411#define FOR_ALL_HASHERS(H) FOR_GENERIC_HASHERS(H) H(10)
419 FOR_ALL_HASHERS(MEMBER_)
425static BROTLI_INLINE
void HasherInit(
Hasher* hasher) {
427 hasher->common.
extra[0] = NULL;
428 hasher->common.
extra[1] = NULL;
429 hasher->common.
extra[2] = NULL;
430 hasher->common.
extra[3] = NULL;
434 if (hasher->common.
extra[0] != NULL) BROTLI_FREE(m, hasher->common.
extra[0]);
435 if (hasher->common.
extra[1] != NULL) BROTLI_FREE(m, hasher->common.
extra[1]);
436 if (hasher->common.
extra[2] != NULL) BROTLI_FREE(m, hasher->common.
extra[2]);
437 if (hasher->common.
extra[3] != NULL) BROTLI_FREE(m, hasher->common.
extra[3]);
440static BROTLI_INLINE
void HasherReset(
Hasher* hasher) {
445 BROTLI_BOOL one_shot,
const size_t input_size,
size_t* alloc_size) {
446 switch (params->hasher.type) {
449 HashMemAllocInBytesH ## N(params, one_shot, input_size, alloc_size); \
451 FOR_ALL_HASHERS(SIZE_)
463 size_t alloc_size[4] = {0};
465 ChooseHasher(params, ¶ms->hasher);
466 hasher->common.params = params->hasher;
467 hasher->common.dict_num_lookups = 0;
468 hasher->common.dict_num_matches = 0;
469 HasherSize(params, one_shot, input_size, alloc_size);
470 for (i = 0; i < 4; ++i) {
471 if (alloc_size[i] == 0)
continue;
472 hasher->common.
extra[i] = BROTLI_ALLOC(m, uint8_t, alloc_size[i]);
473 if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(hasher->common.
extra[i]))
return;
475 switch (hasher->common.params.type) {
476#define INITIALIZE_(N) \
478 InitializeH ## N(&hasher->common, \
479 &hasher->privat._H ## N, params); \
481 FOR_ALL_HASHERS(INITIALIZE_);
491 switch (hasher->common.params.type) {
495 &hasher->privat._H ## N, \
496 one_shot, input_size, data); \
498 FOR_ALL_HASHERS(PREPARE_)
506static BROTLI_INLINE
void InitOrStitchToPreviousBlock(
510 HasherSetup(m, hasher, params, data, position, input_size, is_last);
511 if (BROTLI_IS_OOM(m))
return;
512 switch (hasher->common.params.type) {
515 StitchToPreviousBlockH ## N( \
516 &hasher->privat._H ## N, \
517 input_size, position, data, mask); \
519 FOR_ALL_HASHERS(INIT_)
527static BROTLI_INLINE
void FindCompoundDictionaryMatch(
529 const size_t ring_buffer_mask,
const int* BROTLI_RESTRICT distance_cache,
530 const size_t cur_ix,
const size_t max_length,
const size_t distance_offset,
532 const uint32_t source_size = self->source_size;
533 const size_t boundary = distance_offset - source_size;
534 const uint32_t hash_bits = self->hash_bits;
535 const uint32_t bucket_bits = self->bucket_bits;
536 const uint32_t slot_bits = self->slot_bits;
538 const uint32_t hash_shift = 64u - bucket_bits;
539 const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits);
540 const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits);
542 const uint32_t* slot_offsets = (uint32_t*)(&self[1]);
543 const uint16_t* heads = (uint16_t*)(&slot_offsets[(
size_t)1u << slot_bits]);
544 const uint32_t* items = (uint32_t*)(&heads[(
size_t)1u << bucket_bits]);
545 const uint8_t* source = NULL;
547 const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
548 score_t best_score = out->score;
549 size_t best_len = out->len;
552 (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) *
553 kPreparedDictionaryHashMul64Long;
554 const uint32_t
key = (uint32_t)(h >> hash_shift);
555 const uint32_t slot =
key & slot_mask;
556 const uint32_t head = heads[
key];
557 const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head];
558 uint32_t
item = (head == 0xFFFF) ? 1 : 0;
560 const void* tail = (
void*)&items[self->num_items];
561 if (self->magic == kPreparedDictionaryMagic) {
562 source = (
const uint8_t*)tail;
565 source = (
const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((
const uint8_t**)tail);
568 BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask);
570 for (i = 0; i < 4; ++i) {
571 const size_t distance = (size_t)distance_cache[i];
575 if (distance <= boundary || distance > distance_offset)
continue;
576 offset = distance_offset - distance;
577 limit = source_size - offset;
578 limit = limit > max_length ? max_length : limit;
579 len = FindMatchLengthWithLimit(&source[offset], &data[cur_ix_masked],
582 score_t score = BackwardReferenceScoreUsingLastDistance(len);
583 if (best_score < score) {
584 if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
585 if (best_score < score) {
587 if (len > best_len) best_len = len;
589 out->len_code_delta = 0;
590 out->distance = distance;
591 out->score = best_score;
607 offset =
item & 0x7FFFFFFF;
609 distance = distance_offset - offset;
610 limit = source_size - offset;
611 limit = (limit > max_length) ? max_length : limit;
612 if (distance > max_distance)
continue;
613 if (cur_ix_masked + best_len > ring_buffer_mask || best_len >= limit ||
615 BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) !=
616 BrotliUnalignedRead32(&source[offset + best_len - 3])) {
620 const size_t len = FindMatchLengthWithLimit(&source[offset],
621 &data[cur_ix_masked],
624 score_t score = BackwardReferenceScore(len, distance);
625 if (best_score < score) {
629 out->len_code_delta = 0;
630 out->distance = distance;
631 out->score = best_score;
640static BROTLI_INLINE
size_t FindAllCompoundDictionaryMatches(
642 const size_t ring_buffer_mask,
const size_t cur_ix,
const size_t min_length,
643 const size_t max_length,
const size_t distance_offset,
644 const size_t max_distance,
BackwardMatch* matches,
size_t match_limit) {
645 const uint32_t source_size = self->source_size;
646 const uint32_t hash_bits = self->hash_bits;
647 const uint32_t bucket_bits = self->bucket_bits;
648 const uint32_t slot_bits = self->slot_bits;
650 const uint32_t hash_shift = 64u - bucket_bits;
651 const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits);
652 const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits);
654 const uint32_t* slot_offsets = (uint32_t*)(&self[1]);
655 const uint16_t* heads = (uint16_t*)(&slot_offsets[(
size_t)1u << slot_bits]);
656 const uint32_t* items = (uint32_t*)(&heads[(
size_t)1u << bucket_bits]);
657 const uint8_t* source = NULL;
659 const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
660 size_t best_len = min_length;
662 (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) *
663 kPreparedDictionaryHashMul64Long;
664 const uint32_t
key = (uint32_t)(h >> hash_shift);
665 const uint32_t slot =
key & slot_mask;
666 const uint32_t head = heads[
key];
667 const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head];
668 uint32_t
item = (head == 0xFFFF) ? 1 : 0;
671 const void* tail = (
void*)&items[self->num_items];
672 if (self->magic == kPreparedDictionaryMagic) {
673 source = (
const uint8_t*)tail;
676 source = (
const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((
const uint8_t**)tail);
679 BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask);
688 offset =
item & 0x7FFFFFFF;
690 distance = distance_offset - offset;
691 limit = source_size - offset;
692 limit = (limit > max_length) ? max_length : limit;
693 if (distance > max_distance)
continue;
694 if (cur_ix_masked + best_len > ring_buffer_mask ||
696 data[cur_ix_masked + best_len] != source[offset + best_len]) {
699 len = FindMatchLengthWithLimit(
700 &source[offset], &data[cur_ix_masked], limit);
701 if (len > best_len) {
703 InitBackwardMatch(matches++, distance, len);
705 if (found == match_limit)
break;
711static BROTLI_INLINE
void LookupCompoundDictionaryMatch(
713 const size_t ring_buffer_mask,
const int* BROTLI_RESTRICT distance_cache,
714 const size_t cur_ix,
const size_t max_length,
715 const size_t max_ring_buffer_distance,
const size_t max_distance,
717 size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1;
719 for (d = 0; d < addon->num_chunks; ++d) {
721 FindCompoundDictionaryMatch(
723 distance_cache, cur_ix, max_length,
724 base_offset - addon->chunk_offsets[d], max_distance, sr);
728static BROTLI_INLINE
size_t LookupAllCompoundDictionaryMatches(
730 const size_t ring_buffer_mask,
const size_t cur_ix,
size_t min_length,
731 const size_t max_length,
const size_t max_ring_buffer_distance,
733 size_t match_limit) {
734 size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1;
736 size_t total_found = 0;
737 for (d = 0; d < addon->num_chunks; ++d) {
739 total_found += FindAllCompoundDictionaryMatches(
741 cur_ix, min_length, max_length, base_offset - addon->chunk_offsets[d],
742 max_distance, matches + total_found, match_limit - total_found);
743 if (total_found == match_limit)
break;
744 if (total_found > 0) {
745 min_length = BackwardMatchLength(&matches[total_found - 1]);
751#if defined(__cplusplus) || defined(c_plusplus)
uint8_t size_bits_by_length[32]
Number of bits to encode index of dictionary word in a bucket.
Definition: dictionary.h:30
Definition: encoder_dict.h:61
Definition: compound_dictionary.h:57
void * extra[4]
Dynamically allocated areas; regular hasher uses one or two allocations; "composite" hasher uses up t...
Definition: hash.h:39
BROTLI_BOOL is_setup_
False before the fisrt invocation of HasherSetup (where "extra" memory) is allocated.
Definition: hash.h:45
BROTLI_BOOL is_prepared_
False if hasher needs to be "prepared" before use (before the first invocation of HasherSetup or afte...
Definition: hash.h:57
Definition: compound_dictionary.h:33
Definition: gen_corpora.c:58
Common types used in decoder and encoder API.
#define BROTLI_TRUE
Portable true replacement.
Definition: types.h:51
#define BROTLI_FALSE
Portable false replacement.
Definition: types.h:53
#define BROTLI_BOOL
A portable bool replacement.
Definition: types.h:49