backward_references.c 3.9 KB

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