1207753Smm///////////////////////////////////////////////////////////////////////////////
2207753Smm//
3207753Smm/// \file       lz_encoder_hash.h
4207753Smm/// \brief      Hash macros for match finders
5207753Smm//
6207753Smm//  Author:     Igor Pavlov
7207753Smm//
8207753Smm//  This file has been put into the public domain.
9207753Smm//  You can do whatever you want with this file.
10207753Smm//
11207753Smm///////////////////////////////////////////////////////////////////////////////
12207753Smm
13207753Smm#ifndef LZMA_LZ_ENCODER_HASH_H
14207753Smm#define LZMA_LZ_ENCODER_HASH_H
15207753Smm
16207753Smm#if defined(WORDS_BIGENDIAN) && !defined(HAVE_SMALL)
17207753Smm	// This is to make liblzma produce the same output on big endian
18207753Smm	// systems that it does on little endian systems. lz_encoder.c
19207753Smm	// takes care of including the actual table.
20207753Smm	extern const uint32_t lzma_lz_hash_table[256];
21207753Smm#	define hash_table lzma_lz_hash_table
22207753Smm#else
23207753Smm#	include "check.h"
24207753Smm#	define hash_table lzma_crc32_table[0]
25207753Smm#endif
26207753Smm
27207753Smm#define HASH_2_SIZE (UINT32_C(1) << 10)
28207753Smm#define HASH_3_SIZE (UINT32_C(1) << 16)
29207753Smm#define HASH_4_SIZE (UINT32_C(1) << 20)
30207753Smm
31207753Smm#define HASH_2_MASK (HASH_2_SIZE - 1)
32207753Smm#define HASH_3_MASK (HASH_3_SIZE - 1)
33207753Smm#define HASH_4_MASK (HASH_4_SIZE - 1)
34207753Smm
35207753Smm#define FIX_3_HASH_SIZE (HASH_2_SIZE)
36207753Smm#define FIX_4_HASH_SIZE (HASH_2_SIZE + HASH_3_SIZE)
37207753Smm#define FIX_5_HASH_SIZE (HASH_2_SIZE + HASH_3_SIZE + HASH_4_SIZE)
38207753Smm
39207753Smm// Endianness doesn't matter in hash_2_calc() (no effect on the output).
40207753Smm#ifdef TUKLIB_FAST_UNALIGNED_ACCESS
41207753Smm#	define hash_2_calc() \
42360523Sdelphij		const uint32_t hash_value = read16ne(cur)
43207753Smm#else
44207753Smm#	define hash_2_calc() \
45207753Smm		const uint32_t hash_value \
46207753Smm			= (uint32_t)(cur[0]) | ((uint32_t)(cur[1]) << 8)
47207753Smm#endif
48207753Smm
49207753Smm#define hash_3_calc() \
50207753Smm	const uint32_t temp = hash_table[cur[0]] ^ cur[1]; \
51207753Smm	const uint32_t hash_2_value = temp & HASH_2_MASK; \
52207753Smm	const uint32_t hash_value \
53207753Smm			= (temp ^ ((uint32_t)(cur[2]) << 8)) & mf->hash_mask
54207753Smm
55207753Smm#define hash_4_calc() \
56207753Smm	const uint32_t temp = hash_table[cur[0]] ^ cur[1]; \
57207753Smm	const uint32_t hash_2_value = temp & HASH_2_MASK; \
58207753Smm	const uint32_t hash_3_value \
59207753Smm			= (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK; \
60207753Smm	const uint32_t hash_value = (temp ^ ((uint32_t)(cur[2]) << 8) \
61207753Smm			^ (hash_table[cur[3]] << 5)) & mf->hash_mask
62207753Smm
63207753Smm
64207753Smm// The following are not currently used.
65207753Smm
66207753Smm#define hash_5_calc() \
67207753Smm	const uint32_t temp = hash_table[cur[0]] ^ cur[1]; \
68207753Smm	const uint32_t hash_2_value = temp & HASH_2_MASK; \
69207753Smm	const uint32_t hash_3_value \
70207753Smm			= (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK; \
71207753Smm	uint32_t hash_4_value = (temp ^ ((uint32_t)(cur[2]) << 8) ^ \
72207753Smm			^ hash_table[cur[3]] << 5); \
73207753Smm	const uint32_t hash_value \
74207753Smm			= (hash_4_value ^ (hash_table[cur[4]] << 3)) \
75207753Smm				& mf->hash_mask; \
76207753Smm	hash_4_value &= HASH_4_MASK
77207753Smm
78207753Smm/*
79207753Smm#define hash_zip_calc() \
80207753Smm	const uint32_t hash_value \
81207753Smm			= (((uint32_t)(cur[0]) | ((uint32_t)(cur[1]) << 8)) \
82207753Smm				^ hash_table[cur[2]]) & 0xFFFF
83207753Smm*/
84207753Smm
85207753Smm#define hash_zip_calc() \
86207753Smm	const uint32_t hash_value \
87207753Smm			= (((uint32_t)(cur[2]) | ((uint32_t)(cur[0]) << 8)) \
88207753Smm				^ hash_table[cur[1]]) & 0xFFFF
89207753Smm
90207753Smm#define mt_hash_2_calc() \
91207753Smm	const uint32_t hash_2_value \
92207753Smm			= (hash_table[cur[0]] ^ cur[1]) & HASH_2_MASK
93207753Smm
94207753Smm#define mt_hash_3_calc() \
95207753Smm	const uint32_t temp = hash_table[cur[0]] ^ cur[1]; \
96207753Smm	const uint32_t hash_2_value = temp & HASH_2_MASK; \
97207753Smm	const uint32_t hash_3_value \
98207753Smm			= (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK
99207753Smm
100207753Smm#define mt_hash_4_calc() \
101207753Smm	const uint32_t temp = hash_table[cur[0]] ^ cur[1]; \
102207753Smm	const uint32_t hash_2_value = temp & HASH_2_MASK; \
103207753Smm	const uint32_t hash_3_value \
104207753Smm			= (temp ^ ((uint32_t)(cur[2]) << 8)) & HASH_3_MASK; \
105207753Smm	const uint32_t hash_4_value = (temp ^ ((uint32_t)(cur[2]) << 8) ^ \
106207753Smm			(hash_table[cur[3]] << 5)) & HASH_4_MASK
107207753Smm
108207753Smm#endif
109