Coverage Report

Created: 2025-11-16 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/third_party/brotli/c/enc/hash.h
Line
Count
Source
1
/* Copyright 2010 Google Inc. All Rights Reserved.
2
3
   Distributed under MIT license.
4
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
/* A (forgetful) hash table to the data seen by the compressor, to
8
   help create backward references to previous data. */
9
10
#ifndef BROTLI_ENC_HASH_H_
11
#define BROTLI_ENC_HASH_H_
12
13
#include "../common/constants.h"
14
#include "../common/dictionary.h"
15
#include "../common/platform.h"
16
#include "compound_dictionary.h"
17
#include "encoder_dict.h"
18
#include "fast_log.h"
19
#include "find_match_length.h"
20
#include "hash_base.h"
21
#include "matching_tag_mask.h"
22
#include "memory.h"
23
#include "params.h"
24
#include "quality.h"
25
#include "static_dict.h"
26
27
#if defined(__cplusplus) || defined(c_plusplus)
28
extern "C" {
29
#endif
30
31
typedef struct {
32
  /**
33
   * Dynamically allocated areas; regular hasher uses one or two allocations;
34
   * "composite" hasher uses up to 4 allocations.
35
   */
36
  void* extra[4];
37
38
  /**
39
   * False before the first invocation of HasherSetup (where "extra" memory)
40
   * is allocated.
41
   */
42
  BROTLI_BOOL is_setup_;
43
44
  size_t dict_num_lookups;
45
  size_t dict_num_matches;
46
47
  BrotliHasherParams params;
48
49
  /**
50
   * False if hasher needs to be "prepared" before use (before the first
51
   * invocation of HasherSetup or after HasherReset). "preparation" is hasher
52
   * data initialization (using input ringbuffer).
53
   */
54
  BROTLI_BOOL is_prepared_;
55
} HasherCommon;
56
57
0
#define score_t size_t
58
59
static const uint32_t kCutoffTransformsCount = 10;
60
/*   0,  12,   27,    23,    42,    63,    56,    48,    59,    64 */
61
/* 0+0, 4+8, 8+19, 12+11, 16+26, 20+43, 24+32, 28+20, 32+27, 36+28 */
62
static const uint64_t kCutoffTransforms =
63
    BROTLI_MAKE_UINT64_T(0x071B520A, 0xDA2D3200);
64
65
typedef struct HasherSearchResult {
66
  size_t len;
67
  size_t distance;
68
  score_t score;
69
  int len_code_delta; /* == len_code - len */
70
} HasherSearchResult;
71
72
static BROTLI_INLINE void PrepareDistanceCache(
73
0
    int* BROTLI_RESTRICT distance_cache, const int num_distances) {
74
0
  if (num_distances > 4) {
75
0
    int last_distance = distance_cache[0];
76
0
    distance_cache[4] = last_distance - 1;
77
0
    distance_cache[5] = last_distance + 1;
78
0
    distance_cache[6] = last_distance - 2;
79
0
    distance_cache[7] = last_distance + 2;
80
0
    distance_cache[8] = last_distance - 3;
81
0
    distance_cache[9] = last_distance + 3;
82
0
    if (num_distances > 10) {
83
0
      int next_last_distance = distance_cache[1];
84
0
      distance_cache[10] = next_last_distance - 1;
85
0
      distance_cache[11] = next_last_distance + 1;
86
0
      distance_cache[12] = next_last_distance - 2;
87
0
      distance_cache[13] = next_last_distance + 2;
88
0
      distance_cache[14] = next_last_distance - 3;
89
0
      distance_cache[15] = next_last_distance + 3;
90
0
    }
91
0
  }
92
0
}
Unexecuted instantiation: encode.c:PrepareDistanceCache
Unexecuted instantiation: encoder_dict.c:PrepareDistanceCache
Unexecuted instantiation: backward_references.c:PrepareDistanceCache
Unexecuted instantiation: backward_references_hq.c:PrepareDistanceCache
93
94
0
#define BROTLI_LITERAL_BYTE_SCORE 135
95
0
#define BROTLI_DISTANCE_BIT_PENALTY 30
96
/* Score must be positive after applying maximal penalty. */
97
0
#define BROTLI_SCORE_BASE (BROTLI_DISTANCE_BIT_PENALTY * 8 * sizeof(size_t))
98
99
/* Usually, we always choose the longest backward reference. This function
100
   allows for the exception of that rule.
101
102
   If we choose a backward reference that is further away, it will
103
   usually be coded with more bits. We approximate this by assuming
104
   log2(distance). If the distance can be expressed in terms of the
105
   last four distances, we use some heuristic constants to estimate
106
   the bits cost. For the first up to four literals we use the bit
107
   cost of the literals from the literal cost model, after that we
108
   use the average bit cost of the cost model.
109
110
   This function is used to sometimes discard a longer backward reference
111
   when it is not much longer and the bit cost for encoding it is more
112
   than the saved literals.
113
114
   backward_reference_offset MUST be positive. */
115
static BROTLI_INLINE score_t BackwardReferenceScore(
116
0
    size_t copy_length, size_t backward_reference_offset) {
117
0
  return BROTLI_SCORE_BASE + BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length -
118
0
      BROTLI_DISTANCE_BIT_PENALTY * Log2FloorNonZero(backward_reference_offset);
119
0
}
Unexecuted instantiation: encode.c:BackwardReferenceScore
Unexecuted instantiation: encoder_dict.c:BackwardReferenceScore
Unexecuted instantiation: backward_references.c:BackwardReferenceScore
Unexecuted instantiation: backward_references_hq.c:BackwardReferenceScore
120
121
static BROTLI_INLINE score_t BackwardReferenceScoreUsingLastDistance(
122
0
    size_t copy_length) {
123
0
  return BROTLI_LITERAL_BYTE_SCORE * (score_t)copy_length +
124
0
      BROTLI_SCORE_BASE + 15;
125
0
}
Unexecuted instantiation: encode.c:BackwardReferenceScoreUsingLastDistance
Unexecuted instantiation: encoder_dict.c:BackwardReferenceScoreUsingLastDistance
Unexecuted instantiation: backward_references.c:BackwardReferenceScoreUsingLastDistance
Unexecuted instantiation: backward_references_hq.c:BackwardReferenceScoreUsingLastDistance
126
127
static BROTLI_INLINE score_t BackwardReferencePenaltyUsingLastDistance(
128
0
    size_t distance_short_code) {
129
0
  return (score_t)39 + ((0x1CA10 >> (distance_short_code & 0xE)) & 0xE);
130
0
}
Unexecuted instantiation: encode.c:BackwardReferencePenaltyUsingLastDistance
Unexecuted instantiation: encoder_dict.c:BackwardReferencePenaltyUsingLastDistance
Unexecuted instantiation: backward_references.c:BackwardReferencePenaltyUsingLastDistance
Unexecuted instantiation: backward_references_hq.c:BackwardReferencePenaltyUsingLastDistance
131
132
static BROTLI_INLINE BROTLI_BOOL TestStaticDictionaryItem(
133
    const BrotliEncoderDictionary* dictionary, size_t len, size_t word_idx,
134
    const uint8_t* data, size_t max_length, size_t max_backward,
135
0
    size_t max_distance, HasherSearchResult* out) {
136
0
  size_t offset;
137
0
  size_t matchlen;
138
0
  size_t backward;
139
0
  score_t score;
140
0
  offset = dictionary->words->offsets_by_length[len] + len * word_idx;
141
0
  if (len > max_length) {
142
0
    return BROTLI_FALSE;
143
0
  }
144
145
0
  matchlen =
146
0
      FindMatchLengthWithLimit(data, &dictionary->words->data[offset], len);
147
0
  if (matchlen + dictionary->cutoffTransformsCount <= len || matchlen == 0) {
148
0
    return BROTLI_FALSE;
149
0
  }
150
0
  {
151
0
    size_t cut = len - matchlen;
152
0
    size_t transform_id = (cut << 2) +
153
0
        (size_t)((dictionary->cutoffTransforms >> (cut * 6)) & 0x3F);
154
0
    backward = max_backward + 1 + word_idx +
155
0
        (transform_id << dictionary->words->size_bits_by_length[len]);
156
0
  }
157
0
  if (backward > max_distance) {
158
0
    return BROTLI_FALSE;
159
0
  }
160
0
  score = BackwardReferenceScore(matchlen, backward);
161
0
  if (score < out->score) {
162
0
    return BROTLI_FALSE;
163
0
  }
164
0
  out->len = matchlen;
165
0
  out->len_code_delta = (int)len - (int)matchlen;
166
0
  out->distance = backward;
167
0
  out->score = score;
168
0
  return BROTLI_TRUE;
169
0
}
Unexecuted instantiation: encode.c:TestStaticDictionaryItem
Unexecuted instantiation: encoder_dict.c:TestStaticDictionaryItem
Unexecuted instantiation: backward_references.c:TestStaticDictionaryItem
Unexecuted instantiation: backward_references_hq.c:TestStaticDictionaryItem
170
171
static BROTLI_INLINE void SearchInStaticDictionary(
172
    const BrotliEncoderDictionary* dictionary,
173
    HasherCommon* common, const uint8_t* data, size_t max_length,
174
    size_t max_backward, size_t max_distance,
175
0
    HasherSearchResult* out, BROTLI_BOOL shallow) {
176
0
  size_t key;
177
0
  size_t i;
178
0
  if (common->dict_num_matches < (common->dict_num_lookups >> 7)) {
179
0
    return;
180
0
  }
181
0
  key = Hash14(data) << 1;
182
0
  for (i = 0; i < (shallow ? 1u : 2u); ++i, ++key) {
183
0
    common->dict_num_lookups++;
184
0
    if (dictionary->hash_table_lengths[key] != 0) {
185
0
      BROTLI_BOOL item_matches = TestStaticDictionaryItem(
186
0
          dictionary, dictionary->hash_table_lengths[key],
187
0
          dictionary->hash_table_words[key], data,
188
0
          max_length, max_backward, max_distance, out);
189
0
      if (item_matches) {
190
0
        common->dict_num_matches++;
191
0
      }
192
0
    }
193
0
  }
194
0
}
Unexecuted instantiation: encode.c:SearchInStaticDictionary
Unexecuted instantiation: encoder_dict.c:SearchInStaticDictionary
Unexecuted instantiation: backward_references.c:SearchInStaticDictionary
Unexecuted instantiation: backward_references_hq.c:SearchInStaticDictionary
195
196
typedef struct BackwardMatch {
197
  uint32_t distance;
198
  uint32_t length_and_code;
199
} BackwardMatch;
200
201
static BROTLI_INLINE void InitBackwardMatch(BackwardMatch* self,
202
0
    size_t dist, size_t len) {
203
0
  self->distance = (uint32_t)dist;
204
0
  self->length_and_code = (uint32_t)(len << 5);
205
0
}
Unexecuted instantiation: encode.c:InitBackwardMatch
Unexecuted instantiation: encoder_dict.c:InitBackwardMatch
Unexecuted instantiation: backward_references.c:InitBackwardMatch
Unexecuted instantiation: backward_references_hq.c:InitBackwardMatch
206
207
static BROTLI_INLINE void InitDictionaryBackwardMatch(BackwardMatch* self,
208
0
    size_t dist, size_t len, size_t len_code) {
209
0
  self->distance = (uint32_t)dist;
210
0
  self->length_and_code =
211
0
      (uint32_t)((len << 5) | (len == len_code ? 0 : len_code));
212
0
}
Unexecuted instantiation: encode.c:InitDictionaryBackwardMatch
Unexecuted instantiation: encoder_dict.c:InitDictionaryBackwardMatch
Unexecuted instantiation: backward_references.c:InitDictionaryBackwardMatch
Unexecuted instantiation: backward_references_hq.c:InitDictionaryBackwardMatch
213
214
0
static BROTLI_INLINE size_t BackwardMatchLength(const BackwardMatch* self) {
215
0
  return self->length_and_code >> 5;
216
0
}
Unexecuted instantiation: encode.c:BackwardMatchLength
Unexecuted instantiation: encoder_dict.c:BackwardMatchLength
Unexecuted instantiation: backward_references.c:BackwardMatchLength
Unexecuted instantiation: backward_references_hq.c:BackwardMatchLength
217
218
0
static BROTLI_INLINE size_t BackwardMatchLengthCode(const BackwardMatch* self) {
219
0
  size_t code = self->length_and_code & 31;
220
0
  return code ? code : BackwardMatchLength(self);
221
0
}
Unexecuted instantiation: encode.c:BackwardMatchLengthCode
Unexecuted instantiation: encoder_dict.c:BackwardMatchLengthCode
Unexecuted instantiation: backward_references.c:BackwardMatchLengthCode
Unexecuted instantiation: backward_references_hq.c:BackwardMatchLengthCode
222
223
0
#define EXPAND_CAT(a, b) CAT(a, b)
224
0
#define CAT(a, b) a ## b
225
0
#define FN(X) EXPAND_CAT(X, HASHER())
226
227
#define HASHER() H10
228
0
#define BUCKET_BITS 17
229
0
#define MAX_TREE_SEARCH_DEPTH 64
230
0
#define MAX_TREE_COMP_LENGTH 128
231
#include "hash_to_binary_tree_inc.h"  /* NOLINT(build/include) */
232
#undef MAX_TREE_SEARCH_DEPTH
233
#undef MAX_TREE_COMP_LENGTH
234
#undef BUCKET_BITS
235
#undef HASHER
236
/* MAX_NUM_MATCHES == 64 + MAX_TREE_SEARCH_DEPTH */
237
0
#define MAX_NUM_MATCHES_H10 128
238
239
/* For BUCKET_SWEEP_BITS == 0, enabling the dictionary lookup makes compression
240
   a little faster (0.5% - 1%) and it compresses 0.15% better on small text
241
   and HTML inputs. */
242
243
#define HASHER() H2
244
0
#define BUCKET_BITS 16
245
0
#define BUCKET_SWEEP_BITS 0
246
0
#define HASH_LEN 5
247
0
#define USE_DICTIONARY 1
248
#include "hash_longest_match_quickly_inc.h"  /* NOLINT(build/include) */
249
#undef BUCKET_SWEEP_BITS
250
#undef USE_DICTIONARY
251
#undef HASHER
252
253
#define HASHER() H3
254
0
#define BUCKET_SWEEP_BITS 1
255
0
#define USE_DICTIONARY 0
256
#include "hash_longest_match_quickly_inc.h"  /* NOLINT(build/include) */
257
#undef USE_DICTIONARY
258
#undef BUCKET_SWEEP_BITS
259
#undef BUCKET_BITS
260
#undef HASHER
261
262
#define HASHER() H4
263
0
#define BUCKET_BITS 17
264
0
#define BUCKET_SWEEP_BITS 2
265
0
#define USE_DICTIONARY 1
266
#include "hash_longest_match_quickly_inc.h"  /* NOLINT(build/include) */
267
#undef USE_DICTIONARY
268
#undef HASH_LEN
269
#undef BUCKET_SWEEP_BITS
270
#undef BUCKET_BITS
271
#undef HASHER
272
273
#define HASHER() H5
274
#include "hash_longest_match_inc.h"  /* NOLINT(build/include) */
275
#undef HASHER
276
277
#define HASHER() H6
278
#include "hash_longest_match64_inc.h"  /* NOLINT(build/include) */
279
#undef HASHER
280
281
#if defined(BROTLI_MAX_SIMD_QUALITY)
282
#define HASHER() H58
283
#include "hash_longest_match_simd_inc.h" /* NOLINT(build/include) */
284
#undef HASHER
285
286
#define HASHER() H68
287
#include "hash_longest_match64_simd_inc.h" /* NOLINT(build/include) */
288
#undef HASHER
289
#endif
290
291
0
#define BUCKET_BITS 15
292
293
0
#define NUM_LAST_DISTANCES_TO_CHECK 4
294
0
#define NUM_BANKS 1
295
0
#define BANK_BITS 16
296
#define HASHER() H40
297
#include "hash_forgetful_chain_inc.h"  /* NOLINT(build/include) */
298
#undef HASHER
299
#undef NUM_LAST_DISTANCES_TO_CHECK
300
301
0
#define NUM_LAST_DISTANCES_TO_CHECK 10
302
#define HASHER() H41
303
#include "hash_forgetful_chain_inc.h"  /* NOLINT(build/include) */
304
#undef HASHER
305
#undef NUM_LAST_DISTANCES_TO_CHECK
306
#undef NUM_BANKS
307
#undef BANK_BITS
308
309
0
#define NUM_LAST_DISTANCES_TO_CHECK 16
310
0
#define NUM_BANKS 512
311
0
#define BANK_BITS 9
312
#define HASHER() H42
313
#include "hash_forgetful_chain_inc.h"  /* NOLINT(build/include) */
314
#undef HASHER
315
#undef NUM_LAST_DISTANCES_TO_CHECK
316
#undef NUM_BANKS
317
#undef BANK_BITS
318
319
#undef BUCKET_BITS
320
321
#define HASHER() H54
322
0
#define BUCKET_BITS 20
323
0
#define BUCKET_SWEEP_BITS 2
324
0
#define HASH_LEN 7
325
0
#define USE_DICTIONARY 0
326
#include "hash_longest_match_quickly_inc.h"  /* NOLINT(build/include) */
327
#undef USE_DICTIONARY
328
#undef HASH_LEN
329
#undef BUCKET_SWEEP_BITS
330
#undef BUCKET_BITS
331
#undef HASHER
332
333
/* fast large window hashers */
334
335
#define HASHER() HROLLING_FAST
336
0
#define CHUNKLEN 32
337
0
#define JUMP 4
338
0
#define NUMBUCKETS 16777216
339
0
#define MASK ((NUMBUCKETS * 64) - 1)
340
#include "hash_rolling_inc.h"  /* NOLINT(build/include) */
341
#undef JUMP
342
#undef HASHER
343
344
345
#define HASHER() HROLLING
346
0
#define JUMP 1
347
#include "hash_rolling_inc.h"  /* NOLINT(build/include) */
348
#undef MASK
349
#undef NUMBUCKETS
350
#undef JUMP
351
#undef CHUNKLEN
352
#undef HASHER
353
354
#define HASHER() H35
355
#define HASHER_A H3
356
#define HASHER_B HROLLING_FAST
357
#include "hash_composite_inc.h"  /* NOLINT(build/include) */
358
#undef HASHER_A
359
#undef HASHER_B
360
#undef HASHER
361
362
#define HASHER() H55
363
#define HASHER_A H54
364
#define HASHER_B HROLLING_FAST
365
#include "hash_composite_inc.h"  /* NOLINT(build/include) */
366
#undef HASHER_A
367
#undef HASHER_B
368
#undef HASHER
369
370
#define HASHER() H65
371
#define HASHER_A H6
372
#define HASHER_B HROLLING
373
#include "hash_composite_inc.h"  /* NOLINT(build/include) */
374
#undef HASHER_A
375
#undef HASHER_B
376
#undef HASHER
377
378
#undef FN
379
#undef CAT
380
#undef EXPAND_CAT
381
382
#if defined(BROTLI_MAX_SIMD_QUALITY)
383
#define FOR_SIMPLE_HASHERS(H) \
384
0
  H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54) H(58) H(68)
