-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZmodF_poly.c
1531 lines (1274 loc) · 48.1 KB
/
ZmodF_poly.c
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===============================================================================*/
/****************************************************************************
ZmodF_poly.c
Polynomials over Z/pZ, where p = the Fermat number B^n + 1, where
B = 2^FLINT_BITS. Routines for truncated Schoenhage-Strassen FFTs
and convolutions.
Copyright (C) 2007, William Hart and David Harvey
*****************************************************************************/
#include "flint.h"
#include "memory-manager.h"
#include "ZmodF_poly.h"
#include "ZmodF_mul.h"
#include "fmpz_poly.h"
#include "mpn_extras.h"
#include "fmpz.h"
/****************************************************************************
Memory Management Routines
****************************************************************************/
void ZmodF_poly_init(ZmodF_poly_t poly, unsigned long depth, unsigned long n,
unsigned long scratch_count)
{
poly->n = n;
poly->depth = depth;
poly->scratch_count = scratch_count;
poly->length = 0;
unsigned long bufs = (1 << depth) + scratch_count;
poly->storage = (mp_limb_t*) flint_heap_alloc(bufs * (n+1));
// put scratch array immediately after coeffs array
poly->coeffs = (ZmodF_t*) flint_heap_alloc_bytes(bufs*sizeof(ZmodF_t));
poly->scratch = poly->coeffs + (1 << depth);
poly->coeffs[0] = poly->storage;
unsigned long i;
for (i = 1; i < bufs; i++)
poly->coeffs[i] = poly->coeffs[i-1] + (n+1);
}
void ZmodF_poly_clear(ZmodF_poly_t poly)
{
flint_heap_free(poly->coeffs);
flint_heap_free(poly->storage);
}
void ZmodF_poly_stack_init(ZmodF_poly_t poly, unsigned long depth, unsigned long n,
unsigned long scratch_count)
{
poly->n = n;
poly->depth = depth;
poly->scratch_count = scratch_count;
poly->length = 0;
unsigned long bufs = (1 << depth) + scratch_count;
poly->storage = (mp_limb_t*) flint_stack_alloc(bufs * (n+1));
// put scratch array immediately after coeffs array
poly->coeffs = (ZmodF_t*) flint_stack_alloc_bytes(bufs*sizeof(ZmodF_t));
poly->scratch = poly->coeffs + (1 << depth);
poly->coeffs[0] = poly->storage;
unsigned long i;
for (i = 1; i < bufs; i++)
poly->coeffs[i] = poly->coeffs[i-1] + (n+1);
}
void ZmodF_poly_stack_clear(ZmodF_poly_t poly)
{
flint_stack_release();
flint_stack_release();
}
/****************************************************************************
Basic Arithmetic Routines
****************************************************************************/
void ZmodF_poly_set(ZmodF_poly_t x, ZmodF_poly_t y)
{
FLINT_ASSERT(x->depth == y->depth);
FLINT_ASSERT(x->n == y->n);
unsigned long i;
for (i = 0; i < y->length; i++)
ZmodF_set(x->coeffs[i], y->coeffs[i], x->n);
x->length = y->length;
}
void ZmodF_poly_pointwise_mul(ZmodF_poly_t res, ZmodF_poly_t x, ZmodF_poly_t y)
{
FLINT_ASSERT(x->depth == y->depth);
FLINT_ASSERT(x->depth == res->depth);
FLINT_ASSERT(x->n == y->n);
FLINT_ASSERT(x->n == res->n);
FLINT_ASSERT(x->length == y->length);
unsigned long j;
ZmodF_mul_info_t info;
ZmodF_mul_info_init(info, x->n, x == y);
ulong i;
if (x != y)
for (i = 0; i < x->length; i++)
{
if (i+8 < x->length)
{
for (j = 0; j < x->n; j += 8) FLINT_PREFETCH(x->coeffs[i+8], j);
for (j = 0; j < y->n; j += 8) FLINT_PREFETCH(y->coeffs[i+8], j);
}
ZmodF_mul_info_mul(info, res->coeffs[i], x->coeffs[i], y->coeffs[i]);
}
else
{
unsigned long i;
for (i = 0; i < x->length; i++)
{
if (i+8 < x->length)
{
for (j = 0; j < x->n; j += 8) FLINT_PREFETCH(x->coeffs[i+8], j);
}
ZmodF_mul_info_mul(info, res->coeffs[i], x->coeffs[i], x->coeffs[i]);
}
}
ZmodF_mul_info_clear(info);
res->length = x->length;
}
void ZmodF_poly_add(ZmodF_poly_t res, ZmodF_poly_t x, ZmodF_poly_t y)
{
FLINT_ASSERT(x->depth == y->depth);
FLINT_ASSERT(x->depth == res->depth);
FLINT_ASSERT(x->n == y->n);
FLINT_ASSERT(x->n == res->n);
FLINT_ASSERT(x->length == y->length);
unsigned long i;
for (i = 0; i < x->length; i++)
ZmodF_add(res->coeffs[i], x->coeffs[i], y->coeffs[i], x->n);
res->length = x->length;
}
void ZmodF_poly_sub(ZmodF_poly_t res, ZmodF_poly_t x, ZmodF_poly_t y)
{
FLINT_ASSERT(x->depth == y->depth);
FLINT_ASSERT(x->depth == res->depth);
FLINT_ASSERT(x->n == y->n);
FLINT_ASSERT(x->n == res->n);
FLINT_ASSERT(x->length == y->length);
unsigned long i;
for (i = 0; i < x->length; i++)
ZmodF_sub(res->coeffs[i], x->coeffs[i], y->coeffs[i], x->n);
res->length = x->length;
}
void ZmodF_poly_normalise(ZmodF_poly_t poly)
{
unsigned long i;
for (i = 0; i < poly->length; i++)
ZmodF_normalise(poly->coeffs[i], poly->n);
}
void ZmodF_poly_rescale(ZmodF_poly_t poly)
{
if (poly->depth == 0)
return;
unsigned long i;
for (i = 0; i < poly->length; i++)
ZmodF_short_div_2exp(poly->coeffs[i], poly->coeffs[i],
poly->depth, poly->n);
}
void ZmodF_poly_rescale_range(ZmodF_poly_t poly, unsigned long start, unsigned long n)
{
if (poly->depth == 0)
return;
unsigned long length = FLINT_MIN(n, poly->length);
unsigned long i;
for (i = start; i < length; i++)
ZmodF_short_div_2exp(poly->coeffs[i], poly->coeffs[i],
poly->depth, poly->n);
}
/****************************************************************************
Forward fourier transforms (internal code)
****************************************************************************/
void _ZmodF_poly_FFT_iterative(
ZmodF_t* x, unsigned long depth,
unsigned long skip, unsigned long nonzero, unsigned long length,
unsigned long twist, unsigned long n, ZmodF_t* scratch)
{
FLINT_ASSERT((4*n*FLINT_BITS) % (1 << depth) == 0);
FLINT_ASSERT(skip >= 1);
FLINT_ASSERT(n >= 1);
FLINT_ASSERT(nonzero >= 1 && nonzero <= (1 << depth));
FLINT_ASSERT(length >= 1 && length <= (1 << depth));
FLINT_ASSERT(depth >= 1);
unsigned long i, s, start;
ZmodF_t* y, * z;
// root is the (2^depth)-th root of unity for the current layer,
// measured as a power of sqrt2
unsigned long root = (4*n*FLINT_BITS) >> depth;
FLINT_ASSERT(twist < root);
// half = half the current block length
unsigned long half = 1UL << (depth - 1);
unsigned long half_skip = half * skip;
unsigned long layer;
// =========================================================================
// Special case for first layer, if root and/or twist involve sqrt2
// rotations.
if ((root | twist) & 1)
{
// Let length = multiple of block size plus a remainder.
unsigned long length_quantised = length & (-2*half);
unsigned long length_remainder = length - length_quantised;
if (length <= half)
{
// Only need first output for each butterfly,
// i.e. (a, b) -> (a + b, ?)
if (nonzero > half)
for (i = 0, y = x; i < nonzero - half; i++, y += skip)
ZmodF_add(y[0], y[0], y[half_skip], n);
}
else
{
// Need both outputs for each butterfly.
if (nonzero <= half)
{
// The second half of each butterfly input are zeroes, so we just
// computing (a, 0) -> (a, ra), where r is the appropriate root
// of unity.
for (i = 0, s = twist, y = x; i < nonzero;
i++, s += root, y += skip)
{
ZmodF_mul_sqrt2exp(y[half_skip], y[0], s, n);
}
}
else
{
// If nonzero > half, then we need some full butterflies...
for (i = 0, s = twist, y = x; i < nonzero - half;
i++, s += root, y += skip)
{
ZmodF_forward_butterfly_sqrt2exp(y, y + half_skip,
scratch, s, n);
}
// and also some partial butterflies (a, 0) -> (a, ra).
for (; i < half; i++, s += root, y += skip)
ZmodF_mul_sqrt2exp(y[half_skip], y[0], s, n);
}
}
// Here we switch to measuring roots as powers of 2, but we also need
// to update to the next layer's roots, and these two actions cancel
// each other out :-)
// Update block length.
half >>= 1;
half_skip >>= 1;
if (nonzero > 2*half)
nonzero = 2*half;
layer = 1;
}
else
{
// no special case for first layer
layer = 0;
// switch to measuring roots as powers of 2
root >>= 1;
twist >>= 1;
}
// =========================================================================
// This section handles the layers where there are still zero coefficients
// to take advantage of. In most cases, this will only happen for the
// first layer or two, so we don't bother with specialised limbshift-only
// code for these layers.
// Note: from here on there are no sqrt2 rotations, and we measure all
// roots as powers of 2.
for (; (layer < depth) && (nonzero < 2*half); layer++)
{
// Let length = multiple of block size plus a remainder.
unsigned long length_quantised = length & (-2*half);
unsigned long length_remainder = length - length_quantised;
if (length_remainder > half)
{
// If length overhangs by more than half the block, then we need to
// perform full butterflies on the last block (i.e. the last block
// doesn't get any special treatment).
length_quantised += 2*half;
}
else if (length_remainder)
{
// If length overhangs the block by at most half the block size,
// then we only need to compute the first output of each butterfly
// for this block, i.e. (a, b) -> (a + b)
if (nonzero > half)
{
y = x + skip * length_quantised;
for (i = 0; i < nonzero - half; i++, y += skip)
ZmodF_add(y[0], y[0], y[half_skip], n);
}
}
if (nonzero <= half)
{
// If nonzero <= half, then the second half of each butterfly input
// are zeroes, so we just computing (a, 0) -> (a, ra), where r is the
// appropriate root of unity.
for (start = 0, y = x; start < length_quantised;
start += 2*half, y += 2*half_skip)
{
for (i = 0, s = twist, z = y; i < nonzero;
i++, s += root, z += skip)
{
ZmodF_mul_2exp(z[half_skip], z[0], s, n);
}
}
}
else
{
for (start = 0, y = x; start < length_quantised;
start += 2*half, y += 2*half_skip)
{
// If nonzero > half, then we need some full butterflies...
for (i = 0, s = twist, z = y; i < nonzero - half;
i++, s += root, z += skip)
{
ZmodF_forward_butterfly_2exp(z, z + half_skip, scratch, s, n);
}
// and also some partial butterflies (a, 0) -> (a, ra).
for (; i < half; i++, s += root, z += skip)
ZmodF_mul_2exp(z[half_skip], z[0], s, n);
}
}
// Update roots of unity
twist <<= 1;
root <<= 1;
// Update block length.
half >>= 1;
half_skip >>= 1;
if (nonzero > 2*half)
// no more zero coefficients to take advantage of:
nonzero = 2*half;
}
// =========================================================================
// Now we may assume there are no more zero coefficients.
for (; layer < depth; layer++)
{
// Let length = multiple of block size plus a remainder.
unsigned long length_quantised = length & (-2*half);
unsigned long length_remainder = length - length_quantised;
if (length_remainder > half)
{
// If length overhangs by more than half the block, then we need to
// perform full butterflies on the last block (i.e. the last block
// doesn't get any special treatment).
length_quantised += 2*half;
}
else if (length_remainder)
{
// If length overhangs the block by at most half the block size,
// then we only need to compute the first output of each butterfly
// for this block, i.e. (a, b) -> (a + b)
y = x + skip * length_quantised;
for (i = 0; i < half; i++, y += skip)
ZmodF_add(y[0], y[0], y[half_skip], n);
}
// To keep the inner loops long, we have two versions of the next loop.
if (layer < depth/2)
{
// Version 1: only a few relatively long blocks.
for (start = 0, y = x; start < length_quantised;
start += 2*half, y += 2*half_skip)
{
for (i = 0, s = twist, z = y; i < half; i++, s += root, z += skip)
ZmodF_forward_butterfly_2exp(z, z + half_skip, scratch, s, n);
}
}
else
{
// Version 2: lots of short blocks.
// Two sub-versions, depending on whether the rotations are all by
// a whole number of limbs.
if ((root | twist) & (FLINT_BITS - 1))
{
// Version 2a: rotations still involve bitshifts.
for (i = 0, s = twist, y = x; i < half; i++, s += root, y += skip)
for (start = 0, z = y; start < length_quantised;
start += 2*half, z += 2*half_skip)
{
ZmodF_forward_butterfly_2exp(z, z + half_skip,
scratch, s, n);
}
}
else
{
// Version 2b: rotations involve only limbshifts.
unsigned long root_limbs = root >> FLINT_LG_BITS_PER_LIMB;
if (twist == 0)
{
// special case, since ZmodF_forward_butterfly_Bexp doesn't
// allow zero rotation count
for (start = 0, z = x; start < length_quantised;
start += 2*half, z += 2*half_skip)
{
ZmodF_simple_butterfly(z, z + half_skip, scratch, n);
}
i = 1;
y = x + skip;
s = root_limbs;
}
else
{
i = 0;
y = x;
s = twist >> FLINT_LG_BITS_PER_LIMB;
}
for (; i < half; i++, s += root_limbs, y += skip)
for (start = 0, z = y; start < length_quantised;
start += 2*half, z += 2*half_skip)
{
ZmodF_forward_butterfly_Bexp(z, z + half_skip,
scratch, s, n);
}
}
}
// Update roots of unity
twist <<= 1;
root <<= 1;
// Update block length.
half >>= 1;
half_skip >>= 1;
}
}
/*
Factors FFT of length 2^depth into length 2^rows_depth and length 2^cols_depth
transforms
*/
void _ZmodF_poly_FFT_factor(
ZmodF_t* x, unsigned long rows_depth, unsigned long cols_depth,
unsigned long skip, unsigned long nonzero, unsigned long length,
unsigned long twist, unsigned long n, ZmodF_t* scratch)
{
FLINT_ASSERT(skip >= 1);
FLINT_ASSERT(n >= 1);
FLINT_ASSERT(rows_depth >= 1);
FLINT_ASSERT(cols_depth >= 1);
unsigned long depth = rows_depth + cols_depth;
FLINT_ASSERT((4*n*FLINT_BITS) % (1 << depth) == 0);
FLINT_ASSERT(nonzero >= 1 && nonzero <= (1 << depth));
FLINT_ASSERT(length >= 1 && length <= (1 << depth));
// root is the (2^depth)-th root unity, measured as a power of sqrt2
unsigned long root = (4*n*FLINT_BITS) >> depth;
FLINT_ASSERT(twist < root);
unsigned long rows = 1UL << rows_depth;
unsigned long cols = 1UL << cols_depth;
unsigned long length_rows = length >> cols_depth;
unsigned long length_cols = length & (cols-1);
unsigned long length_whole_rows = length_cols ?
(length_rows + 1) : length_rows;
unsigned long nonzero_rows = nonzero >> cols_depth;
unsigned long nonzero_cols = nonzero & (cols-1);
unsigned long i, j;
ZmodF_t* y;
// column transforms
for (i = 0, y = x, j = twist; i < nonzero_cols; i++, y += skip, j += root)
_ZmodF_poly_FFT(y, rows_depth, skip << cols_depth, nonzero_rows + 1,
length_whole_rows, j, n, scratch);
if (nonzero_rows)
{
for (; i < cols; i++, y += skip, j += root)
_ZmodF_poly_FFT(y, rows_depth, skip << cols_depth, nonzero_rows,
length_whole_rows, j, n, scratch);
nonzero_cols = cols;
}
// row transforms
for (i = 0, y = x; i < length_rows; i++, y += (skip << cols_depth))
_ZmodF_poly_FFT(y, cols_depth, skip, nonzero_cols, cols,
twist << rows_depth, n, scratch);
if (length_cols)
// The relevant portion of the last row:
_ZmodF_poly_FFT(y, cols_depth, skip, nonzero_cols, length_cols,
twist << rows_depth, n, scratch);
}
/*
This is an internal function. It's just a temporary implementation so that
we can get started on higher level code. It is not optimised particularly
well yet.
x = array of buffers to operate on
skip = distance between buffers
depth = log2(number of buffers)
nonzero = number of buffers assumed to be nonzero
length = number of fourier coefficients requested
twist = twisting power of sqrt2
n = coefficient length
scratch = a scratch buffer
*/
void _ZmodF_poly_FFT(ZmodF_t* x, unsigned long depth, unsigned long skip,
unsigned long nonzero, unsigned long length,
unsigned long twist, unsigned long n,
ZmodF_t* scratch)
{
FLINT_ASSERT((4*n*FLINT_BITS) % (1 << depth) == 0);
FLINT_ASSERT(skip >= 1);
FLINT_ASSERT(n >= 1);
FLINT_ASSERT(nonzero >= 1 && nonzero <= (1 << depth));
FLINT_ASSERT(length >= 1 && length <= (1 << depth));
FLINT_ASSERT(depth >= 1);
// If the data fits in L1 (2^depth coefficients of length n+1, plus a
// scratch buffer), then use the iterative transform. Otherwise factor the
// FFT into two chunks.
if (depth == 1 ||
((1 << depth) + 1) * (n+1) <= ZMODFPOLY_FFT_FACTOR_THRESHOLD)
{
_ZmodF_poly_FFT_iterative(x, depth, skip, nonzero, length,
twist, n, scratch);
}
else
{
unsigned long rows_depth = depth >> 1;
unsigned long cols_depth = depth - rows_depth;
_ZmodF_poly_FFT_factor(x, rows_depth, cols_depth, skip, nonzero, length,
twist, n, scratch);
}
}
/****************************************************************************
Inverse fourier transforms (internal code)
****************************************************************************/
/*
This one is for when there is no truncation.
*/
void _ZmodF_poly_IFFT_iterative(
ZmodF_t* x, unsigned long depth, unsigned long skip,
unsigned long twist, unsigned long n, ZmodF_t* scratch)
{
FLINT_ASSERT((4*n*FLINT_BITS) % (1 << depth) == 0);
FLINT_ASSERT(skip >= 1);
FLINT_ASSERT(n >= 1);
FLINT_ASSERT(depth >= 1);
// root is the (2^(layer+1))-th root unity for each layer,
// measured as a power of sqrt2
long root = 2*n*FLINT_BITS;
twist <<= (depth - 1);
FLINT_ASSERT(twist < root);
unsigned long half = 1;
unsigned long half_skip = skip;
unsigned long size = 1UL << depth;
unsigned long layer, start, i, s;
ZmodF_t* y, * z;
// First group of layers; lots of small blocks.
for (layer = 0; layer < depth/2; layer++)
{
// no sqrt2 should be involved here
FLINT_ASSERT(!((twist | root) & 1));
// change roots to be measured as powers of 2
// (also this updates for the next layer in advance)
root >>= 1;
twist >>= 1;
if ((root | twist) & (FLINT_BITS-1))
{
// This version allows bitshifts
for (i = 0, y = x, s = twist; i < half; i++, s += root, y += skip)
for (start = 0, z = y; start < size;
start += 2*half, z += 2*half_skip)
{
ZmodF_inverse_butterfly_2exp(z, z + half_skip, scratch, s, n);
}
}
else
{
// This version is limbshifts only
unsigned long root_limbs = root >> FLINT_LG_BITS_PER_LIMB;
if (twist == 0)
{
// special case since ZmodF_inverse_butterfly_Bexp doesn't allow
// zero rotation count
for (start = 0, z = x; start < size;
start += 2*half, z += 2*half_skip)
{
ZmodF_simple_butterfly(z, z + half_skip, scratch, n);
}
i = 1;
s = root_limbs;
y = x + skip;
}
else
{
i = 0;
s = twist >> FLINT_LG_BITS_PER_LIMB;
y = x;
}
for (; i < half; i++, s += root_limbs, y += skip)
for (start = 0, z = y; start < size;
start += 2*half, z += 2*half_skip)
{
ZmodF_inverse_butterfly_Bexp(z, z + half_skip, scratch, s, n);
}
}
half <<= 1;
half_skip <<= 1;
}
// Second group of layers; just a few large blocks.
for (; layer < depth; layer++)
{
if ((root | twist) & 1)
{
// sqrt2 is involved. This had better be the last layer.
FLINT_ASSERT(layer == depth - 1);
for (i = 0, z = x, s = twist; i < half; i++, s += root, z += skip)
ZmodF_inverse_butterfly_sqrt2exp(z, z + half_skip, scratch, s, n);
return;
}
else
{
// Only bitshifts.
// change roots to be measured as powers of 2
// (also this updates for the next layer in advance)
twist >>= 1;
root >>= 1;
for (start = 0, y = x; start < size;
start += 2*half, y += 2*half_skip)
{
for (i = 0, z = y, s = twist; i < half; i++, s += root, z += skip)
ZmodF_inverse_butterfly_2exp(z, z + half_skip, scratch, s, n);
}
}
half <<= 1;
half_skip <<= 1;
}
}
/*
This one's for working in L1 when truncation is involved. It splits into
two halves.
*/
void _ZmodF_poly_IFFT_recursive(
ZmodF_t* x, unsigned long depth, unsigned long skip,
unsigned long nonzero, unsigned long length, int extra,
unsigned long twist, unsigned long n, ZmodF_t* scratch)
{
FLINT_ASSERT((4*n*FLINT_BITS) % (1 << depth) == 0);
FLINT_ASSERT(skip >= 1);
FLINT_ASSERT(n >= 1);
FLINT_ASSERT(nonzero >= 1 && nonzero <= (1UL << depth));
FLINT_ASSERT(length <= nonzero);
FLINT_ASSERT((length == 0 && extra) ||
(length == (1UL << depth) && !extra) ||
(length > 0 && length < (1UL << depth)));
FLINT_ASSERT(depth >= 1);
long size = 1UL << depth;
if (length == size)
{
// no truncation necessary
_ZmodF_poly_IFFT_iterative(x, depth, skip, twist, n, scratch);
return;
}
// root is the (2^depth)-th root unity, measured as a power of sqrt2
long root = (4*n*FLINT_BITS) >> depth;
FLINT_ASSERT(twist < root);
long cols = size >> 1;
long half = skip << (depth - 1);
// symbols in the following diagrams:
// A = fully untransformed coefficient
// a = fully untransformed coefficient (implied zero)
// B = intermediate coefficient
// b = intermediate coefficient (implied zero)
// C = fully transformed coefficient
// c = fully transformed coefficient (implied zero)
// ? = garbage that we don't care about
// * = the extra C coefficient, or "?" if no extra coefficient requested
// the horizontal transforms convert between B and C
// the vertical butterflies convert between A and B
if ((length < cols) || (length == cols && !extra))
{
// The input could look like one of the following:
// CCCCAAAA CCCCAAAA CCCCAAaa CCCCaaaa
// AAAAAAaa or AAaaaaaa or aaaaaaaa or aaaaaaaa
long i, last_zero_forward_butterfly, last_zero_cross_butterfly;
if (nonzero <= cols)
{
i = nonzero - 1;
last_zero_forward_butterfly = length;
last_zero_cross_butterfly = 0;
}
else
{
i = cols - 1;
if (nonzero > length + cols)
{
last_zero_forward_butterfly = nonzero - cols;
last_zero_cross_butterfly = length;
}
else
{
last_zero_forward_butterfly = length;
last_zero_cross_butterfly = nonzero - cols;
}
}
ZmodF_t* y = x + skip*i;
// First some forward butterflies ("Aa" => "B?") to make them look like:
// CCCCAABB CCCCBBBB CCCCBBaa CCCCaaaa
// AAAAAA?? or AAaa???? or aaaa??aa or aaaaaaaa
for (; i >= last_zero_forward_butterfly; i--, y -= skip)
{
// (2*a0, ?) -> (a0, ?) = (b0, ?)
ZmodF_short_div_2exp(y[0], y[0], 1, n);
}
// Then some forward butterflies ("AA" => "B?") to make them look like:
// CCCCBBBB CCCCBBBB CCCCBBaa CCCCaaaa
// AAAA???? or AAaa???? or aaaa??aa or aaaaaaaa
for (; i >= (long)length; i--, y -= skip)
{
// (2*a0, 2*a1) -> (a0 + a1, ?) = (b0, ?)
ZmodF_add(y[0], y[0], y[half], n);
ZmodF_short_div_2exp(y[0], y[0], 1, n);
}
// Transform the first row to make them look like:
// BBBB*??? BBBB*??? BBBB*??? BBBB*???
// AAAA???? or AAaa???? or aaaa??aa or aaaaaaaa
if (depth > 1)
_ZmodF_poly_IFFT_recursive(x, depth - 1, skip,
(nonzero < cols) ? nonzero : cols,
length, extra, twist << 1, n, scratch);
// Cross butterflies ("Ba" => "A?") to make them look like:
// BBBB*??? BBAA*??? AAAA*??? AAAA*???
// AAAA???? or AA?????? or ??????aa or ????aaaa
for (; i >= last_zero_cross_butterfly; i--, y -= skip)
{
// (b0, ?) -> (2*b0, ?) = (2*a0, ?)
ZmodF_add(y[0], y[0], y[0], n);
}
// Cross butterflies ("BA" => "A?") to make them look like:
// AAAA*??? AAAA*??? AAAA*??? AAAA*???
// ???????? or ???????? or ??????aa or ????aaaa
for (; i >= 0; i--, y -= skip)
{
// (b0, 2*a1) -> (2*b0 - 2*a1, ?) = (2*a0, ?)
ZmodF_add(y[0], y[0], y[0], n);
ZmodF_sub(y[0], y[0], y[half], n);
}
}
else
{
// The input looks like one of these:
// CCCCCCCC CCCCCCCC
// AAAAaaaa (extra == 1) or CCCAAAaa
// Transform first row (no truncation necessary) to make them look like:
// BBBBBBBB BBBBBBBB
// AAAAaaaa (extra == 1) or CCCAAAaa
if (depth > 1)
_ZmodF_poly_IFFT_iterative(x, depth - 1, skip, twist << 1, n, scratch);
long i = cols - 1;
unsigned long s = twist + root*i;
ZmodF_t* y = x + skip*i;
long last_zero_cross_butterfly = nonzero - cols;
long last_cross_butterfly = length - cols;
// Cross butterflies ("Ba" => "AB") to make them look like:
// BBBBAAAA BBBBBBAA
// AAAABBBB (extra == 1) or CCCAAABB
for (; i >= last_zero_cross_butterfly; i--, s -= root, y -= skip)
{
// (b0, ?) -> (2*b0, w*b0) = (2*a0, b1)
ZmodF_mul_sqrt2exp(y[half], y[0], s, n);
ZmodF_add(y[0], y[0], y[0], n);
}
// Cross butterflies ("BA" => "AB") to make them look like:
// AAAAAAAA BBBAAAAA
// BBBBBBBB (extra == 1) or CCCBBBBB
for (; i >= last_cross_butterfly; i--, s -= root, y -= skip)
{
// (b0, 2*a1) -> (2*(b0-a1), w*(b0-2*a1)) = (2*a0, b1)
ZmodF_sub(scratch[0], y[0], y[half], n);
ZmodF_add(y[0], y[0], scratch[0], n);
ZmodF_mul_sqrt2exp(y[half], scratch[0], s, n);
}
// Transform second row to make them look like:
// AAAAAAAA BBBAAAAA
// *??????? (extra == 1) or BBB*????
if (depth > 1)
_ZmodF_poly_IFFT_recursive(x + skip*cols, depth - 1, skip, cols,
length - cols, extra, twist << 1, n,
scratch);
// Inverse butterflies ("BB" => "AA") to make them look like:
// AAAAAAAA AAAAAAAA
// *??????? (extra == 1) or AAA*????
for (; i >= 0; i--, s -= root, y -= skip)
{
// (b0, b1) -> (b0 + w*b1, b0 - w*b1) = (2*a0, 2*a1)
ZmodF_inverse_butterfly_sqrt2exp(y, y + half, scratch, s, n);
}
}
}
/*
This is an internal function. It's just a temporary implementation so that
we can get started on higher level code. It is not optimised particularly
well yet.
x = array of buffers to operate on
skip = distance between buffers
depth = log2(number of buffers)
nonzero = number of *output* buffers assumed to be nonzero
length = number of untransformed coefficients requested
extra = indicates whether an extra *forward* coefficient should be computed
twist = twisting power of sqrt2
n = coefficient length
scratch = a scratch buffer
*/
void _ZmodF_poly_IFFT_factor(
ZmodF_t* x, unsigned long rows_depth, unsigned long cols_depth,
unsigned long skip, unsigned long nonzero, unsigned long length,
int extra, unsigned long twist, unsigned long n, ZmodF_t* scratch)
{
FLINT_ASSERT(skip >= 1);
FLINT_ASSERT(n >= 1);
FLINT_ASSERT(rows_depth >= 1);
FLINT_ASSERT(cols_depth >= 1);
unsigned long depth = rows_depth + cols_depth;
FLINT_ASSERT((4*n*FLINT_BITS) % (1 << depth) == 0);
FLINT_ASSERT(nonzero >= 1 && nonzero <= (1UL << depth));
FLINT_ASSERT(length <= nonzero);
FLINT_ASSERT((length == 0 && extra) ||
(length == (1UL << depth) && !extra) ||
(length > 0 && length < (1UL << depth)));
// root is the (2^depth)-th root unity, measured as a power of sqrt2
unsigned long root = (4*n*FLINT_BITS) >> depth;
FLINT_ASSERT(twist < root);
unsigned long rows = 1UL << rows_depth;
unsigned long cols = 1UL << cols_depth;
unsigned long length_rows = length >> cols_depth;
unsigned long length_cols = length & (cols-1);
unsigned long nonzero_rows = nonzero >> cols_depth;
unsigned long nonzero_cols = nonzero & (cols-1);
unsigned long i, j;
ZmodF_t* y;
// row transforms for the rows where we have all fourier coefficients
for (i = 0, y = x; i < length_rows; i++, y += (skip << cols_depth))
_ZmodF_poly_IFFT(y, cols_depth, skip, cols, cols, 0,
twist << rows_depth, n, scratch);
// column transforms where we have enough information
for (i = length_cols, y = x + (skip * length_cols),
j = twist + (root*length_cols);
i < nonzero_cols; i++, y += skip, j += root)
{
_ZmodF_poly_IFFT(y, rows_depth, skip << cols_depth, nonzero_rows + 1,
length_rows, length_cols ? 1 : extra, j, n, scratch);
}