aboutsummaryrefslogtreecommitdiff
path: root/src/core/CL/cl_kernels/fft.cl
blob: 5f1ef2483b7b0f2cd04df1f335a89179535968cc (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
/*
 * Copyright (c) 2019 ARM Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "helpers.h"

/** Computes the digit reverse stage
 *
 * @param[in]  src_ptr                           Pointer to the source tensor. Supported data types: F32
 * @param[in]  src_stride_x                      Stride of the source tensor in X dimension (in bytes)
 * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_stride_y                      Stride of the source tensor in Y dimension (in bytes)
 * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]  src_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source tensor
 * @param[out] dst_ptr                           Pointer to the destination tensor. Supported data types: same as @p src_ptr
 * @param[in]  dst_stride_x                      Stride of the destination tensor in X dimension (in bytes)
 * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_stride_y                      Stride of the destination tensor in Y dimension (in bytes)
 * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]  dst_step_z                        dst_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
 * @param[in]  idx_ptr                           Pointer to the index tensor. Supported data types: U32
 * @param[in]  idx_stride_x                      Stride of the index tensor in X dimension (in bytes)
 * @param[in]  idx_step_x                        idx_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  idx_offset_first_element_in_bytes The offset of the first element in the index tensor
 */
__kernel void digit_reverse(
    TENSOR3D_DECLARATION(src),
    TENSOR3D_DECLARATION(dst),
    VECTOR_DECLARATION(idx))
{
    // Get tensor pointers
    Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(src);
    Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT(dst);
    Vector   idx = CONVERT_TO_VECTOR_STRUCT(idx);

    const unsigned int iidx = *((__global uint *)(idx.ptr));

    // Load data
    float2 data = vload2(0, (__global float *)tensor3D_offset(&src, iidx, get_global_id(1), get_global_id(2)));

    // Store result
    vstore2(data, 0, (__global float *)dst.ptr);
}

/** Calculates and applies the twiddle factor to a given input.
 *
 * @param[in]     phi   The angle.
 * @param[in,out] input The input on which the factor should be applied.
 */
#define TWIDDLE_FACTOR_MULTIPLICATION(phi, input)  \
    {                                              \
        float2 w, tmp;                             \
        w.x   = native_cos(phi);                   \
        w.y   = native_sin(phi);                   \
        tmp.x = (w.x * input.x) - (w.y * input.y); \
        tmp.y = (w.x * input.y) + (w.y * input.x); \
        input = tmp;                               \
    }

/** Computes radix-2 butterfly unit.
 *
 * @param[in,out] c0 Complex input 0.
 * @param[in,out] c1 Complex input 1.
 */
#define DFT_2(c0, c1) \
    {                 \
        float2 v0;    \
        v0 = c0;      \
        c0 = v0 + c1; \
        c1 = v0 - c1; \
    }

// radix-3 butterfly unit factors
#define SQRT3DIV2 0.86602540378443f

/** Computes radix-3 butterfly unit.
 *
 * @param[in,out] c0 Complex input 0.
 * @param[in,out] c1 Complex input 1.
 * @param[in,out] c2 Complex input 2.
 */
#define DFT_3(c0, c1, c2)                                  \
    {                                                      \
        float2 v0 = c1 + c2;                               \
        float2 v1 = c1 - c2;                               \
        c1.x      = c0.x - 0.5f * v0.x + v1.y * SQRT3DIV2; \
        c1.y      = c0.y - 0.5f * v0.y - v1.x * SQRT3DIV2; \
        c2.x      = c0.x - 0.5f * v0.x - v1.y * SQRT3DIV2; \
        c2.y      = c0.y - 0.5f * v0.y + v1.x * SQRT3DIV2; \
        c0        = c0 + v0;                               \
    }

/**Computes radix-4 butterfly unit.
 *
 * @param[in,out] c0 Complex input 0.
 * @param[in,out] c1 Complex input 1.
 * @param[in,out] c2 Complex input 2.
 * @param[in,out] c3 Complex input 3.
 */
#define DFT_4(c0, c1, c2, c3)  \
    {                          \
        float2 v0, v1, v2, v3; \
        v0   = c0 + c2;        \
        v1   = c1 + c3;        \
        v2   = c0 - c2;        \
        v3.x = c1.y - c3.y;    \
        v3.y = c3.x - c1.x;    \
        c0   = v0 + v1;        \
        c2   = v0 - v1;        \
        c1   = v2 + v3;        \
        c3   = v2 - v3;        \
    }

// radix-5 butterfly unit factors
#define W5_A 0.30901699437494f
#define W5_B 0.95105651629515f
#define W5_C 0.80901699437494f
#define W5_D 0.58778525229247f

/** Computes radix-5 butterfly unit.
 *
 * @param[in,out] c0 Complex input 0.
 * @param[in,out] c1 Complex input 1.
 * @param[in,out] c2 Complex input 2.
 * @param[in,out] c3 Complex input 3.
 * @param[in,out] c4 Complex input 4.
 */
#define DFT_5(c0, c1, c2, c3, c4)                 \
    {                                             \
        float2 v0, v1, v2, v3, v4;                \
        v0 = c0;                                  \
        v1 = W5_A * (c1 + c4) - W5_C * (c2 + c3); \
        v2 = W5_C * (c1 + c4) - W5_A * (c2 + c3); \
        v3 = W5_D * (c1 - c4) - W5_B * (c2 - c3); \
        v4 = W5_B * (c1 - c4) + W5_D * (c2 - c3); \
        c0 = v0 + c1 + c2 + c3 + c4;              \
        c1 = v0 + v1 + (float2)(v4.y, -v4.x);     \
        c2 = v0 - v2 + (float2)(v3.y, -v3.x);     \
        c3 = v0 - v2 + (float2)(-v3.y, v3.x);     \
        c4 = v0 + v1 + (float2)(-v4.y, v4.x);     \
    }

// radix-7 butterfly unit factors
#define W7_A 0.62348980185873f
#define W7_B 0.78183148246802f
#define W7_C 0.22252093395631f
#define W7_D 0.97492791218182f
#define W7_E 0.90096886790241f
#define W7_F 0.43388373911755f

/** Computes radix-7 butterfly unit.
 *
 * @param[in,out] c0 Complex input 0.
 * @param[in,out] c1 Complex input 1.
 * @param[in,out] c2 Complex input 2.
 * @param[in,out] c3 Complex input 3.
 * @param[in,out] c4 Complex input 4.
 * @param[in,out] c5 Complex input 5.
 * @param[in,out] c6 Complex input 6.
 */
#define DFT_7(c0, c1, c2, c3, c4, c5, c6)                            \
    {                                                                \
        float2 v0, v1, v2, v3, v4, v5, v6;                           \
        v0 = c0;                                                     \
        v1 = W7_A * (c1 + c6) - W7_C * (c2 + c5) - W7_E * (c3 + c4); \
        v2 = W7_C * (c1 + c6) + W7_E * (c2 + c5) - W7_A * (c3 + c4); \
        v3 = W7_E * (c1 + c6) - W7_A * (c2 + c5) + W7_C * (c3 + c4); \
        v4 = W7_B * (c1 - c6) + W7_D * (c2 - c5) + W7_F * (c3 - c4); \
        v5 = W7_D * (c1 - c6) - W7_F * (c2 - c5) - W7_B * (c3 - c4); \
        v6 = W7_F * (c1 - c6) - W7_B * (c2 - c5) + W7_D * (c3 - c4); \
        c0 = v0 + c1 + c2 + c3 + c4 + c5 + c6;                       \
        c1 = v0 + v1 + (float2)(v4.y, -v4.x);                        \
        c2 = v0 - v2 + (float2)(v5.y, -v5.x);                        \
        c3 = v0 - v3 + (float2)(v6.y, -v6.x);                        \
        c4 = v0 - v3 + (float2)(-v6.y, v6.x);                        \
        c5 = v0 - v2 + (float2)(-v5.y, v5.x);                        \
        c6 = v0 + v1 + (float2)(-v4.y, v4.x);                        \
    }

/** Computes radix-8 butterfly unit.
 *
 * @param[in,out] c0 Complex input 0.
 * @param[in,out] c1 Complex input 1.
 * @param[in,out] c2 Complex input 2.
 * @param[in,out] c3 Complex input 3.
 * @param[in,out] c4 Complex input 4.
 * @param[in,out] c5 Complex input 5.
 * @param[in,out] c6 Complex input 6.
 * @param[in,out] c7 Complex input 7.
 */
#define DFT_8(c0, c1, c2, c3, c4, c5, c6, c7)  \
    {                                          \
        float2 v0, v1, v2, v3, v4, v5, v6, v7; \
        float2 s0, s1, s2, s3, s4, s5, s6, s7; \
        float2 t0, t1, t2;                     \
        v0   = c0 + c4;                        \
        v1   = c1 + c5;                        \
        v2   = c2 + c6;                        \
        v3   = c3 + c7;                        \
        v4   = c0 - c4;                        \
        v5   = c1 - c5;                        \
        v6   = c2 - c6;                        \
        v7   = c3 - c7;                        \
        s0   = v0 + v2;                        \
        s1   = v1 + v3;                        \
        s2   = v0 - v2;                        \
        s3   = v1 - v3;                        \
        s4.x = v4.x - v6.y;                    \
        s4.y = v4.y + v6.x;                    \
        s5.x = v5.x - v7.y;                    \
        s5.y = v5.y + v7.x;                    \
        s6.x = v4.x + v6.y;                    \
        s6.y = v4.y - v6.x;                    \
        s7.x = v5.x + v7.y;                    \
        s7.y = v5.y - v7.x;                    \
        t0.x = -s3.y;                          \
        t0.y = s3.x;                           \
        t1.x = M_SQRT1_2_F * (s5.x - s5.y);    \
        t1.y = M_SQRT1_2_F * (s5.x + s5.y);    \
        t2.x = -M_SQRT1_2_F * (s7.x + s7.y);   \
        t2.y = M_SQRT1_2_F * (s7.x - s7.y);    \
        c0   = s0 + s1;                        \
        c1   = s6 - t2;                        \
        c2   = s2 - t0;                        \
        c3   = s4 - t1;                        \
        c4   = s0 - s1;                        \
        c5   = s6 + t2;                        \
        c6   = s2 + t0;                        \
        c7   = s4 + t1;                        \
    }

/** Computes the first stage of a radix-2 DFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 */
kernel void fft_radix_2_first_stage_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
)
{
    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
#endif /* IN_PLACE */

    // Load eight complex input values
    float4 data = vload4(0, (__global float *)input.ptr);

    // Compute DFT N = 2
    DFT_2(data.s01, data.s23);

    // Store eight complex output values
    vstore4(data, 0, (__global float *)output.ptr);
}