385
#else
386
#define FOR_SIMPLE_HASHERS(H) \
387
  H(2) H(3) H(4) H(5) H(6) H(40) H(41) H(42) H(54)
388
#endif
389
0
#define FOR_COMPOSITE_HASHERS(H) H(35) H(55) H(65)
390
0
#define FOR_GENERIC_HASHERS(H) FOR_SIMPLE_HASHERS(H) FOR_COMPOSITE_HASHERS(H)
391
0
#define FOR_ALL_HASHERS(H) FOR_GENERIC_HASHERS(H) H(10)
392
393
typedef struct {
394
  HasherCommon common;
395
396
  union {
397
#define MEMBER_(N) \
398
    H ## N _H ## N;
399
    FOR_ALL_HASHERS(MEMBER_)
400
#undef MEMBER_
401
  } privat;
402
} Hasher;
403
404
/* MUST be invoked before any other method. */
405
0
static BROTLI_INLINE void HasherInit(Hasher* hasher) {
406
0
  hasher->common.is_setup_ = BROTLI_FALSE;
407
0
  hasher->common.extra[0] = NULL;
408
0
  hasher->common.extra[1] = NULL;
409
0
  hasher->common.extra[2] = NULL;
410
0
  hasher->common.extra[3] = NULL;
411
0
}
Unexecuted instantiation: encode.c:HasherInit
Unexecuted instantiation: encoder_dict.c:HasherInit
Unexecuted instantiation: backward_references.c:HasherInit
Unexecuted instantiation: backward_references_hq.c:HasherInit
412
413
0
static BROTLI_INLINE void DestroyHasher(MemoryManager* m, Hasher* hasher) {
414
0
  if (hasher->common.extra[0] != NULL) BROTLI_FREE(m, hasher->common.extra[0]);
415
0
  if (hasher->common.extra[1] != NULL) BROTLI_FREE(m, hasher->common.extra[1]);
416
0
  if (hasher->common.extra[2] != NULL) BROTLI_FREE(m, hasher->common.extra[2]);
417
0
  if (hasher->common.extra[3] != NULL) BROTLI_FREE(m, hasher->common.extra[3]);
418
0
}
Unexecuted instantiation: encode.c:DestroyHasher
Unexecuted instantiation: encoder_dict.c:DestroyHasher
Unexecuted instantiation: backward_references.c:DestroyHasher
Unexecuted instantiation: backward_references_hq.c:DestroyHasher
419
420
0
static BROTLI_INLINE void HasherReset(Hasher* hasher) {
421
0
  hasher->common.is_prepared_ = BROTLI_FALSE;
422
0
}
Unexecuted instantiation: encode.c:HasherReset
Unexecuted instantiation: encoder_dict.c:HasherReset
Unexecuted instantiation: backward_references.c:HasherReset
Unexecuted instantiation: backward_references_hq.c:HasherReset
423
424
static BROTLI_INLINE void HasherSize(const BrotliEncoderParams* params,
425
0
    BROTLI_BOOL one_shot, const size_t input_size, size_t* alloc_size) {
426
0
  switch (params->hasher.type) {
427
0
#define SIZE_(N)                                                           \
428
0
    case N:                                                                \
429
0
      HashMemAllocInBytesH ## N(params, one_shot, input_size, alloc_size); \
430
0
      break;
431
0
    FOR_ALL_HASHERS(SIZE_)
432
0
#undef SIZE_
433
0
    default:
434
0
      break;
435
0
  }
436
0
}
Unexecuted instantiation: encode.c:HasherSize
Unexecuted instantiation: encoder_dict.c:HasherSize
Unexecuted instantiation: backward_references.c:HasherSize
Unexecuted instantiation: backward_references_hq.c:HasherSize
437
438
static BROTLI_INLINE void HasherSetup(MemoryManager* m, Hasher* hasher,
439
    BrotliEncoderParams* params, const uint8_t* data, size_t position,
440
0
    size_t input_size, BROTLI_BOOL is_last) {
441
0
  BROTLI_BOOL one_shot = (position == 0 && is_last);
442
0
  if (!hasher->common.is_setup_) {
443
0
    size_t alloc_size[4] = {0};
444
0
    size_t i;
445
0
    ChooseHasher(params, &params->hasher);
446
0
    hasher->common.params = params->hasher;
447
0
    hasher->common.dict_num_lookups = 0;
448
0
    hasher->common.dict_num_matches = 0;
449
0
    HasherSize(params, one_shot, input_size, alloc_size);
450
0
    for (i = 0; i < 4; ++i) {
451
0
      if (alloc_size[i] == 0) continue;
452
0
      hasher->common.extra[i] = BROTLI_ALLOC(m, uint8_t, alloc_size[i]);
453
0
      if (BROTLI_IS_OOM(m) || BROTLI_IS_NULL(hasher->common.extra[i])) return;
454
0
    }
455
0
    switch (hasher->common.params.type) {
456
0
#define INITIALIZE_(N)                        \
457
0
      case N:                                 \
458
0
        InitializeH ## N(&hasher->common,     \
459
0
            &hasher->privat._H ## N, params); \
460
0
        break;
461
0
      FOR_ALL_HASHERS(INITIALIZE_);
462
0
#undef INITIALIZE_
463
0
      default:
464
0
        break;
465
0
    }
466
0
    HasherReset(hasher);
467
0
    hasher->common.is_setup_ = BROTLI_TRUE;
468
0
  }
469
470
0
  if (!hasher->common.is_prepared_) {
471
0
    switch (hasher->common.params.type) {
472
0
#define PREPARE_(N)                      \
473
0
      case N:                            \
474
0
        PrepareH ## N(                   \
475
0
            &hasher->privat._H ## N,     \
476
0
            one_shot, input_size, data); \
477
0
        break;
478
0
      FOR_ALL_HASHERS(PREPARE_)
479
0
#undef PREPARE_
480
0
      default: break;
481
0
    }
482
0
    hasher->common.is_prepared_ = BROTLI_TRUE;
483
0
  }
