-
Notifications
You must be signed in to change notification settings - Fork 58
/
pf_conv.cpp
322 lines (260 loc) · 8.72 KB
/
pf_conv.cpp
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
#include "pf_conv.h"
#include <string.h>
#include <assert.h>
#include <algorithm>
#if 0
#include <stdio.h>
#define DPRINT(...) fprintf(stderr, __VA_ARGS__)
#else
#define DPRINT(...) do { } while (0)
#endif
#ifdef HAVE_MIPP
#include <mipp.h>
#endif
#ifndef CONV_ARCH_POST
#error CONV_ARCH_POST not defined
#endif
#define PP_STRINGIFY(X) #X
#define PP_TOSTRING(X) PP_STRINGIFY(X)
#define PP_CONCAT_IMPL(x, y) x##y
#define PP_CONCAT(x, y) PP_CONCAT_IMPL( x, y )
#define ARCHFUNCNAME(X) PP_CONCAT(X##_,CONV_ARCH_POST)
const char * ARCHFUNCNAME(id)()
{
return PP_TOSTRING(CONV_ARCH_POST);
}
int ARCHFUNCNAME(conv_float_simd_size)()
{
#if defined(MIPP_NO_INTRINSICS) || !defined(HAVE_MIPP)
// have a completely MIPP independent implementation
return 1;
#else
return mipp::N<float>();
#endif
}
void ARCHFUNCNAME(conv_float_move_rest)(float * RESTRICT s, conv_buffer_state * RESTRICT state)
{
int R = state->size - state->offset; // this many samples from prev conv_float were not processed
if (R > 0)
{
// memmove(s, &s[state->offset], R * sizeof(s[0])); // move them to the begin
std::copy(&s[state->offset], &s[state->size], s);
}
else
R = 0;
state->offset = 0; // data - to be processed - is at begin
state->size = R; // this many unprocessed samples
}
void ARCHFUNCNAME(conv_cplx_move_rest)(complexf * RESTRICT s, conv_buffer_state * RESTRICT state)
{
int R = state->size - state->offset; // this many samples from prev conv_float were not processed
if (R > 0)
{
// memmove(s, &s[state->offset], R * sizeof(s[0])); // move them to the begin
std::copy(&s[state->offset], &s[state->size], s);
}
else
R = 0;
state->offset = 0; // data - to be processed - is at begin
state->size = R; // this many unprocessed samples
}
#if defined(MIPP_NO_INTRINSICS)
// have a completely MIPP independent implementation
// #error missing HAVE_MIPP: there is no MIPP-independent implementation
int ARCHFUNCNAME(conv_float_inplace)(
float * RESTRICT s, conv_buffer_state * RESTRICT state,
const float * RESTRICT filter, const int sz_filter
)
{
const int off0 = state->offset;
const int sz_s = state->size;
int offset;
for ( offset = off0; offset + sz_filter <= sz_s; ++offset)
{
float accu = 0.0F;
for (int k = 0; k < sz_filter; ++k)
accu += s[offset+k] * filter[k];
s[offset] = accu;
}
state->offset = offset;
return offset - off0;
}
int ARCHFUNCNAME(conv_float_oop)(
const float * RESTRICT s, conv_buffer_state * RESTRICT state,
const float * RESTRICT filter, const int sz_filter,
float * RESTRICT y
)
{
const int off0 = state->offset;
const int sz_s = state->size;
int offset;
for ( offset = off0; offset + sz_filter <= sz_s; ++offset)
{
float accu = 0.0F;
for (int k = 0; k < sz_filter; ++k)
accu += s[offset+k] * filter[k];
y[offset] = accu;
}
state->offset = offset;
return offset - off0;
}
int ARCHFUNCNAME(conv_cplx_float_oop)(
const complexf * RESTRICT s_cplx, conv_buffer_state * RESTRICT state,
const float * RESTRICT filter, const int sz_filter,
complexf * RESTRICT y_cplx
)
{
const int off0 = state->offset;
const int sz_s = state->size;
const int sz_f = sz_filter;
int offset;
for ( offset = off0; offset + sz_f <= sz_s; ++offset)
{
float accu_re = 0.0F;
float accu_im = 0.0F;
for (int k = 0; k < sz_filter; ++k)
{
accu_re = s_cplx[offset+k].i * filter[k]; // accu += rS * rH;
accu_im = s_cplx[offset+k].q * filter[k]; // accu += rS * rH;
}
y_cplx[offset].i = accu_re; // == hadd() == sum of real parts
y_cplx[offset].q = accu_im; // == hadd() == sum of imag parts
}
state->offset = offset;
return offset - off0;
}
#elif defined(HAVE_MIPP)
int ARCHFUNCNAME(conv_float_inplace)(
float * RESTRICT s, conv_buffer_state * RESTRICT state,
const float * RESTRICT filter, const int sz_filter
)
{
assert( (sz_filter % mipp::N<float>()) == 0 ); // size of filter must be divisible by conv_float_simd_size()
mipp::Reg<float> accu, rS, rH;
const int off0 = state->offset;
const int sz_s = state->size;
int offset;
for ( offset = off0; offset + sz_filter <= sz_s; ++offset)
{
accu.set0();
for (int k = 0; k < sz_filter; k += mipp::N<float>())
{
rS.load(&s[offset+k]);
rH.load(&filter[k]);
accu = mipp::fmadd(rS, rH, accu); // accu += rS * rH;
}
s[offset] = accu.sum(); // == hadd()
}
state->offset = offset;
return offset - off0;
}
int ARCHFUNCNAME(conv_float_oop)(
const float * RESTRICT s, conv_buffer_state * RESTRICT state,
const float * RESTRICT filter, const int sz_filter,
float * RESTRICT y
)
{
assert( (sz_filter % mipp::N<float>()) == 0 ); // size of filter must be divisible by conv_float_simd_size()
mipp::Reg<float> accu, rS, rH;
const int off0 = state->offset;
const int sz_s = state->size;
int offset;
for ( offset = off0; offset + sz_filter <= sz_s; ++offset)
{
accu.set0();
for (int k = 0; k < sz_filter; k += mipp::N<float>())
{
rS.loadu(&s[offset+k]);
rH.load(&filter[k]);
accu = mipp::fmadd(rS, rH, accu); // accu += rS * rH;
}
y[offset] = accu.sum(); // == hadd()
}
state->offset = offset;
return offset - off0;
}
int ARCHFUNCNAME(conv_cplx_float_oop)(
const complexf * RESTRICT s_cplx, conv_buffer_state * RESTRICT state,
const float * RESTRICT filter, const int sz_filter,
complexf * RESTRICT y_cplx
)
{
assert( (sz_filter % mipp::N<float>()) == 0 ); // size of filter must be divisible by conv_float_simd_size()
const float * RESTRICT s = &(s_cplx[0].i);
float * RESTRICT y = &(y_cplx[0].i);
mipp::Regx2<float> accu_x2, rS_x2, H_x2;
const int off0 = 2 * state->offset;
const int sz_s = 2 * state->size;
const int sz_f2 = 2 * sz_filter;
int offset;
for ( offset = off0; offset + sz_f2 <= sz_s; offset += 2)
{
accu_x2.val[0].set0();
accu_x2.val[1].set0();
for (int k = 0; k < sz_filter; k += mipp::N<float>())
{
mipp::Reg<float> rH;
rS_x2.loadu(&s[offset+2*k]);
rH.load(&filter[k]);
H_x2 = mipp::interleave<float>(rH, rH);
accu_x2.val[0] = mipp::fmadd(rS_x2.val[0], H_x2.val[0], accu_x2.val[0]); // accu += rS * rH;
accu_x2.val[1] = mipp::fmadd(rS_x2.val[1], H_x2.val[1], accu_x2.val[1]); // accu += rS * rH;
}
H_x2 = mipp::deinterleave(accu_x2);
y[offset] = H_x2.val[0].sum(); // == hadd() == sum of real parts
y[offset+1] = H_x2.val[1].sum(); // == hadd() == sum of imag parts
}
state->offset = offset /2;
return (offset - off0) / 2;
}
#endif
static const conv_f_ptrs conv_ptrs =
{
PP_TOSTRING(CONV_ARCH_POST),
#ifndef MIPP_NO_INTRINSICS
1,
#else
0,
#endif
ARCHFUNCNAME(id),
ARCHFUNCNAME(conv_float_simd_size),
#if defined(MIPP_NO_INTRINSICS) || defined(HAVE_MIPP)
ARCHFUNCNAME(conv_float_move_rest),
ARCHFUNCNAME(conv_float_inplace),
ARCHFUNCNAME(conv_float_oop),
ARCHFUNCNAME(conv_cplx_move_rest),
ARCHFUNCNAME(conv_cplx_float_oop)
#else
nullptr,
nullptr,
nullptr,
nullptr,
nullptr
#endif
};
const conv_f_ptrs* ARCHFUNCNAME(conv_ptrs)()
{
DPRINT("arch pointer for '%s':\n", conv_ptrs.id);
if (!strcmp(conv_ptrs.id, "none"))
return &conv_ptrs;
#if defined(MIPP_NO_INTRINSICS)
DPRINT("arch pointer for '%s' - BUT defined(MIPP_NO_INTRINSICS)\n", conv_ptrs.id);
return &conv_ptrs;
#elif defined(HAVE_MIPP)
DPRINT("arch pointer for '%s' - defined(HAVE_MIPP)\n", conv_ptrs.id);
DPRINT("'%s': conv_ptrs.using_mipp %d\n", conv_ptrs.id, conv_ptrs.using_mipp);
DPRINT("'%s': simd_size() %d\n", conv_ptrs.id, conv_ptrs.fp_conv_float_simd_size());
if (conv_ptrs.using_mipp && conv_ptrs.fp_conv_float_simd_size() > 1)
return &conv_ptrs;
else
DPRINT("arch pointer for '%s': HAVE_MIPP BUT using_mipp %d, float_simd_size %d\n", conv_ptrs.id, conv_ptrs.using_mipp, conv_ptrs.fp_conv_float_simd_size());
#else
DPRINT("arch pointer for '%s': neither MIPP_NO_INTRINSICS nor HAVE_MIPP\n", conv_ptrs.id);
#endif
DPRINT("arch pointer for '%s' => nullptr\n", conv_ptrs.id);
return nullptr;
}
#if defined(__cplusplus) && (__cplusplus >= 201703L)
[[maybe_unused]]
#endif
static f_conv_ptrs test_f_ptrs = ARCHFUNCNAME(conv_ptrs);