Debian Codegraph @

.cargo/vendor/rustfft/src/common.rs

raw
1 use num_traits::{FromPrimitive, Signed};
2 use std::fmt::Debug;
3
4 /// Generic floating point number, implemented for f32 and f64
5 pub trait FftNum: Copy + FromPrimitive + Signed + Sync + Send + Debug + 'static {}
6
7 impl<T> FftNum for T where T: Copy + FromPrimitive + Signed + Sync + Send + Debug + 'static {}
8
9 // Prints an error raised by an in-place FFT algorithm's `process_inplace` method
10 // Marked cold and inline never to keep all formatting code out of the many monomorphized process_inplace methods
11 #[cold]
12 #[inline(never)]
13 pub fn fft_error_inplace(
14 expected_len: usize,
15 actual_len: usize,
16 expected_scratch: usize,
17 actual_scratch: usize,
18 ) {
19 assert!(
20 actual_len >= expected_len,
21 "Provided FFT buffer was too small. Expected len = {}, got len = {}",
22 expected_len,
23 actual_len
24 );
25 assert_eq!(
26 actual_len % expected_len,
27 0,
28 "Input FFT buffer must be a multiple of FFT length. Expected multiple of {}, got len = {}",
29 expected_len,
30 actual_len
31 );
32 assert!(
33 actual_scratch >= expected_scratch,
34 "Not enough scratch space was provided. Expected scratch len >= {}, got scratch len = {}",
35 expected_scratch,
36 actual_scratch
37 );
38 }
39
40 // Prints an error raised by an in-place FFT algorithm's `process_inplace` method
41 // Marked cold and inline never to keep all formatting code out of the many monomorphized process_inplace methods
42 #[cold]
43 #[inline(never)]
44 pub fn fft_error_outofplace(
45 expected_len: usize,
46 actual_input: usize,
47 actual_output: usize,
48 expected_scratch: usize,
49 actual_scratch: usize,
50 ) {
51 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);
52 assert!(
53 actual_input >= expected_len,
54 "Provided FFT buffer was too small. Expected len = {}, got len = {}",
55 expected_len,
56 actual_input
57 );
58 assert_eq!(
59 actual_input % expected_len,
60 0,
61 "Input FFT buffer must be a multiple of FFT length. Expected multiple of {}, got len = {}",
62 expected_len,
63 actual_input
64 );
65 assert!(
66 actual_scratch >= expected_scratch,
67 "Not enough scratch space was provided. Expected scratch len >= {}, got scratch len = {}",
68 expected_scratch,
69 actual_scratch
70 );
71 }
72
73 macro_rules! boilerplate_fft_oop {
74 ($struct_name:ident, $len_fn:expr) => {
75 impl<T: FftNum> Fft<T> for $struct_name<T> {
76 fn process_outofplace_with_scratch(
77 &self,
78 input: &mut [Complex<T>],
79 output: &mut [Complex<T>],
80 _scratch: &mut [Complex<T>],
81 ) {
82 if self.len() == 0 {
83 return;
84 }
85
86 if input.len() < self.len() || output.len() != input.len() {
87 // 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
88 fft_error_outofplace(self.len(), input.len(), output.len(), 0, 0);
89 return; // Unreachable, because fft_error_outofplace asserts, but it helps codegen to put it here
90 }
91
92 let result = array_utils::iter_chunks_zipped(
93 input,
94 output,
95 self.len(),
96 |in_chunk, out_chunk| {
97 self.perform_fft_out_of_place(in_chunk, out_chunk, &mut [])
98 },
99 );
100
101 if result.is_err() {
102 // We want to trigger a panic, because the buffer sizes weren't cleanly divisible by the FFT size,
103 // 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
104 fft_error_outofplace(self.len(), input.len(), output.len(), 0, 0);
105 }
106 }
107 fn process_with_scratch(&self, buffer: &mut [Complex<T>], scratch: &mut [Complex<T>]) {
108 if self.len() == 0 {
109 return;
110 }
111
112 let required_scratch = self.get_inplace_scratch_len();
113 if scratch.len() < required_scratch || buffer.len() < self.len() {
114 // 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
115 fft_error_inplace(
116 self.len(),
117 buffer.len(),
118 self.get_inplace_scratch_len(),
119 scratch.len(),
120 );
121 return; // Unreachable, because fft_error_inplace asserts, but it helps codegen to put it here
122 }
123
124 let scratch = &mut scratch[..required_scratch];
125 let result = array_utils::iter_chunks(buffer, self.len(), |chunk| {
126 self.perform_fft_out_of_place(chunk, scratch, &mut []);
127 chunk.copy_from_slice(scratch);
128 });
129
130 if result.is_err() {
131 // We want to trigger a panic, because the buffer sizes weren't cleanly divisible by the FFT size,
132 // 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
133 fft_error_inplace(
134 self.len(),
135 buffer.len(),
136 self.get_inplace_scratch_len(),
137 scratch.len(),
138 );
139 }
140 }
141 #[inline(always)]
142 fn get_inplace_scratch_len(&self) -> usize {
143 self.len()
144 }
145 #[inline(always)]
146 fn get_outofplace_scratch_len(&self) -> usize {
147 0
148 }
149 }
150 impl<T> Length for $struct_name<T> {
151 #[inline(always)]
152 fn len(&self) -> usize {
153 $len_fn(self)
154 }
155 }
156 impl<T> Direction for $struct_name<T> {
157 #[inline(always)]
158 fn fft_direction(&self) -> FftDirection {
159 self.direction
160 }
161 }
162 };
163 }
164
165 macro_rules! boilerplate_fft {
166 ($struct_name:ident, $len_fn:expr, $inplace_scratch_len_fn:expr, $out_of_place_scratch_len_fn:expr) => {
167 impl<T: FftNum> Fft<T> for $struct_name<T> {
168 fn process_outofplace_with_scratch(
169 &self,
170 input: &mut [Complex<T>],
171 output: &mut [Complex<T>],
172 scratch: &mut [Complex<T>],
173 ) {
174 if self.len() == 0 {
175 return;
176 }
177
178 let required_scratch = self.get_outofplace_scratch_len();
179 if scratch.len() < required_scratch
180 || input.len() < self.len()
181 || output.len() != input.len()
182 {
183 // 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
184 fft_error_outofplace(
185 self.len(),
186 input.len(),
187 output.len(),
188 self.get_outofplace_scratch_len(),
189 scratch.len(),
190 );
191 return; // Unreachable, because fft_error_outofplace asserts, but it helps codegen to put it here
192 }
193
194 let scratch = &mut scratch[..required_scratch];
195 let result = array_utils::iter_chunks_zipped(
196 input,
197 output,
198 self.len(),
199 |in_chunk, out_chunk| {
200 self.perform_fft_out_of_place(in_chunk, out_chunk, scratch)
201 },
202 );
203
204 if result.is_err() {
205 // We want to trigger a panic, because the buffer sizes weren't cleanly divisible by the FFT size,
206 // 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
207 fft_error_outofplace(
208 self.len(),
209 input.len(),
210 output.len(),
211 self.get_outofplace_scratch_len(),
212 scratch.len(),
213 );
214 }
215 }
216 fn process_with_scratch(&self, buffer: &mut [Complex<T>], scratch: &mut [Complex<T>]) {
217 if self.len() == 0 {
218 return;
219 }
220
221 let required_scratch = self.get_inplace_scratch_len();
222 if scratch.len() < required_scratch || buffer.len() < self.len() {
223 // 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
224 fft_error_inplace(
225 self.len(),
226 buffer.len(),
227 self.get_inplace_scratch_len(),
228 scratch.len(),
229 );
230 return; // Unreachable, because fft_error_inplace asserts, but it helps codegen to put it here
231 }
232
233 let scratch = &mut scratch[..required_scratch];
234 let result = array_utils::iter_chunks(buffer, self.len(), |chunk| {
235 self.perform_fft_inplace(chunk, scratch)
236 });
237
238 if result.is_err() {
239 // We want to trigger a panic, because the buffer sizes weren't cleanly divisible by the FFT size,
240 // 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
241 fft_error_inplace(
242 self.len(),
243 buffer.len(),
244 self.get_inplace_scratch_len(),
245 scratch.len(),
246 );
247 }
248 }
249 #[inline(always)]
250 fn get_inplace_scratch_len(&self) -> usize {
251 $inplace_scratch_len_fn(self)
252 }
253 #[inline(always)]
254 fn get_outofplace_scratch_len(&self) -> usize {
255 $out_of_place_scratch_len_fn(self)
256 }
257 }
258 impl<T: FftNum> Length for $struct_name<T> {
259 #[inline(always)]
260 fn len(&self) -> usize {
261 $len_fn(self)
262 }
263 }
264 impl<T: FftNum> Direction for $struct_name<T> {
265 #[inline(always)]
266 fn fft_direction(&self) -> FftDirection {
267 self.direction
268 }
269 }
270 };
271 }