Debian Codegraph @

third_party/rust/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 slide_hash_chain(state.head.as_mut_slice(), wsize);
5 slide_hash_chain(state.prev.as_mut_slice(), wsize);
6 }
7
8 fn slide_hash_chain(table: &mut [u16], wsize: u16) {
9 #[cfg(target_arch = "x86_64")]
10 if crate::cpu_features::is_enabled_avx2() {
11 return avx2::slide_hash_chain(table, wsize);
12 }
13
14 #[cfg(target_arch = "aarch64")]
15 if crate::cpu_features::is_enabled_neon() {
16 return neon::slide_hash_chain(table, wsize);
17 }
18
19 #[cfg(target_arch = "wasm32")]
20 if crate::cpu_features::is_enabled_simd128() {
21 return wasm::slide_hash_chain(table, wsize);
22 }
23
24 rust::slide_hash_chain(table, wsize);
25 }
26
27 mod rust {
28 pub fn slide_hash_chain(table: &mut [u16], wsize: u16) {
29 for m in table.iter_mut() {
30 *m = m.saturating_sub(wsize);
31 }
32 }
33 }
34
35 /// # Safety
36 ///
37 /// These functions should only be executed on `aarch64` systems with the `neon` feature enabled.
38 #[cfg(target_arch = "aarch64")]
39 mod neon {
40 use core::arch::aarch64::{
41 uint16x8_t, uint16x8x4_t, vdupq_n_u16, vld1q_u16_x4, vqsubq_u16, vst1q_u16_x4,
42 };
43
44 pub fn slide_hash_chain(table: &mut [u16], wsize: u16) {
45 assert!(crate::cpu_features::is_enabled_neon());
46 unsafe { slide_hash_chain_internal(table, wsize) }
47 }
48
49 /// # Safety
50 ///
51 /// Behavior is undefined if the `neon` target feature is not enabled
52 #[target_feature(enable = "neon")]
53 unsafe fn slide_hash_chain_internal(table: &mut [u16], wsize: u16) {
54 debug_assert_eq!(table.len() % 32, 0);
55
56 let v = unsafe { vdupq_n_u16(wsize) };
57
58 for chunk in table.chunks_exact_mut(32) {
59 unsafe {
60 let p0 = vld1q_u16_x4(chunk.as_ptr());
61 let p0 = vqsubq_u16_x4_x1(p0, v);
62 vst1q_u16_x4(chunk.as_mut_ptr(), p0);
63 }
64 }
65 }
66
67 /// # Safety
68 ///
69 /// Behavior is undefined if the `neon` target feature is not enabled
70 #[target_feature(enable = "neon")]
71 unsafe fn vqsubq_u16_x4_x1(a: uint16x8x4_t, b: uint16x8_t) -> uint16x8x4_t {
72 unsafe {
73 uint16x8x4_t(
74 vqsubq_u16(a.0, b),
75 vqsubq_u16(a.1, b),
76 vqsubq_u16(a.2, b),
77 vqsubq_u16(a.3, b),
78 )
79 }
80 }
81 }
82
83 #[cfg(target_arch = "x86_64")]
84 mod avx2 {
85 use core::arch::x86_64::{
86 __m256i, _mm256_loadu_si256, _mm256_set1_epi16, _mm256_storeu_si256, _mm256_subs_epu16,
87 };
88
89 pub fn slide_hash_chain(table: &mut [u16], wsize: u16) {
90 assert!(crate::cpu_features::is_enabled_avx2());
91 unsafe { slide_hash_chain_internal(table, wsize) }
92 }
93
94 /// # Safety
95 ///
96 /// Behavior is undefined if the `avx` target feature is not enabled
97 #[target_feature(enable = "avx2")]
98 unsafe fn slide_hash_chain_internal(table: &mut [u16], wsize: u16) {
99 debug_assert_eq!(table.len() % 16, 0);
100
101 let ymm_wsize = unsafe { _mm256_set1_epi16(wsize as i16) };
102
103 for chunk in table.chunks_exact_mut(16) {
104 let chunk = chunk.as_mut_ptr() as *mut __m256i;
105
106 unsafe {
107 let value = _mm256_loadu_si256(chunk);
108 let result = _mm256_subs_epu16(value, ymm_wsize);
109 _mm256_storeu_si256(chunk, result);
110 }
111 }
112 }
113 }
114
115 #[cfg(target_arch = "wasm32")]
116 mod wasm {
117 use core::arch::wasm32::{u16x8_splat, u16x8_sub_sat, v128, v128_load, v128_store};
118
119 pub fn slide_hash_chain(table: &mut [u16], wsize: u16) {
120 assert_eq!(table.len() % 8, 0);
121 slide_hash_chain_internal(table, wsize)
122 }
123
124 #[target_feature(enable = "simd128")]
125 fn slide_hash_chain_internal(table: &mut [u16], wsize: u16) {
126 let wsize_v128 = u16x8_splat(wsize);
127
128 for chunk in table.chunks_exact_mut(8) {
129 let chunk_ptr = chunk.as_mut_ptr() as *mut v128;
130
131 // Load the 128-bit value.
132 // SAFETY: the pointer we get from chunks_exact_mut() is valid.
133 let value = unsafe { v128_load(chunk_ptr) };
134
135 // Perform saturating subtraction
136 let result = u16x8_sub_sat(value, wsize_v128);
137
138 // Store the result back.
139 // SAFETY: the pointer we get from chunks_exact_mut() is valid.
140 unsafe { v128_store(chunk_ptr, result) };
141 }
142 }
143 }
144
145 #[cfg(test)]
146 mod tests {
147 use super::*;
148
149 const WSIZE: u16 = 32768;
150
151 const INPUT: [u16; 64] = [
152 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,
153 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,
154 0, 0, 0, 0, 0, 0, 64026, 0, 0, 20182,
155 ];
156
157 const OUTPUT: [u16; 64] = [
158 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,
159 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,
160 0, 0, 31258, 0, 0, 0,
161 ];
162
163 #[test]
164 fn test_slide_hash_rust() {
165 let mut input = INPUT;
166
167 rust::slide_hash_chain(&mut input, WSIZE);
168
169 assert_eq!(input, OUTPUT);
170 }
171
172 #[test]
173 #[cfg(target_arch = "x86_64")]
174 fn test_slide_hash_avx2() {
175 if crate::cpu_features::is_enabled_avx2() {
176 let mut input = INPUT;
177
178 avx2::slide_hash_chain(&mut input, WSIZE);
179
180 assert_eq!(input, OUTPUT);
181 }
182 }
183
184 #[test]
185 #[cfg(target_arch = "aarch64")]
186 fn test_slide_hash_neon() {
187 if crate::cpu_features::is_enabled_neon() {
188 let mut input = INPUT;
189
190 neon::slide_hash_chain(&mut input, WSIZE);
191
192 assert_eq!(input, OUTPUT);
193 }
194 }
195
196 #[test]
197 #[cfg(target_arch = "wasm32")]
198 fn test_slide_hash_neon() {
199 if crate::cpu_features::is_enabled_simd128() {
200 let mut input = INPUT;
201
202 wasm::slide_hash_chain(&mut input, WSIZE);
203
204 assert_eq!(input, OUTPUT);
205 }
206 }
207
208 quickcheck::quickcheck! {
209 fn slide_is_rust_slide(v: Vec<u16>, wsize: u16) -> bool {
210 // pad to a multiple of 32
211 let difference = v.len().next_multiple_of(32) - v.len();
212 let mut v = v;
213 v.extend(core::iter::repeat(u16::MAX).take(difference));
214
215
216 let mut a = v.clone();
217 let mut b = v;
218
219 rust::slide_hash_chain(&mut a, wsize);
220 slide_hash_chain(&mut b, wsize);
221
222 a == b
223 }
224 }
225 }