484
0
}
Unexecuted instantiation: encode.c:HasherSetup
Unexecuted instantiation: encoder_dict.c:HasherSetup
Unexecuted instantiation: backward_references.c:HasherSetup
Unexecuted instantiation: backward_references_hq.c:HasherSetup
485
486
static BROTLI_INLINE void InitOrStitchToPreviousBlock(
487
    MemoryManager* m, Hasher* hasher, const uint8_t* data, size_t mask,
488
    BrotliEncoderParams* params, size_t position, size_t input_size,
489
0
    BROTLI_BOOL is_last) {
490
0
  HasherSetup(m, hasher, params, data, position, input_size, is_last);
491
0
  if (BROTLI_IS_OOM(m)) return;
492
0
  switch (hasher->common.params.type) {
493
0
#define INIT_(N)                             \
494
0
    case N:                                  \
495
0
      StitchToPreviousBlockH ## N(           \
496
0
          &hasher->privat._H ## N,           \
497
0
          input_size, position, data, mask); \
498
0
    break;
499
0
    FOR_ALL_HASHERS(INIT_)
500
0
#undef INIT_
501
0
    default: break;
502
0
  }
503
0
}
Unexecuted instantiation: encode.c:InitOrStitchToPreviousBlock
Unexecuted instantiation: encoder_dict.c:InitOrStitchToPreviousBlock
Unexecuted instantiation: backward_references.c:InitOrStitchToPreviousBlock
Unexecuted instantiation: backward_references_hq.c:InitOrStitchToPreviousBlock
504
505
/* NB: when seamless dictionary-ring-buffer copies are implemented, don't forget
506
       to add proper guards for non-zero-BROTLI_PARAM_STREAM_OFFSET. */
