hash_composite_inc.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, HASHER_A, HASHER_B */
  7. /* Composite hasher: This hasher allows to combine two other hashers, HASHER_A
  8. and HASHER_B. */
  9. #define HashComposite HASHER()
  10. #define FN_A(X) EXPAND_CAT(X, HASHER_A)
  11. #define FN_B(X) EXPAND_CAT(X, HASHER_B)
  12. static BROTLI_INLINE size_t FN(HashTypeLength)(void) {
  13. size_t a = FN_A(HashTypeLength)();
  14. size_t b = FN_B(HashTypeLength)();
  15. return a > b ? a : b;
  16. }
  17. static BROTLI_INLINE size_t FN(StoreLookahead)(void) {
  18. size_t a = FN_A(StoreLookahead)();
  19. size_t b = FN_B(StoreLookahead)();
  20. return a > b ? a : b;
  21. }
  22. typedef struct HashComposite {
  23. HasherHandle ha;
  24. HasherHandle hb;
  25. const BrotliEncoderParams* params;
  26. } HashComposite;
  27. static BROTLI_INLINE HashComposite* FN(Self)(HasherHandle handle) {
  28. return (HashComposite*)&(GetHasherCommon(handle)[1]);
  29. }
  30. static void FN(Initialize)(
  31. HasherHandle handle, const BrotliEncoderParams* params) {
  32. HashComposite* self = FN(Self)(handle);
  33. self->ha = 0;
  34. self->hb = 0;
  35. self->params = params;
  36. /* TODO: Initialize of the hashers is defered to Prepare (and params
  37. remembered here) because we don't get the one_shot and input_size params
  38. here that are needed to know the memory size of them. Instead provide
  39. those params to all hashers FN(Initialize) */
  40. }
  41. static void FN(Prepare)(HasherHandle handle, BROTLI_BOOL one_shot,
  42. size_t input_size, const uint8_t* data) {
  43. HashComposite* self = FN(Self)(handle);
  44. if (!self->ha) {
  45. HasherCommon* common_a;
  46. HasherCommon* common_b;
  47. self->ha = handle + sizeof(HasherCommon) + sizeof(HashComposite);
  48. common_a = (HasherCommon*)self->ha;
  49. common_a->params = self->params->hasher;
  50. common_a->is_prepared_ = BROTLI_FALSE;
  51. common_a->dict_num_lookups = 0;
  52. common_a->dict_num_matches = 0;
  53. FN_A(Initialize)(self->ha, self->params);
  54. self->hb = self->ha + sizeof(HasherCommon) + FN_A(HashMemAllocInBytes)(
  55. self->params, one_shot, input_size);
  56. common_b = (HasherCommon*)self->hb;
  57. common_b->params = self->params->hasher;
  58. common_b->is_prepared_ = BROTLI_FALSE;
  59. common_b->dict_num_lookups = 0;
  60. common_b->dict_num_matches = 0;
  61. FN_B(Initialize)(self->hb, self->params);
  62. }
  63. FN_A(Prepare)(self->ha, one_shot, input_size, data);
  64. FN_B(Prepare)(self->hb, one_shot, input_size, data);
  65. }
  66. static BROTLI_INLINE size_t FN(HashMemAllocInBytes)(
  67. const BrotliEncoderParams* params, BROTLI_BOOL one_shot,
  68. size_t input_size) {
  69. return sizeof(HashComposite) + 2 * sizeof(HasherCommon) +
  70. FN_A(HashMemAllocInBytes)(params, one_shot, input_size) +
  71. FN_B(HashMemAllocInBytes)(params, one_shot, input_size);
  72. }
  73. static BROTLI_INLINE void FN(Store)(HasherHandle BROTLI_RESTRICT handle,
  74. const uint8_t* BROTLI_RESTRICT data, const size_t mask, const size_t ix) {
  75. HashComposite* self = FN(Self)(handle);
  76. FN_A(Store)(self->ha, data, mask, ix);
  77. FN_B(Store)(self->hb, data, mask, ix);
  78. }
  79. static BROTLI_INLINE void FN(StoreRange)(HasherHandle handle,
  80. const uint8_t* data, const size_t mask, const size_t ix_start,
  81. const size_t ix_end) {
  82. HashComposite* self = FN(Self)(handle);
  83. FN_A(StoreRange)(self->ha, data, mask, ix_start, ix_end);
  84. FN_B(StoreRange)(self->hb, data, mask, ix_start, ix_end);
  85. }
  86. static BROTLI_INLINE void FN(StitchToPreviousBlock)(HasherHandle handle,
  87. size_t num_bytes, size_t position, const uint8_t* ringbuffer,
  88. size_t ring_buffer_mask) {
  89. HashComposite* self = FN(Self)(handle);
  90. FN_A(StitchToPreviousBlock)(self->ha, num_bytes, position, ringbuffer,
  91. ring_buffer_mask);
  92. FN_B(StitchToPreviousBlock)(self->hb, num_bytes, position, ringbuffer,
  93. ring_buffer_mask);
  94. }
  95. static BROTLI_INLINE void FN(PrepareDistanceCache)(
  96. HasherHandle handle, int* BROTLI_RESTRICT distance_cache) {
  97. HashComposite* self = FN(Self)(handle);
  98. FN_A(PrepareDistanceCache)(self->ha, distance_cache);
  99. FN_B(PrepareDistanceCache)(self->hb, distance_cache);
  100. }
  101. static BROTLI_INLINE void FN(FindLongestMatch)(HasherHandle handle,
  102. const BrotliEncoderDictionary* dictionary,
  103. const uint8_t* BROTLI_RESTRICT data, const size_t ring_buffer_mask,
  104. const int* BROTLI_RESTRICT distance_cache, const size_t cur_ix,
  105. const size_t max_length, const size_t max_backward,
  106. const size_t gap, const size_t max_distance,
  107. HasherSearchResult* BROTLI_RESTRICT out) {
  108. HashComposite* self = FN(Self)(handle);
  109. FN_A(FindLongestMatch)(self->ha, dictionary, data, ring_buffer_mask,
  110. distance_cache, cur_ix, max_length, max_backward, gap,
  111. max_distance, out);
  112. FN_B(FindLongestMatch)(self->hb, dictionary, data, ring_buffer_mask,
  113. distance_cache, cur_ix, max_length, max_backward, gap,
  114. max_distance, out);
  115. }
  116. #undef HashComposite