-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfft_ser.c
218 lines (194 loc) · 7.13 KB
/
fft_ser.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <alloca.h>
#include <string.h>
#include "fft_ser.h"
void fft_r2c_1d_finish(double complex * const v, const int n)
{
// Fill in the remaining values using the hermitian property
// v[i] = conj(v[n - i]) for i in [n/2+1,n) where n/2 is rounded downward
double complex *dst = v + n/2 + 1;
double complex *src = v + n - n/2 - 1;
for(int i = n/2 + 1; i < n; ++i) {
*dst++ = conj(*src--);
}
}
void fft_r2c_1d_vec_finish(double complex * const v, const int n, const int vl)
{
// Fill in the remaining values using the hermitian property
double complex *dst = v + (n/2 + 1)*vl;
double complex *src = v + (n - n/2 - 1)*vl;
for(int i = n/2 + 1; i < n; ++i) {
for(int j = 0; j < vl; ++j) {
/* What we want to do is this:
* v[i*vl + j] = conj(v[(n - i)*vl + j]);
*/
*dst++ = conj(*src++);
}
/* Reset the source vector back 2 vector lengths. The inner loop has moved
* us forward one vector length, so we need to subtract that away. The next
* loop should start one vector length before the previous, so we need to
* subtract yet another vector length.
*/
src -= 2*vl;
}
}
void fft_r2c_finish_packed(double complex * const v, int const dims,
const int *const size)
{
// The output we get from an r2c routine is a row major dimensioned matrix of
// the form {size[0],size[1],...,size[dims-1]/2 + 1}. The first thing to do
// is to sscatter this back into an array of size {size[0]...size[dims-1]}
// with some holes in it.
int leading_size = 1;
for(int d = 0; d < dims - 1; ++d) leading_size *= size[d];
int last_len = size[dims - 1]/2 + 1;
// There are size[0]*size[1]*..size[dims-2] "rows" in the matrix. We move the
// data into its final position from the back of the array, so we don't
// overwrite the data we have yet to deal with. Additionally, we don't need
// to worry about the first row, since it's already at index zero.
for(int i = leading_size - 1; i > 0; --i) {
memmove(v + size[dims - 1]*i, v + last_len*i,
last_len*sizeof(complex double));
}
// Now we have the unpacked format
fft_r2c_finish_unpacked(v, dims, size);
}
void fft_r2c_finish_packed_vec(double complex * const v, int const dims,
const int *const size, const int VL)
{
/* The output we get from an r2c routine is a row major dimensioned matrix of
* the form {size[0],size[1],...,size[dims-1]/2 + 1} where each element is a
* VL-length vector. The first thing to do is to scatter this back into an
* array of VL-length vectors, the array being of size
* {size[0]...size[dims-1]} with some holes in it.
*/
int leading_size = 1;
for(int d = 0; d < dims - 1; ++d) leading_size *= size[d];
int last_len = size[dims - 1]/2 + 1;
// There are size[0]*size[1]*..size[dims-2] "rows" in the matrix. We move the
// data into its final position from the back of the array, so we don't
// overwrite the data we have yet to deal with. Additionally, we don't need
// to worry about the first row, since it's already at index zero.
for(int i = leading_size - 1; i > 0; --i) {
memmove(v + size[dims - 1]*i*VL, v + last_len*i*VL,
last_len*VL*sizeof(complex double));
}
// Now we have the unpacked format
fft_r2c_finish_unpacked_vec(v, dims, size, VL);
}
void fft_r2c_finish_unpacked(double complex * const v, int const dims,
const int *const size)
{
const int last_len = size[dims - 1]/2 + 1;
// Now to fill in the remaining values, we need to exploit hermitian
// symmetry. Since we don't know how many dimensions we have, we'll use
// multi-indices for looping.
int *idx = alloca(dims*sizeof(int));
/* Cache the stride in each dimension. In row major format, the last dimension
* is fastest, ie, stored contiguously, so initialize this stride to one. For
* successive dimensions, going backward, the stride increases by a factor of
* the length of the preceeding dimension.
*/
int *stride = alloca(dims*sizeof(int));
stride[dims - 1] = 1;
for(int d = dims - 2; d >= 0; --d) {
stride[d] = stride[d + 1]*size[d + 1];
}
// Initialize the multi index to the first value that needs to be filled.
idx[dims - 1] = last_len;
for(int d = dims - 2; d >= 0; --d) {
idx[d] = 0;
}
// If we should continue the iteration
int cont = 1;
for(int d = 0; d < dims; ++d) {
cont = cont && idx[d] < size[d];
}
double complex *dst = v + last_len;
double complex *src = v + size[dims - 1] - last_len;
while(cont) {
*dst++ = conj(*src--);
// Increment the multi-index. We increment the last index so that we write
// to contiguous memory blocks.
++idx[dims-1];
for(int d = dims - 1; d >= 1; --d) {
if(idx[d] >= size[d]) {
if(d == dims - 1) {
idx[d] = last_len;
dst += last_len;
src += size[d] - last_len;
} else {
idx[d] = 0;
}
// If we're about to increment a zero index, we need to wrap to the
// highest hyperrow in this dimension.
if(idx[d-1] == 0) {
src += size[d-1]*stride[d-1];
}
// Wrap to the next line
src -= stride[d - 1];
++idx[d-1];
}
}
cont = idx[0] < size[0];
}
}
void fft_r2c_finish_unpacked_vec(double complex * const v, int const dims,
const int *const size, const int VL)
{
const int last_len = size[dims - 1]/2 + 1;
// Now to fill in the remaining values, we need to exploit hermitian
// symmetry. Since we don't know how many dimensions we have, we'll use
// multi-indices for looping.
int *idx = alloca(dims*sizeof(int));
/* Cache the stride in each dimension. In row major format, the last dimension
* is fastest, ie, stored contiguously, so initialize this stride to one. For
* successive dimensions, going backward, the stride increases by a factor of
* the length of the preceeding dimension.
*/
int *stride = alloca(dims*sizeof(int));
stride[dims - 1] = VL;
for(int d = dims - 2; d >= 0; --d) {
stride[d] = stride[d + 1]*size[d + 1];
}
// Initialize the multi index to the first value that needs to be filled.
idx[dims - 1] = last_len;
for(int d = dims - 2; d >= 0; --d) {
idx[d] = 0;
}
// If we should continue the iteration
int cont = 1;
for(int d = 0; d < dims; ++d) {
cont = cont && idx[d] < size[d];
}
double complex *dst = v + last_len*VL;
double complex *src = v + (size[dims - 1] - last_len)*VL;
while(cont) {
for(int j = 0; j < VL; ++j) {
*dst++ = conj(*src++);
}
src -= 2*VL;
// Increment the multi-index. We increment the last index so that we write
// to contiguous memory blocks.
++idx[dims-1];
for(int d = dims - 1; d >= 1; --d) {
if(idx[d] >= size[d]) {
if(d == dims - 1) {
idx[d] = last_len;
dst += last_len*VL;
src += (size[d] - last_len)*VL;
} else {
idx[d] = 0;
}
// If we're about to increment a zero index, we need to wrap to the
// highest hyperrow in this dimension.
if(idx[d-1] == 0) {
src += size[d-1]*stride[d-1];
}
// Wrap to the next line
src -= stride[d - 1];
++idx[d-1];
}
}
cont = idx[0] < size[0];
}
}