507
static BROTLI_INLINE void FindCompoundDictionaryMatch(
508
    const PreparedDictionary* self, const uint8_t* BROTLI_RESTRICT data,
509
    const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
510
    const size_t cur_ix, const size_t max_length, const size_t distance_offset,
511
0
    const size_t max_distance, HasherSearchResult* BROTLI_RESTRICT out) {
512
0
  const uint32_t source_size = self->source_size;
513
0
  const size_t boundary = distance_offset - source_size;
514
0
  const uint32_t hash_bits = self->hash_bits;
515
0
  const uint32_t bucket_bits = self->bucket_bits;
516
0
  const uint32_t slot_bits = self->slot_bits;
517
518
0
  const uint32_t hash_shift = 64u - bucket_bits;
519
0
  const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits);
520
0
  const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits);
521
522
0
  const uint32_t* slot_offsets = (uint32_t*)(&self[1]);
523
0
  const uint16_t* heads = (uint16_t*)(&slot_offsets[(size_t)1u << slot_bits]);
524
0
  const uint32_t* items = (uint32_t*)(&heads[(size_t)1u << bucket_bits]);
525
0
  const uint8_t* source = NULL;
526
527
0
  const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
528
0
  score_t best_score = out->score;
529
0
  size_t best_len = out->len;
