Debian Codegraph @

c/enc/hash_rolling_inc.h

raw symbols SCIP
1 /* NOLINT(build/header_guard) */cxx . . $ `<file>/c/enc/hash_rolling_inc.h`/
2 /* Copyright 2018 Google Inc. All Rights Reserved.
3
4 Distributed under MIT license.
5 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6 */
7
8 /* template parameters: FN, JUMP, NUMBUCKETS, MASK, CHUNKLEN */
9 /* NUMBUCKETS / (MASK + 1) = probability of storing and using hash code. */
10 /* JUMP = skip bytes for speedup */
11
12 /* Rolling hash for long distance long string matches. Stores one position
13 per bucket, bucket key is computed over a long region. */
14
15 #define HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`! HASHERcxx . . $ `c/enc/hash.h:335:9`!()
16
17 static const uint32_tcxx . . $ uint32_t# FNcxx . . $ `c/enc/hash.h:225:9`!(kRollingHashMul32) = 69069;
18 static const uint32_tcxx . . $ uint32_t# FNcxx . . $ `c/enc/hash.h:225:9`!(kInvalidPos) = 0xffffffff;
19
20 /* This hasher uses a longer forward length, but returning a higher value here
21 will hurt compression by the main hasher when combined with a composite
22 hasher. The hasher tests for forward itself instead. */
23 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! size_t FNcxx . . $ `c/enc/hash.h:225:9`!(HashTypeLength)(void) { return 4; }
24 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! size_t FNcxx . . $ `c/enc/hash.h:225:9`!(StoreLookahead)(void) { return 4; }
25
26 /* Computes a code from a single byte. A lookup table of 256 values could be
27 used, but simply adding 1 works about as good. */
28 static uint32_tcxx . . $ uint32_t# FNcxx . . $ `c/enc/hash.h:225:9`!(HashByte)(uint8_tcxx . . $ uint8_t# byte) {
29 return (uint32_tcxx . . $ uint32_t#)byte + 1u;
30 }
31
32 static uint32_tcxx . . $ uint32_t# FNcxx . . $ `c/enc/hash.h:225:9`!(HashRollingFunctionInitial)(uint32_tcxx . . $ uint32_t# state, uint8_tcxx . . $ uint8_t# add,
33 uint32_tcxx . . $ uint32_t# factor) {
34 return (uint32_tcxx . . $ uint32_t#)(factor * state + FNcxx . . $ `c/enc/hash.h:225:9`!(HashByte)(add));
35 }
36
37 static uint32_tcxx . . $ uint32_t# FNcxx . . $ `c/enc/hash.h:225:9`!(HashRollingFunction)(uint32_tcxx . . $ uint32_t# state, uint8_tcxx . . $ uint8_t# add,
38 uint8_tcxx . . $ uint8_t# rem, uint32_tcxx . . $ uint32_t# factor,
39 uint32_tcxx . . $ uint32_t# factor_remove) {
40 return (uint32_tcxx . . $ uint32_t#)(factor * state +
41 FNcxx . . $ `c/enc/hash.h:225:9`!(HashByte)(add) - factor_remove * FNcxx . . $ `c/enc/hash.h:225:9`!(HashByte)(rem));
42 }
43
44 typedef struct HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`! {
45 uint32_tcxx . . $ uint32_t# statecxx . . $ HROLLING_FAST#state.;
46 uint32_tcxx . . $ uint32_t#* tablecxx . . $ HROLLING_FAST#table.;
47 size_t next_ixcxx . . $ HROLLING_FAST#next_ix.;
48
49 uint32_tcxx . . $ uint32_t# chunk_lencxx . . $ HROLLING_FAST#chunk_len.;
50 uint32_tcxx . . $ uint32_t# factorcxx . . $ HROLLING_FAST#factor.;
51 uint32_tcxx . . $ uint32_t# factor_removecxx . . $ HROLLING_FAST#factor_remove.;
52 } HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`!;
53
54 static void FNcxx . . $ `c/enc/hash.h:225:9`!(Initialize)(
55 HasherCommoncxx . . $ HasherCommon#* common, HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
56 const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params) {
57 size_t i;
58 self->statecxx . . $ HROLLING_FAST#state. = 0;
59 self->next_ixcxx . . $ HROLLING_FAST#next_ix. = 0;
60
61 self->factorcxx . . $ HROLLING_FAST#factor. = FNcxx . . $ `c/enc/hash.h:225:9`!(kRollingHashMul32);
62
63 /* Compute the factor of the oldest byte to remove: factor**steps modulo
64 0xffffffff (the multiplications rely on 32-bit overflow) */
65 self->factor_removecxx . . $ HROLLING_FAST#factor_remove. = 1;
66 for (i = 0; i < CHUNKLENcxx . . $ `c/enc/hash.h:336:9`!; i += JUMPcxx . . $ `c/enc/hash.h:337:9`!) {
67 self->factor_removecxx . . $ HROLLING_FAST#factor_remove. *= self->factorcxx . . $ HROLLING_FAST#factor.;
68 }
69
70 self->tablecxx . . $ HROLLING_FAST#table. = (uint32_tcxx . . $ uint32_t#*)common->extracxx . . $ $anonymous_type_5b62deaf4afff4b7_0#extra.[0];
71 for (i = 0; i < NUMBUCKETScxx . . $ `c/enc/hash.h:338:9`!; i++) {
72 self->tablecxx . . $ HROLLING_FAST#table.[i] = FNcxx . . $ `c/enc/hash.h:225:9`!(kInvalidPos);
73 }
74
75 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(params);
76 }
77
78 static void FNcxx . . $ `c/enc/hash.h:225:9`!(Prepare)(HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self, BROTLI_BOOLcxx . . $ `c/include/brotli/types.h:49:9`! one_shot,
79 size_t input_size, const uint8_tcxx . . $ uint8_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! data) {
80 size_t i;
81 /* Too small size, cannot use this hasher. */
82 if (input_size < CHUNKLENcxx . . $ `c/enc/hash.h:336:9`!) return;
83 self->statecxx . . $ HROLLING_FAST#state. = 0;
84 for (i = 0; i < CHUNKLENcxx . . $ `c/enc/hash.h:336:9`!; i += JUMPcxx . . $ `c/enc/hash.h:337:9`!) {
85 self->statecxx . . $ HROLLING_FAST#state. = FNcxx . . $ `c/enc/hash.h:225:9`!(HashRollingFunctionInitial)(
86 self->statecxx . . $ HROLLING_FAST#state., data[i], self->factorcxx . . $ HROLLING_FAST#factor.);
87 }
88 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(one_shot);
89 }
90
91 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(HashMemAllocInBytes)(
92 const BrotliEncoderParamscxx . . $ BrotliEncoderParams#* params, BROTLI_BOOLcxx . . $ `c/include/brotli/types.h:49:9`! one_shot,
93 size_t input_size, size_t* alloc_size) {
94 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(params);
95 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(one_shot);
96 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(input_size);
97 alloc_size[0] = NUMBUCKETScxx . . $ `c/enc/hash.h:338:9`! * sizeof(uint32_tcxx . . $ uint32_t#);
98 }
99
100 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(Store)(HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
101 const uint8_tcxx . . $ uint8_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! data, const size_t mask, const size_t ix) {
102 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(self);
103 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(data);
104 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(mask);
105 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(ix);
106 }
107
108 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(StoreRange)(HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
109 const uint8_tcxx . . $ uint8_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! data, const size_t mask,
110 const size_t ix_start, const size_t ix_end) {
111 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(self);
112 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(data);
113 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(mask);
114 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(ix_start);
115 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(ix_end);
116 }
117
118 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(StitchToPreviousBlock)(
119 HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
120 size_t num_bytes, size_t position, const uint8_tcxx . . $ uint8_t#* ringbuffer,
121 size_t ring_buffer_mask) {
122 /* In this case we must re-initialize the hasher from scratch from the
123 current position. */
124 size_t position_masked;
125 size_t available = num_bytes;
126 if ((position & (JUMPcxx . . $ `c/enc/hash.h:337:9`! - 1)) != 0) {
127 size_t diff = JUMPcxx . . $ `c/enc/hash.h:337:9`! - (position & (JUMPcxx . . $ `c/enc/hash.h:337:9`! - 1));
128 available = (diff > available) ? 0 : (available - diff);
129 position += diff;
130 }
131 position_masked = position & ring_buffer_mask;
132 /* wrapping around ringbuffer not handled. */
133 if (available > ring_buffer_mask - position_masked) {
134 available = ring_buffer_mask - position_masked;
135 }
136
137 FNcxx . . $ `c/enc/hash.h:225:9`!(Prepare)(self, BROTLI_FALSEcxx . . $ `c/include/brotli/types.h:53:9`!, available,
138 ringbuffer + (position & ring_buffer_mask));
139 self->next_ixcxx . . $ HROLLING_FAST#next_ix. = position;
140 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(num_bytes);
141 }
142
143 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(PrepareDistanceCache)(
144 HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
145 int* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! distance_cache) {
146 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(self);
147 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(distance_cache);
148 }
149
150 static BROTLI_INLINEcxx . . $ `c/common/platform.h:119:9`! void FNcxx . . $ `c/enc/hash.h:225:9`!(FindLongestMatch)(
151 HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`!* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! self,
152 const BrotliEncoderDictionarycxx . . $ BrotliEncoderDictionary#* dictionary,
153 const uint8_tcxx . . $ uint8_t#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! data, const size_t ring_buffer_mask,
154 const int* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! distance_cache, const size_t cur_ix,
155 const size_t max_length, const size_t max_backward,
156 const size_t dictionary_distance, const size_t max_distance,
157 HasherSearchResultcxx . . $ HasherSearchResult#* BROTLI_RESTRICTcxx . . $ `c/common/platform.h:82:9`! out) {
158 const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
159 size_t pos;
160
161 if ((cur_ix & (JUMPcxx . . $ `c/enc/hash.h:337:9`! - 1)) != 0) return;
162
163 /* Not enough lookahead */
164 if (max_length < CHUNKLENcxx . . $ `c/enc/hash.h:336:9`!) return;
165
166 for (pos = self->next_ixcxx . . $ HROLLING_FAST#next_ix.; pos <= cur_ix; pos += JUMPcxx . . $ `c/enc/hash.h:337:9`!) {
167 uint32_tcxx . . $ uint32_t# code = self->statecxx . . $ HROLLING_FAST#state. & MASKcxx . . $ `c/enc/hash.h:339:9`!;
168
169 uint8_tcxx . . $ uint8_t# rem = data[pos & ring_buffer_mask];
170 uint8_tcxx . . $ uint8_t# add = data[(pos + CHUNKLENcxx . . $ `c/enc/hash.h:336:9`!) & ring_buffer_mask];
171 size_t found_ix = FNcxx . . $ `c/enc/hash.h:225:9`!(kInvalidPos);
172
173 self->statecxx . . $ HROLLING_FAST#state. = FNcxx . . $ `c/enc/hash.h:225:9`!(HashRollingFunction)(
174 self->statecxx . . $ HROLLING_FAST#state., add, rem, self->factorcxx . . $ HROLLING_FAST#factor., self->factor_removecxx . . $ HROLLING_FAST#factor_remove.);
175
176 if (code < NUMBUCKETScxx . . $ `c/enc/hash.h:338:9`!) {
177 found_ix = self->tablecxx . . $ HROLLING_FAST#table.[code];
178 self->tablecxx . . $ HROLLING_FAST#table.[code] = (uint32_tcxx . . $ uint32_t#)pos;
179 if (pos == cur_ix && found_ix != FNcxx . . $ `c/enc/hash.h:225:9`!(kInvalidPos)) {
180 /* The cast to 32-bit makes backward distances up to 4GB work even
181 if cur_ix is above 4GB, despite using 32-bit values in the table. */
182 size_t backward = (uint32_tcxx . . $ uint32_t#)(cur_ix - found_ix);
183 if (backward <= max_backward) {
184 const size_t found_ix_masked = found_ix & ring_buffer_mask;
185 const size_t len = FindMatchLengthWithLimitcxx . . $ FindMatchLengthWithLimit(c2ccccc71581acc8).(&data[found_ix_masked],
186 &data[cur_ix_masked],
187 max_length);
188 if (len >= 4 && len > out->lencxx . . $ HasherSearchResult#len.) {
189 score_tcxx . . $ `c/enc/hash.h:57:9`! score = BackwardReferenceScorecxx . . $ BackwardReferenceScore(80bad76eed2d2579).(len, backward);
190 if (score > out->scorecxx . . $ HasherSearchResult#score.) {
191 out->lencxx . . $ HasherSearchResult#len. = len;
192 out->distancecxx . . $ HasherSearchResult#distance. = backward;
193 out->scorecxx . . $ HasherSearchResult#score. = score;
194 out->len_code_deltacxx . . $ HasherSearchResult#len_code_delta. = 0;
195 }
196 }
197 }
198 }
199 }
200 }
201
202 self->next_ixcxx . . $ HROLLING_FAST#next_ix. = cur_ix + JUMPcxx . . $ `c/enc/hash.h:337:9`!;
203
204 /* NOTE: this hasher does not search in the dictionary. It is used as
205 backup-hasher, the main hasher already searches in it. */
206 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(dictionary);
207 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(distance_cache);
208 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(dictionary_distance);
209 BROTLI_UNUSEDcxx . . $ `c/common/platform.h:514:9`!(max_distance);
210 }
211
212 #undef HashRollingcxx . . $ `c/enc/hash_rolling_inc.h:15:9`!