lz_encoder_hash.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // SPDX-License-Identifier: 0BSD
  2. ///////////////////////////////////////////////////////////////////////////////
  3. //
  4. /// \file lz_encoder_hash.h
  5. /// \brief Hash macros for match finders
  6. //
  7. // Author: Igor Pavlov
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #ifndef LZMA_LZ_ENCODER_HASH_H
  11. #define LZMA_LZ_ENCODER_HASH_H
  12. #if defined(WORDS_BIGENDIAN) && !defined(HAVE_SMALL)
  13. // This is to make liblzma produce the same output on big endian
  14. // systems that it does on little endian systems. lz_encoder.c
  15. // takes care of including the actual table.
  16. lzma_attr_visibility_hidden
  17. extern const uint32_t lzma_lz_hash_table[256];
  18. # define hash_table lzma_lz_hash_table
  19. #else
  20. # include "check.h"
  21. # define hash_table lzma_crc32_table[0]
  22. #endif
  23. #define HASH_2_SIZE (UINT32_C(1) << 10)
  24. #define HASH_3_SIZE (UINT32_C(1) << 16)
  25. #define HASH_4_SIZE (UINT32_C(1) << 20)
  26. #define HASH_2_MASK (HASH_2_SIZE - 1)
  27. #define HASH_3_MASK (HASH_3_SIZE - 1)
  28. #define HASH_4_MASK (HASH_4_SIZE - 1)
  29. #define FIX_3_HASH_SIZE (HASH_2_SIZE)
  30. #define FIX_4_HASH_SIZE (HASH_2_SIZE + HASH_3_SIZE)
  31. #define FIX_5_HASH_SIZE (HASH_2_SIZE + HASH_3_SIZE + HASH_4_SIZE)
  32. // Endianness doesn't matter in hash_2_calc() (no effect on the output).
  33. #ifdef TUKLIB_FAST_UNALIGNED_ACCESS
  34. # define hash_2_calc() \
  35. const uint32_t hash_value = read16ne(cur)
  36. #else
  37. # define hash_2_calc() \
  38. const uint32_t hash_value \
  39. = (uint32_t)(cur[0]) | ((uint32_t)(cur[1]) << 8)
  40. #endif
  41. #define hash_3_calc() \
  42. const uint32_t temp = hash_table[cur[0]] ^ cur[1]; \
  43. const uint32_t hash_2_value = temp & HASH_2_MASK; \
  44. const uint32_t hash_value \
  45. = (temp ^ ((uint32_t)(cur[2]) << 8)) & mf->hash_mask
  46. #define hash_4_calc() \
  47. const uint32_t temp = hash_table[cur[0]] ^ cur[1]; \
  48. const uint32_t hash_2_value = temp & HASH_2_MASK; \
  49. const uint32_t hash_3_value \
  50. = (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK; \
  51. const uint32_t hash_value = (temp ^ ((uint32_t)(cur[2]) << 8) \
  52. ^ (hash_table[cur[3]] << 5)) & mf->hash_mask
  53. // The following are not currently used.
  54. #define hash_5_calc() \
  55. const uint32_t temp = hash_table[cur[0]] ^ cur[1]; \
  56. const uint32_t hash_2_value = temp & HASH_2_MASK; \
  57. const uint32_t hash_3_value \
  58. = (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK; \
  59. uint32_t hash_4_value = (temp ^ ((uint32_t)(cur[2]) << 8) ^ \
  60. ^ hash_table[cur[3]] << 5); \
  61. const uint32_t hash_value \
  62. = (hash_4_value ^ (hash_table[cur[4]] << 3)) \
  63. & mf->hash_mask; \
  64. hash_4_value &= HASH_4_MASK
  65. /*
  66. #define hash_zip_calc() \
  67. const uint32_t hash_value \
  68. = (((uint32_t)(cur[0]) | ((uint32_t)(cur[1]) << 8)) \
  69. ^ hash_table[cur[2]]) & 0xFFFF
  70. */
  71. #define hash_zip_calc() \
  72. const uint32_t hash_value \
  73. = (((uint32_t)(cur[2]) | ((uint32_t)(cur[0]) << 8)) \
  74. ^ hash_table[cur[1]]) & 0xFFFF
  75. #define mt_hash_2_calc() \
  76. const uint32_t hash_2_value \
  77. = (hash_table[cur[0]] ^ cur[1]) & HASH_2_MASK
  78. #define mt_hash_3_calc() \
  79. const uint32_t temp = hash_table[cur[0]] ^ cur[1]; \
  80. const uint32_t hash_2_value = temp & HASH_2_MASK; \
  81. const uint32_t hash_3_value \
  82. = (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK
  83. #define mt_hash_4_calc() \
  84. const uint32_t temp = hash_table[cur[0]] ^ cur[1]; \
  85. const uint32_t hash_2_value = temp & HASH_2_MASK; \
  86. const uint32_t hash_3_value \
  87. = (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK; \
  88. const uint32_t hash_4_value = (temp ^ ((uint32_t)(cur[2]) << 8) ^ \
  89. (hash_table[cur[3]] << 5)) & HASH_4_MASK
  90. #endif