/** Computes the first stage of a radix-3 DFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 */
kernel void fft_radix_3_first_stage_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
)
{
    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
#endif /* IN_PLACE */

    // Load eight complex input values
    float4 data0 = vload4(0, (__global float *)input.ptr);
    float2 data1 = vload2(0, (__global float *)tensor3D_offset(&input, 2, 0, 0));

    // Compute DFT N = 3
    DFT_3(data0.s01, data0.s23, data1.s01);

    // Store eight complex output values
    vstore4(data0, 0, (__global float *)output.ptr);
    vstore2(data1, 0, (__global float *)tensor3D_offset(&output, 2, 0, 0));
}

/** Computes the first stage of a radix-4 DFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 */
kernel void fft_radix_4_first_stage_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
)
{
    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
#endif /* IN_PLACE */

    // Load eight complex input values
    float8 data = vload8(0, (__global float *)input.ptr);

    // Compute DFT N = 4
    DFT_4(data.s01, data.s23, data.s45, data.s67);

    // Store eight complex output values
    vstore8(data, 0, (__global float *)output.ptr);
}

/** Computes the first stage of a radix-5 DFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 */
kernel void fft_radix_5_first_stage_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
)
{
    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
#endif /* IN_PLACE */

    // Load eight complex input values
    float8 data0 = vload8(0, (__global float *)input.ptr);
    float2 data1 = vload2(0, (__global float *)tensor3D_offset(&input, 4, 0, 0));

    // Compute DFT N = 5
    DFT_5(data0.s01, data0.s23, data0.s45, data0.s67, data1.s01);

    // Store eight complex output values
    vstore8(data0, 0, (__global float *)output.ptr);
    vstore2(data1, 0, (__global float *)tensor3D_offset(&output, 4, 0, 0));
}

