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