lz_encoder_hash.h 3.5 KB

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