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
use std::sync::Arc;
use num_complex::Complex;
use num_integer::Integer;
use num_traits::Zero;
use primal_check::miller_rabin;
use strength_reduce::StrengthReducedUsize;
use crate::array_utils;
use crate::common::{fft_error_inplace, fft_error_outofplace};
use crate::math_utils;
use crate::{common::FftNum, twiddles, FftDirection};
use crate::{Direction, Fft, Length};
pub struct RadersAlgorithm<T> {
inner_fft: Arc<dyn Fft<T>>,
inner_fft_data: Box<[Complex<T>]>,
primitive_root: usize,
primitive_root_inverse: usize,
len: StrengthReducedUsize,
inplace_scratch_len: usize,
outofplace_scratch_len: usize,
direction: FftDirection,
}
impl<T: FftNum> RadersAlgorithm<T> {
pub fn new(inner_fft: Arc<dyn Fft<T>>) -> Self {
let inner_fft_len = inner_fft.len();
let len = inner_fft_len + 1;
assert!(miller_rabin(len as u64), "For raders algorithm, inner_fft.len() + 1 must be prime. Expected prime number, got {} + 1 = {}", inner_fft_len, len);
let direction = inner_fft.fft_direction();
let reduced_len = StrengthReducedUsize::new(len);
let primitive_root = math_utils::primitive_root(len as u64).unwrap() as usize;
let gcd_data = i64::extended_gcd(&(primitive_root as i64), &(len as i64));
let primitive_root_inverse = if gcd_data.x >= 0 {
gcd_data.x
} else {
gcd_data.x + len as i64
} as usize;
let unity_scale = T::from_f64(1f64 / inner_fft_len as f64).unwrap();
let mut inner_fft_input = vec![Complex::zero(); inner_fft_len];
let mut twiddle_input = 1;
for input_cell in &mut inner_fft_input {
let twiddle = twiddles::compute_twiddle(twiddle_input, len, direction);
*input_cell = twiddle * unity_scale;
twiddle_input = (twiddle_input * primitive_root_inverse) % reduced_len;
}
let required_inner_scratch = inner_fft.get_inplace_scratch_len();
let extra_inner_scratch = if required_inner_scratch <= inner_fft_len {
0
} else {
required_inner_scratch
};
let mut inner_fft_scratch = vec![Zero::zero(); required_inner_scratch];
inner_fft.process_with_scratch(&mut inner_fft_input, &mut inner_fft_scratch);
Self {
inner_fft,
inner_fft_data: inner_fft_input.into_boxed_slice(),
primitive_root,
primitive_root_inverse,
len: reduced_len,
inplace_scratch_len: inner_fft_len + extra_inner_scratch,
outofplace_scratch_len: extra_inner_scratch,
direction,
}
}
fn perform_fft_out_of_place(
&self,
input: &mut [Complex<T>],
output: &mut [Complex<T>],
scratch: &mut [Complex<T>],
) {
let (output_first, output) = output.split_first_mut().unwrap();
let (input_first, input) = input.split_first_mut().unwrap();
let first_input_val = *input_first;
*output_first = *input_first;
let mut input_index = 1;
for output_element in output.iter_mut() {
input_index = (input_index * self.primitive_root) % self.len;
let input_element = input[input_index - 1];
*output_first = *output_first + input_element;
*output_element = input_element;
}
let inner_scratch = if scratch.len() > 0 {
&mut scratch[..]
} else {
&mut input[..]
};
self.inner_fft.process_with_scratch(output, inner_scratch);
for ((output_cell, input_cell), &multiple) in output
.iter()
.zip(input.iter_mut())
.zip(self.inner_fft_data.iter())
{
*input_cell = (*output_cell * multiple).conj();
}
let inner_scratch = if scratch.len() > 0 {
scratch
} else {
&mut output[..]
};
self.inner_fft.process_with_scratch(input, inner_scratch);
let mut output_index = 1;
for input_element in input {
output_index = (output_index * self.primitive_root_inverse) % self.len;
output[output_index - 1] = input_element.conj() + first_input_val;
}
}
fn perform_fft_inplace(&self, buffer: &mut [Complex<T>], scratch: &mut [Complex<T>]) {
let (buffer_first, buffer) = buffer.split_first_mut().unwrap();
let buffer_first_val = *buffer_first;
let (scratch, extra_scratch) = scratch.split_at_mut(self.len() - 1);
let mut input_index = 1;
for scratch_element in scratch.iter_mut() {
input_index = (input_index * self.primitive_root) % self.len;
let buffer_element = buffer[input_index - 1];
*buffer_first = *buffer_first + buffer_element;
*scratch_element = buffer_element;
}
let inner_scratch = if extra_scratch.len() > 0 {
extra_scratch
} else {
&mut buffer[..]
};
self.inner_fft.process_with_scratch(scratch, inner_scratch);
for (scratch_cell, &twiddle) in scratch.iter_mut().zip(self.inner_fft_data.iter()) {
*scratch_cell = (*scratch_cell * twiddle).conj();
}
self.inner_fft.process_with_scratch(scratch, inner_scratch);
let mut output_index = 1;
for scratch_element in scratch {
output_index = (output_index * self.primitive_root_inverse) % self.len;
buffer[output_index - 1] = scratch_element.conj() + buffer_first_val;
}
}
}
boilerplate_fft!(
RadersAlgorithm,
|this: &RadersAlgorithm<_>| this.len.get(),
|this: &RadersAlgorithm<_>| this.inplace_scratch_len,
|this: &RadersAlgorithm<_>| this.outofplace_scratch_len
);
#[cfg(test)]
mod unit_tests {
use super::*;
use crate::algorithm::Dft;
use crate::test_utils::check_fft_algorithm;
use std::sync::Arc;
#[test]
fn test_raders() {
for len in 3..100 {
if miller_rabin(len as u64) {
test_raders_with_length(len, FftDirection::Forward);
test_raders_with_length(len, FftDirection::Inverse);
}
}
}
fn test_raders_with_length(len: usize, direction: FftDirection) {
let inner_fft = Arc::new(Dft::new(len - 1, direction));
let fft = RadersAlgorithm::new(inner_fft);
check_fft_algorithm::<f32>(&fft, len, direction);
}
}