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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use std::cmp::max;
use std::sync::Arc;
use num_complex::Complex;
use num_integer::Integer;
use strength_reduce::StrengthReducedUsize;
use transpose;
use crate::array_utils;
use crate::common::{fft_error_inplace, fft_error_outofplace};
use crate::{common::FftNum, FftDirection};
use crate::{Direction, Fft, Length};
pub struct GoodThomasAlgorithm<T> {
width: usize,
width_size_fft: Arc<dyn Fft<T>>,
height: usize,
height_size_fft: Arc<dyn Fft<T>>,
reduced_width: StrengthReducedUsize,
reduced_width_plus_one: StrengthReducedUsize,
inplace_scratch_len: usize,
outofplace_scratch_len: usize,
len: usize,
direction: FftDirection,
}
impl<T: FftNum> GoodThomasAlgorithm<T> {
pub fn new(mut width_fft: Arc<dyn Fft<T>>, mut height_fft: Arc<dyn Fft<T>>) -> Self {
assert_eq!(
width_fft.fft_direction(), height_fft.fft_direction(),
"width_fft and height_fft must have the same direction. got width direction={}, height direction={}",
width_fft.fft_direction(), height_fft.fft_direction());
let mut width = width_fft.len();
let mut height = height_fft.len();
let direction = width_fft.fft_direction();
let gcd = num_integer::gcd(width as i64, height as i64);
assert!(gcd == 1,
"Invalid width and height for Good-Thomas Algorithm (width={}, height={}): Inputs must be coprime",
width,
height);
if width > height {
std::mem::swap(&mut width, &mut height);
std::mem::swap(&mut width_fft, &mut height_fft);
}
let len = width * height;
let width_inplace_scratch = width_fft.get_inplace_scratch_len();
let height_inplace_scratch = height_fft.get_inplace_scratch_len();
let height_outofplace_scratch = height_fft.get_outofplace_scratch_len();
let max_inner_inplace_scratch = max(height_inplace_scratch, width_inplace_scratch);
let outofplace_scratch_len = if max_inner_inplace_scratch > len {
max_inner_inplace_scratch
} else {
0
};
let inplace_scratch_len = len
+ max(
if width_inplace_scratch > len {
width_inplace_scratch
} else {
0
},
height_outofplace_scratch,
);
Self {
width,
width_size_fft: width_fft,
height,
height_size_fft: height_fft,
reduced_width: StrengthReducedUsize::new(width),
reduced_width_plus_one: StrengthReducedUsize::new(width + 1),
inplace_scratch_len,
outofplace_scratch_len,
len,
direction,
}
}
fn reindex_input(&self, source: &[Complex<T>], destination: &mut [Complex<T>]) {
let mut destination_index = 0;
for mut source_row in source.chunks_exact(self.width) {
let increments_until_cycle =
1 + (self.len() - destination_index) / self.reduced_width_plus_one;
if increments_until_cycle < self.width {
let (pre_cycle_row, post_cycle_row) = source_row.split_at(increments_until_cycle);
for input_element in pre_cycle_row {
destination[destination_index] = *input_element;
destination_index += self.reduced_width_plus_one.get();
}
source_row = post_cycle_row;
destination_index -= self.len();
}
for input_element in source_row {
destination[destination_index] = *input_element;
destination_index += self.reduced_width_plus_one.get();
}
destination_index -= self.width;
}
}
fn reindex_output(&self, source: &[Complex<T>], destination: &mut [Complex<T>]) {
for (y, source_chunk) in source.chunks_exact(self.height).enumerate() {
let (quotient, remainder) =
StrengthReducedUsize::div_rem(y * self.height, self.reduced_width);
let mut destination_index = remainder;
let start_x = self.height - quotient;
for x in start_x..self.height {
destination[destination_index] = source_chunk[x];
destination_index += self.width;
}
for x in 0..start_x {
destination[destination_index] = source_chunk[x];
destination_index += self.width;
}
}
}
fn perform_fft_inplace(&self, buffer: &mut [Complex<T>], scratch: &mut [Complex<T>]) {
let (scratch, inner_scratch) = scratch.split_at_mut(self.len());
self.reindex_input(buffer, scratch);
let width_scratch = if inner_scratch.len() > buffer.len() {
&mut inner_scratch[..]
} else {
&mut buffer[..]
};
self.width_size_fft
.process_with_scratch(scratch, width_scratch);
transpose::transpose(scratch, buffer, self.width, self.height);
self.height_size_fft
.process_outofplace_with_scratch(buffer, scratch, inner_scratch);
self.reindex_output(scratch, buffer);
}
fn perform_fft_out_of_place(
&self,
input: &mut [Complex<T>],
output: &mut [Complex<T>],
scratch: &mut [Complex<T>],
) {
self.reindex_input(input, output);
let width_scratch = if scratch.len() > input.len() {
&mut scratch[..]
} else {
&mut input[..]
};
self.width_size_fft
.process_with_scratch(output, width_scratch);
transpose::transpose(output, input, self.width, self.height);
let height_scratch = if scratch.len() > output.len() {
&mut scratch[..]
} else {
&mut output[..]
};
self.height_size_fft
.process_with_scratch(input, height_scratch);
self.reindex_output(input, output);
}
}
boilerplate_fft!(
GoodThomasAlgorithm,
|this: &GoodThomasAlgorithm<_>| this.len,
|this: &GoodThomasAlgorithm<_>| this.inplace_scratch_len,
|this: &GoodThomasAlgorithm<_>| this.outofplace_scratch_len
);
pub struct GoodThomasAlgorithmSmall<T> {
width: usize,
width_size_fft: Arc<dyn Fft<T>>,
height: usize,
height_size_fft: Arc<dyn Fft<T>>,
input_output_map: Box<[usize]>,
direction: FftDirection,
}
impl<T: FftNum> GoodThomasAlgorithmSmall<T> {
pub fn new(width_fft: Arc<dyn Fft<T>>, height_fft: Arc<dyn Fft<T>>) -> Self {
assert_eq!(
width_fft.fft_direction(), height_fft.fft_direction(),
"n1_fft and height_fft must have the same direction. got width direction={}, height direction={}",
width_fft.fft_direction(), height_fft.fft_direction());
let width = width_fft.len();
let height = height_fft.len();
let len = width * height;
assert_eq!(width_fft.get_outofplace_scratch_len(), 0, "GoodThomasAlgorithmSmall should only be used with algorithms that require 0 out-of-place scratch. Width FFT (len={}) requires {}, should require 0", width, width_fft.get_outofplace_scratch_len());
assert_eq!(height_fft.get_outofplace_scratch_len(), 0, "GoodThomasAlgorithmSmall should only be used with algorithms that require 0 out-of-place scratch. Height FFT (len={}) requires {}, should require 0", height, height_fft.get_outofplace_scratch_len());
assert!(width_fft.get_inplace_scratch_len() <= width, "GoodThomasAlgorithmSmall should only be used with algorithms that require little inplace scratch. Width FFT (len={}) requires {}, should require {} or less", width, width_fft.get_inplace_scratch_len(), width);
assert!(height_fft.get_inplace_scratch_len() <= height, "GoodThomasAlgorithmSmall should only be used with algorithms that require little inplace scratch. Height FFT (len={}) requires {}, should require {} or less", height, height_fft.get_inplace_scratch_len(), height);
let gcd_data = i64::extended_gcd(&(width as i64), &(height as i64));
assert!(gcd_data.gcd == 1,
"Invalid input width and height to Good-Thomas Algorithm: ({},{}): Inputs must be coprime",
width,
height);
let width_inverse = if gcd_data.x >= 0 {
gcd_data.x
} else {
gcd_data.x + height as i64
} as usize;
let height_inverse = if gcd_data.y >= 0 {
gcd_data.y
} else {
gcd_data.y + width as i64
} as usize;
let input_iter = (0..len)
.map(|i| (i % width, i / width))
.map(|(x, y)| (x * height + y * width) % len);
let output_iter = (0..len).map(|i| (i % height, i / height)).map(|(y, x)| {
(x * height * height_inverse as usize + y * width * width_inverse as usize) % len
});
let input_output_map: Vec<usize> = input_iter.chain(output_iter).collect();
Self {
direction: width_fft.fft_direction(),
width,
width_size_fft: width_fft,
height,
height_size_fft: height_fft,
input_output_map: input_output_map.into_boxed_slice(),
}
}
fn perform_fft_out_of_place(
&self,
input: &mut [Complex<T>],
output: &mut [Complex<T>],
_scratch: &mut [Complex<T>],
) {
assert_eq!(self.len(), input.len());
assert_eq!(self.len(), output.len());
let (input_map, output_map) = self.input_output_map.split_at(self.len());
for (output_element, &input_index) in output.iter_mut().zip(input_map.iter()) {
*output_element = input[input_index];
}
self.width_size_fft.process_with_scratch(output, input);
unsafe { array_utils::transpose_small(self.width, self.height, output, input) };
self.height_size_fft.process_with_scratch(input, output);
for (input_element, &output_index) in input.iter().zip(output_map.iter()) {
output[output_index] = *input_element;
}
}
fn perform_fft_inplace(&self, buffer: &mut [Complex<T>], scratch: &mut [Complex<T>]) {
assert_eq!(self.len(), buffer.len());
assert_eq!(self.len(), scratch.len());
let (input_map, output_map) = self.input_output_map.split_at(self.len());
for (output_element, &input_index) in scratch.iter_mut().zip(input_map.iter()) {
*output_element = buffer[input_index];
}
self.width_size_fft.process_with_scratch(scratch, buffer);
unsafe { array_utils::transpose_small(self.width, self.height, scratch, buffer) };
self.height_size_fft
.process_outofplace_with_scratch(buffer, scratch, &mut []);
for (input_element, &output_index) in scratch.iter().zip(output_map.iter()) {
buffer[output_index] = *input_element;
}
}
}
boilerplate_fft!(
GoodThomasAlgorithmSmall,
|this: &GoodThomasAlgorithmSmall<_>| this.width * this.height,
|this: &GoodThomasAlgorithmSmall<_>| this.len(),
|_| 0
);
#[cfg(test)]
mod unit_tests {
use super::*;
use crate::test_utils::check_fft_algorithm;
use crate::{algorithm::Dft, test_utils::BigScratchAlgorithm};
use num_integer::gcd;
use num_traits::Zero;
use std::sync::Arc;
#[test]
fn test_good_thomas() {
for width in 1..12 {
for height in 1..12 {
if gcd(width, height) == 1 {
test_good_thomas_with_lengths(width, height, FftDirection::Forward);
test_good_thomas_with_lengths(width, height, FftDirection::Inverse);
}
}
}
}
#[test]
fn test_good_thomas_small() {
let butterfly_sizes = [2, 3, 4, 5, 6, 7, 8, 16];
for width in &butterfly_sizes {
for height in &butterfly_sizes {
if gcd(*width, *height) == 1 {
test_good_thomas_small_with_lengths(*width, *height, FftDirection::Forward);
test_good_thomas_small_with_lengths(*width, *height, FftDirection::Inverse);
}
}
}
}
fn test_good_thomas_with_lengths(width: usize, height: usize, direction: FftDirection) {
let width_fft = Arc::new(Dft::new(width, direction)) as Arc<dyn Fft<f32>>;
let height_fft = Arc::new(Dft::new(height, direction)) as Arc<dyn Fft<f32>>;
let fft = GoodThomasAlgorithm::new(width_fft, height_fft);
check_fft_algorithm(&fft, width * height, direction);
}
fn test_good_thomas_small_with_lengths(width: usize, height: usize, direction: FftDirection) {
let width_fft = Arc::new(Dft::new(width, direction)) as Arc<dyn Fft<f32>>;
let height_fft = Arc::new(Dft::new(height, direction)) as Arc<dyn Fft<f32>>;
let fft = GoodThomasAlgorithmSmall::new(width_fft, height_fft);
check_fft_algorithm(&fft, width * height, direction);
}
#[test]
fn test_output_mapping() {
let width = 15;
for height in 3..width {
if gcd(width, height) == 1 {
let width_fft =
Arc::new(Dft::new(width, FftDirection::Forward)) as Arc<dyn Fft<f32>>;
let height_fft =
Arc::new(Dft::new(height, FftDirection::Forward)) as Arc<dyn Fft<f32>>;
let fft = GoodThomasAlgorithm::new(width_fft, height_fft);
let mut buffer = vec![Complex { re: 0.0, im: 0.0 }; fft.len()];
fft.process(&mut buffer);
}
}
}
#[test]
fn test_good_thomas_inner_scratch() {
let scratch_lengths = [1, 5, 24];
let mut inner_ffts = Vec::new();
for &len in &scratch_lengths {
for &inplace_scratch in &scratch_lengths {
for &outofplace_scratch in &scratch_lengths {
inner_ffts.push(Arc::new(BigScratchAlgorithm {
len,
inplace_scratch,
outofplace_scratch,
direction: FftDirection::Forward,
}) as Arc<dyn Fft<f32>>);
}
}
}
for width_fft in inner_ffts.iter() {
for height_fft in inner_ffts.iter() {
if width_fft.len() == height_fft.len() {
continue;
}
let fft = GoodThomasAlgorithm::new(Arc::clone(width_fft), Arc::clone(height_fft));
let mut inplace_buffer = vec![Complex::zero(); fft.len()];
let mut inplace_scratch = vec![Complex::zero(); fft.get_inplace_scratch_len()];
fft.process_with_scratch(&mut inplace_buffer, &mut inplace_scratch);
let mut outofplace_input = vec![Complex::zero(); fft.len()];
let mut outofplace_output = vec![Complex::zero(); fft.len()];
let mut outofplace_scratch =
vec![Complex::zero(); fft.get_outofplace_scratch_len()];
fft.process_outofplace_with_scratch(
&mut outofplace_input,
&mut outofplace_output,
&mut outofplace_scratch,
);
}
}
}
}