530
0
  size_t i;
531
0
  const uint64_t h =
532
0
      (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) *
533
0
      kPreparedDictionaryHashMul64Long;
534
0
  const uint32_t key = (uint32_t)(h >> hash_shift);
535
0
  const uint32_t slot = key & slot_mask;
536
0
  const uint32_t head = heads[key];
537
0
  const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head];
538
0
  uint32_t item = (head == 0xFFFF) ? 1 : 0;
539
540
0
  const void* tail = (void*)&items[self->num_items];
541
0
  if (self->magic == kPreparedDictionaryMagic) {
542
0
    source = (const uint8_t*)tail;
543
0
  } else {
544
    /* kLeanPreparedDictionaryMagic */
545
0
    source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail);
546
0
  }
547
548
0
  BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask);
549
550
0
  for (i = 0; i < 4; ++i) {
551
0
    const size_t distance = (size_t)distance_cache[i];
552
0
    size_t offset;
553
0
    size_t limit;
554
0
    size_t len;
555
0
    if (distance <= boundary || distance > distance_offset) continue;
556
0
    offset = distance_offset - distance;
557
0
    limit = source_size - offset;
558
0
    limit = limit > max_length ? max_length : limit;
559
0
    len = FindMatchLengthWithLimit(&source[offset], &data[cur_ix_masked],
560
0
                                   limit);
561
0
    if (len >= 2) {
562
0
      score_t score = BackwardReferenceScoreUsingLastDistance(len);
563
0
      if (best_score < score) {
564
0
        if (i != 0) score -= BackwardReferencePenaltyUsingLastDistance(i);
565
0
        if (best_score < score) {
566
0
          best_score = score;
567
0
          if (len > best_len) best_len = len;
568
0
          out->len = len;
569
0
          out->len_code_delta = 0;
570
0
          out->distance = distance;
571
0
          out->score = best_score;
572
0
        }
573
0
      }
574
0
    }
575
0
  }
