backward_references.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright 2013 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Function to find backward reference copies. */
  6. #include "./backward_references.h"
  7. #include "../common/constants.h"
  8. #include "../common/context.h"
  9. #include "../common/dictionary.h"
  10. #include "../common/platform.h"
  11. #include <brotli/types.h>
  12. #include "./command.h"
  13. #include "./dictionary_hash.h"
  14. #include "./memory.h"
  15. #include "./quality.h"
  16. #if defined(__cplusplus) || defined(c_plusplus)
  17. extern "C" {
  18. #endif
  19. static BROTLI_INLINE size_t ComputeDistanceCode(size_t distance,
  20. size_t max_distance,
  21. const int* dist_cache) {
  22. if (distance <= max_distance) {
  23. size_t distance_plus_3 = distance + 3;
  24. size_t offset0 = distance_plus_3 - (size_t)dist_cache[0];
  25. size_t offset1 = distance_plus_3 - (size_t)dist_cache[1];
  26. if (distance == (size_t)dist_cache[0]) {
  27. return 0;
  28. } else if (distance == (size_t)dist_cache[1]) {
  29. return 1;
  30. } else if (offset0 < 7) {
  31. return (0x9750468 >> (4 * offset0)) & 0xF;
  32. } else if (offset1 < 7) {
  33. return (0xFDB1ACE >> (4 * offset1)) & 0xF;
  34. } else if (distance == (size_t)dist_cache[2]) {
  35. return 2;
  36. } else if (distance == (size_t)dist_cache[3]) {
  37. return 3;
  38. }
  39. }
  40. return distance + BROTLI_NUM_DISTANCE_SHORT_CODES - 1;
  41. }
  42. #define EXPAND_CAT(a, b) CAT(a, b)
  43. #define CAT(a, b) a ## b
  44. #define FN(X) EXPAND_CAT(X, HASHER())
  45. #define EXPORT_FN(X) EXPAND_CAT(X, EXPAND_CAT(PREFIX(), HASHER()))
  46. #define PREFIX() N
  47. #define HASHER() H2
  48. /* NOLINTNEXTLINE(build/include) */
  49. #include "./backward_references_inc.h"
  50. #undef HASHER
  51. #define HASHER() H3
  52. /* NOLINTNEXTLINE(build/include) */
  53. #include "./backward_references_inc.h"
  54. #undef HASHER
  55. #define HASHER() H4
  56. /* NOLINTNEXTLINE(build/include) */
  57. #include "./backward_references_inc.h"
  58. #undef HASHER
  59. #define HASHER() H5
  60. /* NOLINTNEXTLINE(build/include) */
  61. #include "./backward_references_inc.h"
  62. #undef HASHER
  63. #define HASHER() H6
  64. /* NOLINTNEXTLINE(build/include) */
  65. #include "./backward_references_inc.h"
  66. #undef HASHER
  67. #define HASHER() H40
  68. /* NOLINTNEXTLINE(build/include) */
  69. #include "./backward_references_inc.h"
  70. #undef HASHER
  71. #define HASHER() H41
  72. /* NOLINTNEXTLINE(build/include) */
  73. #include "./backward_references_inc.h"
  74. #undef HASHER
  75. #define HASHER() H42
  76. /* NOLINTNEXTLINE(build/include) */
  77. #include "./backward_references_inc.h"
  78. #undef HASHER
  79. #define HASHER() H54
  80. /* NOLINTNEXTLINE(build/include) */
  81. #include "./backward_references_inc.h"
  82. #undef HASHER
  83. #define HASHER() H35
  84. /* NOLINTNEXTLINE(build/include) */
  85. #include "./backward_references_inc.h"
  86. #undef HASHER
  87. #define HASHER() H55
  88. /* NOLINTNEXTLINE(build/include) */
  89. #include "./backward_references_inc.h"
  90. #undef HASHER
  91. #define HASHER() H65
  92. /* NOLINTNEXTLINE(build/include) */
  93. #include "./backward_references_inc.h"
  94. #undef HASHER
  95. #undef PREFIX
  96. #undef EXPORT_FN
  97. #undef FN
  98. #undef CAT
  99. #undef EXPAND_CAT
  100. void BrotliCreateBackwardReferences(size_t num_bytes,
  101. size_t position, const uint8_t* ringbuffer, size_t ringbuffer_mask,
  102. ContextLut literal_context_lut, const BrotliEncoderParams* params,
  103. Hasher* hasher, int* dist_cache, size_t* last_insert_len,
  104. Command* commands, size_t* num_commands, size_t* num_literals) {
  105. switch (params->hasher.type) {
  106. #define CASE_(N) \
  107. case N: \
  108. CreateBackwardReferencesNH ## N(num_bytes, \
  109. position, ringbuffer, ringbuffer_mask, \
  110. literal_context_lut, params, hasher, dist_cache, \
  111. last_insert_len, commands, num_commands, num_literals); \
  112. return;
  113. FOR_GENERIC_HASHERS(CASE_)
  114. #undef CASE_
  115. default:
  116. break;
  117. }
  118. }
  119. #if defined(__cplusplus) || defined(c_plusplus)
  120. } /* extern "C" */
  121. #endif