/** Computes the first stage of a radix-7 DFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 */
kernel void fft_radix_7_first_stage_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
)
{
    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
#endif /* IN_PLACE */

    // Load eight complex input values
    float8 data0 = vload8(0, (__global float *)input.ptr);
    float4 data1 = vload4(0, (__global float *)tensor3D_offset(&input, 4, 0, 0));
    float2 data2 = vload2(0, (__global float *)tensor3D_offset(&input, 6, 0, 0));

    // Compute DFT N = 7
    DFT_7(data0.s01, data0.s23, data0.s45, data0.s67, data1.s01, data1.s23, data2.s01);

    // Store eight complex output values
    vstore8(data0, 0, (__global float *)output.ptr);
    vstore4(data1, 0, (__global float *)tensor3D_offset(&output, 4, 0, 0));
    vstore2(data2, 0, (__global float *)tensor3D_offset(&output, 6, 0, 0));
}

/** Computes the first stage of a radix-8 DFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 */
kernel void fft_radix_8_first_stage_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
)
{
    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
#endif /* IN_PLACE */

    // Load eight complex input values
    float16 data = vload16(0, (__global float *)input.ptr);

    // Compute DFT N = 8
    DFT_8(data.s01, data.s23, data.s45, data.s67, data.s89, data.sAB, data.sCD, data.sEF);

    // Store eight complex output values
    vstore16(data, 0, (__global float *)output.ptr);
}