576
  /* we require matches of len >4, so increase best_len to 3, so we can compare
577
   * 4 bytes all the time. */
578
0
  if (best_len < 3) {
579
0
    best_len = 3;
580
0
  }
581
0
  while (item == 0) {
582
0
    size_t offset;
583
0
    size_t distance;
584
0
    size_t limit;
585
0
    item = *chain;
586
0
    chain++;
587
0
    offset = item & 0x7FFFFFFF;
588
0
    item &= 0x80000000;
589
0
    distance = distance_offset - offset;
590
0
    limit = source_size - offset;
591
0
    limit = (limit > max_length) ? max_length : limit;
592
0
    if (distance > max_distance) continue;
593
0
    if (cur_ix_masked + best_len > ring_buffer_mask || best_len >= limit ||
594
        /* compare 4 bytes ending at best_len + 1 */
595
0
        BrotliUnalignedRead32(&data[cur_ix_masked + best_len - 3]) !=
596
0
            BrotliUnalignedRead32(&source[offset + best_len - 3])) {
597
0
      continue;
598
0
    }
599
0
    {
600
0
      const size_t len = FindMatchLengthWithLimit(&source[offset],
601
0
                                                  &data[cur_ix_masked],
602
0
                                                  limit);
603
0
      if (len >= 4) {
604
0
        score_t score = BackwardReferenceScore(len, distance);
605
0
        if (best_score < score) {
606
0
          best_score = score;
607
0
          best_len = len;
608
0
          out->len = best_len;
609
0
          out->len_code_delta = 0;
610
0
          out->distance = distance;
611
0
          out->score = best_score;
612
0
        }
613
0
      }
614
0
    }
615
0
  }
