forked from flintlib/flint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft.h
289 lines (212 loc) · 11.3 KB
/
fft.h
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
/* mul_fft -- radix 2 fft routines for MPIR.
Copyright 2009, 2011 William Hart. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY William Hart ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL William Hart OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of William Hart.
*/
/******************************************************************************
Copyright (C) 2009, 2011 William Hart
******************************************************************************/
#ifndef FFT_H
#define FFT_H
#undef ulong
#define ulong ulongxx /* interferes with system includes */
#include <stdlib.h>
#include <stdio.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "mpn_extras.h"
#ifdef __cplusplus
extern "C" {
#endif
#if defined(__MPIR_VERSION)
#if !defined(__MPIR_RELEASE ) || __MPIR_RELEASE < 20600
#define mpn_sumdiff_n __MPN(sumdiff_n)
extern
mp_limb_t mpn_sumdiff_n(mp_ptr, mp_ptr, mp_srcptr, mp_srcptr, mp_size_t);
#endif
#else
static __inline__ mp_limb_t
mpn_sumdiff_n(mp_ptr s, mp_ptr d, mp_srcptr x, mp_srcptr y, mp_size_t n)
{
mp_limb_t ret;
mp_ptr t;
if (n == 0)
return 0;
if ((s == x && d == y) || (s == y && d == x))
{
t = flint_malloc(n * sizeof(mp_limb_t));
ret = mpn_sub_n(t, x, y, n);
ret += 2 * mpn_add_n(s, x, y, n);
flint_mpn_copyi(d, t, n);
flint_free(t);
return ret;
}
if (s == x || s == y)
{
ret = mpn_sub_n(d, x, y, n);
ret += 2 * mpn_add_n(s, x, y, n);
return ret;
}
ret = 2 * mpn_add_n(s, x, y, n);
ret += mpn_sub_n(d, x, y, n);
return ret;
}
#endif
#define fft_sumdiff(t, u, r, s, n) \
(n == 0 ? 0 : mpn_sumdiff_n(t, u, r, s, n))
#define SWAP_PTRS(xx, yy) \
do { \
mp_limb_t * __ptr = xx; \
xx = yy; \
yy = __ptr; \
} while (0)
/* used for generating random values mod p in test code */
#define random_fermat(nn, state, limbs) \
do { \
if (n_randint(state, 10) == 0) { \
flint_mpn_zero(nn, limbs); \
nn[limbs] = 1; \
} else { \
if (n_randint(state, 2) == 0) \
flint_mpn_rrandom(nn, state->gmp_state, limbs); \
else \
flint_mpn_urandomb(nn, state->gmp_state, limbs*FLINT_BITS); \
nn[limbs] = n_randint(state, 1024); \
} \
if (n_randint(state, 2)) \
nn[limbs] = -nn[limbs]; \
} while (0)
static __inline__
void mpn_addmod_2expp1_1(mp_limb_t * r, mp_size_t limbs, mp_limb_signed_t c)
{
mp_limb_t sum = r[0] + c;
/* check if adding c would cause a carry to propagate */
if ((mp_limb_signed_t)(sum ^ r[0]) >= 0)
r[0] = sum;
else
{
if (c >= 0) mpn_add_1(r, r, limbs + 1, c);
else mpn_sub_1(r, r, limbs + 1, -c);
}
}
FLINT_DLL void fft_combine_limbs(mp_limb_t * res, mp_limb_t ** poly, slong length,
mp_size_t coeff_limbs, mp_size_t output_limbs, mp_size_t total_limbs);
FLINT_DLL void fft_combine_bits(mp_limb_t * res, mp_limb_t ** poly, slong length,
mp_bitcnt_t bits, mp_size_t output_limbs, mp_size_t total_limbs);
FLINT_DLL mp_size_t fft_split_limbs(mp_limb_t ** poly, mp_srcptr limbs,
mp_size_t total_limbs, mp_size_t coeff_limbs, mp_size_t output_limbs);
FLINT_DLL mp_size_t fft_split_bits(mp_limb_t ** poly, mp_srcptr limbs,
mp_size_t total_limbs, mp_bitcnt_t bits, mp_size_t output_limbs);
FLINT_DLL void fermat_to_mpz(mpz_t m, mp_limb_t * i, mp_size_t limbs);
FLINT_DLL void mpn_normmod_2expp1(mp_limb_t * t, mp_size_t limbs);
FLINT_DLL void butterfly_lshB(mp_limb_t * t, mp_limb_t * u, mp_limb_t * i1,
mp_limb_t * i2, mp_size_t limbs, mp_size_t x, mp_size_t y);
FLINT_DLL void butterfly_rshB(mp_limb_t * t, mp_limb_t * u, mp_limb_t * i1,
mp_limb_t * i2, mp_size_t limbs, mp_size_t x, mp_size_t y);
FLINT_DLL void mpn_mul_2expmod_2expp1(mp_limb_t * t,
mp_limb_t * i1, mp_size_t limbs, mp_bitcnt_t d);
FLINT_DLL void mpn_div_2expmod_2expp1(mp_limb_t * t,
mp_limb_t * i1, mp_size_t limbs, mp_bitcnt_t d);
FLINT_DLL void fft_adjust(mp_limb_t * r, mp_limb_t * i1,
mp_size_t i, mp_size_t limbs, mp_bitcnt_t w);
FLINT_DLL void fft_butterfly(mp_limb_t * s, mp_limb_t * t, mp_limb_t * i1,
mp_limb_t * i2, mp_size_t i, mp_size_t limbs, mp_bitcnt_t w);
FLINT_DLL void ifft_butterfly(mp_limb_t * s, mp_limb_t * t, mp_limb_t * i1,
mp_limb_t * i2, mp_size_t i, mp_size_t limbs, mp_bitcnt_t w);
FLINT_DLL void fft_radix2(mp_limb_t ** ii,
mp_size_t n, mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2);
FLINT_DLL void fft_truncate1(mp_limb_t ** ii, mp_size_t n, mp_bitcnt_t w,
mp_limb_t ** t1, mp_limb_t ** t2, mp_size_t trunc);
FLINT_DLL void fft_truncate(mp_limb_t ** ii, mp_size_t n, mp_bitcnt_t w,
mp_limb_t ** t1, mp_limb_t ** t2, mp_size_t trunc);
FLINT_DLL void ifft_radix2(mp_limb_t ** ii, mp_size_t n,
mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2);
FLINT_DLL void ifft_truncate1(mp_limb_t ** ii, mp_size_t n, mp_bitcnt_t w,
mp_limb_t ** t1, mp_limb_t ** t2, mp_size_t trunc);
FLINT_DLL void ifft_truncate(mp_limb_t ** ii, mp_size_t n, mp_bitcnt_t w,
mp_limb_t ** t1, mp_limb_t ** t2, mp_size_t trunc);
FLINT_DLL void fft_butterfly_sqrt2(mp_limb_t * s, mp_limb_t * t,
mp_limb_t * i1, mp_limb_t * i2, mp_size_t i,
mp_size_t limbs, mp_bitcnt_t w, mp_limb_t * temp);
FLINT_DLL void ifft_butterfly_sqrt2(mp_limb_t * s, mp_limb_t * t, mp_limb_t * i1,
mp_limb_t * i2, mp_size_t i, mp_size_t limbs, mp_bitcnt_t w, mp_limb_t * temp);
FLINT_DLL void fft_adjust_sqrt2(mp_limb_t * r, mp_limb_t * i1,
mp_size_t i, mp_size_t limbs, mp_bitcnt_t w, mp_limb_t * temp);
FLINT_DLL void fft_truncate_sqrt2(mp_limb_t ** ii, mp_size_t n, mp_bitcnt_t w,
mp_limb_t ** t1, mp_limb_t ** t2, mp_limb_t ** temp, mp_size_t trunc);
FLINT_DLL void ifft_truncate_sqrt2(mp_limb_t ** ii, mp_size_t n, mp_bitcnt_t w,
mp_limb_t ** t1, mp_limb_t ** t2, mp_limb_t ** temp, mp_size_t trunc);
FLINT_DLL void mul_truncate_sqrt2(mp_ptr r1, mp_srcptr i1, mp_size_t n1,
mp_srcptr i2, mp_size_t n2, mp_bitcnt_t depth, mp_bitcnt_t w);
FLINT_DLL void fft_butterfly_twiddle(mp_limb_t * u, mp_limb_t * v,
mp_limb_t * s, mp_limb_t * t, mp_size_t limbs, mp_bitcnt_t b1, mp_bitcnt_t b2);
FLINT_DLL void ifft_butterfly_twiddle(mp_limb_t * u, mp_limb_t * v,
mp_limb_t * s, mp_limb_t * t, mp_size_t limbs, mp_bitcnt_t b1, mp_bitcnt_t b2);
FLINT_DLL void fft_radix2_twiddle(mp_limb_t ** ii, mp_size_t is,
mp_size_t n, mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2,
mp_size_t ws, mp_size_t r, mp_size_t c, mp_size_t rs);
FLINT_DLL void ifft_radix2_twiddle(mp_limb_t ** ii, mp_size_t is,
mp_size_t n, mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2,
mp_size_t ws, mp_size_t r, mp_size_t c, mp_size_t rs);
FLINT_DLL void fft_truncate1_twiddle(mp_limb_t ** ii, mp_size_t is,
mp_size_t n, mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2,
mp_size_t ws, mp_size_t r, mp_size_t c, mp_size_t rs, mp_size_t trunc);
FLINT_DLL void ifft_truncate1_twiddle(mp_limb_t ** ii, mp_size_t is,
mp_size_t n, mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2,
mp_size_t ws, mp_size_t r, mp_size_t c, mp_size_t rs, mp_size_t trunc);
FLINT_DLL void fft_mfa_truncate_sqrt2(mp_limb_t ** ii, mp_size_t n,
mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2,
mp_limb_t ** temp, mp_size_t n1, mp_size_t trunc);
FLINT_DLL void ifft_mfa_truncate_sqrt2(mp_limb_t ** ii, mp_size_t n,
mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2,
mp_limb_t ** temp, mp_size_t n1, mp_size_t trunc);
FLINT_DLL void mul_mfa_truncate_sqrt2(mp_ptr r1, mp_srcptr i1, mp_size_t n1,
mp_srcptr i2, mp_size_t n2, mp_bitcnt_t depth, mp_bitcnt_t w);
FLINT_DLL void fft_mfa_truncate_sqrt2_outer(mp_limb_t ** ii, mp_size_t n,
mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2,
mp_limb_t ** temp, mp_size_t n1, mp_size_t trunc);
FLINT_DLL void fft_mfa_truncate_sqrt2_inner(mp_limb_t ** ii, mp_limb_t ** jj,
mp_size_t n, mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2,
mp_limb_t ** temp, mp_size_t n1, mp_size_t trunc, mp_limb_t * tt);
FLINT_DLL void ifft_mfa_truncate_sqrt2_outer(mp_limb_t ** ii, mp_size_t n,
mp_bitcnt_t w, mp_limb_t ** t1, mp_limb_t ** t2,
mp_limb_t ** temp, mp_size_t n1, mp_size_t trunc);
FLINT_DLL void fft_negacyclic(mp_limb_t ** ii, mp_size_t n, mp_bitcnt_t w,
mp_limb_t ** t1, mp_limb_t ** t2, mp_limb_t ** temp);
FLINT_DLL void ifft_negacyclic(mp_limb_t ** ii, mp_size_t n, mp_bitcnt_t w,
mp_limb_t ** t1, mp_limb_t ** t2, mp_limb_t ** temp);
FLINT_DLL void fft_naive_convolution_1(mp_limb_t * r, mp_limb_t * ii,
mp_limb_t * jj, mp_size_t m);
FLINT_DLL void _fft_mulmod_2expp1(mp_limb_t * r1, mp_limb_t * i1, mp_limb_t * i2,
mp_size_t r_limbs, mp_bitcnt_t depth, mp_bitcnt_t w);
FLINT_DLL slong fft_adjust_limbs(mp_size_t limbs);
FLINT_DLL void fft_mulmod_2expp1(mp_limb_t * r, mp_limb_t * i1, mp_limb_t * i2,
mp_size_t n, mp_size_t w, mp_limb_t * tt);
FLINT_DLL void flint_mpn_mul_fft_main(mp_ptr r1, mp_srcptr i1, mp_size_t n1,
mp_srcptr i2, mp_size_t n2);
FLINT_DLL void fft_convolution(mp_limb_t ** ii, mp_limb_t ** jj, slong depth,
slong limbs, slong trunc, mp_limb_t ** t1,
mp_limb_t ** t2, mp_limb_t ** s1, mp_limb_t * tt);
#ifdef __cplusplus
}
#endif
#endif