1*f4ee7fbaSAndroid Build Coastguard Worker /* NOLINT(build/header_guard) */
2*f4ee7fbaSAndroid Build Coastguard Worker /* Copyright 2018 Google Inc. All Rights Reserved.
3*f4ee7fbaSAndroid Build Coastguard Worker
4*f4ee7fbaSAndroid Build Coastguard Worker Distributed under MIT license.
5*f4ee7fbaSAndroid Build Coastguard Worker See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
6*f4ee7fbaSAndroid Build Coastguard Worker */
7*f4ee7fbaSAndroid Build Coastguard Worker
8*f4ee7fbaSAndroid Build Coastguard Worker /* template parameters: FN, HASHER_A, HASHER_B */
9*f4ee7fbaSAndroid Build Coastguard Worker
10*f4ee7fbaSAndroid Build Coastguard Worker /* Composite hasher: This hasher allows to combine two other hashers, HASHER_A
11*f4ee7fbaSAndroid Build Coastguard Worker and HASHER_B. */
12*f4ee7fbaSAndroid Build Coastguard Worker
13*f4ee7fbaSAndroid Build Coastguard Worker #define HashComposite HASHER()
14*f4ee7fbaSAndroid Build Coastguard Worker
15*f4ee7fbaSAndroid Build Coastguard Worker #define FN_A(X) EXPAND_CAT(X, HASHER_A)
16*f4ee7fbaSAndroid Build Coastguard Worker #define FN_B(X) EXPAND_CAT(X, HASHER_B)
17*f4ee7fbaSAndroid Build Coastguard Worker
FN(HashTypeLength)18*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE size_t FN(HashTypeLength)(void) {
19*f4ee7fbaSAndroid Build Coastguard Worker size_t a = FN_A(HashTypeLength)();
20*f4ee7fbaSAndroid Build Coastguard Worker size_t b = FN_B(HashTypeLength)();
21*f4ee7fbaSAndroid Build Coastguard Worker return a > b ? a : b;
22*f4ee7fbaSAndroid Build Coastguard Worker }
23*f4ee7fbaSAndroid Build Coastguard Worker
FN(StoreLookahead)24*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE size_t FN(StoreLookahead)(void) {
25*f4ee7fbaSAndroid Build Coastguard Worker size_t a = FN_A(StoreLookahead)();
26*f4ee7fbaSAndroid Build Coastguard Worker size_t b = FN_B(StoreLookahead)();
27*f4ee7fbaSAndroid Build Coastguard Worker return a > b ? a : b;
28*f4ee7fbaSAndroid Build Coastguard Worker }
29*f4ee7fbaSAndroid Build Coastguard Worker
30*f4ee7fbaSAndroid Build Coastguard Worker typedef struct HashComposite {
31*f4ee7fbaSAndroid Build Coastguard Worker HASHER_A ha;
32*f4ee7fbaSAndroid Build Coastguard Worker HASHER_B hb;
33*f4ee7fbaSAndroid Build Coastguard Worker HasherCommon hb_common;
34*f4ee7fbaSAndroid Build Coastguard Worker
35*f4ee7fbaSAndroid Build Coastguard Worker /* Shortcuts. */
36*f4ee7fbaSAndroid Build Coastguard Worker void* extra;
37*f4ee7fbaSAndroid Build Coastguard Worker HasherCommon* common;
38*f4ee7fbaSAndroid Build Coastguard Worker
39*f4ee7fbaSAndroid Build Coastguard Worker BROTLI_BOOL fresh;
40*f4ee7fbaSAndroid Build Coastguard Worker const BrotliEncoderParams* params;
41*f4ee7fbaSAndroid Build Coastguard Worker } HashComposite;
42*f4ee7fbaSAndroid Build Coastguard Worker
FN(Initialize)43*f4ee7fbaSAndroid Build Coastguard Worker static void FN(Initialize)(HasherCommon* common,
44*f4ee7fbaSAndroid Build Coastguard Worker HashComposite* BROTLI_RESTRICT self, const BrotliEncoderParams* params) {
45*f4ee7fbaSAndroid Build Coastguard Worker self->common = common;
46*f4ee7fbaSAndroid Build Coastguard Worker self->extra = common->extra;
47*f4ee7fbaSAndroid Build Coastguard Worker
48*f4ee7fbaSAndroid Build Coastguard Worker self->hb_common = *self->common;
49*f4ee7fbaSAndroid Build Coastguard Worker self->fresh = BROTLI_TRUE;
50*f4ee7fbaSAndroid Build Coastguard Worker self->params = params;
51*f4ee7fbaSAndroid Build Coastguard Worker /* TODO: Initialize of the hashers is defered to Prepare (and params
52*f4ee7fbaSAndroid Build Coastguard Worker remembered here) because we don't get the one_shot and input_size params
53*f4ee7fbaSAndroid Build Coastguard Worker here that are needed to know the memory size of them. Instead provide
54*f4ee7fbaSAndroid Build Coastguard Worker those params to all hashers FN(Initialize) */
55*f4ee7fbaSAndroid Build Coastguard Worker }
56*f4ee7fbaSAndroid Build Coastguard Worker
FN(Prepare)57*f4ee7fbaSAndroid Build Coastguard Worker static void FN(Prepare)(
58*f4ee7fbaSAndroid Build Coastguard Worker HashComposite* BROTLI_RESTRICT self, BROTLI_BOOL one_shot,
59*f4ee7fbaSAndroid Build Coastguard Worker size_t input_size, const uint8_t* BROTLI_RESTRICT data) {
60*f4ee7fbaSAndroid Build Coastguard Worker if (self->fresh) {
61*f4ee7fbaSAndroid Build Coastguard Worker self->fresh = BROTLI_FALSE;
62*f4ee7fbaSAndroid Build Coastguard Worker self->hb_common.extra = (uint8_t*)self->extra +
63*f4ee7fbaSAndroid Build Coastguard Worker FN_A(HashMemAllocInBytes)(self->params, one_shot, input_size);
64*f4ee7fbaSAndroid Build Coastguard Worker
65*f4ee7fbaSAndroid Build Coastguard Worker FN_A(Initialize)(self->common, &self->ha, self->params);
66*f4ee7fbaSAndroid Build Coastguard Worker FN_B(Initialize)(&self->hb_common, &self->hb, self->params);
67*f4ee7fbaSAndroid Build Coastguard Worker }
68*f4ee7fbaSAndroid Build Coastguard Worker FN_A(Prepare)(&self->ha, one_shot, input_size, data);
69*f4ee7fbaSAndroid Build Coastguard Worker FN_B(Prepare)(&self->hb, one_shot, input_size, data);
70*f4ee7fbaSAndroid Build Coastguard Worker }
71*f4ee7fbaSAndroid Build Coastguard Worker
FN(HashMemAllocInBytes)72*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(
73*f4ee7fbaSAndroid Build Coastguard Worker const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
74*f4ee7fbaSAndroid Build Coastguard Worker size_t input_size) {
75*f4ee7fbaSAndroid Build Coastguard Worker return FN_A(HashMemAllocInBytes)(params, one_shot, input_size) +
76*f4ee7fbaSAndroid Build Coastguard Worker FN_B(HashMemAllocInBytes)(params, one_shot, input_size);
77*f4ee7fbaSAndroid Build Coastguard Worker }
78*f4ee7fbaSAndroid Build Coastguard Worker
FN(Store)79*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void FN(Store)(HashComposite* BROTLI_RESTRICT self,
80*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
81*f4ee7fbaSAndroid Build Coastguard Worker FN_A(Store)(&self->ha, data, mask, ix);
82*f4ee7fbaSAndroid Build Coastguard Worker FN_B(Store)(&self->hb, data, mask, ix);
83*f4ee7fbaSAndroid Build Coastguard Worker }
84*f4ee7fbaSAndroid Build Coastguard Worker
FN(StoreRange)85*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void FN(StoreRange)(
86*f4ee7fbaSAndroid Build Coastguard Worker HashComposite* BROTLI_RESTRICT self, const uint8_t* BROTLI_RESTRICT data,
87*f4ee7fbaSAndroid Build Coastguard Worker const size_t mask, const size_t ix_start,
88*f4ee7fbaSAndroid Build Coastguard Worker const size_t ix_end) {
89*f4ee7fbaSAndroid Build Coastguard Worker FN_A(StoreRange)(&self->ha, data, mask, ix_start, ix_end);
90*f4ee7fbaSAndroid Build Coastguard Worker FN_B(StoreRange)(&self->hb, data, mask, ix_start, ix_end);
91*f4ee7fbaSAndroid Build Coastguard Worker }
92*f4ee7fbaSAndroid Build Coastguard Worker
FN(StitchToPreviousBlock)93*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void FN(StitchToPreviousBlock)(
94*f4ee7fbaSAndroid Build Coastguard Worker HashComposite* BROTLI_RESTRICT self,
95*f4ee7fbaSAndroid Build Coastguard Worker size_t num_bytes, size_t position, const uint8_t* ringbuffer,
96*f4ee7fbaSAndroid Build Coastguard Worker size_t ring_buffer_mask) {
97*f4ee7fbaSAndroid Build Coastguard Worker FN_A(StitchToPreviousBlock)(&self->ha, num_bytes, position,
98*f4ee7fbaSAndroid Build Coastguard Worker ringbuffer, ring_buffer_mask);
99*f4ee7fbaSAndroid Build Coastguard Worker FN_B(StitchToPreviousBlock)(&self->hb, num_bytes, position,
100*f4ee7fbaSAndroid Build Coastguard Worker ringbuffer, ring_buffer_mask);
101*f4ee7fbaSAndroid Build Coastguard Worker }
102*f4ee7fbaSAndroid Build Coastguard Worker
FN(PrepareDistanceCache)103*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void FN(PrepareDistanceCache)(
104*f4ee7fbaSAndroid Build Coastguard Worker HashComposite* BROTLI_RESTRICT self, int* BROTLI_RESTRICT distance_cache) {
105*f4ee7fbaSAndroid Build Coastguard Worker FN_A(PrepareDistanceCache)(&self->ha, distance_cache);
106*f4ee7fbaSAndroid Build Coastguard Worker FN_B(PrepareDistanceCache)(&self->hb, distance_cache);
107*f4ee7fbaSAndroid Build Coastguard Worker }
108*f4ee7fbaSAndroid Build Coastguard Worker
FN(FindLongestMatch)109*f4ee7fbaSAndroid Build Coastguard Worker static BROTLI_INLINE void FN(FindLongestMatch)(
110*f4ee7fbaSAndroid Build Coastguard Worker HashComposite* BROTLI_RESTRICT self,
111*f4ee7fbaSAndroid Build Coastguard Worker const BrotliEncoderDictionary* dictionary,
112*f4ee7fbaSAndroid Build Coastguard Worker const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
113*f4ee7fbaSAndroid Build Coastguard Worker const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
114*f4ee7fbaSAndroid Build Coastguard Worker const size_t max_length, const size_t max_backward,
115*f4ee7fbaSAndroid Build Coastguard Worker const size_t dictionary_distance, const size_t max_distance,
116*f4ee7fbaSAndroid Build Coastguard Worker HasherSearchResult* BROTLI_RESTRICT out) {
117*f4ee7fbaSAndroid Build Coastguard Worker FN_A(FindLongestMatch)(&self->ha, dictionary, data, ring_buffer_mask,
118*f4ee7fbaSAndroid Build Coastguard Worker distance_cache, cur_ix, max_length, max_backward, dictionary_distance,
119*f4ee7fbaSAndroid Build Coastguard Worker max_distance, out);
120*f4ee7fbaSAndroid Build Coastguard Worker FN_B(FindLongestMatch)(&self->hb, dictionary, data, ring_buffer_mask,
121*f4ee7fbaSAndroid Build Coastguard Worker distance_cache, cur_ix, max_length, max_backward, dictionary_distance,
122*f4ee7fbaSAndroid Build Coastguard Worker max_distance, out);
123*f4ee7fbaSAndroid Build Coastguard Worker }
124*f4ee7fbaSAndroid Build Coastguard Worker
125*f4ee7fbaSAndroid Build Coastguard Worker #undef HashComposite
126