Debian Codegraph @

third_party/rust/zlib-rs/src/deflate/hash_calc.rs

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