-
Notifications
You must be signed in to change notification settings - Fork 0
/
HalfBand4F64Avx.hpp
418 lines (310 loc) · 11.7 KB
/
HalfBand4F64Avx.hpp
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
/*****************************************************************************
HalfBand4F64Avx.hpp
Author: Laurent de Soras, 2021
--- Legal stuff ---
This program is free software. It comes without any warranty, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://www.wtfpl.net/ for more details.
*Tab=3***********************************************************************/
#if ! defined (hiir_HalfBand4F64Avx_CODEHEADER_INCLUDED)
#define hiir_HalfBand4F64Avx_CODEHEADER_INCLUDED
/*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
#include "hiir/StageProc4F64Avx.h"
#include <cassert>
namespace hiir
{
/*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
template <int NC>
constexpr int HalfBand4F64Avx <NC>::_nbr_chn;
template <int NC>
constexpr int HalfBand4F64Avx <NC>::NBR_COEFS;
template <int NC>
constexpr double HalfBand4F64Avx <NC>::_delay;
/*
==============================================================================
Name: ctor
Throws: Nothing
==============================================================================
*/
template <int NC>
HalfBand4F64Avx <NC>::HalfBand4F64Avx () noexcept
: _bifilter ()
, _phase (0)
{
for (int i = 0; i < NBR_COEFS + 2; ++i)
{
_mm256_store_pd (_bifilter [0] [i]._coef, _mm256_setzero_pd ());
_mm256_store_pd (_bifilter [1] [i]._coef, _mm256_setzero_pd ());
}
clear_buffers ();
}
/*
==============================================================================
Name: set_coefs
Description:
Sets filter coefficients. Generate them with the PolyphaseIir2Designer
class.
Call this function before doing any processing.
Input parameters:
- coef_arr: Array of coefficients. There should be as many coefficients as
mentioned in the class template parameter.
Throws: Nothing
==============================================================================
*/
template <int NC>
void HalfBand4F64Avx <NC>::set_coefs (const double coef_arr []) noexcept
{
assert (coef_arr != nullptr);
for (int i = 0; i < NBR_COEFS; ++i)
{
const auto c = _mm256_set1_pd (DataType (coef_arr [i]));
_mm256_store_pd (_bifilter [0] [i + 2]._coef, c);
_mm256_store_pd (_bifilter [1] [i + 2]._coef, c);
}
}
/*
==============================================================================
Name: process_sample
Description:
Performs a half-band low-pass filtering on a multichannel vector.
Input parameters:
- in: sample from the stream to be filtered.
Returns: filtered vector.
Throws: Nothing
==============================================================================
*/
template <int NC>
__m256d HalfBand4F64Avx <NC>::process_sample (__m256d input) noexcept
{
const auto path_arr = process_2_paths (input);
const auto low = _mm256_add_pd (path_arr._e, path_arr._o);
return low;
}
/*
==============================================================================
Name: process_block
Description:
Performs a half-band low-pass filtering on a block of multichannel vectors.
Input and output blocks may overlap, see assert() for details.
Input parameters:
- in_ptr: Input array, containing nbr_spl vectors.
- nbr_spl: Number of vectors to output, > 0
Output parameters:
- out_ptr: Array for the output vectors, capacity: nbr_spl vectors.
Throws: Nothing
==============================================================================
*/
template <int NC>
void HalfBand4F64Avx <NC>::process_block (double out_ptr [], const double in_ptr [], long nbr_spl) noexcept
{
assert (out_ptr != nullptr);
assert (out_ptr <= in_ptr || out_ptr >= in_ptr + nbr_spl * _nbr_chn);
process_block_2_paths (
out_ptr, nullptr, in_ptr, nbr_spl, store_low, bypass
);
}
/*
==============================================================================
Name: process_sample_hpf
Description:
Performs a half-band high-pass filtering on a multichannel vector.
Input parameters:
- in: sample from the stream to be filtered.
Returns: filtered vector.
Throws: Nothing
==============================================================================
*/
template <int NC>
__m256d HalfBand4F64Avx <NC>::process_sample_hpf (__m256d input) noexcept
{
const auto path_arr = process_2_paths (input);
const auto high = _mm256_sub_pd (path_arr._e, path_arr._o);
return high;
}
/*
==============================================================================
Name: process_block_hpf
Description:
Performs a half-band high-pass filtering on a block of multichannel
vectors.
Input and output blocks may overlap, see assert() for details.
Input parameters:
- in_ptr: Input array, containing nbr_spl vectors.
- nbr_spl: Number of vectors to output, > 0
Output parameters:
- out_ptr: Array for the output vectors, capacity: nbr_spl vectors.
Throws: Nothing
==============================================================================
*/
template <int NC>
void HalfBand4F64Avx <NC>::process_block_hpf (double out_ptr [], const double in_ptr [], long nbr_spl) noexcept
{
assert (out_ptr != nullptr);
assert (out_ptr <= in_ptr || out_ptr >= in_ptr + nbr_spl * _nbr_chn);
process_block_2_paths (
nullptr, out_ptr, in_ptr, nbr_spl, bypass, store_high
);
}
/*
==============================================================================
Name: process_sample_split
Description:
Splits (spectrum-wise) in half a vector from a 4-channel stream. Both part
are results of a low-pass and a high-pass filtering, equivalent to the
output of process_sample() and process_sample_hpf().
Input parameters:
- input: sample from the stream to be filtered.
Output parameters:
- low: output sample, lower part of the spectrum.
- high: output sample, higher part of the spectrum.
Throws: Nothing
==============================================================================
*/
template <int NC>
void HalfBand4F64Avx <NC>::process_sample_split (__m256d &low, __m256d &high, __m256d input) noexcept
{
const auto path_arr = process_2_paths (input);
low = _mm256_add_pd (path_arr._e, path_arr._o);
high = _mm256_sub_pd (path_arr._e, path_arr._o);
}
/*
==============================================================================
Name: process_block_split
Description:
Splits (spectrum-wise) in half a block of 4-channel vectors. Both part are
results of a low-pass and a high-pass filtering, equivalent to the output
of process_sample() and process_sample_hpf().
Input and output blocks may overlap, see assert() for details.
Input parameters:
- in_ptr: Input array, containing nbr_spl vectors.
- nbr_spl: Number of vectors to output, > 0
Output parameters:
- out_l_ptr: Array for the output samples, lower part of the spectrum
Capacity: nbr_spl vectors.
- out_h_ptr: Array for the output samples, higher part of the spectrum.
Capacity: nbr_spl vectors.
Throws: Nothing
==============================================================================
*/
template <int NC>
void HalfBand4F64Avx <NC>::process_block_split (double out_l_ptr [], double out_h_ptr [], const double in_ptr [], long nbr_spl) noexcept
{
assert (out_l_ptr != nullptr);
assert (out_h_ptr != nullptr);
assert (out_l_ptr <= in_ptr || out_l_ptr >= in_ptr + nbr_spl * _nbr_chn);
assert (out_h_ptr <= in_ptr || out_h_ptr >= in_ptr + nbr_spl * _nbr_chn);
assert ( out_l_ptr + nbr_spl * _nbr_chn <= out_h_ptr
|| out_h_ptr + nbr_spl * _nbr_chn <= out_l_ptr);
process_block_2_paths (
out_l_ptr, out_h_ptr, in_ptr, nbr_spl, store_low, store_high
);
}
/*
==============================================================================
Name: clear_buffers
Description:
Clears filter memory, as if it processed silence since an infinite amount
of time.
Throws: Nothing
==============================================================================
*/
template <int NC>
void HalfBand4F64Avx <NC>::clear_buffers () noexcept
{
_phase = 0;
for (int i = 0; i < NBR_COEFS + 2; ++i)
{
_mm256_store_pd (_bifilter [0] [i]._mem, _mm256_setzero_pd ());
_mm256_store_pd (_bifilter [1] [i]._mem, _mm256_setzero_pd ());
}
_mm256_store_pd (_prev, _mm256_setzero_pd ());
}
/*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
/*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
template <int NC>
constexpr int HalfBand4F64Avx <NC>::_nbr_phases;
// Shared processing function, outputs both paths of the all-pass filter pair.
// Returns: even path, odd path
template <int NC>
typename HalfBand4F64Avx <NC>::EvenOdd HalfBand4F64Avx <NC>::process_2_paths (__m256d input) noexcept
{
const auto half = _mm256_set1_pd (0.5f);
input = _mm256_mul_pd (input, half);
auto path_arr = EvenOdd { input, _mm256_load_pd (_prev) };
StageProc4F64Avx <NBR_COEFS>::process_sample_pos (
NBR_COEFS, path_arr._e, path_arr._o, &_bifilter [_phase] [0]
);
_mm256_store_pd (_prev, input);
_phase = 1 - _phase;
return path_arr;
}
// One of the out_l_ptr or out_h_ptr can be nullptr
template <int NC>
template <typename FL, typename FH>
void HalfBand4F64Avx <NC>::process_block_2_paths (double out_l_ptr [], double out_h_ptr [], const double in_ptr [], long nbr_spl, FL fnc_l, FH fnc_h) noexcept
{
assert (in_ptr != nullptr);
assert (nbr_spl > 0);
long pos = 0;
const auto half = _mm256_set1_pd (0.5f);
if (_phase == 1)
{
const auto path_arr = process_2_paths (_mm256_loadu_pd (in_ptr));
fnc_l (out_l_ptr, path_arr._e, path_arr._o);
fnc_h (out_h_ptr, path_arr._e, path_arr._o);
++ pos;
}
const long end = ((nbr_spl - pos) & -_nbr_phases) + pos;
auto prev = _mm256_load_pd (_prev);
while (pos < end)
{
const auto ofs_0 = pos * _nbr_chn;
const auto ofs_1 = ofs_0 + _nbr_chn;
auto input_0 = _mm256_loadu_pd (in_ptr + ofs_0);
auto input_1 = _mm256_loadu_pd (in_ptr + ofs_1);
input_0 = _mm256_mul_pd (input_0, half);
input_1 = _mm256_mul_pd (input_1, half);
auto tmp_0 = input_0;
auto tmp_1 = prev;
StageProc4F64Avx <NBR_COEFS>::process_sample_pos (
NBR_COEFS, tmp_0, tmp_1, &_bifilter [0] [0]
);
auto tmp_2 = input_1;
auto tmp_3 = input_0; // prev
StageProc4F64Avx <NBR_COEFS>::process_sample_pos (
NBR_COEFS, tmp_2, tmp_3, &_bifilter [1] [0]
);
fnc_l (out_l_ptr + ofs_0, tmp_0, tmp_1);
fnc_h (out_h_ptr + ofs_0, tmp_0, tmp_1);
fnc_l (out_l_ptr + ofs_1, tmp_2, tmp_3);
fnc_h (out_h_ptr + ofs_1, tmp_2, tmp_3);
prev = input_1;
pos += 2;
}
_mm256_store_pd (_prev, prev);
if (pos < nbr_spl)
{
assert (pos + 1 == nbr_spl);
const auto ofs = pos * _nbr_chn;
const auto path_arr = process_2_paths (_mm256_loadu_pd (in_ptr + ofs));
fnc_l (out_l_ptr + ofs, path_arr._e, path_arr._o);
fnc_h (out_h_ptr + ofs, path_arr._e, path_arr._o);
}
}
template <int NC>
void HalfBand4F64Avx <NC>::store_low (double *ptr, __m256d tmp_0, __m256d tmp_1) noexcept
{
const auto low = _mm256_add_pd (tmp_0, tmp_1);
_mm256_storeu_pd (ptr, low);
}
template <int NC>
void HalfBand4F64Avx <NC>::store_high (double *ptr, __m256d tmp_0, __m256d tmp_1) noexcept
{
const auto high = _mm256_sub_pd (tmp_0, tmp_1);
_mm256_storeu_pd (ptr, high);
}
} // namespace hiir
#endif // hiir_HalfBand4F64Avx_CODEHEADER_INCLUDED
/*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/