/** Computes a stage of a radix-2 FFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[in]     Nx                                   The butterfly span. Products of radix order of previous radix's stage
 * @param[in]     Ni                                   Nx * Ny.
 * @param[in]     exp_const                            Exponent constant
 */
kernel void fft_radix_2_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
    ,
    uint Nx, uint Ni, float exp_const)
{
    // Each work-item computes a single radix-2
    uint kx = get_global_id(0);

    // Compute nx
    uint nx = kx % Nx;

    // Compute n index
    uint n = nx + (kx / Nx) * Ni;

    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(input);
    input.ptr += n * input.stride_x + get_global_id(1) * input.stride_y + get_global_id(2) * input.stride_z;
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(output);
    output.ptr += n * output.stride_x + get_global_id(1) * output.stride_y + get_global_id(2) * output.stride_z;
#endif /* IN_PLACE */

    // Load two complex input values
    float2 c0 = vload2(0, (__global float *)input.ptr);
    float2 c1 = vload2(0, (__global float *)tensor3D_offset(&input, Nx, 0, 0));

    // Compute phi
    float phi = (float)nx * exp_const;

    // Multiply by twiddle factor
    TWIDDLE_FACTOR_MULTIPLICATION(phi, c1);

    // Compute DFT N = 2
    DFT_2(c0, c1);

    // Store two complex output values
    vstore2(c0, 0, (__global float *)output.ptr);
    vstore2(c1, 0, (__global float *)tensor3D_offset(&output, Nx, 0, 0));
}

/** Computes a stage of a radix-3 FFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[in]     Nx                                   The butterfly span. Products of radix order of previous radix's stage
 * @param[in]     Ni                                   Nx * Ny.
 * @param[in]     exp_const                            Exponent constant
 */
kernel void fft_radix_3_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
    ,
    uint Nx, uint Ni, float exp_const)
{
    // Each work-item computes a single radix-3
    uint kx = get_global_id(0);

    // Compute nx
    uint nx = kx % Nx;

    // Compute n index
    uint n = nx + (kx / Nx) * Ni;

    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(input);
    input.ptr += n * input.stride_x + get_global_id(1) * input.stride_y + get_global_id(2) * input.stride_z;
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(output);
    output.ptr += n * output.stride_x + get_global_id(1) * output.stride_y + get_global_id(2) * output.stride_z;
#endif /* IN_PLACE */

    // Load three complex input values
    float2 c0 = vload2(0, (__global float *)input.ptr);
    float2 c1 = vload2(0, (__global float *)tensor3D_offset(&input, Nx, 0, 0));
    float2 c2 = vload2(0, (__global float *)tensor3D_offset(&input, 2 * Nx, 0, 0));

    // Compute phi
    float phi = (float)nx * exp_const;

    // Multiply by twiddle factor
    TWIDDLE_FACTOR_MULTIPLICATION(phi, c1);
    TWIDDLE_FACTOR_MULTIPLICATION(2 * phi, c2);

    // Compute DFT N = 3
    DFT_3(c0, c1, c2);

    // Store three complex output values
    vstore2(c0, 0, (__global float *)output.ptr);
    vstore2(c1, 0, (__global float *)tensor3D_offset(&output, Nx, 0, 0));
    vstore2(c2, 0, (__global float *)tensor3D_offset(&output, 2 * Nx, 0, 0));
}

