Debian Codegraph @

debian/rust-vendor/zlib-rs/src/deflate/hash_calc.rs

raw
1 #![forbid(unsafe_code)]
2
3 use crate::deflate::{State, HASH_SIZE, STD_MIN_MATCH};
4
5 #[derive(Debug, Clone, Copy)]
6 pub enum HashCalcVariant {
7 Standard,
8 Roll,
9 }
10
11 impl HashCalcVariant {
12 /// Use rolling hash for deflate_slow algorithm with level 9. It allows us to
13 /// properly lookup different hash chains to speed up longest_match search.
14 pub fn for_max_chain_length(max_chain_length: u16) -> Self {
15 if max_chain_length > 1024 {
16 HashCalcVariant::Roll
17 } else {
18 HashCalcVariant::Standard
19 }
20 }
21 }
22
23 pub struct StandardHashCalc;
24
25 impl StandardHashCalc {
26 const HASH_CALC_OFFSET: usize = 0;
27
28 const HASH_CALC_MASK: u32 = (HASH_SIZE - 1) as u32;
29
30 fn hash_calc(_: u32, val: u32) -> u32 {
31 const HASH_SLIDE: u32 = 16;
32 val.wrapping_mul(2654435761) >> HASH_SLIDE
33 }
34
35 pub fn update_hash(h: u32, val: u32) -> u32 {
36 Self::hash_calc(h, val) & Self::HASH_CALC_MASK
37 }
38
39 #[inline]
40 pub fn quick_insert_string(state: &mut State, string: usize) -> u16 {
41 let slice = &state.window.filled()[string + Self::HASH_CALC_OFFSET..];
42 let val = u32::from_le_bytes(slice[..4].try_into().unwrap());
43
44 Self::quick_insert_value(state, string, val)
45 }
46
47 #[inline]
48 pub fn quick_insert_value(state: &mut State, string: usize, val: u32) -> u16 {
49 let hm = Self::update_hash(0, val) as usize;
50
51 let head = state.head.as_slice()[hm];
52 if head != string as u16 {
53 let w_mask = state.w_mask();
54 state.prev.as_mut_slice()[string & w_mask] = head;
55 state.head.as_mut_slice()[hm] = string as u16;
56 }
57
58 head
59 }
60
61 pub fn insert_string(state: &mut State, string: usize, count: usize) {
62 let slice = &state.window.filled()[string + Self::HASH_CALC_OFFSET..];
63
64 // it can happen that insufficient bytes are initialized
65 // .take(count) generates worse assembly
66 let slice = &slice[..Ord::min(slice.len(), count + 3)];
67
68 let w_mask = state.w_mask();
69 for (i, w) in slice.windows(4).enumerate() {
70 let idx = string as u16 + i as u16;
71
72 let val = u32::from_le_bytes(w.try_into().unwrap());
73
74 let hm = Self::update_hash(0, val) as usize;
75
76 let head = state.head.as_slice()[hm];
77 if head != idx {
78 state.prev.as_mut_slice()[idx as usize & w_mask] = head;
79 state.head.as_mut_slice()[hm] = idx;
80 }
81 }
82 }
83 }
84
85 pub struct RollHashCalc;
86
87 impl RollHashCalc {
88 const HASH_CALC_OFFSET: usize = STD_MIN_MATCH - 1;
89
90 const HASH_CALC_MASK: u32 = (1 << 15) - 1;
91
92 fn hash_calc(h: u32, val: u32) -> u32 {
93 const HASH_SLIDE: u32 = 5;
94 (h << HASH_SLIDE) ^ val
95 }
96
97 pub fn update_hash(h: u32, val: u32) -> u32 {
98 Self::hash_calc(h, val) & Self::HASH_CALC_MASK
99 }
100
101 pub fn quick_insert_string(state: &mut State, string: usize) -> u16 {
102 let val = state.window.filled()[string + Self::HASH_CALC_OFFSET] as u32;
103
104 state.ins_h = Self::hash_calc(state.ins_h, val);
105 state.ins_h &= Self::HASH_CALC_MASK;
106
107 let hm = state.ins_h as usize;
108
109 let head = state.head.as_slice()[hm];
110 if head != string as u16 {
111 let w_mask = state.w_mask();
112 state.prev.as_mut_slice()[string & w_mask] = head;
113 state.head.as_mut_slice()[hm] = string as u16;
114 }
115
116 head
117 }
118
119 pub fn insert_string(state: &mut State, string: usize, count: usize) {
120 let slice = &state.window.filled()[string + Self::HASH_CALC_OFFSET..][..count];
121
122 let w_mask = state.w_mask();
123 for (i, val) in slice.iter().copied().enumerate() {
124 let idx = string as u16 + i as u16;
125
126 state.ins_h = Self::hash_calc(state.ins_h, val as u32);
127 state.ins_h &= Self::HASH_CALC_MASK;
128 let hm = state.ins_h as usize;
129
130 let head = state.head.as_slice()[hm];
131 if head != idx {
132 state.prev.as_mut_slice()[idx as usize & w_mask] = head;
133 state.head.as_mut_slice()[hm] = idx;
134 }
135 }
136 }
137 }
138
139 #[cfg(test)]
140 mod tests {
141 use super::*;
142
143 #[test]
144 fn roll_hash_calc() {
145 assert_eq!(RollHashCalc::hash_calc(2565, 93), 82173);
146 assert_eq!(RollHashCalc::hash_calc(16637, 10), 532394);
147 assert_eq!(RollHashCalc::hash_calc(8106, 100), 259364);
148 assert_eq!(RollHashCalc::hash_calc(29988, 101), 959717);
149 assert_eq!(RollHashCalc::hash_calc(9445, 98), 302274);
150 assert_eq!(RollHashCalc::hash_calc(7362, 117), 235573);
151 assert_eq!(RollHashCalc::hash_calc(6197, 103), 198343);
152 assert_eq!(RollHashCalc::hash_calc(1735, 32), 55488);
153 assert_eq!(RollHashCalc::hash_calc(22720, 61), 727101);
154 assert_eq!(RollHashCalc::hash_calc(6205, 32), 198528);
155 assert_eq!(RollHashCalc::hash_calc(3826, 117), 122421);
156 assert_eq!(RollHashCalc::hash_calc(24117, 101), 771781);
157 }
158
159 #[test]
160 fn standard_hash_calc() {
161 assert_eq!(StandardHashCalc::hash_calc(0, 807411760), 65468);
162 assert_eq!(StandardHashCalc::hash_calc(0, 540024864), 42837);
163 assert_eq!(StandardHashCalc::hash_calc(0, 538980384), 33760);
164 assert_eq!(StandardHashCalc::hash_calc(0, 775430176), 8925);
165 assert_eq!(StandardHashCalc::hash_calc(0, 941629472), 42053);
166 }
167 }