-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFFT.cpp
executable file
·264 lines (225 loc) · 6.3 KB
/
FFT.cpp
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
#include "FHE/FFT.h"
#include "Math/Zp_Data.h"
#include "Processor/BaseMachine.h"
#include "Math/modp.hpp"
/* Computes the FFT via Horner's Rule
theta is assumed to be an Nth root of unity
*/
void NaiveFFT(vector<modp>& ans,vector<modp>& a,int N,const modp& theta,const Zp_Data& PrD)
{
int i,j;
modp thetaPow;
assignOne(thetaPow,PrD);
for (i=0; i<N; i++)
{ ans[i]=a[N-1];
for (j=N-2; j>=0; j--)
{ Mul(ans[i],ans[i],thetaPow,PrD);
Add(ans[i],ans[i],a[j],PrD);
}
Mul(thetaPow,thetaPow,theta,PrD);
}
}
void FFT(vector<modp>& a,int N,const modp& theta,const Zp_Data& PrD)
{
if (N==1) { return; }
if (N<5)
{ vector<modp> b(N);
NaiveFFT(b,a,N,theta,PrD);
a=b;
return;
}
vector<modp> a0(N/2),a1(N/2);
int i;
for (i=0; i<N/2; i++)
{ a0[i]=a[2*i];
a1[i]=a[2*i+1];
}
modp theta2,w,t;
Sqr(theta2,theta,PrD);
FFT(a0,N/2,theta2,PrD);
FFT(a1,N/2,theta2,PrD);
assignOne(w,PrD);
for (i=0; i<N/2; i++)
{ Mul(t,w,a1[i],PrD);
Add(a[i],a0[i],t,PrD);
Sub(a[i+N/2],a0[i],t,PrD);
Mul(w,w,theta,PrD);
}
}
/*
* Standard FFT for n a power of two, root a n-th primitive root of unity.
*/
template<class T,class P>
void FFT_Iter(vector<T>& ioput, int n, const T& root, const P& PrD)
{
int i, j, m;
T t;
// Bit-reversal of input
for( i = j = 0; i < n; ++i )
{
if( j >= i )
{
t = ioput[i];
ioput[i] = ioput[j];
ioput[j] = t;
}
m = n / 2;
while( (m >= 1) && (j >= m) )
{
j -= m;
m /= 2;
}
j += m;
}
T u, alpha, alpha2;
m = 0; j = 0; i = 0;
// Do the transform
for (int s = 1; s < n; s = 2*s)
{
m = 2*s;
Power(alpha, root, n/m, PrD);
assignOne(alpha2,PrD);
for (int j = 0; j < m/2; ++j)
{
//root = root_table[j*n/m];
for (int k = j; k < n; k += m)
{
Mul(t, alpha2, ioput[k + m/2], PrD);
u = ioput[k];
Add(ioput[k], u, t, PrD);
Sub(ioput[k + m/2], u, t, PrD);
}
Mul(alpha2, alpha2, alpha, PrD);
}
}
}
/*
* FFT modulo x^n + 1.
*
* n must be a power of two, root a 2n-th primitive root of unity.
*/
void FFT_Iter2(vector<modp>& ioput, int n, const modp& root, const Zp_Data& PrD)
{
FFT_Iter(ioput, n, root, PrD, false);
}
void FFT_Iter2(vector<modp>& ioput, int n, const vector<modp>& roots,
const Zp_Data& PrD)
{
FFT_Iter(ioput, n, roots, PrD, false);
}
void FFT_Iter(vector<modp>& ioput, int n, const modp& root, const Zp_Data& PrD,
bool start_with_one)
{
vector<modp> roots(n + 1);
assignOne(roots[0], PrD);
for (int i = 1; i < n + 1; i++)
Mul(roots[i], roots[i - 1], root, PrD);
FFT_Iter(ioput, n, roots, PrD, start_with_one);
}
void FFT_Iter(vector<modp>& ioput, int n, const vector<modp>& roots,
const Zp_Data& PrD, bool start_with_one)
{
assert(roots.size() > size_t(n));
int i, j, m;
// Bit-reversal of input
for( i = j = 0; i < n; ++i )
{
if( j >= i )
{
swap(ioput[i], ioput[j]);
}
m = n / 2;
while( (m >= 1) && (j >= m) )
{
j -= m;
m /= 2;
}
j += m;
}
m = 0; j = 0; i = 0;
// Do the transform
vector<modp> alpha2;
alpha2.reserve(n / 2);
for (int s = 1; s < n; s = 2*s)
{
m = 2*s;
alpha2.clear();
if (start_with_one)
{
for (int j = 0; j < m / 2; j++)
alpha2.push_back(roots[j * n / m]);
}
else
{
for (int j = 0; j < m / 2; j++)
alpha2.push_back(roots.at((j * 2 + 1) * (n / m)));
}
if (BaseMachine::thread_num == 0 and BaseMachine::has_singleton())
{
auto& queues = BaseMachine::s().queues;
FftJob job(ioput, alpha2, m, PrD);
int start = queues.distribute(job, n / 2);
for (int i = start; i < n / 2; i++)
FFT_Iter2_body(ioput, alpha2, i, m, PrD);
if (start > 0)
queues.wrap_up(job);
}
else
for (int i = 0; i < n / 2; i++)
FFT_Iter2_body(ioput, alpha2, i, m, PrD);
}
}
/* This does FFT for X^N+1,
Input and output is an array of size N (shared)
alpha is assumed to be a generator of the N'th roots of unity mod p
Starts at w=alpha and updates by alpha^2
*/
void FFT2(vector<modp>& a, int N, const modp& alpha, const Zp_Data& PrD)
{
int i;
if (N==1) { return; }
vector<modp> a0(N/2),a1(N/2);
for (i=0; i<N/2; i++)
{ a0[i]=a[2*i];
a1[i]=a[2*i+1];
}
modp w,alpha2,temp;
Sqr(alpha2,alpha,PrD);
FFT2(a0,N/2,alpha2,PrD); FFT2(a1,N/2,alpha2,PrD);
w=alpha;
for (i=0; i<N/2; i++)
{ Mul(temp,w,a1[i],PrD);
Add(a[i],a0[i],temp,PrD);
Sub(a[i+N/2],a0[i],temp,PrD);
Mul(w,w,alpha2,PrD);
}
}
void FFT_non_power_of_two(vector<modp>& res, const vector<modp>& input, const FFT_Data& FFTD)
{
vector<modp> tmp(FFTD.m());
BFFT(tmp, input, FFTD);
for (int i = 0; i < (FFTD).phi_m(); i++)
res[i] = tmp[(FFTD).p(i)];
}
void BFFT(vector<modp>& ans,const vector<modp>& a,const FFT_Data& FFTD,bool forward)
{
int k2=FFTD.twop,n=FFTD.m();
if (k2<0) { k2=-k2; }
int r=0;
if (forward==false) { r=1; }
if (FFTD.twop>0)
{ vector<modp> x(k2);
for (unsigned int i=0; i<a.size(); i++)
{ Mul(x[i],FFTD.powers[r][i],a[i],FFTD.get_prD()); }
for (int i=a.size(); i<k2; i++)
{ assignZero(x[i],FFTD.get_prD()); }
FFT_Iter(x,k2,FFTD.two_root[0],FFTD.get_prD());
for (int i=0; i<k2; i++)
{ Mul(x[i],x[i],FFTD.b[r][i],FFTD.get_prD()); }
FFT_Iter(x,k2,FFTD.two_root[1],FFTD.get_prD());
for (int i=0; i<n; i++)
{ Mul(ans[i],x[i+n-1],FFTD.powers_i[r][i],FFTD.get_prD()); }
}
else
{ throw crash_requested(); }
}