/** Computes a stage of a radix-4 FFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[in]     Nx                                   The butterfly span. Products of radix order of previous radix's stage
 * @param[in]     Ni                                   Nx * Ny.
 * @param[in]     exp_const                            Exponent constant
 */
kernel void fft_radix_4_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
    ,
    uint Nx, uint Ni, float exp_const)
{
    // Each work-item computes a single radix-4
    uint kx = get_global_id(0);

    // Compute nx
    uint nx = kx % Nx;

    // Compute n index
    uint n = nx + (kx / Nx) * Ni;

    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(input);
    input.ptr += n * input.stride_x + get_global_id(1) * input.stride_y + get_global_id(2) * input.stride_z;
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(output);
    output.ptr += n * output.stride_x + get_global_id(1) * output.stride_y + get_global_id(2) * output.stride_z;
#endif /* IN_PLACE */

    // Load four complex input values
    float2 c0 = vload2(0, (__global float *)input.ptr);
    float2 c1 = vload2(0, (__global float *)tensor3D_offset(&input, Nx, 0, 0));
    float2 c2 = vload2(0, (__global float *)tensor3D_offset(&input, 2 * Nx, 0, 0));
    float2 c3 = vload2(0, (__global float *)tensor3D_offset(&input, 3 * Nx, 0, 0));

    // Compute phi
    float phi = (float)nx * exp_const;

    // Multiply by twiddle factor
    TWIDDLE_FACTOR_MULTIPLICATION(phi, c1);
    TWIDDLE_FACTOR_MULTIPLICATION(2 * phi, c2);
    TWIDDLE_FACTOR_MULTIPLICATION(3 * phi, c3);

    // Compute DFT N = 4
    DFT_4(c0, c1, c2, c3);

    // Store four complex output values
    vstore2(c0, 0, (__global float *)output.ptr);
    vstore2(c1, 0, (__global float *)tensor3D_offset(&output, Nx, 0, 0));
    vstore2(c2, 0, (__global float *)tensor3D_offset(&output, 2 * Nx, 0, 0));
    vstore2(c3, 0, (__global float *)tensor3D_offset(&output, 3 * Nx, 0, 0));
}

/** Computes a stage of a radix-5 FFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[in]     Nx                                   The butterfly span. Products of radix order of previous radix's stage
 * @param[in]     Ni                                   Nx * Ny.
 * @param[in]     exp_const                            Exponent constant
 */
kernel void fft_radix_5_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
    ,
    uint Nx, uint Ni, float exp_const)
{
    // Each work-item computes a single radix-5
    uint kx = get_global_id(0);

    // Compute nx
    uint nx = kx % Nx;

    // Compute n index
    uint n = nx + (kx / Nx) * Ni;

    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(input);
    input.ptr += n * input.stride_x + get_global_id(1) * input.stride_y + get_global_id(2) * input.stride_z;
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(output);
    output.ptr += n * output.stride_x + get_global_id(1) * output.stride_y + get_global_id(2) * output.stride_z;
#endif /* IN_PLACE */

    // Load five complex input values
    float2 c0 = vload2(0, (__global float *)input.ptr);
    float2 c1 = vload2(0, (__global float *)tensor3D_offset(&input, Nx, 0, 0));
    float2 c2 = vload2(0, (__global float *)tensor3D_offset(&input, 2 * Nx, 0, 0));
    float2 c3 = vload2(0, (__global float *)tensor3D_offset(&input, 3 * Nx, 0, 0));
    float2 c4 = vload2(0, (__global float *)tensor3D_offset(&input, 4 * Nx, 0, 0));

    // Compute phi
    float phi = (float)nx * exp_const;

    // Multiply by twiddle factor
    TWIDDLE_FACTOR_MULTIPLICATION(phi, c1);
    TWIDDLE_FACTOR_MULTIPLICATION(2 * phi, c2);
    TWIDDLE_FACTOR_MULTIPLICATION(3 * phi, c3);
    TWIDDLE_FACTOR_MULTIPLICATION(4 * phi, c4);

    // Compute DFT N = 5
    DFT_5(c0, c1, c2, c3, c4);

    // Store five complex output values
    vstore2(c0, 0, (__global float *)output.ptr);
    vstore2(c1, 0, (__global float *)tensor3D_offset(&output, Nx, 0, 0));
    vstore2(c2, 0, (__global float *)tensor3D_offset(&output, 2 * Nx, 0, 0));
    vstore2(c3, 0, (__global float *)tensor3D_offset(&output, 3 * Nx, 0, 0));
    vstore2(c4, 0, (__global float *)tensor3D_offset(&output, 4 * Nx, 0, 0));
}

