forked from thesofproject/sof
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrc_hifi2ep.c
More file actions
554 lines (485 loc) · 15 KB
/
src_hifi2ep.c
File metadata and controls
554 lines (485 loc) · 15 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// SPDX-License-Identifier: BSD-3-Clause
//
// Copyright(c) 2017 Intel Corporation. All rights reserved.
//
// Author: Seppo Ingalsuo <seppo.ingalsuo@linux.intel.com>
/* HiFi EP optimized code parts for SRC */
#include "src_config.h"
#if SRC_HIFIEP
#include "src.h"
#include <xtensa/config/defs.h>
#include <xtensa/tie/xt_hifi2.h>
#include <stddef.h>
#include <stdint.h>
/* HiFi EP has
* 4x 56 bit registers in register file Q
* 8x 48 bit registers in register file P
*/
#if SRC_SHORT /* 16 bit coefficients version */
static inline void fir_filter(ae_q32s *rp, const void *cp, ae_q32s *wp0,
const int taps_div_4, const int shift,
const int nch)
{
/* This function uses
* 2x 56 bit registers Q,
* 4x 48 bit registers P
* 3x integers
* 4x address pointers,
*/
ae_q56s a0;
ae_q56s a1;
ae_p24x2f data2;
ae_p24x2f coef2;
ae_p24x2f p0;
ae_p24x2f p1;
ae_p16x2s *coefp;
ae_p24x2f *dp = (ae_p24x2f *)rp;
ae_p24x2f *dp0;
ae_q32s *wp = wp0;
int i;
int j;
const int inc = sizeof(ae_p24x2f);
/* 2ch FIR case */
if (nch == 2) {
/* Move data pointer back by one sample to start from right
* channel sample. Discard read value p0.
*/
AE_LP24F_C(p0, dp, -sizeof(ae_p24f));
/* Reset coefficient pointer and clear accumulator */
coefp = (ae_p16x2s *)cp;
a0 = AE_ZEROQ56();
a1 = AE_ZEROQ56();
/* Compute FIR filter for current channel with four
* taps per every loop iteration. Two coefficients
* are loaded simultaneously. Data is read
* from interleaved buffer with stride of channels
* count.
*/
for (i = 0; i < taps_div_4; i++) {
/* Load two coefficients. Coef2_h contains tap *coefp
* and coef2_l contains the next tap.
*/
coef2 = AE_LP16X2F_I(coefp, 0);
coefp++;
/* Load two data samples from two channels */
AE_LP24X2F_C(p0, dp, inc); /* r0, l0 */
AE_LP24X2F_C(p1, dp, inc); /* r1, l1 */
/* Select to d0 successive left channel samples, to d1
* successive right channel samples. Then accumulate
* data2_h * coef2_h + data2_l * coef2_l. The Q1.31
* data and Q1.15 coefficients are used as 24 bits as
* Q1.23 values.
*/
data2 = AE_SELP24_LL(p0, p1);
AE_MULAAFP24S_HH_LL(a0, data2, coef2);
data2 = AE_SELP24_HH(p0, p1);
AE_MULAAFP24S_HH_LL(a1, data2, coef2);
/* Repeat for next two taps */
coef2 = AE_LP16X2F_I(coefp, 0);
coefp++;
AE_LP24X2F_C(p0, dp, inc); /* r2, l2 */
AE_LP24X2F_C(p1, dp, inc); /* r3, l3 */
data2 = AE_SELP24_LL(p0, p1);
AE_MULAAFP24S_HH_LL(a0, data2, coef2);
data2 = AE_SELP24_HH(p0, p1);
AE_MULAAFP24S_HH_LL(a1, data2, coef2);
}
/* Scale FIR output with right shifts, round/saturate
* to Q1.31, and store 32 bit output.
*/
AE_SQ32F_I(AE_ROUNDSQ32SYM(AE_SRAAQ56(a0, shift)), wp, 0);
AE_SQ32F_I(AE_ROUNDSQ32SYM(AE_SRAAQ56(a1, shift)), wp,
sizeof(int32_t));
return;
}
for (j = 0; j < nch; j++) {
/* Copy pointer and advance to next ch with dummy load */
dp0 = dp;
AE_LP24F_C(p0, dp, -sizeof(ae_p24f));
/* Reset coefficient pointer and clear accumulator */
coefp = (ae_p16x2s *)cp;
a0 = AE_ZEROQ56();
/* Compute FIR filter for current channel with four
* taps per every loop iteration. Two coefficients
* are loaded simultaneously. Data is read
* from interleaved buffer with stride of channels
* count.
*/
for (i = 0; i < taps_div_4; i++) {
/* Load two coefficients */
coef2 = *coefp++;
/* Load two data samples */
AE_LP24F_C(p0, dp0, inc);
AE_LP24F_C(p1, dp0, inc);
/* Pack p0 and p1 to data2_h and data2_l */
data2 = AE_SELP24_LL(p0, p1);
/* Accumulate data2_h * coef2_h + data2_l * coef2_l */
AE_MULAAFP24S_HH_LL(a0, data2, coef2);
/* Repeat for next two filter taps */
coef2 = *coefp++;
AE_LP24F_C(p0, dp0, inc);
AE_LP24F_C(p1, dp0, inc);
data2 = AE_SELP24_LL(p0, p1);
AE_MULAAFP24S_HH_LL(a0, data2, coef2);
}
/* Scale FIR output with right shifts, round/saturate
* to Q1.31, and store 32 bit output. Advance write
* pointer to next sample.
*/
AE_SQ32F_I(AE_ROUNDSQ32SYM(AE_SRAAQ56(a0, shift)), wp, 0);
wp++;
}
}
#else /* 32bit coefficients version */
static inline void fir_filter(ae_q32s *rp, const void *cp, ae_q32s *wp0,
const int taps_div_4, const int shift,
const int nch)
{
/* This function uses
* 2x 56 bit registers Q,
* 4x 48 bit registers P
* 3x integers
* 4x address pointers,
*/
ae_q56s a0;
ae_q56s a1;
ae_p24x2f p0;
ae_p24x2f p1;
ae_p24x2f data2;
ae_p24x2f coef2;
ae_p24x2f *coefp;
ae_p24x2f *dp = (ae_p24x2f *)rp;
ae_p24x2f *dp0;
ae_q32s *wp = wp0;
int i;
int j;
const int inc = sizeof(ae_p24x2f);
/* 2ch FIR case */
if (nch == 2) {
/* Move data pointer back by one sample to start from right
* channel sample. Discard read value p0.
*/
AE_LP24F_C(p0, dp, -sizeof(ae_p24f));
/* Reset coefficient pointer and clear accumulator */
coefp = (ae_p24x2f *)cp;
a0 = AE_ZEROQ56();
a1 = AE_ZEROQ56();
/* Compute FIR filter for current channel with four
* taps per every loop iteration. Two coefficients
* are loaded simultaneously. Data is read
* from interleaved buffer with stride of channels
* count.
*/
for (i = 0; i < taps_div_4; i++) {
/* Load two coefficients. Coef2_h contains tap *coefp
* and coef2_l contains the next tap.
*/
/* TODO: Ensure coefficients are 64 bits aligned */
coef2 = AE_LP24X2F_I(coefp, 0);
coefp++;
/* Load two data samples from two channels */
AE_LP24X2F_C(p0, dp, inc); /* r0, l0 */
AE_LP24X2F_C(p1, dp, inc); /* r1, l1 */
/* Select to d0 successive left channel samples, to d1
* successive right channel samples.
*/
/* Accumulate to m
* data2_h * coef2_h + data2_l * coef2_l. The Q1.31
* data and Q1.15 coefficients are used as 24 bits as
* Q1.23 values.
*/
data2 = AE_SELP24_LL(p0, p1);
AE_MULAAFP24S_HH_LL(a0, data2, coef2);
data2 = AE_SELP24_HH(p0, p1);
AE_MULAAFP24S_HH_LL(a1, data2, coef2);
/* Repeat for next two taps */
coef2 = AE_LP24X2F_I(coefp, 0);
coefp++;
AE_LP24X2F_C(p0, dp, inc); /* r2, l2 */
AE_LP24X2F_C(p1, dp, inc); /* r3, l3 */
data2 = AE_SELP24_LL(p0, p1);
AE_MULAAFP24S_HH_LL(a0, data2, coef2);
data2 = AE_SELP24_HH(p0, p1);
AE_MULAAFP24S_HH_LL(a1, data2, coef2);
}
/* Scale FIR output with right shifts, round/saturate
* to Q1.31, and store 32 bit output.
*/
AE_SQ32F_I(AE_ROUNDSQ32SYM(AE_SRAAQ56(a0, shift)), wp, 0);
AE_SQ32F_I(AE_ROUNDSQ32SYM(AE_SRAAQ56(a1, shift)), wp,
sizeof(int32_t));
return;
}
for (j = 0; j < nch; j++) {
/* Copy pointer and advance to next ch with dummy load */
dp0 = dp;
AE_LP24F_C(p0, dp, -sizeof(ae_p24f));
/* Reset coefficient pointer and clear accumulator */
coefp = (ae_p24x2f *)cp;
a0 = AE_ZEROQ56();
/* Compute FIR filter for current channel with four
* taps per every loop iteration. Two coefficients
* are loaded simultaneously. Data is read
* from interleaved buffer with stride of channels
* count.
*/
for (i = 0; i < taps_div_4; i++) {
/* Load two coefficients */
coef2 = *coefp++;
/* Load two data samples and place them to L and H of
* data2.
*/
AE_LP24F_C(p0, dp0, inc);
AE_LP24F_C(p1, dp0, inc);
data2 = AE_SELP24_LH(p0, p1);
/* Accumulate to m
* data2_h * coef2_h + data2_l * coef2_l. The Q1.31
* data and coefficients are used as the most
* significant 24 bits as Q1.23 values.
*/
AE_MULAAFP24S_HH_LL(a0, data2, coef2);
/* Repeat for next two filter taps */
coef2 = *coefp++;
AE_LP24F_C(p0, dp0, inc);
AE_LP24F_C(p1, dp0, inc);
data2 = AE_SELP24_LH(p0, p1);
AE_MULAAFP24S_HH_LL(a0, data2, coef2);
}
/* Scale FIR output with right shifts, round/saturate
* to Q1.31, and store 32 bit output. Advance write
* pointer to next sample.
*/
AE_SQ32F_I(AE_ROUNDSQ32SYM(AE_SRAAQ56(a0, shift)), wp, 0);
wp++;
}
}
#endif /* 32bit coefficients version */
#if CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE
void src_polyphase_stage_cir(struct src_stage_prm *s)
{
/* This function uses
* 1x 56 bit registers Q,
* 0x 48 bit registers P,
* 16x integers
* 11x address pointers,
*/
ae_q56s q;
ae_q32s *rp;
ae_q32s *wp;
int i;
int n;
int m;
int n_wrap_buf;
int n_min;
struct src_state *fir = s->state;
struct src_stage *cfg = s->stage;
int32_t *fir_end = &fir->fir_delay[fir->fir_delay_size];
int32_t *out_delay_end = &fir->out_delay[fir->out_delay_size];
const char *cp; /* Can be int32_t or int16_t */
const size_t out_size = fir->out_delay_size * sizeof(int32_t);
const int nch = s->nch;
const int nch_x_odm = cfg->odm * nch;
const int blk_in_words = nch * cfg->blk_in;
const int blk_out_words = nch * cfg->num_of_subfilters;
const int sz = sizeof(int32_t);
const int n_sz = -sizeof(int32_t);
const int rewind_sz = sz * nch * (cfg->blk_in + (cfg->num_of_subfilters - 1) * cfg->idm);
const int nch_x_idm_sz = -nch * cfg->idm * sizeof(int32_t);
const int taps_div_4 = cfg->subfilter_length >> 2;
int32_t *x_rptr = (int32_t *)s->x_rptr;
int32_t *y_wptr = (int32_t *)s->y_wptr;
int32_t *x_end_addr = (int32_t *)s->x_end_addr;
int32_t *y_end_addr = (int32_t *)s->y_end_addr;
#if SRC_SHORT
const size_t subfilter_size = cfg->subfilter_length * sizeof(int16_t);
#else
const size_t subfilter_size = cfg->subfilter_length * sizeof(int32_t);
#endif
for (n = 0; n < s->times; n++) {
/* Input data to filter */
m = blk_in_words;
/* Setup circular buffer for FIR input data delay */
AE_SETCBEGIN0(fir->fir_delay);
AE_SETCEND0(fir_end);
while (m > 0) {
/* Number of words until circular wrap */
n_wrap_buf = x_end_addr - x_rptr;
n_min = (m < n_wrap_buf) ? m : n_wrap_buf;
m -= n_min;
for (i = 0; i < n_min; i++) {
/* Load 32 bits sample to accumulator */
q = AE_LQ32F_I((ae_q32s *)x_rptr++, 0);
/* Saturating shift left */
q = AE_SLLASQ56S(q, s->shift);
/* Store to circular buffer, advance pointer */
AE_SQ32F_C(q, (ae_q32s *)fir->fir_wp, n_sz);
}
/* Check for wrap */
src_inc_wrap(&x_rptr, x_end_addr, s->x_size);
}
/* Do filter */
cp = cfg->coefs; /* Reset to 1st coefficient */
rp = (ae_q32s *)fir->fir_wp;
/* Do circular modification to pointer rp by amount of
* rewind to to data start. Loaded value q is discarded.
*/
AE_LQ32F_C(q, (ae_q32s *)rp, rewind_sz);
/* Reset FIR write pointer and compute all polyphase
* sub-filters.
*/
wp = (ae_q32s *)fir->out_rp;
for (i = 0; i < cfg->num_of_subfilters; i++) {
fir_filter(rp, cp, wp, taps_div_4, cfg->shift, nch);
wp += nch_x_odm;
cp += subfilter_size;
src_inc_wrap((int32_t **)&wp, out_delay_end, out_size);
/* Circular advance pointer rp by number of
* channels x input delay multiplier. Loaded value q
* is discarded.
*/
AE_LQ32F_C(q, rp, nch_x_idm_sz);
}
/* Output */
/* Setup circular buffer for SRC out delay access */
AE_SETCBEGIN0(fir->out_delay);
AE_SETCEND0(out_delay_end);
m = blk_out_words;
while (m > 0) {
n_wrap_buf = y_end_addr - y_wptr;
n_min = (m < n_wrap_buf) ? m : n_wrap_buf;
m -= n_min;
for (i = 0; i < n_min; i++) {
/* Circular load followed by shift right
* for optional s24 and linear store
*/
AE_LQ32F_C(q, (ae_q32s *)fir->out_rp, sz);
q = AE_SRAAQ56(q, s->shift);
AE_SQ32F_I(q, (ae_q32s *)y_wptr, 0);
y_wptr++;
}
/* Check wrap */
src_inc_wrap(&y_wptr, y_end_addr, s->y_size);
}
}
s->x_rptr = x_rptr;
s->y_wptr = y_wptr;
}
#endif /* CONFIG_FORMAT_S24LE || CONFIG_FORMAT_S32LE */
#if CONFIG_FORMAT_S16LE
void src_polyphase_stage_cir_s16(struct src_stage_prm *s)
{
/* This function uses
* 0x 56 bit registers Q,
* 1x 48 bit registers P,
* 16x integers
* 11x address pointers,
*/
ae_p24x2s d;
ae_q32s *rp;
ae_q32s *wp;
int i;
int n;
int m;
int n_wrap_buf;
int n_min;
struct src_state *fir = s->state;
struct src_stage *cfg = s->stage;
int32_t *fir_end = &fir->fir_delay[fir->fir_delay_size];
int32_t *out_delay_end = &fir->out_delay[fir->out_delay_size];
const char *cp; /* Can be int32_t or int16_t */
const size_t out_size = fir->out_delay_size * sizeof(int32_t);
const int nch = s->nch;
const int nch_x_odm = cfg->odm * nch;
const int blk_in_words = nch * cfg->blk_in;
const int blk_out_words = nch * cfg->num_of_subfilters;
const int sz = sizeof(int32_t);
const int n_sz = -sizeof(int32_t);
const int rewind_sz = sz * nch * (cfg->blk_in + (cfg->num_of_subfilters - 1) * cfg->idm);
const int nch_x_idm_sz = -nch * cfg->idm * sizeof(int32_t);
const int taps_div_4 = cfg->subfilter_length >> 2;
int16_t *x_rptr = (int16_t *)s->x_rptr;
int16_t *y_wptr = (int16_t *)s->y_wptr;
int16_t *x_end_addr = (int16_t *)s->x_end_addr;
int16_t *y_end_addr = (int16_t *)s->y_end_addr;
#if SRC_SHORT
const size_t subfilter_size = cfg->subfilter_length * sizeof(int16_t);
#else
const size_t subfilter_size = cfg->subfilter_length * sizeof(int32_t);
#endif
for (n = 0; n < s->times; n++) {
/* Input data to filter */
m = blk_in_words;
/* Setup circular buffer for FIR input data delay */
AE_SETCBEGIN0(fir->fir_delay);
AE_SETCEND0(fir_end);
while (m > 0) {
/* Number of words without circular wrap */
n_wrap_buf = x_end_addr - x_rptr;
n_min = (m < n_wrap_buf) ? m : n_wrap_buf;
m -= n_min;
for (i = 0; i < n_min; i++) {
/* Load 16 bits sample to 24 bit register
* and advance read pointer.
*/
d = AE_LP16F_I((ae_p16s *)x_rptr, 0);
x_rptr++;
/* Store to 32 bit circular buffer, advance
* write pointer.
*/
AE_SP24F_L_C(d, (ae_p24f *)fir->fir_wp, n_sz);
}
/* Check for wrap */
src_inc_wrap_s16(&x_rptr, x_end_addr, s->x_size);
}
/* Do filter */
cp = cfg->coefs; /* Reset to 1st coefficient */
rp = (ae_q32s *)fir->fir_wp;
/* Do circular modification to pointer rp by amount of
* rewind to to data start. Loaded value d is discarded.
*/
AE_LP24F_C(d, (ae_p24f *)rp, rewind_sz);
/* Reset FIR output write pointer and compute all polyphase
* sub-filters.
*/
wp = (ae_q32s *)fir->out_rp;
for (i = 0; i < cfg->num_of_subfilters; i++) {
fir_filter(rp, cp, wp, taps_div_4, cfg->shift, nch);
wp += nch_x_odm;
cp += subfilter_size;
src_inc_wrap((int32_t **)&wp, out_delay_end, out_size);
/* Circular advance pointer rp by number of
* channels x input delay multiplier. Loaded value d
* is discarded.
*/
AE_LP24F_C(d, (ae_p24f *)rp, nch_x_idm_sz);
}
/* Output */
/* Setup circular buffer for SRC out delay access */
AE_SETCBEGIN0(fir->out_delay);
AE_SETCEND0(out_delay_end);
m = blk_out_words;
while (m > 0) {
n_wrap_buf = y_end_addr - y_wptr;
n_min = (m < n_wrap_buf) ? m : n_wrap_buf;
m -= n_min;
for (i = 0; i < n_min; i++) {
/* Circular load for 32 bit sample, get 24
* high bits, circular advance pointer.
*/
AE_LP24F_C(d, (ae_p24f *)fir->out_rp, sz);
/* Round Q1.12 value and store as Q1.15 and
* advance pointer.
*/
d = AE_ROUNDSP16SYM(d);
AE_SP16F_L_I(d, (ae_p16s *)y_wptr, 0);
y_wptr++;
}
/* Check wrap */
src_inc_wrap_s16(&y_wptr, y_end_addr, s->y_size);
}
}
s->x_rptr = x_rptr;
s->y_wptr = y_wptr;
}
#endif /* CONFIG_FORMAT_S16LE */
#endif