forked from scottransom/psrfits_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fold.c
371 lines (335 loc) · 10.7 KB
/
fold.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* Simple fold routines */
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#ifdef FOLD_USE_INTRINSICS
# include <xmmintrin.h>
# define _MM_LOAD_PS _mm_load_ps
# define _MM_STORE_PS _mm_store_ps
#endif
#include "fold.h"
#include "polyco.h"
void malloc_foldbuf(struct foldbuf *f) {
#ifdef FOLD_USE_INTRINSICS
const int alignment = 64;
if ((f->npol * f->nchan * sizeof(float)) % alignment) {
fprintf(stderr,
"Error: foldbuf dimension are not appropriate for alignment:\n"
" npol=%d nchan=%d\n", f->npol, f->nchan);
exit(1);
}
int rv = posix_memalign((void *)&f->data, alignment,
sizeof(float) * f->nbin * f->npol * f->nchan);
if (rv) {
fprintf(stderr, "Error in posix_memalign");
exit(1);
}
#else
f->data = (float *)malloc(sizeof(float) * f->nbin * f->npol * f->nchan);
#endif
f->count = (unsigned *)malloc(sizeof(unsigned) * f->nbin);
}
void free_foldbuf(struct foldbuf *f) {
if (f->data!=NULL) { free(f->data); f->data=NULL; }
if (f->count!=NULL) { free(f->count); f->count=NULL; }
}
void clear_foldbuf(struct foldbuf *f) {
memset(f->data, 0, sizeof(float) * f->nbin * f->npol * f->nchan);
memset(f->count, 0, sizeof(unsigned) * f->nbin);
}
size_t foldbuf_data_size(const struct foldbuf *f) {
if (f->data==NULL) return(0);
return(sizeof(float) * f->nbin * f->npol * f->nchan);
}
size_t foldbuf_count_size(const struct foldbuf *f) {
if (f->count==NULL) return(0);
return(sizeof(unsigned) * f->nbin);
}
/* Combines unpack and accumulate */
void vector_accumulate_8bit(float *out, const char *in, int n) {
#ifdef FOLD_USE_INTRINSICS
__m128 in_, out_, tmp_;
float ftmp;
int ii;
for (ii = 0 ; ii < (n & -16) ; ii += 16) {
__builtin_prefetch(out + 64, 1, 0);
__builtin_prefetch(in + 64, 0, 0);
out_ = _MM_LOAD_PS(out);
in_ = _mm_cvtpi8_ps(*((__m64 *)in));
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
out_ = _MM_LOAD_PS(out);
in_ = _mm_cvtpi8_ps(*((__m64 *)in));
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
out_ = _MM_LOAD_PS(out);
in_ = _mm_cvtpi8_ps(*((__m64 *)in));
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
out_ = _MM_LOAD_PS(out);
in_ = _mm_cvtpi8_ps(*((__m64 *)in));
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
}
for (; ii < (n & -4) ; ii += 4) {
out_ = _MM_LOAD_PS(out);
in_ = _mm_cvtpi8_ps(*((__m64 *)in));
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
}
for (; ii < n ; ii++) { // Cast these without intrinsics
ftmp = (float)(*in);
out_ = _mm_load_ss(out);
in_ = _mm_load_ss(&ftmp);
tmp_ = _mm_add_ss(out_, in_);
_mm_store_ss(out, tmp_);
in += 1;
out += 1;
}
_mm_empty();
#else
int i;
for (i=0; i<n; i++) { out[i] += (float)in[i]; }
#endif
}
void vector_accumulate_8bit_unsigned(float *out,
const unsigned char *in, int n) {
#ifdef FOLD_USE_INTRINSICS
__m128 in_, out_, tmp_;
float ftmp;
int ii;
for (ii = 0 ; ii < (n & -16) ; ii += 16) {
__builtin_prefetch(out + 64, 1, 0);
__builtin_prefetch(in + 64, 0, 0);
out_ = _MM_LOAD_PS(out);
in_ = _mm_cvtpu8_ps(*((__m64 *)in));
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
out_ = _MM_LOAD_PS(out);
in_ = _mm_cvtpu8_ps(*((__m64 *)in));
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
out_ = _MM_LOAD_PS(out);
in_ = _mm_cvtpu8_ps(*((__m64 *)in));
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
out_ = _MM_LOAD_PS(out);
in_ = _mm_cvtpu8_ps(*((__m64 *)in));
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
}
for (; ii < (n & -4) ; ii += 4) {
out_ = _MM_LOAD_PS(out);
in_ = _mm_cvtpu8_ps(*((__m64 *)in));
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
}
for (; ii < n ; ii++) { // Cast these without intrinsics
ftmp = (float)(*in);
out_ = _mm_load_ss(out);
in_ = _mm_load_ss(&ftmp);
tmp_ = _mm_add_ss(out_, in_);
_mm_store_ss(out, tmp_);
in += 1;
out += 1;
}
_mm_empty();
#else
int i;
for (i=0; i<n; i++) { out[i] += (float)in[i]; }
#endif
}
void vector_accumulate(float *out, const float *in, int n) {
#ifdef FOLD_USE_INTRINSICS
__m128 in_, out_, tmp_;
int ii;
for (ii = 0 ; ii < (n & -16) ; ii += 16) {
__builtin_prefetch(out + 64, 1, 0);
__builtin_prefetch(in + 64, 0, 0);
in_ = _MM_LOAD_PS(in);
out_ = _MM_LOAD_PS(out);
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
in_ = _MM_LOAD_PS(in);
out_ = _MM_LOAD_PS(out);
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
in_ = _MM_LOAD_PS(in);
out_ = _MM_LOAD_PS(out);
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
in_ = _MM_LOAD_PS(in);
out_ = _MM_LOAD_PS(out);
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
}
for (; ii < (n & -4) ; ii += 4) {
in_ = _MM_LOAD_PS(in);
out_ = _MM_LOAD_PS(out);
tmp_ = _mm_add_ps(out_, in_);
_MM_STORE_PS(out, tmp_);
in += 4;
out += 4;
}
for (; ii < n ; ii++) {
in_ = _mm_load_ss(in);
out_ = _mm_load_ss(out);
tmp_ = _mm_add_ss(out_, in_);
_mm_store_ss(out, tmp_);
in += 1;
out += 1;
}
_mm_empty();
#else
int i;
for (i=0; i<n; i++) { out[i] += in[i]; }
#endif
}
int zero_check(const char *dat, int len) {
int i, z=1;
for (i=0; i<len; i++) {
if (dat[i]!='\0') { z=0; break; }
}
return(z);
}
void unpack_8bit(float *out, const char *in, int n) {
int i;
for (i=0; i<n; i++) { out[i] = (float)in[i]; }
}
void unpack_8bit_unsigned(float *out, const unsigned char *in, int n) {
int i;
for (i=0; i<n; i++) { out[i] = (float)in[i]; }
}
void *fold_8bit_power_thread(void *_args) {
struct fold_args *args = (struct fold_args *)_args;
int rv = fold_8bit_power(args->pc, args->imjd, args->fmjd, args->data,
args->nsamp, args->tsamp, args->raw_signed, args->fb);
pthread_exit(&rv);
}
int fold_8bit_power(const struct polyco *pc, int imjd, double fmjd,
const char *data, int nsamp, double tsamp, int raw_signed,
struct foldbuf *f) {
/* Find midtime */
double fmjd_mid = fmjd + nsamp*tsamp/2.0/86400.0;
/* Check polyco set, allow 5% expansion of range */
if (pc_out_of_range_sloppy(pc, imjd, fmjd,1.05)) { return(-1); }
/* Calc phase, phase step */
/* NOTE: Starting sample phase is computed for the middle
* of the first sample, assuming input fmjd refers to
* the rising edge of the first sample given
*/
double dphase=0.0;
double phase = psr_phase(pc, imjd, fmjd + tsamp/2.0/86400.0, NULL, NULL);
phase = fmod(phase, 1.0);
if (phase<0.0) { phase += 1.0; }
psr_phase(pc, imjd, fmjd_mid, &dphase, NULL);
dphase *= tsamp;
/* Fold em */
int i, ibin;
float *fptr;
for (i=0; i<nsamp; i++) {
ibin = (int)(phase * (double)f->nbin);
if (ibin<0) { ibin+=f->nbin; }
if (ibin>=f->nbin) { ibin-=f->nbin; }
fptr = &f->data[ibin*f->nchan*f->npol];
if (zero_check(&data[i*f->nchan*f->npol],f->nchan*f->npol)==0) {
if (raw_signed==1)
vector_accumulate_8bit(fptr,
&data[i*f->nchan*f->npol],
f->nchan*f->npol);
else if (raw_signed==2) {
// First 2 polns are unsigned, last 2 are signed
vector_accumulate_8bit_unsigned(fptr,
&data[i*f->nchan*f->npol],
f->nchan*2);
vector_accumulate_8bit(fptr + 2*f->nchan,
&data[i*f->nchan*f->npol + 2*f->nchan],
f->nchan*2);
} else if (raw_signed==3) {
// First 1 pol is unsigned, last 3 are signed
vector_accumulate_8bit_unsigned(fptr,
&data[i*f->nchan*f->npol],
f->nchan);
vector_accumulate_8bit(fptr + f->nchan,
&data[i*f->nchan*f->npol + f->nchan],
f->nchan*3);
} else
// All unsigned
vector_accumulate_8bit_unsigned(fptr,
(unsigned char *)&data[i*f->nchan*f->npol],
f->nchan*f->npol);
f->count[ibin]++;
}
phase += dphase;
if (phase>1.0) { phase -= 1.0; }
}
return(0);
}
int accumulate_folds(struct foldbuf *ftot, const struct foldbuf *f) {
if (ftot->nbin!=f->nbin || ftot->nchan!=f->nchan || ftot->npol!=f->npol) {
return(-1);
}
int i;
for (i=0; i<f->nbin; i++) { ftot->count[i] += f->count[i]; }
vector_accumulate(ftot->data, f->data, f->nbin * f->nchan * f->npol);
return(0);
}
/* normalize and transpose to psrfits order */
int normalize_transpose_folds(float *out, const struct foldbuf *f) {
int ibin, ii;
for (ibin=0; ibin<f->nbin; ibin++) {
if (f->count[ibin]==0) {
for (ii=0; ii<f->nchan*f->npol; ii++)
out[ibin + ii*f->nbin] = 0.0;
} else {
for (ii=0; ii<f->nchan*f->npol; ii++)
out[ibin + ii*f->nbin] =
f->data[ii + ibin*f->nchan*f->npol] / (float)f->count[ibin];
}
}
return(0);
}
/* Apply a per-channel/poln scale and offset */
/* Note, this only works on pre-normalized fold results! */
int scale_offset_folds(struct foldbuf *f,
const float *scale, const float *offset) {
int ibin, ii;
float tmp;
for (ibin=0; ibin<f->nbin; ibin++) {
for (ii=0; ii<f->nchan*f->npol; ii++) {
tmp = f->data[ii + ibin*f->nchan*f->npol];
f->data[ii + ibin*f->nchan*f->npol] = tmp*scale[ii] +
(float)f->count[ibin]*offset[ii];
}
}
return(0);
}