/** Computes a stage of a radix-7 FFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[in]     Nx                                   The butterfly span. Products of radix order of previous radix's stage
 * @param[in]     Ni                                   Nx * Ny.
 * @param[in]     exp_const                            Exponent constant
 */
kernel void fft_radix_7_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
    ,
    uint Nx, uint Ni, float exp_const)
{
    // Each work-item computes a single radix-7
    uint kx = get_global_id(0);

    // Compute nx
    uint nx = kx % Nx;

    // Compute n index
    uint n = nx + (kx / Nx) * Ni;

    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(input);
    input.ptr += n * input.stride_x + get_global_id(1) * input.stride_y + get_global_id(2) * input.stride_z;
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(output);
    output.ptr += n * output.stride_x + get_global_id(1) * output.stride_y + get_global_id(2) * output.stride_z;
#endif /* IN_PLACE */

    // Load seven complex input values
    float2 c0 = vload2(0, (__global float *)input.ptr);
    float2 c1 = vload2(0, (__global float *)tensor3D_offset(&input, Nx, 0, 0));
    float2 c2 = vload2(0, (__global float *)tensor3D_offset(&input, 2 * Nx, 0, 0));
    float2 c3 = vload2(0, (__global float *)tensor3D_offset(&input, 3 * Nx, 0, 0));
    float2 c4 = vload2(0, (__global float *)tensor3D_offset(&input, 4 * Nx, 0, 0));
    float2 c5 = vload2(0, (__global float *)tensor3D_offset(&input, 5 * Nx, 0, 0));
    float2 c6 = vload2(0, (__global float *)tensor3D_offset(&input, 6 * Nx, 0, 0));

    // Compute phi
    float phi = (float)nx * exp_const;

    // Multiply by twiddle factor
    TWIDDLE_FACTOR_MULTIPLICATION(phi, c1);
    TWIDDLE_FACTOR_MULTIPLICATION(2 * phi, c2);
    TWIDDLE_FACTOR_MULTIPLICATION(3 * phi, c3);
    TWIDDLE_FACTOR_MULTIPLICATION(4 * phi, c4);
    TWIDDLE_FACTOR_MULTIPLICATION(5 * phi, c5);
    TWIDDLE_FACTOR_MULTIPLICATION(6 * phi, c6);

    // Compute DFT N = 7
    DFT_7(c0, c1, c2, c3, c4, c5, c6);

    // Store seven complex output values
    vstore2(c0, 0, (__global float *)output.ptr);
    vstore2(c1, 0, (__global float *)tensor3D_offset(&output, Nx, 0, 0));
    vstore2(c2, 0, (__global float *)tensor3D_offset(&output, 2 * Nx, 0, 0));
    vstore2(c3, 0, (__global float *)tensor3D_offset(&output, 3 * Nx, 0, 0));
    vstore2(c4, 0, (__global float *)tensor3D_offset(&output, 4 * Nx, 0, 0));
    vstore2(c5, 0, (__global float *)tensor3D_offset(&output, 5 * Nx, 0, 0));
    vstore2(c6, 0, (__global float *)tensor3D_offset(&output, 6 * Nx, 0, 0));
}

