-
Notifications
You must be signed in to change notification settings - Fork 48
/
ftbase.pas
1733 lines (1625 loc) · 53 KB
/
ftbase.pas
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
{$MODESWITCH RESULT+}
{$GOTO ON}
(*************************************************************************
Copyright (c) 2009, Sergey Bochkanov (ALGLIB project).
>>> SOURCE LICENSE >>>
This program 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 (www.fsf.org); either version 2 of the
License, or (at your option) any later version.
This program 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.
A copy of the GNU General Public License is available at
http://www.fsf.org/licensing/licenses
>>> END OF LICENSE >>>
*************************************************************************)
unit ftbase;
interface
uses Math, Sysutils, Ap;
type
FTPlan = record
Plan : TInteger1DArray;
Precomputed : TReal1DArray;
TmpBuf : TReal1DArray;
StackBuf : TReal1DArray;
end;
procedure FTBaseGenerateComplexFFTPlan(N : AlglibInteger; var Plan : FTPlan);
procedure FTBaseGenerateRealFFTPlan(N : AlglibInteger; var Plan : FTPlan);
procedure FTBaseGenerateRealFHTPlan(N : AlglibInteger; var Plan : FTPlan);
procedure FTBaseExecutePlan(var A : TReal1DArray;
AOffset : AlglibInteger;
N : AlglibInteger;
var Plan : FTPlan);
procedure FTBaseExecutePlanRec(var A : TReal1DArray;
AOffset : AlglibInteger;
var Plan : FTPlan;
EntryOffset : AlglibInteger;
StackPtr : AlglibInteger);
procedure FTBaseFactorize(N : AlglibInteger;
TaskType : AlglibInteger;
var N1 : AlglibInteger;
var N2 : AlglibInteger);
function FTBaseIsSmooth(N : AlglibInteger):Boolean;
function FTBaseFindSmooth(N : AlglibInteger):AlglibInteger;
function FTBaseFindSmoothEven(N : AlglibInteger):AlglibInteger;
function FTBaseGetFLOPEstimate(N : AlglibInteger):Double;
implementation
const
FTBasePlanEntrySize = 8;
FTBaseCFFTTask = 0;
FTBaseRFHTTask = 1;
FTBaseRFFTTask = 2;
FFTCooleyTukeyPlan = 0;
FFTBluesteinPlan = 1;
FFTCodeletPlan = 2;
FHTCooleyTukeyPlan = 3;
FHTCodeletPlan = 4;
FFTRealCooleyTukeyPlan = 5;
FFTEmptyPlan = 6;
FHTN2Plan = 999;
FTBaseUpdateTw = 4;
FTBaseCodeletMax = 5;
FTBaseCodeletRecommended = 5;
FTBaseInefficiencyFactor = Double(1.3);
FTBaseMaxSmoothFactor = 5;
procedure FTBaseGeneratePlanRec(N : AlglibInteger;
TaskType : AlglibInteger;
var Plan : FTPlan;
var PlanSize : AlglibInteger;
var PrecomputedSize : AlglibInteger;
var PlanArraySize : AlglibInteger;
var TmpMemSize : AlglibInteger;
var StackMemSize : AlglibInteger;
StackPtr : AlglibInteger);forward;
procedure FTBasePrecomputePlanRec(var Plan : FTPlan;
EntryOffset : AlglibInteger;
StackPtr : AlglibInteger);forward;
procedure FFTTwCalc(var A : TReal1DArray;
AOffset : AlglibInteger;
N1 : AlglibInteger;
N2 : AlglibInteger);forward;
procedure InternalComplexLinTranspose(var A : TReal1DArray;
M : AlglibInteger;
N : AlglibInteger;
AStart : AlglibInteger;
var Buf : TReal1DArray);forward;
procedure InternalRealLinTranspose(var A : TReal1DArray;
M : AlglibInteger;
N : AlglibInteger;
AStart : AlglibInteger;
var Buf : TReal1DArray);forward;
procedure FFTICLTRec(var A : TReal1DArray;
AStart : AlglibInteger;
AStride : AlglibInteger;
var B : TReal1DArray;
BStart : AlglibInteger;
BStride : AlglibInteger;
M : AlglibInteger;
N : AlglibInteger);forward;
procedure FFTIRLTRec(var A : TReal1DArray;
AStart : AlglibInteger;
AStride : AlglibInteger;
var B : TReal1DArray;
BStart : AlglibInteger;
BStride : AlglibInteger;
M : AlglibInteger;
N : AlglibInteger);forward;
procedure FTBaseFindSmoothRec(N : AlglibInteger;
Seed : AlglibInteger;
LeastFactor : AlglibInteger;
var Best : AlglibInteger);forward;
procedure FFTArrayResize(var A : TInteger1DArray;
var ASize : AlglibInteger;
NewASize : AlglibInteger);forward;
procedure RefFHT(var A : TReal1DArray;
N : AlglibInteger;
Offs : AlglibInteger);forward;
(*************************************************************************
This subroutine generates FFT plan - a decomposition of a N-length FFT to
the more simpler operations. Plan consists of the root entry and the child
entries.
Subroutine parameters:
N task size
Output parameters:
Plan plan
-- ALGLIB --
Copyright 01.05.2009 by Bochkanov Sergey
*************************************************************************)
procedure FTBaseGenerateComplexFFTPlan(N : AlglibInteger; var Plan : FTPlan);
var
PlanArraySize : AlglibInteger;
PlanSize : AlglibInteger;
PrecomputedSize : AlglibInteger;
TmpMemSize : AlglibInteger;
StackMemSize : AlglibInteger;
StackPtr : AlglibInteger;
begin
PlanArraySize := 1;
PlanSize := 0;
PrecomputedSize := 0;
StackMemSize := 0;
StackPtr := 0;
TmpMemSize := 2*N;
SetLength(Plan.Plan, PlanArraySize);
FTBaseGeneratePlanRec(N, FTBaseCFFTTask, Plan, PlanSize, PrecomputedSize, PlanArraySize, TmpMemSize, StackMemSize, StackPtr);
Assert(StackPtr=0, 'Internal error in FTBaseGenerateComplexFFTPlan: stack ptr!');
SetLength(Plan.StackBuf, Max(StackMemSize, 1));
SetLength(Plan.TmpBuf, Max(TmpMemSize, 1));
SetLength(Plan.Precomputed, Max(PrecomputedSize, 1));
StackPtr := 0;
FTBasePrecomputePlanRec(Plan, 0, StackPtr);
Assert(StackPtr=0, 'Internal error in FTBaseGenerateComplexFFTPlan: stack ptr!');
end;
(*************************************************************************
Generates real FFT plan
*************************************************************************)
procedure FTBaseGenerateRealFFTPlan(N : AlglibInteger; var Plan : FTPlan);
var
PlanArraySize : AlglibInteger;
PlanSize : AlglibInteger;
PrecomputedSize : AlglibInteger;
TmpMemSize : AlglibInteger;
StackMemSize : AlglibInteger;
StackPtr : AlglibInteger;
begin
PlanArraySize := 1;
PlanSize := 0;
PrecomputedSize := 0;
StackMemSize := 0;
StackPtr := 0;
TmpMemSize := 2*N;
SetLength(Plan.Plan, PlanArraySize);
FTBaseGeneratePlanRec(N, FTBaseRFFTTask, Plan, PlanSize, PrecomputedSize, PlanArraySize, TmpMemSize, StackMemSize, StackPtr);
Assert(StackPtr=0, 'Internal error in FTBaseGenerateRealFFTPlan: stack ptr!');
SetLength(Plan.StackBuf, Max(StackMemSize, 1));
SetLength(Plan.TmpBuf, Max(TmpMemSize, 1));
SetLength(Plan.Precomputed, Max(PrecomputedSize, 1));
StackPtr := 0;
FTBasePrecomputePlanRec(Plan, 0, StackPtr);
Assert(StackPtr=0, 'Internal error in FTBaseGenerateRealFFTPlan: stack ptr!');
end;
(*************************************************************************
Generates real FHT plan
*************************************************************************)
procedure FTBaseGenerateRealFHTPlan(N : AlglibInteger; var Plan : FTPlan);
var
PlanArraySize : AlglibInteger;
PlanSize : AlglibInteger;
PrecomputedSize : AlglibInteger;
TmpMemSize : AlglibInteger;
StackMemSize : AlglibInteger;
StackPtr : AlglibInteger;
begin
PlanArraySize := 1;
PlanSize := 0;
PrecomputedSize := 0;
StackMemSize := 0;
StackPtr := 0;
TmpMemSize := N;
SetLength(Plan.Plan, PlanArraySize);
FTBaseGeneratePlanRec(N, FTBaseRFHTTask, Plan, PlanSize, PrecomputedSize, PlanArraySize, TmpMemSize, StackMemSize, StackPtr);
Assert(StackPtr=0, 'Internal error in FTBaseGenerateRealFHTPlan: stack ptr!');
SetLength(Plan.StackBuf, Max(StackMemSize, 1));
SetLength(Plan.TmpBuf, Max(TmpMemSize, 1));
SetLength(Plan.Precomputed, Max(PrecomputedSize, 1));
StackPtr := 0;
FTBasePrecomputePlanRec(Plan, 0, StackPtr);
Assert(StackPtr=0, 'Internal error in FTBaseGenerateRealFHTPlan: stack ptr!');
end;
(*************************************************************************
This subroutine executes FFT/FHT plan.
If Plan is a:
* complex FFT plan - sizeof(A)=2*N,
A contains interleaved real/imaginary values
* real FFT plan - sizeof(A)=2*N,
A contains real values interleaved with zeros
* real FHT plan - sizeof(A)=2*N,
A contains real values interleaved with zeros
-- ALGLIB --
Copyright 01.05.2009 by Bochkanov Sergey
*************************************************************************)
procedure FTBaseExecutePlan(var A : TReal1DArray;
AOffset : AlglibInteger;
N : AlglibInteger;
var Plan : FTPlan);
var
StackPtr : AlglibInteger;
begin
StackPtr := 0;
FTBaseExecutePlanRec(A, AOffset, Plan, 0, StackPtr);
end;
(*************************************************************************
Recurrent subroutine for the FTBaseExecutePlan
Parameters:
A FFT'ed array
AOffset offset of the FFT'ed part (distance is measured in doubles)
-- ALGLIB --
Copyright 01.05.2009 by Bochkanov Sergey
*************************************************************************)
procedure FTBaseExecutePlanRec(var A : TReal1DArray;
AOffset : AlglibInteger;
var Plan : FTPlan;
EntryOffset : AlglibInteger;
StackPtr : AlglibInteger);
var
I : AlglibInteger;
J : AlglibInteger;
K : AlglibInteger;
N1 : AlglibInteger;
N2 : AlglibInteger;
N : AlglibInteger;
M : AlglibInteger;
Offs : AlglibInteger;
Offs1 : AlglibInteger;
Offs2 : AlglibInteger;
OffsA : AlglibInteger;
OffsB : AlglibInteger;
OffsP : AlglibInteger;
HK : Double;
HNK : Double;
X : Double;
Y : Double;
BX : Double;
BY : Double;
EmptyArray : TReal1DArray;
A0X : Double;
A0Y : Double;
A1X : Double;
A1Y : Double;
A2X : Double;
A2Y : Double;
A3X : Double;
A3Y : Double;
V0 : Double;
V1 : Double;
V2 : Double;
V3 : Double;
T1X : Double;
T1Y : Double;
T2X : Double;
T2Y : Double;
T3X : Double;
T3Y : Double;
T4X : Double;
T4Y : Double;
T5X : Double;
T5Y : Double;
M1X : Double;
M1Y : Double;
M2X : Double;
M2Y : Double;
M3X : Double;
M3Y : Double;
M4X : Double;
M4Y : Double;
M5X : Double;
M5Y : Double;
S1X : Double;
S1Y : Double;
S2X : Double;
S2Y : Double;
S3X : Double;
S3Y : Double;
S4X : Double;
S4Y : Double;
S5X : Double;
S5Y : Double;
C1 : Double;
C2 : Double;
C3 : Double;
C4 : Double;
C5 : Double;
Tmp : TReal1DArray;
begin
if Plan.Plan[EntryOffset+3]=FFTEmptyPlan then
begin
Exit;
end;
if Plan.Plan[EntryOffset+3]=FFTCooleyTukeyPlan then
begin
//
// Cooley-Tukey plan
// * transposition
// * row-wise FFT
// * twiddle factors:
// - TwBase is a basis twiddle factor for I=1, J=1
// - TwRow is a twiddle factor for a second element in a row (J=1)
// - Tw is a twiddle factor for a current element
// * transposition again
// * row-wise FFT again
//
N1 := Plan.Plan[EntryOffset+1];
N2 := Plan.Plan[EntryOffset+2];
InternalComplexLinTranspose(A, N1, N2, AOffset, Plan.TmpBuf);
I:=0;
while I<=N2-1 do
begin
FTBaseExecutePlanRec(A, AOffset+I*N1*2, Plan, Plan.Plan[EntryOffset+5], StackPtr);
Inc(I);
end;
FFTTwCalc(A, AOffset, N1, N2);
InternalComplexLinTranspose(A, N2, N1, AOffset, Plan.TmpBuf);
I:=0;
while I<=N1-1 do
begin
FTBaseExecutePlanRec(A, AOffset+I*N2*2, Plan, Plan.Plan[EntryOffset+6], StackPtr);
Inc(I);
end;
InternalComplexLinTranspose(A, N1, N2, AOffset, Plan.TmpBuf);
Exit;
end;
if Plan.Plan[EntryOffset+3]=FFTRealCooleyTukeyPlan then
begin
//
// Cooley-Tukey plan
// * transposition
// * row-wise FFT
// * twiddle factors:
// - TwBase is a basis twiddle factor for I=1, J=1
// - TwRow is a twiddle factor for a second element in a row (J=1)
// - Tw is a twiddle factor for a current element
// * transposition again
// * row-wise FFT again
//
N1 := Plan.Plan[EntryOffset+1];
N2 := Plan.Plan[EntryOffset+2];
InternalComplexLinTranspose(A, N2, N1, AOffset, Plan.TmpBuf);
I:=0;
while I<=N1 div 2-1 do
begin
//
// pack two adjacent smaller real FFT's together,
// make one complex FFT,
// unpack result
//
Offs := AOffset+2*I*N2*2;
K:=0;
while K<=N2-1 do
begin
A[Offs+2*K+1] := A[Offs+2*N2+2*K+0];
Inc(K);
end;
FTBaseExecutePlanRec(A, Offs, Plan, Plan.Plan[EntryOffset+6], StackPtr);
Plan.TmpBuf[0] := A[Offs+0];
Plan.TmpBuf[1] := 0;
Plan.TmpBuf[2*N2+0] := A[Offs+1];
Plan.TmpBuf[2*N2+1] := 0;
K:=1;
while K<=N2-1 do
begin
Offs1 := 2*K;
Offs2 := 2*N2+2*K;
HK := A[Offs+2*K+0];
HNK := A[Offs+2*(N2-K)+0];
Plan.TmpBuf[Offs1+0] := +Double(0.5)*(HK+HNK);
Plan.TmpBuf[Offs2+1] := -Double(0.5)*(HK-HNK);
HK := A[Offs+2*K+1];
HNK := A[Offs+2*(N2-K)+1];
Plan.TmpBuf[Offs2+0] := +Double(0.5)*(HK+HNK);
Plan.TmpBuf[Offs1+1] := +Double(0.5)*(HK-HNK);
Inc(K);
end;
APVMove(@A[0], Offs, Offs+2*N2*2-1, @Plan.TmpBuf[0], 0, 2*N2*2-1);
Inc(I);
end;
if N1 mod 2<>0 then
begin
FTBaseExecutePlanRec(A, AOffset+(N1-1)*N2*2, Plan, Plan.Plan[EntryOffset+6], StackPtr);
end;
FFTTwCalc(A, AOffset, N2, N1);
InternalComplexLinTranspose(A, N1, N2, AOffset, Plan.TmpBuf);
I:=0;
while I<=N2-1 do
begin
FTBaseExecutePlanRec(A, AOffset+I*N1*2, Plan, Plan.Plan[EntryOffset+5], StackPtr);
Inc(I);
end;
InternalComplexLinTranspose(A, N2, N1, AOffset, Plan.TmpBuf);
Exit;
end;
if Plan.Plan[EntryOffset+3]=FHTCooleyTukeyPlan then
begin
//
// Cooley-Tukey FHT plan:
// * transpose \
// * smaller FHT's |
// * pre-process |
// * multiply by twiddle factors | corresponds to multiplication by H1
// * post-process |
// * transpose again /
// * multiply by H2 (smaller FHT's)
// * final transposition
//
// For more details see Vitezslav Vesely, "Fast algorithms
// of Fourier and Hartley transform and their implementation in MATLAB",
// page 31.
//
N1 := Plan.Plan[EntryOffset+1];
N2 := Plan.Plan[EntryOffset+2];
N := N1*N2;
InternalRealLinTranspose(A, N1, N2, AOffset, Plan.TmpBuf);
I:=0;
while I<=N2-1 do
begin
FTBaseExecutePlanRec(A, AOffset+I*N1, Plan, Plan.Plan[EntryOffset+5], StackPtr);
Inc(I);
end;
I:=0;
while I<=N2-1 do
begin
J:=0;
while J<=N1-1 do
begin
OffsA := AOffset+I*N1;
HK := A[OffsA+J];
HNK := A[OffsA+(N1-J) mod N1];
Offs := 2*(I*N1+J);
Plan.TmpBuf[Offs+0] := -Double(0.5)*(HNK-HK);
Plan.TmpBuf[Offs+1] := +Double(0.5)*(HK+HNK);
Inc(J);
end;
Inc(I);
end;
FFTTwCalc(Plan.TmpBuf, 0, N1, N2);
J:=0;
while J<=N1-1 do
begin
A[AOffset+J] := Plan.TmpBuf[2*J+0]+Plan.TmpBuf[2*J+1];
Inc(J);
end;
if N2 mod 2=0 then
begin
Offs := 2*(N2 div 2)*N1;
OffsA := AOffset+N2 div 2*N1;
J:=0;
while J<=N1-1 do
begin
A[OffsA+J] := Plan.TmpBuf[Offs+2*J+0]+Plan.TmpBuf[Offs+2*J+1];
Inc(J);
end;
end;
I:=1;
while I<=(N2+1) div 2-1 do
begin
Offs := 2*I*N1;
Offs2 := 2*(N2-I)*N1;
OffsA := AOffset+I*N1;
J:=0;
while J<=N1-1 do
begin
A[OffsA+J] := Plan.TmpBuf[Offs+2*J+1]+Plan.TmpBuf[Offs2+2*J+0];
Inc(J);
end;
OffsA := AOffset+(N2-I)*N1;
J:=0;
while J<=N1-1 do
begin
A[OffsA+J] := Plan.TmpBuf[Offs+2*J+0]+Plan.TmpBuf[Offs2+2*J+1];
Inc(J);
end;
Inc(I);
end;
InternalRealLinTranspose(A, N2, N1, AOffset, Plan.TmpBuf);
I:=0;
while I<=N1-1 do
begin
FTBaseExecutePlanRec(A, AOffset+I*N2, Plan, Plan.Plan[EntryOffset+6], StackPtr);
Inc(I);
end;
InternalRealLinTranspose(A, N1, N2, AOffset, Plan.TmpBuf);
Exit;
end;
if Plan.Plan[EntryOffset+3]=FHTN2Plan then
begin
//
// Cooley-Tukey FHT plan
//
N1 := Plan.Plan[EntryOffset+1];
N2 := Plan.Plan[EntryOffset+2];
N := N1*N2;
RefFHT(A, N, AOffset);
Exit;
end;
if Plan.Plan[EntryOffset+3]=FFTCodeletPlan then
begin
N1 := Plan.Plan[EntryOffset+1];
N2 := Plan.Plan[EntryOffset+2];
N := N1*N2;
if N=2 then
begin
A0X := A[AOffset+0];
A0Y := A[AOffset+1];
A1X := A[AOffset+2];
A1Y := A[AOffset+3];
V0 := A0X+A1X;
V1 := A0Y+A1Y;
V2 := A0X-A1X;
V3 := A0Y-A1Y;
A[AOffset+0] := V0;
A[AOffset+1] := V1;
A[AOffset+2] := V2;
A[AOffset+3] := V3;
Exit;
end;
if N=3 then
begin
Offs := Plan.Plan[EntryOffset+7];
C1 := Plan.Precomputed[Offs+0];
C2 := Plan.Precomputed[Offs+1];
A0X := A[AOffset+0];
A0Y := A[AOffset+1];
A1X := A[AOffset+2];
A1Y := A[AOffset+3];
A2X := A[AOffset+4];
A2Y := A[AOffset+5];
T1X := A1X+A2X;
T1Y := A1Y+A2Y;
A0X := A0X+T1X;
A0Y := A0Y+T1Y;
M1X := C1*T1X;
M1Y := C1*T1Y;
M2X := C2*(A1Y-A2Y);
M2Y := C2*(A2X-A1X);
S1X := A0X+M1X;
S1Y := A0Y+M1Y;
A1X := S1X+M2X;
A1Y := S1Y+M2Y;
A2X := S1X-M2X;
A2Y := S1Y-M2Y;
A[AOffset+0] := A0X;
A[AOffset+1] := A0Y;
A[AOffset+2] := A1X;
A[AOffset+3] := A1Y;
A[AOffset+4] := A2X;
A[AOffset+5] := A2Y;
Exit;
end;
if N=4 then
begin
A0X := A[AOffset+0];
A0Y := A[AOffset+1];
A1X := A[AOffset+2];
A1Y := A[AOffset+3];
A2X := A[AOffset+4];
A2Y := A[AOffset+5];
A3X := A[AOffset+6];
A3Y := A[AOffset+7];
T1X := A0X+A2X;
T1Y := A0Y+A2Y;
T2X := A1X+A3X;
T2Y := A1Y+A3Y;
M2X := A0X-A2X;
M2Y := A0Y-A2Y;
M3X := A1Y-A3Y;
M3Y := A3X-A1X;
A[AOffset+0] := T1X+T2X;
A[AOffset+1] := T1Y+T2Y;
A[AOffset+4] := T1X-T2X;
A[AOffset+5] := T1Y-T2Y;
A[AOffset+2] := M2X+M3X;
A[AOffset+3] := M2Y+M3Y;
A[AOffset+6] := M2X-M3X;
A[AOffset+7] := M2Y-M3Y;
Exit;
end;
if N=5 then
begin
Offs := Plan.Plan[EntryOffset+7];
C1 := Plan.Precomputed[Offs+0];
C2 := Plan.Precomputed[Offs+1];
C3 := Plan.Precomputed[Offs+2];
C4 := Plan.Precomputed[Offs+3];
C5 := Plan.Precomputed[Offs+4];
T1X := A[AOffset+2]+A[AOffset+8];
T1Y := A[AOffset+3]+A[AOffset+9];
T2X := A[AOffset+4]+A[AOffset+6];
T2Y := A[AOffset+5]+A[AOffset+7];
T3X := A[AOffset+2]-A[AOffset+8];
T3Y := A[AOffset+3]-A[AOffset+9];
T4X := A[AOffset+6]-A[AOffset+4];
T4Y := A[AOffset+7]-A[AOffset+5];
T5X := T1X+T2X;
T5Y := T1Y+T2Y;
A[AOffset+0] := A[AOffset+0]+T5X;
A[AOffset+1] := A[AOffset+1]+T5Y;
M1X := C1*T5X;
M1Y := C1*T5Y;
M2X := C2*(T1X-T2X);
M2Y := C2*(T1Y-T2Y);
M3X := -C3*(T3Y+T4Y);
M3Y := C3*(T3X+T4X);
M4X := -C4*T4Y;
M4Y := C4*T4X;
M5X := -C5*T3Y;
M5Y := C5*T3X;
S3X := M3X-M4X;
S3Y := M3Y-M4Y;
S5X := M3X+M5X;
S5Y := M3Y+M5Y;
S1X := A[AOffset+0]+M1X;
S1Y := A[AOffset+1]+M1Y;
S2X := S1X+M2X;
S2Y := S1Y+M2Y;
S4X := S1X-M2X;
S4Y := S1Y-M2Y;
A[AOffset+2] := S2X+S3X;
A[AOffset+3] := S2Y+S3Y;
A[AOffset+4] := S4X+S5X;
A[AOffset+5] := S4Y+S5Y;
A[AOffset+6] := S4X-S5X;
A[AOffset+7] := S4Y-S5Y;
A[AOffset+8] := S2X-S3X;
A[AOffset+9] := S2Y-S3Y;
Exit;
end;
end;
if Plan.Plan[EntryOffset+3]=FHTCodeletPlan then
begin
N1 := Plan.Plan[EntryOffset+1];
N2 := Plan.Plan[EntryOffset+2];
N := N1*N2;
if N=2 then
begin
A0X := A[AOffset+0];
A1X := A[AOffset+1];
A[AOffset+0] := A0X+A1X;
A[AOffset+1] := A0X-A1X;
Exit;
end;
if N=3 then
begin
Offs := Plan.Plan[EntryOffset+7];
C1 := Plan.Precomputed[Offs+0];
C2 := Plan.Precomputed[Offs+1];
A0X := A[AOffset+0];
A1X := A[AOffset+1];
A2X := A[AOffset+2];
T1X := A1X+A2X;
A0X := A0X+T1X;
M1X := C1*T1X;
M2Y := C2*(A2X-A1X);
S1X := A0X+M1X;
A[AOffset+0] := A0X;
A[AOffset+1] := S1X-M2Y;
A[AOffset+2] := S1X+M2Y;
Exit;
end;
if N=4 then
begin
A0X := A[AOffset+0];
A1X := A[AOffset+1];
A2X := A[AOffset+2];
A3X := A[AOffset+3];
T1X := A0X+A2X;
T2X := A1X+A3X;
M2X := A0X-A2X;
M3Y := A3X-A1X;
A[AOffset+0] := T1X+T2X;
A[AOffset+1] := M2X-M3Y;
A[AOffset+2] := T1X-T2X;
A[AOffset+3] := M2X+M3Y;
Exit;
end;
if N=5 then
begin
Offs := Plan.Plan[EntryOffset+7];
C1 := Plan.Precomputed[Offs+0];
C2 := Plan.Precomputed[Offs+1];
C3 := Plan.Precomputed[Offs+2];
C4 := Plan.Precomputed[Offs+3];
C5 := Plan.Precomputed[Offs+4];
T1X := A[AOffset+1]+A[AOffset+4];
T2X := A[AOffset+2]+A[AOffset+3];
T3X := A[AOffset+1]-A[AOffset+4];
T4X := A[AOffset+3]-A[AOffset+2];
T5X := T1X+T2X;
V0 := A[AOffset+0]+T5X;
A[AOffset+0] := V0;
M2X := C2*(T1X-T2X);
M3Y := C3*(T3X+T4X);
S3Y := M3Y-C4*T4X;
S5Y := M3Y+C5*T3X;
S1X := V0+C1*T5X;
S2X := S1X+M2X;
S4X := S1X-M2X;
A[AOffset+1] := S2X-S3Y;
A[AOffset+2] := S4X-S5Y;
A[AOffset+3] := S4X+S5Y;
A[AOffset+4] := S2X+S3Y;
Exit;
end;
end;
if Plan.Plan[EntryOffset+3]=FFTBluesteinPlan then
begin
//
// Bluestein plan:
// 1. multiply by precomputed coefficients
// 2. make convolution: forward FFT, multiplication by precomputed FFT
// and backward FFT. backward FFT is represented as
//
// invfft(x) = fft(x')'/M
//
// for performance reasons reduction of inverse FFT to
// forward FFT is merged with multiplication of FFT components
// and last stage of Bluestein's transformation.
// 3. post-multiplication by Bluestein factors
//
N := Plan.Plan[EntryOffset+1];
M := Plan.Plan[EntryOffset+4];
Offs := Plan.Plan[EntryOffset+7];
I:=StackPtr+2*N;
while I<=StackPtr+2*M-1 do
begin
Plan.StackBuf[I] := 0;
Inc(I);
end;
OffsP := Offs+2*M;
OffsA := AOffset;
OffsB := StackPtr;
I:=0;
while I<=N-1 do
begin
BX := Plan.Precomputed[OffsP+0];
BY := Plan.Precomputed[OffsP+1];
X := A[OffsA+0];
Y := A[OffsA+1];
Plan.StackBuf[OffsB+0] := X*BX-Y*-BY;
Plan.StackBuf[OffsB+1] := X*-BY+Y*BX;
OffsP := OffsP+2;
OffsA := OffsA+2;
OffsB := OffsB+2;
Inc(I);
end;
FTBaseExecutePlanRec(Plan.StackBuf, StackPtr, Plan, Plan.Plan[EntryOffset+5], StackPtr+2*2*M);
OffsB := StackPtr;
OffsP := Offs;
I:=0;
while I<=M-1 do
begin
X := Plan.StackBuf[OffsB+0];
Y := Plan.StackBuf[OffsB+1];
BX := Plan.Precomputed[OffsP+0];
BY := Plan.Precomputed[OffsP+1];
Plan.StackBuf[OffsB+0] := X*BX-Y*BY;
Plan.StackBuf[OffsB+1] := -(X*BY+Y*BX);
OffsB := OffsB+2;
OffsP := OffsP+2;
Inc(I);
end;
FTBaseExecutePlanRec(Plan.StackBuf, StackPtr, Plan, Plan.Plan[EntryOffset+5], StackPtr+2*2*M);
OffsB := StackPtr;
OffsP := Offs+2*M;
OffsA := AOffset;
I:=0;
while I<=N-1 do
begin
X := +Plan.StackBuf[OffsB+0]/M;
Y := -Plan.StackBuf[OffsB+1]/M;
BX := Plan.Precomputed[OffsP+0];
BY := Plan.Precomputed[OffsP+1];
A[OffsA+0] := X*BX-Y*-BY;
A[OffsA+1] := X*-BY+Y*BX;
OffsP := OffsP+2;
OffsA := OffsA+2;
OffsB := OffsB+2;
Inc(I);
end;
Exit;
end;
end;
(*************************************************************************
Returns good factorization N=N1*N2.
Usually N1<=N2 (but not always - small N's may be exception).
if N1<>1 then N2<>1.
Factorization is chosen depending on task type and codelets we have.
-- ALGLIB --
Copyright 01.05.2009 by Bochkanov Sergey
*************************************************************************)
procedure FTBaseFactorize(N : AlglibInteger;
TaskType : AlglibInteger;
var N1 : AlglibInteger;
var N2 : AlglibInteger);
var
J : AlglibInteger;
begin
N1 := 0;
N2 := 0;
//
// try to find good codelet
//
if N1*N2<>N then
begin
J:=FTBaseCodeletRecommended;
while J>=2 do
begin
if N mod J=0 then
begin
N1 := J;
N2 := N div J;
Break;
end;
Dec(J);
end;
end;
//
// try to factorize N
//
if N1*N2<>N then
begin
J:=FTBaseCodeletRecommended+1;
while J<=N-1 do
begin
if N mod J=0 then
begin
N1 := J;
N2 := N div J;
Break;
end;
Inc(J);
end;
end;
//
// looks like N is prime :(
//
if N1*N2<>N then
begin
N1 := 1;
N2 := N;
end;
//
// normalize
//
if (N2=1) and (N1<>1) then
begin
N2 := N1;
N1 := 1;
end;
end;
(*************************************************************************
Is number smooth?
-- ALGLIB --
Copyright 01.05.2009 by Bochkanov Sergey
*************************************************************************)
function FTBaseIsSmooth(N : AlglibInteger):Boolean;
var
I : AlglibInteger;
begin
I:=2;
while I<=FTBaseMaxSmoothFactor do
begin
while N mod I=0 do
begin
N := N div I;
end;
Inc(I);
end;
Result := N=1;
end;
(*************************************************************************
Returns smallest smooth (divisible only by 2, 3, 5) number that is greater
than or equal to max(N,2)
-- ALGLIB --
Copyright 01.05.2009 by Bochkanov Sergey
*************************************************************************)
function FTBaseFindSmooth(N : AlglibInteger):AlglibInteger;
var
Best : AlglibInteger;
begin
Best := 2;
while Best<N do
begin
Best := 2*Best;
end;
FTBaseFindSmoothRec(N, 1, 2, Best);
Result := Best;
end;
(*************************************************************************
Returns smallest smooth (divisible only by 2, 3, 5) even number that is
greater than or equal to max(N,2)
-- ALGLIB --
Copyright 01.05.2009 by Bochkanov Sergey
*************************************************************************)
function FTBaseFindSmoothEven(N : AlglibInteger):AlglibInteger;
var
Best : AlglibInteger;
begin
Best := 2;
while Best<N do
begin
Best := 2*Best;
end;
FTBaseFindSmoothRec(N, 2, 2, Best);
Result := Best;
end;
(*************************************************************************
Returns estimate of FLOP count for the FFT.
It is only an estimate based on operations count for the PERFECT FFT
and relative inefficiency of the algorithm actually used.
N should be power of 2, estimates are badly wrong for non-power-of-2 N's.
-- ALGLIB --
Copyright 01.05.2009 by Bochkanov Sergey
*************************************************************************)