hash_rolling_inc.h 7.2 KB

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