Home | History | Annotate | Download | only in enc
      1 /* NOLINT(build/header_guard) */
      2 /* Copyright 2010 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, BUCKET_BITS, BUCKET_SWEEP, HASH_LEN,
      9                         USE_DICTIONARY
     10  */
     11 
     12 #define HashLongestMatchQuickly HASHER()
     13 
     14 #define BUCKET_SIZE (1 << BUCKET_BITS)
     15 
     16 #define HASH_MAP_SIZE (4 << BUCKET_BITS)
     17 
     18 static BROTLI_INLINE size_t FN(HashTypeLength)(void) { return 8; }
     19 static BROTLI_INLINE size_t FN(StoreLookahead)(void) { return 8; }
     20 
     21 /* HashBytes is the function that chooses the bucket to place
     22    the address in. The HashLongestMatch and HashLongestMatchQuickly
     23    classes have separate, different implementations of hashing. */
     24 static uint32_t FN(HashBytes)(const uint8_t* data) {
     25   const uint64_t h = ((BROTLI_UNALIGNED_LOAD64(data) << (64 - 8 * HASH_LEN)) *
     26                       kHashMul64);
     27   /* The higher bits contain more mixture from the multiplication,
     28      so we take our results from there. */
     29   return (uint32_t)(h >> (64 - BUCKET_BITS));
     30 }
     31 
     32 /* A (forgetful) hash table to the data seen by the compressor, to
     33    help create backward references to previous data.
     34 
     35    This is a hash map of fixed size (BUCKET_SIZE). Starting from the
     36    given index, BUCKET_SWEEP buckets are used to store values of a key. */
     37 typedef struct HashLongestMatchQuickly {
     38   uint32_t buckets_[BUCKET_SIZE + BUCKET_SWEEP];
     39 } HashLongestMatchQuickly;
     40 
     41 static BROTLI_INLINE HashLongestMatchQuickly* FN(Self)(HasherHandle handle) {
     42   return (HashLongestMatchQuickly*)&(GetHasherCommon(handle)[1]);
     43 }
     44 
     45 static void FN(Initialize)(
     46     HasherHandle handle, const BrotliEncoderParams* params) {
     47   BROTLI_UNUSED(handle);
     48   BROTLI_UNUSED(params);
     49 }
     50 
     51 static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot,
     52     size_t input_size, const uint8_t* data) {
     53   HashLongestMatchQuickly* self = FN(Self)(handle);
     54   /* Partial preparation is 100 times slower (per socket). */
     55   size_t partial_prepare_threshold = HASH_MAP_SIZE >> 7;
     56   if (one_shot && input_size <= partial_prepare_threshold) {
     57     size_t i;
     58     for (i = 0; i < input_size; ++i) {
     59       const uint32_t key = FN(HashBytes)(&data[i]);
     60       memset(&self->buckets_[key], 0, BUCKET_SWEEP * sizeof(self->buckets_[0]));
     61     }
     62   } else {
     63     /* It is not strictly necessary to fill this buffer here, but
     64        not filling will make the results of the compression stochastic
     65        (but correct). This is because random data would cause the
     66        system to find accidentally good backward references here and there. */
     67     memset(&self->buckets_[0], 0, sizeof(self->buckets_));
     68   }
     69 }
     70 
     71 static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(
     72     const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
     73     size_t input_size) {
     74   BROTLI_UNUSED(params);
     75   BROTLI_UNUSED(one_shot);
     76   BROTLI_UNUSED(input_size);
     77   return sizeof(HashLongestMatchQuickly);
     78 }
     79 
     80 /* Look at 5 bytes at &data[ix & mask].
     81    Compute a hash from these, and store the value somewhere within
     82    [ix .. ix+3]. */
     83 static BROTLI_INLINE void FN(Store)(HasherHandle handle,
     84     const uint8_t *data, const size_t mask, const size_t ix) {
     85   const uint32_t key = FN(HashBytes)(&data[ix & mask]);
     86   /* Wiggle the value with the bucket sweep range. */
     87   const uint32_t off = (ix >> 3) % BUCKET_SWEEP;
     88   FN(Self)(handle)->buckets_[key + off] = (uint32_t)ix;
     89 }
     90 
     91 static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle,
     92     const uint8_t *data, const size_t mask, const size_t ix_start,
     93     const size_t ix_end) {
     94   size_t i;
     95   for (i = ix_start; i < ix_end; ++i) {
     96     FN(Store)(handle, data, mask, i);
     97   }
     98 }
     99 
    100 static BROTLI_INLINE void FN(StitchToPreviousBlock)(
    101     HasherHandle handle, size_t num_bytes, size_t position,
    102     const uint8_t* ringbuffer, size_t ringbuffer_mask) {
    103   if (num_bytes >= FN(HashTypeLength)() - 1 && position >= 3) {
    104     /* Prepare the hashes for three last bytes of the last write.
    105        These could not be calculated before, since they require knowledge
    106        of both the previous and the current block. */
    107     FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 3);
    108     FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 2);
    109     FN(Store)(handle, ringbuffer, ringbuffer_mask, position - 1);
    110   }
    111 }
    112 
    113 static BROTLI_INLINE void FN(PrepareDistanceCache)(
    114     HasherHandle handle, int* BROTLI_RESTRICT distance_cache) {
    115   BROTLI_UNUSED(handle);
    116   BROTLI_UNUSED(distance_cache);
    117 }
    118 
    119 /* Find a longest backward match of &data[cur_ix & ring_buffer_mask]
    120    up to the length of max_length and stores the position cur_ix in the
    121    hash table.
    122 
    123    Does not look for matches longer than max_length.
    124    Does not look for matches further away than max_backward.
    125    Writes the best match into |out|.
    126    Returns true if match is found, otherwise false. */
    127 static BROTLI_INLINE BROTLI_BOOL FN(FindLongestMatch)(
    128     HasherHandle handle, const BrotliDictionary* dictionary,
    129     const uint16_t* dictionary_hash, const uint8_t* BROTLI_RESTRICT data,
    130     const size_t ring_buffer_mask, const int* BROTLI_RESTRICT distance_cache,
    131     const size_t cur_ix, const size_t max_length, const size_t max_backward,
    132     HasherSearchResult* BROTLI_RESTRICT out) {
    133   HashLongestMatchQuickly* self = FN(Self)(handle);
    134   const size_t best_len_in = out->len;
    135   const size_t cur_ix_masked = cur_ix & ring_buffer_mask;
    136   const uint32_t key = FN(HashBytes)(&data[cur_ix_masked]);
    137   int compare_char = data[cur_ix_masked + best_len_in];
    138   score_t best_score = out->score;
    139   size_t best_len = best_len_in;
    140   size_t cached_backward = (size_t)distance_cache[0];
    141   size_t prev_ix = cur_ix - cached_backward;
    142   BROTLI_BOOL is_match_found = BROTLI_FALSE;
    143   out->len_x_code = 0;
    144   if (prev_ix < cur_ix) {
    145     prev_ix &= (uint32_t)ring_buffer_mask;
    146     if (compare_char == data[prev_ix + best_len]) {
    147       size_t len = FindMatchLengthWithLimit(&data[prev_ix],
    148                                             &data[cur_ix_masked],
    149                                             max_length);
    150       if (len >= 4) {
    151         best_score = BackwardReferenceScoreUsingLastDistance(len);
    152         best_len = len;
    153         out->len = len;
    154         out->distance = cached_backward;
    155         out->score = best_score;
    156         compare_char = data[cur_ix_masked + best_len];
    157         if (BUCKET_SWEEP == 1) {
    158           self->buckets_[key] = (uint32_t)cur_ix;
    159           return BROTLI_TRUE;
    160         } else {
    161           is_match_found = BROTLI_TRUE;
    162         }
    163       }
    164     }
    165   }
    166   if (BUCKET_SWEEP == 1) {
    167     size_t backward;
    168     size_t len;
    169     /* Only one to look for, don't bother to prepare for a loop. */
    170     prev_ix = self->buckets_[key];
    171     self->buckets_[key] = (uint32_t)cur_ix;
    172     backward = cur_ix - prev_ix;
    173     prev_ix &= (uint32_t)ring_buffer_mask;
    174     if (compare_char != data[prev_ix + best_len_in]) {
    175       return BROTLI_FALSE;
    176     }
    177     if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
    178       return BROTLI_FALSE;
    179     }
    180     len = FindMatchLengthWithLimit(&data[prev_ix],
    181                                    &data[cur_ix_masked],
    182                                    max_length);
    183     if (len >= 4) {
    184       out->len = len;
    185       out->distance = backward;
    186       out->score = BackwardReferenceScore(len, backward);
    187       return BROTLI_TRUE;
    188     }
    189   } else {
    190     uint32_t *bucket = self->buckets_ + key;
    191     int i;
    192     prev_ix = *bucket++;
    193     for (i = 0; i < BUCKET_SWEEP; ++i, prev_ix = *bucket++) {
    194       const size_t backward = cur_ix - prev_ix;
    195       size_t len;
    196       prev_ix &= (uint32_t)ring_buffer_mask;
    197       if (compare_char != data[prev_ix + best_len]) {
    198         continue;
    199       }
    200       if (BROTLI_PREDICT_FALSE(backward == 0 || backward > max_backward)) {
    201         continue;
    202       }
    203       len = FindMatchLengthWithLimit(&data[prev_ix],
    204                                      &data[cur_ix_masked],
    205                                      max_length);
    206       if (len >= 4) {
    207         const score_t score = BackwardReferenceScore(len, backward);
    208         if (best_score < score) {
    209           best_score = score;
    210           best_len = len;
    211           out->len = best_len;
    212           out->distance = backward;
    213           out->score = score;
    214           compare_char = data[cur_ix_masked + best_len];
    215           is_match_found = BROTLI_TRUE;
    216         }
    217       }
    218     }
    219   }
    220   if (USE_DICTIONARY && !is_match_found) {
    221     is_match_found = SearchInStaticDictionary(dictionary, dictionary_hash,
    222         handle, &data[cur_ix_masked], max_length, max_backward, out,
    223         BROTLI_TRUE);
    224   }
    225   self->buckets_[key + ((cur_ix >> 3) % BUCKET_SWEEP)] = (uint32_t)cur_ix;
    226   return is_match_found;
    227 }
    228 
    229 #undef HASH_MAP_SIZE
    230 #undef BUCKET_SIZE
    231 
    232 #undef HashLongestMatchQuickly
    233