Debian Codegraph @

.cargo/vendor/rustfft/src/wasm_simd/wasm_simd_vector.rs

raw
1 use core::arch::wasm32::*;
2 use num_complex::Complex;
3 use std::ops::{Deref, DerefMut};
4
5 use crate::array_utils::DoubleBuf;
6
7 /// Read these indexes from an WasmSimdArray and build an array of simd vectors.
8 /// Takes a name of a vector to read from, and a list of indexes to read.
9 /// This statement:
10 /// ```
11 /// let values = read_complex_to_array!(input, {0, 1, 2, 3});
12 /// ```
13 /// is equivalent to:
14 /// ```
15 /// let values = [
16 /// input.load_complex(0),
17 /// input.load_complex(1),
18 /// input.load_complex(2),
19 /// input.load_complex(3),
20 /// ];
21 /// ```
22 macro_rules! read_complex_to_array {
23 ($input:ident, { $($idx:literal),* }) => {
24 [
25 $(
26 $input.load_complex($idx),
27 )*
28 ]
29 }
30 }
31
32 /// Read these indexes from an WasmSimdArray and build an array or partially filled simd vectors.
33 /// Takes a name of a vector to read from, and a list of indexes to read.
34 /// This statement:
35 /// ```
36 /// let values = read_partial1_complex_to_array!(input, {0, 1, 2, 3});
37 /// ```
38 /// is equivalent to:
39 /// ```
40 /// let values = [
41 /// input.load1_complex(0),
42 /// input.load1_complex(1),
43 /// input.load1_complex(2),
44 /// input.load1_complex(3),
45 /// ];
46 /// ```
47 macro_rules! read_partial1_complex_to_array {
48 ($input:ident, { $($idx:literal),* }) => {
49 [
50 $(
51 $input.load1_complex($idx),
52 )*
53 ]
54 }
55 }
56
57 /// Write these indexes of an array of simd vectors to the same indexes of an WasmSimdArray.
58 /// Takes a name of a vector to read from, one to write to, and a list of indexes.
59 /// This statement:
60 /// ```
61 /// let values = write_complex_to_array!(input, output, {0, 1, 2, 3});
62 /// ```
63 /// is equivalent to:
64 /// ```
65 /// let values = [
66 /// output.store_complex(input[0], 0),
67 /// output.store_complex(input[1], 1),
68 /// output.store_complex(input[2], 2),
69 /// output.store_complex(input[3], 3),
70 /// ];
71 /// ```
72 macro_rules! write_complex_to_array {
73 ($input:ident, $output:ident, { $($idx:literal),* }) => {
74 $(
75 $output.store_complex($input[$idx], $idx);
76 )*
77 }
78 }
79
80 /// Write the low half of these indexes of an array of simd vectors to the same indexes of an WasmSimdArray.
81 /// Takes a name of a vector to read from, one to write to, and a list of indexes.
82 /// This statement:
83 /// ```
84 /// let values = write_partial_lo_complex_to_array!(input, output, {0, 1, 2, 3});
85 /// ```
86 /// is equivalent to:
87 /// ```
88 /// let values = [
89 /// output.store_partial_lo_complex(input[0], 0),
90 /// output.store_partial_lo_complex(input[1], 1),
91 /// output.store_partial_lo_complex(input[2], 2),
92 /// output.store_partial_lo_complex(input[3], 3),
93 /// ];
94 /// ```
95 macro_rules! write_partial_lo_complex_to_array {
96 ($input:ident, $output:ident, { $($idx:literal),* }) => {
97 $(
98 $output.store_partial_lo_complex($input[$idx], $idx);
99 )*
100 }
101 }
102
103 /// Write these indexes of an array of simd vectors to the same indexes, multiplied by a stride, of an WasmSimdArray.
104 /// Takes a name of a vector to read from, one to write to, an integer stride, and a list of indexes.
105 /// This statement:
106 /// ```
107 /// let values = write_complex_to_array_separate!(input, output, {0, 1, 2, 3});
108 /// ```
109 /// is equivalent to:
110 /// ```
111 /// let values = [
112 /// output.store_complex(input[0], 0),
113 /// output.store_complex(input[1], 2),
114 /// output.store_complex(input[2], 4),
115 /// output.store_complex(input[3], 6),
116 /// ];
117 /// ```
118 macro_rules! write_complex_to_array_strided {
119 ($input:ident, $output:ident, $stride:literal, { $($idx:literal),* }) => {
120 $(
121 $output.store_complex($input[$idx], $idx*$stride);
122 )*
123 }
124 }
125
126 pub trait WasmSimdNum {
127 type VectorType;
128 const COMPLEX_PER_VECTOR: usize;
129 }
130 impl WasmSimdNum for f32 {
131 type VectorType = v128;
132 const COMPLEX_PER_VECTOR: usize = 2;
133 }
134 impl WasmSimdNum for f64 {
135 type VectorType = v128;
136 const COMPLEX_PER_VECTOR: usize = 1;
137 }
138
139 /// A trait to handle reading from an array of complex floats into WASM SIMD vectors.
140 /// WASM works with 128-bit vectors, meaning a vector can hold two complex f32,
141 /// or a single complex f64.
142 pub trait WasmSimdArray<T: WasmSimdNum>: Deref {
143 /// Load complex numbers from the array to fill a WASM SIMD vector.
144 unsafe fn load_complex(&self, index: usize) -> T::VectorType;
145 /// Load a single complex number from the array into a WASM SIMD vector, setting the unused elements to zero.
146 unsafe fn load_partial1_complex(&self, index: usize) -> T::VectorType;
147 /// Load a single complex number from the array, and copy it to all elements of a WASM SIMD vector.
148 unsafe fn load1_complex(&self, index: usize) -> T::VectorType;
149 }
150
151 impl WasmSimdArray<f32> for &[Complex<f32>] {
152 #[inline(always)]
153 unsafe fn load_complex(&self, index: usize) -> <f32 as WasmSimdNum>::VectorType {
154 debug_assert!(self.len() >= index + <f32 as WasmSimdNum>::COMPLEX_PER_VECTOR);
155 v128_load(self.as_ptr().add(index) as *const v128)
156 }
157
158 #[inline(always)]
159 unsafe fn load_partial1_complex(&self, index: usize) -> <f32 as WasmSimdNum>::VectorType {
160 debug_assert!(self.len() >= index + 1);
161 v128_load64_lane::<0>(f32x4_splat(0.0), self.as_ptr().add(index) as *const u64)
162 }
163
164 #[inline(always)]
165 unsafe fn load1_complex(&self, index: usize) -> <f32 as WasmSimdNum>::VectorType {
166 debug_assert!(self.len() >= index + 1);
167
168 v128_load64_splat(self.as_ptr().add(index) as *const u64)
169 }
170 }
171
172 impl WasmSimdArray<f32> for &mut [Complex<f32>] {
173 #[inline(always)]
174 unsafe fn load_complex(&self, index: usize) -> <f32 as WasmSimdNum>::VectorType {
175 debug_assert!(self.len() >= index + <f32 as WasmSimdNum>::COMPLEX_PER_VECTOR);
176 v128_load(self.as_ptr().add(index) as *const v128)
177 }
178
179 #[inline(always)]
180 unsafe fn load_partial1_complex(&self, index: usize) -> <f32 as WasmSimdNum>::VectorType {
181 debug_assert!(self.len() >= index + 1);
182 v128_load64_lane::<0>(f32x4_splat(0.0), self.as_ptr().add(index) as *const u64)
183 }
184
185 #[inline(always)]
186 unsafe fn load1_complex(&self, index: usize) -> <f32 as WasmSimdNum>::VectorType {
187 debug_assert!(self.len() >= index + 1);
188 v128_load64_splat(self.as_ptr().add(index) as *const u64)
189 }
190 }
191
192 impl WasmSimdArray<f64> for &[Complex<f64>] {
193 #[inline(always)]
194 unsafe fn load_complex(&self, index: usize) -> <f64 as WasmSimdNum>::VectorType {
195 debug_assert!(self.len() >= index + <f64 as WasmSimdNum>::COMPLEX_PER_VECTOR);
196 v128_load(self.as_ptr().add(index) as *const v128)
197 }
198
199 #[inline(always)]
200 unsafe fn load_partial1_complex(&self, _index: usize) -> <f64 as WasmSimdNum>::VectorType {
201 unimplemented!("Impossible to do a partial load of complex f64's");
202 }
203
204 #[inline(always)]
205 unsafe fn load1_complex(&self, _index: usize) -> <f64 as WasmSimdNum>::VectorType {
206 unimplemented!("Impossible to do a partial load of complex f64's");
207 }
208 }
209
210 impl WasmSimdArray<f64> for &mut [Complex<f64>] {
211 #[inline(always)]
212 unsafe fn load_complex(&self, index: usize) -> <f64 as WasmSimdNum>::VectorType {
213 debug_assert!(self.len() >= index + <f64 as WasmSimdNum>::COMPLEX_PER_VECTOR);
214 v128_load(self.as_ptr().add(index) as *const v128)
215 }
216
217 #[inline(always)]
218 unsafe fn load_partial1_complex(&self, _index: usize) -> <f64 as WasmSimdNum>::VectorType {
219 unimplemented!("Impossible to do a partial load of complex f64's");
220 }
221
222 #[inline(always)]
223 unsafe fn load1_complex(&self, _index: usize) -> <f64 as WasmSimdNum>::VectorType {
224 unimplemented!("Impossible to do a partial load of complex f64's");
225 }
226 }
227
228 impl<'a, T: WasmSimdNum> WasmSimdArray<T> for DoubleBuf<'a, T>
229 where
230 &'a [Complex<T>]: WasmSimdArray<T>,
231 {
232 #[inline(always)]
233 unsafe fn load_complex(&self, index: usize) -> T::VectorType {
234 self.input.load_complex(index)
235 }
236 #[inline(always)]
237 unsafe fn load_partial1_complex(&self, index: usize) -> T::VectorType {
238 self.input.load_partial1_complex(index)
239 }
240 #[inline(always)]
241 unsafe fn load1_complex(&self, index: usize) -> T::VectorType {
242 self.input.load1_complex(index)
243 }
244 }
245
246 /// A trait to handle writing to an array of complex floats from WASM SIMD vectors.
247 /// WASM works with 128-bit vectors, meaning a vector can hold two complex f32,
248 /// or a single complex f64.
249 pub trait WasmSimdArrayMut<T: WasmSimdNum>: WasmSimdArray<T> + DerefMut {
250 /// Store all complex numbers from a WASM SIMD vector to the array.
251 unsafe fn store_complex(&mut self, vector: T::VectorType, index: usize);
252 /// Store the low complex number from a WASM SIMD vector to the array.
253 unsafe fn store_partial_lo_complex(&mut self, vector: T::VectorType, index: usize);
254 /// Store the high complex number from a WASM SIMD vector to the array.
255 unsafe fn store_partial_hi_complex(&mut self, vector: T::VectorType, index: usize);
256 }
257
258 impl WasmSimdArrayMut<f32> for &mut [Complex<f32>] {
259 #[inline(always)]
260 unsafe fn store_complex(&mut self, vector: <f32 as WasmSimdNum>::VectorType, index: usize) {
261 debug_assert!(self.len() >= index + <f32 as WasmSimdNum>::COMPLEX_PER_VECTOR);
262 v128_store(self.as_mut_ptr().add(index) as *mut v128, vector);
263 }
264
265 #[inline(always)]
266 unsafe fn store_partial_hi_complex(
267 &mut self,
268 vector: <f32 as WasmSimdNum>::VectorType,
269 index: usize,
270 ) {
271 debug_assert!(self.len() >= index + 1);
272 v128_store64_lane::<1>(vector, self.as_mut_ptr().add(index) as *mut u64);
273 }
274
275 #[inline(always)]
276 unsafe fn store_partial_lo_complex(
277 &mut self,
278 vector: <f32 as WasmSimdNum>::VectorType,
279 index: usize,
280 ) {
281 debug_assert!(self.len() >= index + 1);
282 v128_store64_lane::<0>(vector, self.as_mut_ptr().add(index) as *mut u64);
283 }
284 }
285
286 impl WasmSimdArrayMut<f64> for &mut [Complex<f64>] {
287 #[inline(always)]
288 unsafe fn store_complex(&mut self, vector: <f64 as WasmSimdNum>::VectorType, index: usize) {
289 debug_assert!(self.len() >= index + <f64 as WasmSimdNum>::COMPLEX_PER_VECTOR);
290 v128_store(self.as_mut_ptr().add(index) as *mut v128, vector);
291 }
292
293 #[inline(always)]
294 unsafe fn store_partial_hi_complex(
295 &mut self,
296 _vector: <f64 as WasmSimdNum>::VectorType,
297 _index: usize,
298 ) {
299 unimplemented!("Impossible to do a partial store of complex f64's");
300 }
301 #[inline(always)]
302 unsafe fn store_partial_lo_complex(
303 &mut self,
304 _vector: <f64 as WasmSimdNum>::VectorType,
305 _index: usize,
306 ) {
307 unimplemented!("Impossible to do a partial store of complex f64's");
308 }
309 }
310
311 impl<'a, T: WasmSimdNum> WasmSimdArrayMut<T> for DoubleBuf<'a, T>
312 where
313 Self: WasmSimdArray<T>,
314 &'a mut [Complex<T>]: WasmSimdArrayMut<T>,
315 {
316 #[inline(always)]
317 unsafe fn store_complex(&mut self, vector: T::VectorType, index: usize) {
318 self.output.store_complex(vector, index);
319 }
320 #[inline(always)]
321 unsafe fn store_partial_hi_complex(&mut self, vector: T::VectorType, index: usize) {
322 self.output.store_partial_hi_complex(vector, index);
323 }
324 #[inline(always)]
325 unsafe fn store_partial_lo_complex(&mut self, vector: T::VectorType, index: usize) {
326 self.output.store_partial_lo_complex(vector, index);
327 }
328 }
329
330 #[cfg(test)]
331 mod unit_tests {
332 use super::*;
333
334 use num_complex::Complex;
335 use wasm_bindgen_test::wasm_bindgen_test;
336
337 #[wasm_bindgen_test]
338 fn test_load_f64() {
339 unsafe {
340 let val1: Complex<f64> = Complex::new(1.0, 2.0);
341 let val2: Complex<f64> = Complex::new(3.0, 4.0);
342 let val3: Complex<f64> = Complex::new(5.0, 6.0);
343 let val4: Complex<f64> = Complex::new(7.0, 8.0);
344 let values = vec![val1, val2, val3, val4];
345 let slice = values.as_slice();
346 let load1 = slice.load_complex(0);
347 let load2 = slice.load_complex(1);
348 let load3 = slice.load_complex(2);
349 let load4 = slice.load_complex(3);
350 assert_eq!(val1, std::mem::transmute::<v128, Complex<f64>>(load1));
351 assert_eq!(val2, std::mem::transmute::<v128, Complex<f64>>(load2));
352 assert_eq!(val3, std::mem::transmute::<v128, Complex<f64>>(load3));
353 assert_eq!(val4, std::mem::transmute::<v128, Complex<f64>>(load4));
354 }
355 }
356
357 #[wasm_bindgen_test]
358 fn test_store_f64() {
359 unsafe {
360 let val1: Complex<f64> = Complex::new(1.0, 2.0);
361 let val2: Complex<f64> = Complex::new(3.0, 4.0);
362 let val3: Complex<f64> = Complex::new(5.0, 6.0);
363 let val4: Complex<f64> = Complex::new(7.0, 8.0);
364
365 let nbr1 = v128_load(&val1 as *const _ as *const v128);
366 let nbr2 = v128_load(&val2 as *const _ as *const v128);
367 let nbr3 = v128_load(&val3 as *const _ as *const v128);
368 let nbr4 = v128_load(&val4 as *const _ as *const v128);
369
370 let mut values: Vec<Complex<f64>> = vec![Complex::new(0.0, 0.0); 4];
371 let mut slice = values.as_mut_slice();
372 slice.store_complex(nbr1, 0);
373 slice.store_complex(nbr2, 1);
374 slice.store_complex(nbr3, 2);
375 slice.store_complex(nbr4, 3);
376 assert_eq!(val1, values[0]);
377 assert_eq!(val2, values[1]);
378 assert_eq!(val3, values[2]);
379 assert_eq!(val4, values[3]);
380 }
381 }
382 }