616
0
}
Unexecuted instantiation: encode.c:FindCompoundDictionaryMatch
Unexecuted instantiation: encoder_dict.c:FindCompoundDictionaryMatch
Unexecuted instantiation: backward_references.c:FindCompoundDictionaryMatch
Unexecuted instantiation: backward_references_hq.c:FindCompoundDictionaryMatch
617
618
/* NB: when seamless dictionary-ring-buffer copies are implemented, don't forget
619
       to add proper guards for non-zero-BROTLI_PARAM_STREAM_OFFSET. */
620
static BROTLI_INLINE size_t FindAllCompoundDictionaryMatches(
621
    const PreparedDictionary* self, const uint8_t* BROTLI_RESTRICT data,
622
    const size_t ring_buffer_mask, const size_t cur_ix, const size_t min_length,
623
    const size_t max_length, const size_t distance_offset,
624
0
    const size_t max_distance, BackwardMatch* matches, size_t match_limit) {
625
0
  const uint32_t source_size = self->source_size;
626
0
  const uint32_t hash_bits = self->hash_bits;
627
0
  const uint32_t bucket_bits = self->bucket_bits;
628
0
  const uint32_t slot_bits = self->slot_bits;
629
630
0
  const uint32_t hash_shift = 64u - bucket_bits;
631
0
  const uint32_t slot_mask = (~((uint32_t)0U)) >> (32 - slot_bits);
632
0
  const uint64_t hash_mask = (~((uint64_t)0U)) >> (64 - hash_bits);
633
634
0
  const uint32_t* slot_offsets = (uint32_t*)(&self[1]);
635
0
  const uint16_t* heads = (uint16_t*)(&slot_offsets[(size_t)1u << slot_bits]);
636
0
  const uint32_t* items = (uint32_t*)(&heads[(size_t)1u << bucket_bits]);
637
0
  const uint8_t* source = NULL;
638
639
0
  const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
640
0
  size_t best_len = min_length;
641
0
  const uint64_t h =
642
0
      (BROTLI_UNALIGNED_LOAD64LE(&data[cur_ix_masked]) & hash_mask) *
643
0
      kPreparedDictionaryHashMul64Long;
644
0
  const uint32_t key = (uint32_t)(h >> hash_shift);
645
0
  const uint32_t slot = key & slot_mask;
646
0
  const uint32_t head = heads[key];
647
0
  const uint32_t* BROTLI_RESTRICT chain = &items[slot_offsets[slot] + head];
648
0
  uint32_t item = (head == 0xFFFF) ? 1 : 0;
649
0
  size_t found = 0;
650
651
0
  const void* tail = (void*)&items[self->num_items];
652
0
  if (self->magic == kPreparedDictionaryMagic) {
653
0
    source = (const uint8_t*)tail;
654
0
  } else {
655
    /* kLeanPreparedDictionaryMagic */
656
0
    source = (const uint8_t*)BROTLI_UNALIGNED_LOAD_PTR((const uint8_t**)tail);
657
0
  }
658
659
0
  BROTLI_DCHECK(cur_ix_masked + max_length <= ring_buffer_mask);
660
661
0
  while (item == 0) {
662
0
    size_t offset;
663
0
    size_t distance;
664
0
    size_t limit;
665
0
    size_t len;
666
0
    item = *chain;
667
0
    chain++;
668
0
    offset = item & 0x7FFFFFFF;
669
0
    item &= 0x80000000;
670
0
    distance = distance_offset - offset;
671
0
    limit = source_size - offset;
672
0
    limit = (limit > max_length) ? max_length : limit;
673
0
    if (distance > max_distance) continue;
674
0
    if (cur_ix_masked + best_len > ring_buffer_mask ||
675
0
        best_len >= limit ||
676
0
        data[cur_ix_masked + best_len] != source[offset + best_len]) {
677
0
      continue;
678
0
    }
679
0
    len = FindMatchLengthWithLimit(
680
0
        &source[offset], &data[cur_ix_masked], limit);
681
0
    if (len > best_len) {
682
0
      best_len = len;
683
0
      InitBackwardMatch(matches++, distance, len);
684
0
      found++;
685
0
      if (found == match_limit) break;
686
0
    }
687
0
  }
688
0
  return found;
689
0
}
Unexecuted instantiation: encode.c:FindAllCompoundDictionaryMatches
Unexecuted instantiation: encoder_dict.c:FindAllCompoundDictionaryMatches
Unexecuted instantiation: backward_references.c:FindAllCompoundDictionaryMatches
Unexecuted instantiation: backward_references_hq.c:FindAllCompoundDictionaryMatches
690
691
static BROTLI_INLINE void LookupCompoundDictionaryMatch(
692
    const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data,
693
    const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
694
    const size_t cur_ix, const size_t max_length,
695
    const size_t max_ring_buffer_distance, const size_t max_distance,
696
0
    HasherSearchResult* sr) {
697
0
  size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1;
698
0
  size_t d;
699
0
  for (d = 0; d < addon->num_chunks; ++d) {
700
    /* Only one prepared dictionary type is currently supported. */
701
0
    FindCompoundDictionaryMatch(
702
0
        (const PreparedDictionary*)addon->chunks[d], data, ring_buffer_mask,
703
0
        distance_cache, cur_ix, max_length,
704
0
        base_offset - addon->chunk_offsets[d], max_distance, sr);
705
0
  }
706
0
}
Unexecuted instantiation: encode.c:LookupCompoundDictionaryMatch
Unexecuted instantiation: encoder_dict.c:LookupCompoundDictionaryMatch
Unexecuted instantiation: backward_references.c:LookupCompoundDictionaryMatch
Unexecuted instantiation: backward_references_hq.c:LookupCompoundDictionaryMatch
707
708
static BROTLI_INLINE size_t LookupAllCompoundDictionaryMatches(
709
    const CompoundDictionary* addon, const uint8_t* BROTLI_RESTRICT data,
710
    const size_t ring_buffer_mask, const size_t cur_ix, size_t min_length,
711
    const size_t max_length, const size_t max_ring_buffer_distance,
712
    const size_t max_distance, BackwardMatch* matches,
713
0
    size_t match_limit) {
714
0
  size_t base_offset = max_ring_buffer_distance + 1 + addon->total_size - 1;
715
0
  size_t d;
716
0
  size_t total_found = 0;
717
0
  for (d = 0; d < addon->num_chunks; ++d) {
718
    /* Only one prepared dictionary type is currently supported. */
719
0
    total_found += FindAllCompoundDictionaryMatches(
720
0
        (const PreparedDictionary*)addon->chunks[d], data, ring_buffer_mask,
721
0
        cur_ix, min_length, max_length, base_offset - addon->chunk_offsets[d],
722
0
        max_distance, matches + total_found, match_limit - total_found);
723
0
    if (total_found == match_limit) break;
724
0
    if (total_found > 0) {
725
0
      min_length = BackwardMatchLength(&matches[total_found - 1]);
726
0
    }
727
0
  }
728
0
  return total_found;
729
0
}
Unexecuted instantiation: encode.c:LookupAllCompoundDictionaryMatches
Unexecuted instantiation: encoder_dict.c:LookupAllCompoundDictionaryMatches
Unexecuted instantiation: backward_references.c:LookupAllCompoundDictionaryMatches
Unexecuted instantiation: backward_references_hq.c:LookupAllCompoundDictionaryMatches
730
731
#if defined(__cplusplus) || defined(c_plusplus)
732
}  /* extern "C" */
733
#endif
734
735
#endif  /* BROTLI_ENC_HASH_H_ */