| 1 |
use std::any::TypeId; |
| 2 |
|
| 3 |
/// Calculate the sum of an expression consisting of just plus and minus, like `value = a + b - c + d`. |
| 4 |
/// The expression is rewritten to `value = a + (b - (c - d))` (note the flipped sign on d). |
| 5 |
/// After this the `$add` and `$sub` functions are used to make the calculation. |
| 6 |
/// For f32 using `f32x4_add` and `f32x4_sub`, the expression `value = a + b - c + d` becomes: |
| 7 |
/// ``` |
| 8 |
/// let value = f32x4_add(a, f32x4_sub(b, f32x4_sub(c, d))); |
| 9 |
/// ``` |
| 10 |
/// Only plus and minus are supported, and all the terms must be plain scalar variables. |
| 11 |
/// Using array indices, like `value = temp[0] + temp[1]` is not supported. |
| 12 |
macro_rules! calc_sum { |
| 13 |
($add:ident, $sub:ident, + $acc:tt + $($rest:tt)*)=> { |
| 14 |
$add($acc, calc_sum!($add, $sub, + $($rest)*)) |
| 15 |
}; |
| 16 |
($add:ident, $sub:ident, + $acc:tt - $($rest:tt)*)=> { |
| 17 |
$sub($acc, calc_sum!($add, $sub, - $($rest)*)) |
| 18 |
}; |
| 19 |
($add:ident, $sub:ident, - $acc:tt + $($rest:tt)*)=> { |
| 20 |
$sub($acc, calc_sum!($add, $sub, + $($rest)*)) |
| 21 |
}; |
| 22 |
($add:ident, $sub:ident, - $acc:tt - $($rest:tt)*)=> { |
| 23 |
$add($acc, calc_sum!($add, $sub, - $($rest)*)) |
| 24 |
}; |
| 25 |
($add:ident, $sub:ident, $acc:tt + $($rest:tt)*)=> { |
| 26 |
$add($acc, calc_sum!($add, $sub, + $($rest)*)) |
| 27 |
}; |
| 28 |
($add:ident, $sub:ident, $acc:tt - $($rest:tt)*)=> { |
| 29 |
$sub($acc, calc_sum!($add, $sub, - $($rest)*)) |
| 30 |
}; |
| 31 |
($add:ident, $sub:ident, + $val:tt) => {$val}; |
| 32 |
($add:ident, $sub:ident, - $val:tt) => {$val}; |
| 33 |
} |
| 34 |
|
| 35 |
/// Calculate the sum of an expression consisting of just plus and minus, like a + b - c + d |
| 36 |
macro_rules! calc_f32 { |
| 37 |
($($tokens:tt)*) => { calc_sum!(f32x4_add, f32x4_sub, $($tokens)*)}; |
| 38 |
} |
| 39 |
|
| 40 |
/// Calculate the sum of an expression consisting of just plus and minus, like a + b - c + d |
| 41 |
macro_rules! calc_f64 { |
| 42 |
($($tokens:tt)*) => { calc_sum!(f64x2_add, f64x2_sub, $($tokens)*)}; |
| 43 |
} |
| 44 |
|
| 45 |
/// Helper function to assert we have the right float type |
| 46 |
pub fn assert_f32<T: 'static>() { |
| 47 |
let id_f32 = TypeId::of::<f32>(); |
| 48 |
let id_t = TypeId::of::<T>(); |
| 49 |
assert!(id_t == id_f32, "Wrong float type, must be f32"); |
| 50 |
} |
| 51 |
|
| 52 |
/// Helper function to assert we have the right float type |
| 53 |
pub fn assert_f64<T: 'static>() { |
| 54 |
let id_f64 = TypeId::of::<f64>(); |
| 55 |
let id_t = TypeId::of::<T>(); |
| 56 |
assert!(id_t == id_f64, "Wrong float type, must be f64"); |
| 57 |
} |
| 58 |
|
| 59 |
/// Shuffle elements to interleave two contiguous sets of f32, from an array of simd vectors to a new array of simd vectors |
| 60 |
macro_rules! interleave_complex_f32 { |
| 61 |
($input:ident, $offset:literal, { $($idx:literal),* }) => { |
| 62 |
[ |
| 63 |
$( |
| 64 |
extract_lo_lo_f32($input[$idx], $input[$idx+$offset]), |
| 65 |
extract_hi_hi_f32($input[$idx], $input[$idx+$offset]), |
| 66 |
)* |
| 67 |
] |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/// Shuffle elements to interleave two contiguous sets of f32, from an array of simd vectors to a new array of simd vectors |
| 72 |
/// This statement: |
| 73 |
/// ``` |
| 74 |
/// let values = separate_interleaved_complex_f32!(input, {0, 2, 4}); |
| 75 |
/// ``` |
| 76 |
/// is equivalent to: |
| 77 |
/// ``` |
| 78 |
/// let values = [ |
| 79 |
/// extract_lo_lo_f32(input[0], input[1]), |
| 80 |
/// extract_lo_lo_f32(input[2], input[3]), |
| 81 |
/// extract_lo_lo_f32(input[4], input[5]), |
| 82 |
/// extract_hi_hi_f32(input[0], input[1]), |
| 83 |
/// extract_hi_hi_f32(input[2], input[3]), |
| 84 |
/// extract_hi_hi_f32(input[4], input[5]), |
| 85 |
/// ]; |
| 86 |
macro_rules! separate_interleaved_complex_f32 { |
| 87 |
($input:ident, { $($idx:literal),* }) => { |
| 88 |
[ |
| 89 |
$( |
| 90 |
extract_lo_lo_f32($input[$idx], $input[$idx+1]), |
| 91 |
)* |
| 92 |
$( |
| 93 |
extract_hi_hi_f32($input[$idx], $input[$idx+1]), |
| 94 |
)* |
| 95 |
] |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
macro_rules! boilerplate_fft_wasm_simd_oop { |
| 100 |
($struct_name:ident, $len_fn:expr) => { |
| 101 |
impl<T: FftNum> Fft<T> for $struct_name<T> { |
| 102 |
fn process_outofplace_with_scratch( |
| 103 |
&self, |
| 104 |
input: &mut [Complex<T>], |
| 105 |
output: &mut [Complex<T>], |
| 106 |
_scratch: &mut [Complex<T>], |
| 107 |
) { |
| 108 |
if self.len() == 0 { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
if input.len() < self.len() || output.len() != input.len() { |
| 113 |
// We want to trigger a panic, but we want to avoid doing it in this function to reduce code size, so call a function marked cold and inline(never) that will do it for us |
| 114 |
fft_error_outofplace(self.len(), input.len(), output.len(), 0, 0); |
| 115 |
return; // Unreachable, because fft_error_outofplace asserts, but it helps codegen to put it here |
| 116 |
} |
| 117 |
|
| 118 |
let result = unsafe { |
| 119 |
array_utils::iter_chunks_zipped( |
| 120 |
input, |
| 121 |
output, |
| 122 |
self.len(), |
| 123 |
|in_chunk, out_chunk| { |
| 124 |
self.perform_fft_out_of_place(in_chunk, out_chunk, &mut []) |
| 125 |
}, |
| 126 |
) |
| 127 |
}; |
| 128 |
|
| 129 |
if result.is_err() { |
| 130 |
// We want to trigger a panic, because the buffer sizes weren't cleanly divisible by the FFT size, |
| 131 |
// but we want to avoid doing it in this function to reduce code size, so call a function marked cold and inline(never) that will do it for us |
| 132 |
fft_error_outofplace(self.len(), input.len(), output.len(), 0, 0); |
| 133 |
} |
| 134 |
} |
| 135 |
fn process_with_scratch(&self, buffer: &mut [Complex<T>], scratch: &mut [Complex<T>]) { |
| 136 |
if self.len() == 0 { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
let required_scratch = self.get_inplace_scratch_len(); |
| 141 |
if scratch.len() < required_scratch || buffer.len() < self.len() { |
| 142 |
// We want to trigger a panic, but we want to avoid doing it in this function to reduce code size, so call a function marked cold and inline(never) that will do it for us |
| 143 |
fft_error_inplace( |
| 144 |
self.len(), |
| 145 |
buffer.len(), |
| 146 |
self.get_inplace_scratch_len(), |
| 147 |
scratch.len(), |
| 148 |
); |
| 149 |
return; // Unreachable, because fft_error_inplace asserts, but it helps codegen to put it here |
| 150 |
} |
| 151 |
|
| 152 |
let scratch = &mut scratch[..required_scratch]; |
| 153 |
let result = unsafe { |
| 154 |
array_utils::iter_chunks(buffer, self.len(), |chunk| { |
| 155 |
self.perform_fft_out_of_place(chunk, scratch, &mut []); |
| 156 |
chunk.copy_from_slice(scratch); |
| 157 |
}) |
| 158 |
}; |
| 159 |
if result.is_err() { |
| 160 |
// We want to trigger a panic, because the buffer sizes weren't cleanly divisible by the FFT size, |
| 161 |
// but we want to avoid doing it in this function to reduce code size, so call a function marked cold and inline(never) that will do it for us |
| 162 |
fft_error_inplace( |
| 163 |
self.len(), |
| 164 |
buffer.len(), |
| 165 |
self.get_inplace_scratch_len(), |
| 166 |
scratch.len(), |
| 167 |
); |
| 168 |
} |
| 169 |
} |
| 170 |
#[inline(always)] |
| 171 |
fn get_inplace_scratch_len(&self) -> usize { |
| 172 |
self.len() |
| 173 |
} |
| 174 |
#[inline(always)] |
| 175 |
fn get_outofplace_scratch_len(&self) -> usize { |
| 176 |
0 |
| 177 |
} |
| 178 |
} |
| 179 |
impl<T> Length for $struct_name<T> { |
| 180 |
#[inline(always)] |
| 181 |
fn len(&self) -> usize { |
| 182 |
$len_fn(self) |
| 183 |
} |
| 184 |
} |
| 185 |
impl<T> Direction for $struct_name<T> { |
| 186 |
#[inline(always)] |
| 187 |
fn fft_direction(&self) -> FftDirection { |
| 188 |
self.direction |
| 189 |
} |
| 190 |
} |
| 191 |
}; |
| 192 |
} |
| 193 |
|
| 194 |
#[cfg(test)] |
| 195 |
mod unit_tests { |
| 196 |
use core::arch::wasm32::*; |
| 197 |
|
| 198 |
use wasm_bindgen_test::wasm_bindgen_test; |
| 199 |
|
| 200 |
#[wasm_bindgen_test] |
| 201 |
fn test_calc_f32() { |
| 202 |
unsafe { |
| 203 |
let a = f32x4(1.0, 1.0, 1.0, 1.0); |
| 204 |
let b = f32x4(2.0, 2.0, 2.0, 2.0); |
| 205 |
let c = f32x4(3.0, 3.0, 3.0, 3.0); |
| 206 |
let d = f32x4(4.0, 4.0, 4.0, 4.0); |
| 207 |
let e = f32x4(5.0, 5.0, 5.0, 5.0); |
| 208 |
let f = f32x4(6.0, 6.0, 6.0, 6.0); |
| 209 |
let g = f32x4(7.0, 7.0, 7.0, 7.0); |
| 210 |
let h = f32x4(8.0, 8.0, 8.0, 8.0); |
| 211 |
let i = f32x4(9.0, 9.0, 9.0, 9.0); |
| 212 |
let expected: f32 = 1.0 + 2.0 - 3.0 + 4.0 - 5.0 + 6.0 - 7.0 - 8.0 + 9.0; |
| 213 |
let res = calc_f32!(a + b - c + d - e + f - g - h + i); |
| 214 |
let sum = std::mem::transmute::<v128, [f32; 4]>(res); |
| 215 |
assert_eq!(sum[0], expected); |
| 216 |
assert_eq!(sum[1], expected); |
| 217 |
assert_eq!(sum[2], expected); |
| 218 |
assert_eq!(sum[3], expected); |
| 219 |
} |
| 220 |
} |
| 221 |
#[wasm_bindgen_test] |
| 222 |
fn test_calc_f64() { |
| 223 |
unsafe { |
| 224 |
let a = f64x2(1.0, 1.0); |
| 225 |
let b = f64x2(2.0, 2.0); |
| 226 |
let c = f64x2(3.0, 3.0); |
| 227 |
let d = f64x2(4.0, 4.0); |
| 228 |
let e = f64x2(5.0, 5.0); |
| 229 |
let f = f64x2(6.0, 6.0); |
| 230 |
let g = f64x2(7.0, 7.0); |
| 231 |
let h = f64x2(8.0, 8.0); |
| 232 |
let i = f64x2(9.0, 9.0); |
| 233 |
let expected: f64 = 1.0 + 2.0 - 3.0 + 4.0 - 5.0 + 6.0 - 7.0 - 8.0 + 9.0; |
| 234 |
let res = calc_f64!(a + b - c + d - e + f - g - h + i); |
| 235 |
let sum = std::mem::transmute::<v128, [f64; 2]>(res); |
| 236 |
assert_eq!(sum[0], expected); |
| 237 |
assert_eq!(sum[1], expected); |
| 238 |
} |
| 239 |
} |
| 240 |
} |