Debian Codegraph @

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

raw
1 pub fn slide_hash(state: &mut crate::deflate::State) {
2 let wsize = state.w_size as u16;
3
4 // The state.head and state.prev slices have a length that is a power of 2 between 8 and 16.
5 // That knowledge means `chunks_exact` with a (small) power of 2 can be used without risk of
6 // missing elements.
7 slide_hash_chain(state.head.as_mut_slice(), wsize);
8 slide_hash_chain(state.prev.as_mut_slice(), wsize);
9 }
10
11 fn slide_hash_chain(table: &mut [u16], wsize: u16) {
12 #[cfg(target_arch = "x86_64")]
13 if crate::cpu_features::is_enabled_avx2_and_bmi2() {
14 // SAFETY: the avx2 and bmi2 target feature are enabled.
15 return unsafe { avx2::slide_hash_chain(table, wsize) };
16 }
17
18 #[cfg(target_arch = "aarch64")]
19 if crate::cpu_features::is_enabled_neon() {
20 return unsafe { neon::slide_hash_chain(table, wsize) };
21 }
22
23 // FIXME: Remove lsx feature guard once MSRV is 1.89.0 or higher
24 #[cfg(all(target_arch = "loongarch64", feature = "lsx"))]
25 if crate::cpu_features::is_enabled_lsx() {
26 return unsafe { lsx::slide_hash_chain(table, wsize) };
27 }
28
29 #[cfg(target_arch = "wasm32")]
30 if crate::cpu_features::is_enabled_simd128() {
31 // SAFETY: the simd128 target feature is enabled.
32 return unsafe { wasm::slide_hash_chain(table, wsize) };
33 }
34
35 rust::slide_hash_chain(table, wsize);
36 }
37
38 #[inline(always)]
39 fn generic_slide_hash_chain<const N: usize>(table: &mut [u16], wsize: u16) {
40 debug_assert_eq!(table.len() % N, 0);
41
42 for chunk in table.chunks_exact_mut(N) {
43 for m in chunk.iter_mut() {
44 *m = m.saturating_sub(wsize);
45 }
46 }
47 }
48
49 mod rust {
50 pub fn slide_hash_chain(table: &mut [u16], wsize: u16) {
51 // 32 means that 4 128-bit values can be processed per iteration. That appear to be the
52 // optimal amount on x86_64 (SSE) and aarch64 (NEON).
53 super::generic_slide_hash_chain::<32>(table, wsize);
54 }
55 }
56
57 #[cfg(target_arch = "x86_64")]
58 mod avx2 {
59 /// # Safety
60 ///
61 /// Behavior is undefined if the `avx2` target feature is not enabled
62 #[target_feature(enable = "avx2")]
63 #[target_feature(enable = "bmi2")]
64 #[target_feature(enable = "bmi1")]
65 pub unsafe fn slide_hash_chain(table: &mut [u16], wsize: u16) {
66 // 64 means that 4 256-bit values can be processed per iteration.
67 // That appear to be the optimal amount for avx2.
68 //
69 // This vectorizes well https://godbolt.org/z/sGbdYba7K
70 super::generic_slide_hash_chain::<64>(table, wsize);
71 }
72 }
73
74 #[cfg(target_arch = "aarch64")]
75 mod neon {
76 /// # Safety
77 ///
78 /// Behavior is undefined if the `neon` target feature is not enabled
79 #[target_feature(enable = "neon")]
80 pub unsafe fn slide_hash_chain(table: &mut [u16], wsize: u16) {
81 // 32 means that 4 128-bit values can be processed per iteration. That appear to be the
82 // optimal amount for neon.
83 super::generic_slide_hash_chain::<32>(table, wsize);
84 }
85 }
86
87 // FIXME: Remove lsx feature guard once MSRV is 1.89.0 or higher
88 #[cfg(all(target_arch = "loongarch64", feature = "lsx"))]
89 mod lsx {
90 /// # Safety
91 ///
92 /// Behavior is undefined if the `lsx` target feature is not enabled.
93 #[target_feature(enable = "lsx")]
94 pub unsafe fn slide_hash_chain(table: &mut [u16], wsize: u16) {
95 // 32 means that 4 128-bit values can be processed per iteration. That appear to be the
96 // optimal amount for LSX.
97 super::generic_slide_hash_chain::<32>(table, wsize);
98 }
99 }
100
101 #[cfg(target_arch = "wasm32")]
102 mod wasm {
103 /// # Safety
104 ///
105 /// Behavior is undefined if the `simd128` target feature is not enabled
106 #[target_feature(enable = "simd128")]
107 pub unsafe fn slide_hash_chain(table: &mut [u16], wsize: u16) {
108 // 32 means that 4 128-bit values can be processed per iteration. That appear to be the
109 // optimal amount on x86_64 (SSE) and aarch64 (NEON), which is what this will ultimately
110 // compile down to.
111 super::generic_slide_hash_chain::<32>(table, wsize);
112 }
113 }
114
115 #[cfg(test)]
116 mod tests {
117 use super::*;
118
119 const WSIZE: u16 = 32768;
120
121 const INPUT: [u16; 64] = [
122 0, 0, 28790, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 43884, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123 0, 0, 0, 0, 64412, 0, 0, 0, 0, 0, 21043, 0, 0, 0, 0, 0, 23707, 0, 0, 0, 0, 0, 0, 0, 0, 0,
124 0, 0, 0, 0, 0, 0, 64026, 0, 0, 20182,
125 ];
126
127 const OUTPUT: [u16; 64] = [
128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11116, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
129 0, 0, 0, 31644, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
130 0, 0, 31258, 0, 0, 0,
131 ];
132
133 #[test]
134 fn test_slide_hash_rust() {
135 let mut input = INPUT;
136
137 rust::slide_hash_chain(&mut input, WSIZE);
138
139 assert_eq!(input, OUTPUT);
140 }
141
142 #[test]
143 #[cfg(target_arch = "x86_64")]
144 fn test_slide_hash_avx2() {
145 if crate::cpu_features::is_enabled_avx2_and_bmi2() {
146 let mut input = INPUT;
147
148 unsafe { avx2::slide_hash_chain(&mut input, WSIZE) };
149
150 assert_eq!(input, OUTPUT);
151 }
152 }
153
154 #[test]
155 #[cfg(target_arch = "aarch64")]
156 fn test_slide_hash_neon() {
157 if crate::cpu_features::is_enabled_neon() {
158 let mut input = INPUT;
159
160 unsafe { neon::slide_hash_chain(&mut input, WSIZE) };
161
162 assert_eq!(input, OUTPUT);
163 }
164 }
165
166 // FIXME: Remove lsx feature guard once MSRV is 1.89.0 or higher
167 #[test]
168 #[cfg(all(target_arch = "loongarch64", feature = "lsx"))]
169 fn test_slide_hash_lsx() {
170 if crate::cpu_features::is_enabled_lsx() {
171 let mut input = INPUT;
172
173 unsafe { lsx::slide_hash_chain(&mut input, WSIZE) };
174
175 assert_eq!(input, OUTPUT);
176 }
177 }
178
179 #[test]
180 #[cfg(target_arch = "wasm32")]
181 fn test_slide_hash_wasm() {
182 if crate::cpu_features::is_enabled_simd128() {
183 let mut input = INPUT;
184
185 unsafe { wasm::slide_hash_chain(&mut input, WSIZE) };
186
187 assert_eq!(input, OUTPUT);
188 }
189 }
190
191 quickcheck::quickcheck! {
192 fn slide_is_rust_slide(v: Vec<u16>, wsize: u16) -> bool {
193 // pad to a multiple of 64 (the biggest chunk size currently in use)
194 let difference = v.len().next_multiple_of(64) - v.len();
195 let mut v = v;
196 v.extend(core::iter::repeat(u16::MAX).take(difference));
197
198
199 let mut a = v.clone();
200 let mut b = v;
201
202 rust::slide_hash_chain(&mut a, wsize);
203 slide_hash_chain(&mut b, wsize);
204
205 a == b
206 }
207 }
208 }