rustfft/common.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
use num_traits::{FromPrimitive, Signed};
use std::fmt::Debug;
/// Generic floating point number, implemented for f32 and f64
pub trait FftNum: Copy + FromPrimitive + Signed + Sync + Send + Debug + 'static {}
impl<T> FftNum for T where T: Copy + FromPrimitive + Signed + Sync + Send + Debug + 'static {}
// Prints an error raised by an in-place FFT algorithm's `process_inplace` method
// Marked cold and inline never to keep all formatting code out of the many monomorphized process_inplace methods
#[cold]
#[inline(never)]
pub fn fft_error_inplace(
expected_len: usize,
actual_len: usize,
expected_scratch: usize,
actual_scratch: usize,
) {
assert!(
actual_len >= expected_len,
"Provided FFT buffer was too small. Expected len = {}, got len = {}",
expected_len,
actual_len
);
assert_eq!(
actual_len % expected_len,
0,
"Input FFT buffer must be a multiple of FFT length. Expected multiple of {}, got len = {}",
expected_len,
actual_len
);
assert!(
actual_scratch >= expected_scratch,
"Not enough scratch space was provided. Expected scratch len >= {}, got scratch len = {}",
expected_scratch,
actual_scratch
);
}
// Prints an error raised by an in-place FFT algorithm's `process_inplace` method
// Marked cold and inline never to keep all formatting code out of the many monomorphized process_inplace methods
#[cold]
#[inline(never)]
pub fn fft_error_outofplace(
expected_len: usize,
actual_input: usize,
actual_output: usize,
expected_scratch: usize,
actual_scratch: usize,
) {
assert_eq!(actual_input, actual_output, "Provided FFT input buffer and output buffer must have the same length. Got input.len() = {}, output.len() = {}", actual_input, actual_output);
assert!(
actual_input >= expected_len,
"Provided FFT buffer was too small. Expected len = {}, got len = {}",
expected_len,
actual_input
);
assert_eq!(
actual_input % expected_len,
0,
"Input FFT buffer must be a multiple of FFT length. Expected multiple of {}, got len = {}",
expected_len,
actual_input
);
assert!(
actual_scratch >= expected_scratch,
"Not enough scratch space was provided. Expected scratch len >= {}, got scratch len = {}",
expected_scratch,
actual_scratch
);
}
macro_rules! boilerplate_fft_oop {
($struct_name:ident, $len_fn:expr) => {
impl<T: FftNum> Fft<T> for $struct_name<T> {
fn process_outofplace_with_scratch(
&self,
input: &mut [Complex<T>],
output: &mut [Complex<T>],
_scratch: &mut [Complex<T>],
) {
if self.len() == 0 {
return;
}
if 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(), 0, 0);
return; // Unreachable, because fft_error_outofplace asserts, but it helps codegen to put it here
}
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, &mut [])
},
);
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(), 0, 0);
}
}
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_out_of_place(chunk, scratch, &mut []);
chunk.copy_from_slice(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.len()
}
#[inline(always)]
fn get_outofplace_scratch_len(&self) -> usize {
0
}
}
impl<T> Length for $struct_name<T> {
#[inline(always)]
fn len(&self) -> usize {
$len_fn(self)
}
}
impl<T> Direction for $struct_name<T> {
#[inline(always)]
fn fft_direction(&self) -> FftDirection {
self.direction
}
}
};
}
macro_rules! boilerplate_fft {
($struct_name:ident, $len_fn:expr, $inplace_scratch_len_fn:expr, $out_of_place_scratch_len_fn:expr) => {
impl<T: FftNum> Fft<T> for $struct_name<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 {
$inplace_scratch_len_fn(self)
}
#[inline(always)]
fn get_outofplace_scratch_len(&self) -> usize {
$out_of_place_scratch_len_fn(self)
}
}
impl<T: FftNum> Length for $struct_name<T> {
#[inline(always)]
fn len(&self) -> usize {
$len_fn(self)
}
}
impl<T: FftNum> Direction for $struct_name<T> {
#[inline(always)]
fn fft_direction(&self) -> FftDirection {
self.direction
}
}
};
}