/** Computes a stage of a radix-8 FFT.
 *
 * @note In order to perform the FFT function "in-place", the pre-processor -DIN_PLACE must be passed at compile time
 *
 * @param[in,out] input_ptr                            Pointer to the source tensor. Supported data types: F32
 * @param[in,out] input_stride_x                       Stride of the source tensor in X dimension (in bytes)
 * @param[in,out] input_step_x                         input_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in,out] input_stride_y                       Stride of the source tensor in Y dimension (in bytes)
 * @param[in,out] input_step_y                         input_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in,out] input_stride_z                       Stride of the source tensor in Z dimension (in bytes)
 * @param[in,out] input_step_z                         input_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in,out] input_offset_first_element_in_bytes  The offset of the first element in the source tensor
 * @param[out]    output_ptr                           Pointer to the destination image. Supported data types: same as @p input_ptr
 * @param[in]     output_stride_x                      Stride of the destination image in X dimension (in bytes)
 * @param[in]     output_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]     output_stride_y                      Stride of the destination image in Y dimension (in bytes)
 * @param[in]     output_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]     output_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]     output_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]     output_offset_first_element_in_bytes The offset of the first element in the destination image
 * @param[in]     Nx                                   The butterfly span. Products of radix order of previous radix's stage
 * @param[in]     Ni                                   Nx * Ny.
 * @param[in]     exp_const                            Exponent constant
 */
kernel void fft_radix_8_axis_0(
    TENSOR3D_DECLARATION(input)
#ifndef IN_PLACE
    ,
    TENSOR3D_DECLARATION(output)
#endif /* not IN_PLACE */
    ,
    uint Nx, uint Ni, float exp_const)
{
    // Each work-item computes a single radix-8
    uint kx = get_global_id(0);

    // Compute nx
    uint nx = kx % Nx;

    // Compute n index
    uint n = nx + (kx / Nx) * Ni;

    // Get tensor pointers
    Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(input);
    input.ptr += n * input.stride_x + get_global_id(1) * input.stride_y + get_global_id(2) * input.stride_z;
#ifdef IN_PLACE
    Tensor3D output = input;
#else  /* IN_PLACE */
    Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(output);
    output.ptr += n * output.stride_x + get_global_id(1) * output.stride_y + get_global_id(2) * output.stride_z;
#endif /* IN_PLACE */

    // Load eight complex input values
    float2 c0 = vload2(0, (__global float *)input.ptr);
    float2 c1 = vload2(0, (__global float *)tensor3D_offset(&input, Nx, 0, 0));
    float2 c2 = vload2(0, (__global float *)tensor3D_offset(&input, 2 * Nx, 0, 0));
    float2 c3 = vload2(0, (__global float *)tensor3D_offset(&input, 3 * Nx, 0, 0));
    float2 c4 = vload2(0, (__global float *)tensor3D_offset(&input, 4 * Nx, 0, 0));
    float2 c5 = vload2(0, (__global float *)tensor3D_offset(&input, 5 * Nx, 0, 0));
    float2 c6 = vload2(0, (__global float *)tensor3D_offset(&input, 6 * Nx, 0, 0));
    float2 c7 = vload2(0, (__global float *)tensor3D_offset(&input, 7 * Nx, 0, 0));

    // Compute phi
    float phi = (float)nx * exp_const;

    // Multiply by twiddle factor
    TWIDDLE_FACTOR_MULTIPLICATION(phi, c1);
    TWIDDLE_FACTOR_MULTIPLICATION(2 * phi, c2);
    TWIDDLE_FACTOR_MULTIPLICATION(3 * phi, c3);
    TWIDDLE_FACTOR_MULTIPLICATION(4 * phi, c4);
    TWIDDLE_FACTOR_MULTIPLICATION(5 * phi, c5);
    TWIDDLE_FACTOR_MULTIPLICATION(6 * phi, c6);
    TWIDDLE_FACTOR_MULTIPLICATION(7 * phi, c7);

    // Compute DFT N = 8
    DFT_8(c0, c1, c2, c3, c4, c5, c6, c7);

    // Store eight complex output values
    vstore2(c0, 0, (__global float *)output.ptr);
    vstore2(c1, 0, (__global float *)tensor3D_offset(&output, Nx, 0, 0));
    vstore2(c2, 0, (__global float *)tensor3D_offset(&output, 2 * Nx, 0, 0));
    vstore2(c3, 0, (__global float *)tensor3D_offset(&output, 3 * Nx, 0, 0));
    vstore2(c4, 0, (__global float *)tensor3D_offset(&output, 4 * Nx, 0, 0));
    vstore2(c5, 0, (__global float *)tensor3D_offset(&output, 5 * Nx, 0, 0));
    vstore2(c6, 0, (__global float *)tensor3D_offset(&output, 6 * Nx, 0, 0));
    vstore2(c7, 0, (__global float *)tensor3D_offset(&output, 7 * Nx, 0, 0));
}