1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file lz_encoder_hash.h
4 /// \brief Hash macros for match finders
6 // Copyright (C) 1999-2008 Igor Pavlov
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 #ifndef LZMA_LZ_ENCODER_HASH_H
21 #define LZMA_LZ_ENCODER_HASH_H
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)
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)
31 #define FIX_3_HASH_SIZE (HASH_2_SIZE)
32 #define FIX_4_HASH_SIZE (HASH_2_SIZE + HASH_3_SIZE)
33 #define FIX_5_HASH_SIZE (HASH_2_SIZE + HASH_3_SIZE + HASH_4_SIZE)
35 // TODO Benchmark, and probably doesn't need to be endian dependent.
36 #if !defined(WORDS_BIGENDIAN) && defined(HAVE_FAST_UNALIGNED_ACCESS)
37 # define hash_2_calc() \
38 const uint32_t hash_value = *(const uint16_t *)(cur);
40 # define hash_2_calc() \
41 const uint32_t hash_value \
42 = (uint32_t)(cur[0]) | ((uint32_t)(cur[1]) << 8)
45 #define hash_3_calc() \
46 const uint32_t temp = lzma_crc32_table[0][cur[0]] ^ cur[1]; \
47 const uint32_t hash_2_value = temp & HASH_2_MASK; \
48 const uint32_t hash_value \
49 = (temp ^ ((uint32_t)(cur[2]) << 8)) & mf->hash_mask
51 #define hash_4_calc() \
52 const uint32_t temp = lzma_crc32_table[0][cur[0]] ^ cur[1]; \
53 const uint32_t hash_2_value = temp & HASH_2_MASK; \
54 const uint32_t hash_3_value \
55 = (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK; \
56 const uint32_t hash_value = (temp ^ ((uint32_t)(cur[2]) << 8) \
57 ^ (lzma_crc32_table[0][cur[3]] << 5)) & mf->hash_mask
60 // The following are not currently used.
62 #define hash_5_calc() \
63 const uint32_t temp = lzma_crc32_table[0][cur[0]] ^ cur[1]; \
64 const uint32_t hash_2_value = temp & HASH_2_MASK; \
65 const uint32_t hash_3_value \
66 = (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK; \
67 uint32_t hash_4_value = (temp ^ ((uint32_t)(cur[2]) << 8) ^ \
68 ^ lzma_crc32_table[0][cur[3]] << 5); \
69 const uint32_t hash_value \
70 = (hash_4_value ^ (lzma_crc32_table[0][cur[4]] << 3)) \
72 hash_4_value &= HASH_4_MASK
75 #define hash_zip_calc() \
76 const uint32_t hash_value \
77 = (((uint32_t)(cur[0]) | ((uint32_t)(cur[1]) << 8)) \
78 ^ lzma_crc32_table[0][cur[2]]) & 0xFFFF
81 #define hash_zip_calc() \
82 const uint32_t hash_value \
83 = (((uint32_t)(cur[2]) | ((uint32_t)(cur[0]) << 8)) \
84 ^ lzma_crc32_table[0][cur[1]]) & 0xFFFF
86 #define mt_hash_2_calc() \
87 const uint32_t hash_2_value \
88 = (lzma_crc32_table[0][cur[0]] ^ cur[1]) & HASH_2_MASK
90 #define mt_hash_3_calc() \
91 const uint32_t temp = lzma_crc32_table[0][cur[0]] ^ cur[1]; \
92 const uint32_t hash_2_value = temp & HASH_2_MASK; \
93 const uint32_t hash_3_value \
94 = (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK
96 #define mt_hash_4_calc() \
97 const uint32_t temp = lzma_crc32_table[0][cur[0]] ^ cur[1]; \
98 const uint32_t hash_2_value = temp & HASH_2_MASK; \
99 const uint32_t hash_3_value \
100 = (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK; \
101 const uint32_t hash_4_value = (temp ^ ((uint32_t)(cur[2]) << 8) ^ \
102 (lzma_crc32_table[0][cur[3]] << 5)) & HASH_4_MASK