-
Notifications
You must be signed in to change notification settings - Fork 11
/
fft.metal
279 lines (201 loc) · 7.47 KB
/
fft.metal
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
#include <metal_stdlib>
using namespace metal;
kernel void fft_512_radix_2 (
device float* time_re [[ buffer(0) ]],
device float* time_im [[ buffer(1) ]],
device float* freq_re [[ buffer(2) ]],
device float* freq_im [[ buffer(3) ]],
device const int* shuffled_indices [[ buffer(4) ]],
device const float* cos_table [[ buffer(5) ]],
device const float* sin_table [[ buffer(6) ]],
const uint tid [[ thread_position_in_threadgroup ]]
) {
threadgroup float v_re[512];
threadgroup float v_im[512];
// preparation
threadgroup float cos_cache[256];
threadgroup float sin_cache[256];
if ( tid < 256 ) {
cos_cache[ tid ] = cos_table[ tid ];
}
else {
sin_cache[ tid - 256 ] = sin_table[ tid - 256 ];
}
threadgroup_barrier( mem_flags::mem_threadgroup );
float re_this = time_re[ shuffled_indices[ tid ] ];
float im_this = time_im[ shuffled_indices[ tid ] ];
float tw_re;
float tw_im;
float offset_re;
float offset_im;
// straddle 1
float re_other = simd_shuffle_xor( re_this, 0x1 );
float im_other = simd_shuffle_xor( im_this, 0x1 );
int lane = tid % 2;
if (lane == 0){
re_this = re_this + re_other;
im_this = im_this + im_other;
}
else {
re_this = re_other - re_this;
im_this = im_other - im_this;
}
// straddle 2
re_other = simd_shuffle_xor( re_this, 0x2 );
im_other = simd_shuffle_xor( im_this, 0x2 );
lane = tid % 4;
tw_re = cos_cache[ 0xff & (128 * lane) ];
tw_im = sin_cache[ 0xff & (128 * lane) ];
if ( lane < 2 ) {
offset_re = tw_re * re_other - tw_im * im_other;
offset_im = tw_re * im_other + tw_im * re_other;
re_this = re_this + offset_re;
im_this = im_this + offset_im;
}
else {
offset_re = tw_re * re_this - tw_im * im_this;
offset_im = tw_re * im_this + tw_im * re_this;
re_this = re_other - offset_re;
im_this = im_other - offset_im;
}
// straddle 4
re_other = simd_shuffle_xor( re_this, 0x4 );
im_other = simd_shuffle_xor( im_this, 0x4 );
lane = tid % 8;
tw_re = cos_cache[ 0xff & (64 * lane) ];
tw_im = sin_cache[ 0xff & (64 * lane) ];
if ( lane < 4 ) {
offset_re = tw_re * re_other - tw_im * im_other;
offset_im = tw_re * im_other + tw_im * re_other;
re_this = re_this + offset_re;
im_this = im_this + offset_im;
}
else {
offset_re = tw_re * re_this - tw_im * im_this;
offset_im = tw_re * im_this + tw_im * re_this;
re_this = re_other - offset_re;
im_this = im_other - offset_im;
}
// straddle 8
re_other = simd_shuffle_xor( re_this, 0x8 );
im_other = simd_shuffle_xor( im_this, 0x8 );
lane = tid % 16;
tw_re = cos_cache[ 0xff & (32 * lane) ];
tw_im = sin_cache[ 0xff & (32 * lane) ];
if ( lane < 8 ) {
offset_re = tw_re * re_other - tw_im * im_other;
offset_im = tw_re * im_other + tw_im * re_other;
re_this = re_this + offset_re;
im_this = im_this + offset_im;
}
else {
offset_re = tw_re * re_this - tw_im * im_this;
offset_im = tw_re * im_this + tw_im * re_this;
re_this = re_other - offset_re;
im_this = im_other - offset_im;
}
// straddle 16
re_other = simd_shuffle_xor( re_this, 0x10 );
im_other = simd_shuffle_xor( im_this, 0x10 );
lane = tid % 32;
tw_re = cos_cache[ 0xff & (16 * lane) ];
tw_im = sin_cache[ 0xff & (16 * lane) ];
if ( lane < 16 ) {
offset_re = tw_re * re_other - tw_im * im_other;
offset_im = tw_re * im_other + tw_im * re_other;
re_this = re_this + offset_re;
im_this = im_this + offset_im;
}
else {
offset_re = tw_re * re_this - tw_im * im_this;
offset_im = tw_re * im_this + tw_im * re_this;
re_this = re_other - offset_re;
im_this = im_other - offset_im;
}
v_re[ tid ] = re_this;
v_im[ tid ] = im_this;
threadgroup_barrier( mem_flags::mem_threadgroup );
// straddle 32
lane = tid % 64;
re_other = (lane<32) ? v_re[tid + 32] : v_re[tid - 32];
im_other = (lane<32) ? v_im[tid + 32] : v_im[tid - 32];
tw_re = cos_cache[ 0xff & (8 * lane) ];
tw_im = sin_cache[ 0xff & (8 * lane) ];
if ( lane < 32 ) {
offset_re = tw_re * re_other - tw_im * im_other;
offset_im = tw_re * im_other + tw_im * re_other;
re_this = re_this + offset_re;
im_this = im_this + offset_im;
}
else {
offset_re = tw_re * re_this - tw_im * im_this;
offset_im = tw_re * im_this + tw_im * re_this;
re_this = re_other - offset_re;
im_this = im_other - offset_im;
}
v_re[ tid ] = re_this;
v_im[ tid ] = im_this;
threadgroup_barrier( mem_flags::mem_threadgroup );
// straddle 64
lane = tid % 128;
re_other = (lane<64) ? v_re[tid + 64] : v_re[tid - 64];
im_other = (lane<64) ? v_im[tid + 64] : v_im[tid - 64];
tw_re = cos_cache[ 0xff & (4 * lane) ];
tw_im = sin_cache[ 0xff & (4 * lane) ];
if ( lane < 64 ) {
offset_re = tw_re * re_other - tw_im * im_other;
offset_im = tw_re * im_other + tw_im * re_other;
re_this = re_this + offset_re;
im_this = im_this + offset_im;
}
else {
offset_re = tw_re * re_this - tw_im * im_this;
offset_im = tw_re * im_this + tw_im * re_this;
re_this = re_other - offset_re;
im_this = im_other - offset_im;
}
v_re[ tid ] = re_this;
v_im[ tid ] = im_this;
threadgroup_barrier( mem_flags::mem_threadgroup );
// straddle 128
lane = tid % 256;
re_other = (lane<128) ? v_re[tid + 128] : v_re[tid - 128];
im_other = (lane<128) ? v_im[tid + 128] : v_im[tid - 128];
tw_re = cos_cache[ 0xff & (2 * lane) ];
tw_im = sin_cache[ 0xff & (2 * lane) ];
if ( lane < 128 ) {
offset_re = tw_re * re_other - tw_im * im_other;
offset_im = tw_re * im_other + tw_im * re_other;
re_this = re_this + offset_re;
im_this = im_this + offset_im;
}
else {
offset_re = tw_re * re_this - tw_im * im_this;
offset_im = tw_re * im_this + tw_im * re_this;
re_this = re_other - offset_re;
im_this = im_other - offset_im;
}
v_re[ tid ] = re_this;
v_im[ tid ] = im_this;
threadgroup_barrier( mem_flags::mem_threadgroup );
// straddle 256
lane = tid % 512;
re_other = (lane<256) ? v_re[tid + 256] : v_re[tid - 256];
im_other = (lane<256) ? v_im[tid + 256] : v_im[tid - 256];
tw_re = cos_cache[ 0xff & lane ];
tw_im = sin_cache[ 0xff & lane ];
if ( lane < 256 ) {
offset_re = tw_re * re_other - tw_im * im_other;
offset_im = tw_re * im_other + tw_im * re_other;
re_this = re_this + offset_re;
im_this = im_this + offset_im;
}
else {
offset_re = tw_re * re_this - tw_im * im_this;
offset_im = tw_re * im_this + tw_im * re_this;
re_this = re_other - offset_re;
im_this = im_other - offset_im;
}
freq_re[ tid ] = re_this;
freq_im[ tid ] = im_this;
}