4 * Hashing for Lempel-Ziv matchfinding.
9 * The author dedicates this file to the public domain.
10 * You can do whatever you want with this file.
16 #include "wimlib/unaligned.h"
19 * The hash function: given a sequence prefix held in the low-order bits of a
20 * 32-bit value, multiply by a carefully-chosen large constant. Discard any
21 * bits of the product that don't fit in a 32-bit value, but take the
22 * next-highest @num_bits bits of the product as the hash value, as those have
23 * the most randomness.
26 lz_hash(u32 seq, unsigned num_bits)
28 return (u32)(seq * 0x1E35A7BD) >> (32 - num_bits);
32 * Hash the 2-byte sequence beginning at @p, producing a hash of length
33 * @num_bits bits. At least 2 bytes of data must be available at @p.
36 lz_hash_2_bytes(const u8 *p, unsigned num_bits)
38 u32 seq = load_u16_unaligned(p);
41 return lz_hash(seq, num_bits);
45 * Hash the 3-byte sequence beginning at @p, producing a hash of length
46 * @num_bits bits. At least LZ_HASH3_REQUIRED_NBYTES bytes of data must be
47 * available at @p; note that this may be more than 3.
50 lz_hash_3_bytes(const u8 *p, unsigned num_bits)
52 u32 seq = load_u24_unaligned(p);
55 return lz_hash(seq, num_bits);
58 #define LZ_HASH3_REQUIRED_NBYTES LOAD_U24_REQUIRED_NBYTES
60 #endif /* _LZ_HASH_H */