rustfft/avx/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
use crate::{Fft, FftDirection, FftNum};
use std::arch::x86_64::{__m256, __m256d};
use std::sync::Arc;
pub trait AvxNum: FftNum {
type VectorType: AvxVector256<ScalarType = Self>;
}
impl AvxNum for f32 {
type VectorType = __m256;
}
impl AvxNum for f64 {
type VectorType = __m256d;
}
// Data that most (non-butterfly) SIMD FFT algorithms share
// Algorithms aren't required to use this struct, but it allows for a lot of reduction in code duplication
struct CommonSimdData<T, V> {
inner_fft: Arc<dyn Fft<T>>,
twiddles: Box<[V]>,
len: usize,
inplace_scratch_len: usize,
outofplace_scratch_len: usize,
direction: FftDirection,
}
macro_rules! boilerplate_avx_fft {
($struct_name:ident, $len_fn:expr, $inplace_scratch_len_fn:expr, $out_of_place_scratch_len_fn:expr) => {
impl<A: AvxNum, T: FftNum> Fft<T> for $struct_name<A, T> {
fn process_outofplace_with_scratch(
&self,
input: &mut [Complex<T>],
output: &mut [Complex<T>],
scratch: &mut [Complex<T>],
) {
let required_scratch = self.get_outofplace_scratch_len();
if scratch.len() < required_scratch
|| input.len() < self.len()
|| output.len() != input.len()
{
// 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
fft_error_outofplace(
self.len(),
input.len(),
output.len(),
self.get_outofplace_scratch_len(),
scratch.len(),
);
return; // Unreachable, because fft_error_outofplace asserts, but it helps codegen to put it here
}
let scratch = &mut scratch[..required_scratch];
let result = array_utils::iter_chunks_zipped(
input,
output,
self.len(),
|in_chunk, out_chunk| {
self.perform_fft_out_of_place(in_chunk, out_chunk, scratch)
},
);
if result.is_err() {
// We want to trigger a panic, because the buffer sizes weren't cleanly divisible by the FFT size,
// 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
fft_error_outofplace(
self.len(),
input.len(),
output.len(),
self.get_outofplace_scratch_len(),
scratch.len(),
)
}
}
fn process_with_scratch(&self, buffer: &mut [Complex<T>], scratch: &mut [Complex<T>]) {
let required_scratch = self.get_inplace_scratch_len();
if scratch.len() < required_scratch || buffer.len() < self.len() {
// 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
fft_error_inplace(
self.len(),
buffer.len(),
self.get_inplace_scratch_len(),
scratch.len(),
);
return; // Unreachable, because fft_error_inplace asserts, but it helps codegen to put it here
}
let scratch = &mut scratch[..required_scratch];
let result = array_utils::iter_chunks(buffer, self.len(), |chunk| {
self.perform_fft_inplace(chunk, scratch)
});
if result.is_err() {
// We want to trigger a panic, because the buffer sizes weren't cleanly divisible by the FFT size,
// 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
fft_error_inplace(
self.len(),
buffer.len(),
self.get_inplace_scratch_len(),
scratch.len(),
)
}
}
#[inline(always)]
fn get_inplace_scratch_len(&self) -> usize {
$inplace_scratch_len_fn(self)
}
#[inline(always)]
fn get_outofplace_scratch_len(&self) -> usize {
$out_of_place_scratch_len_fn(self)
}
}
impl<A: AvxNum, T> Length for $struct_name<A, T> {
#[inline(always)]
fn len(&self) -> usize {
$len_fn(self)
}
}
impl<A: AvxNum, T> Direction for $struct_name<A, T> {
#[inline(always)]
fn fft_direction(&self) -> FftDirection {
self.direction
}
}
};
}
macro_rules! boilerplate_avx_fft_commondata {
($struct_name:ident) => {
impl<A: AvxNum, T: FftNum> Fft<T> for $struct_name<A, T> {
fn process_outofplace_with_scratch(
&self,
input: &mut [Complex<T>],
output: &mut [Complex<T>],
scratch: &mut [Complex<T>],
) {
if self.len() == 0 {
return;
}
let required_scratch = self.get_outofplace_scratch_len();
if scratch.len() < required_scratch
|| input.len() < self.len()
|| output.len() != input.len()
{
// 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
fft_error_outofplace(
self.len(),
input.len(),
output.len(),
self.get_outofplace_scratch_len(),
scratch.len(),
);
return; // Unreachable, because fft_error_outofplace asserts, but it helps codegen to put it here
}
let scratch = &mut scratch[..required_scratch];
let result = array_utils::iter_chunks_zipped(
input,
output,
self.len(),
|in_chunk, out_chunk| {
self.perform_fft_out_of_place(in_chunk, out_chunk, scratch)
},
);
if result.is_err() {
// We want to trigger a panic, because the buffer sizes weren't cleanly divisible by the FFT size,
// 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
fft_error_outofplace(
self.len(),
input.len(),
output.len(),
self.get_outofplace_scratch_len(),
scratch.len(),
);
}
}
fn process_with_scratch(&self, buffer: &mut [Complex<T>], scratch: &mut [Complex<T>]) {
if self.len() == 0 {
return;
}
let required_scratch = self.get_inplace_scratch_len();
if scratch.len() < required_scratch || buffer.len() < self.len() {
// 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
fft_error_inplace(
self.len(),
buffer.len(),
self.get_inplace_scratch_len(),
scratch.len(),
);
return; // Unreachable, because fft_error_inplace asserts, but it helps codegen to put it here
}
let scratch = &mut scratch[..required_scratch];
let result = array_utils::iter_chunks(buffer, self.len(), |chunk| {
self.perform_fft_inplace(chunk, scratch)
});
if result.is_err() {
// We want to trigger a panic, because the buffer sizes weren't cleanly divisible by the FFT size,
// 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
fft_error_inplace(
self.len(),
buffer.len(),
self.get_inplace_scratch_len(),
scratch.len(),
);
}
}
#[inline(always)]
fn get_inplace_scratch_len(&self) -> usize {
self.common_data.inplace_scratch_len
}
#[inline(always)]
fn get_outofplace_scratch_len(&self) -> usize {
self.common_data.outofplace_scratch_len
}
}
impl<A: AvxNum, T> Length for $struct_name<A, T> {
#[inline(always)]
fn len(&self) -> usize {
self.common_data.len
}
}
impl<A: AvxNum, T> Direction for $struct_name<A, T> {
#[inline(always)]
fn fft_direction(&self) -> FftDirection {
self.common_data.direction
}
}
};
}
#[macro_use]
mod avx_vector;
mod avx32_butterflies;
mod avx32_utils;
mod avx64_butterflies;
mod avx64_utils;
mod avx_bluesteins;
mod avx_mixed_radix;
mod avx_raders;
pub mod avx_planner;
pub use self::avx32_butterflies::{
Butterfly11Avx, Butterfly128Avx, Butterfly12Avx, Butterfly16Avx, Butterfly24Avx,
Butterfly256Avx, Butterfly27Avx, Butterfly32Avx, Butterfly36Avx, Butterfly48Avx,
Butterfly512Avx, Butterfly54Avx, Butterfly5Avx, Butterfly64Avx, Butterfly72Avx, Butterfly7Avx,
Butterfly8Avx, Butterfly9Avx,
};
pub use self::avx64_butterflies::{
Butterfly11Avx64, Butterfly128Avx64, Butterfly12Avx64, Butterfly16Avx64, Butterfly18Avx64,
Butterfly24Avx64, Butterfly256Avx64, Butterfly27Avx64, Butterfly32Avx64, Butterfly36Avx64,
Butterfly512Avx64, Butterfly5Avx64, Butterfly64Avx64, Butterfly7Avx64, Butterfly8Avx64,
Butterfly9Avx64,
};
pub use self::avx_bluesteins::BluesteinsAvx;
pub use self::avx_mixed_radix::{
MixedRadix11xnAvx, MixedRadix12xnAvx, MixedRadix16xnAvx, MixedRadix2xnAvx, MixedRadix3xnAvx,
MixedRadix4xnAvx, MixedRadix5xnAvx, MixedRadix6xnAvx, MixedRadix7xnAvx, MixedRadix8xnAvx,
MixedRadix9xnAvx,
};
pub use self::avx_raders::RadersAvx2;
use self::avx_vector::AvxVector256;