1 /*
2 * Copyright (c) 2021 Stephanos Ioannidis <root@stephanos.io>
3 * Copyright (C) 2010-2021 ARM Limited or its affiliates. All rights reserved.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #include <zephyr/dsp/dsp.h>
9 #include <zephyr/ztest.h>
10 #include <zephyr/kernel.h>
11 #include <stdlib.h>
12 #include "common/test_common.h"
13
14 #include "q31.pat"
15
16 #define SNR_ERROR_THRESH ((float32_t)100)
17 #define ABS_ERROR_THRESH_Q31 ((q31_t)4)
18 #define ABS_ERROR_THRESH_Q63 ((q63_t)(1 << 17))
19
test_zdsp_add_q31(const DSP_DATA q31_t * input1,const DSP_DATA q31_t * input2,const q31_t * ref,size_t length)20 static void test_zdsp_add_q31(const DSP_DATA q31_t *input1, const DSP_DATA q31_t *input2,
21 const q31_t *ref, size_t length)
22 {
23 DSP_DATA q31_t *output;
24
25 /* Allocate output buffer */
26 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
27 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
28
29 /* Run test function */
30 zdsp_add_q31(input1, input2, output, length);
31
32 /* Validate output */
33 zassert_true(
34 test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
35 ASSERT_MSG_SNR_LIMIT_EXCEED);
36
37 zassert_true(
38 test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
39 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
40
41 /* Free output buffer */
42 free(output);
43 }
44
45 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31, 3, in_com1, in_com2, ref_add, 3);
46 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31, 8, in_com1, in_com2, ref_add, 8);
47 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31, 11, in_com1, in_com2, ref_add, 11);
48 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31, possat, in_maxpos, in_maxpos, ref_add_possat, 9);
49 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31, negsat, in_maxneg, in_maxneg, ref_add_negsat, 9);
50 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31, long, in_com1, in_com2, ref_add,
51 ARRAY_SIZE(in_com1));
52
test_zdsp_add_q31_in_place(const DSP_DATA q31_t * input1,const DSP_DATA q31_t * input2,const q31_t * ref,size_t length)53 static void test_zdsp_add_q31_in_place(const DSP_DATA q31_t *input1, const DSP_DATA q31_t *input2,
54 const q31_t *ref, size_t length)
55 {
56 DSP_DATA q31_t *output;
57
58 /* Allocate output buffer */
59 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
60 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
61
62 /* Copy input data to output*/
63 memcpy(output, input1, length * sizeof(q31_t));
64
65 /* Run test function */
66 zdsp_add_q31(output, input2, output, length);
67
68 /* Validate output */
69 zassert_true(test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
70 ASSERT_MSG_SNR_LIMIT_EXCEED);
71
72 zassert_true(test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
73 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
74
75 /* Free output buffer */
76 free(output);
77 }
78
79 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31_in_place, 3, in_com1, in_com2, ref_add, 3);
80 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31_in_place, 8, in_com1, in_com2, ref_add, 8);
81 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31_in_place, 11, in_com1, in_com2, ref_add, 11);
82 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31_in_place, possat, in_maxpos, in_maxpos,
83 ref_add_possat, 9);
84 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31_in_place, negsat, in_maxneg, in_maxneg,
85 ref_add_negsat, 9);
86 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_add_q31_in_place, long, in_com1, in_com2, ref_add,
87 ARRAY_SIZE(in_com1));
88
test_zdsp_sub_q31(const DSP_DATA q31_t * input1,const DSP_DATA q31_t * input2,const q31_t * ref,size_t length)89 static void test_zdsp_sub_q31(const DSP_DATA q31_t *input1, const DSP_DATA q31_t *input2,
90 const q31_t *ref, size_t length)
91 {
92 DSP_DATA q31_t *output;
93
94 /* Allocate output buffer */
95 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
96 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
97
98 /* Run test function */
99 zdsp_sub_q31(input1, input2, output, length);
100
101 /* Validate output */
102 zassert_true(
103 test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
104 ASSERT_MSG_SNR_LIMIT_EXCEED);
105
106 zassert_true(
107 test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
108 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
109
110 /* Free output buffer */
111 free(output);
112 }
113
114 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31, 3, in_com1, in_com2, ref_sub, 3);
115 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31, 8, in_com1, in_com2, ref_sub, 8);
116 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31, 11, in_com1, in_com2, ref_sub, 11);
117 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31, possat, in_maxpos, in_maxneg, ref_sub_possat, 9);
118 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31, negsat, in_maxneg, in_maxpos, ref_sub_negsat, 9);
119 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31, long, in_com1, in_com2, ref_sub,
120 ARRAY_SIZE(in_com1));
121
test_zdsp_sub_q31_in_place(const DSP_DATA q31_t * input1,const DSP_DATA q31_t * input2,const q31_t * ref,size_t length)122 static void test_zdsp_sub_q31_in_place(const DSP_DATA q31_t *input1, const DSP_DATA q31_t *input2,
123 const q31_t *ref, size_t length)
124 {
125 DSP_DATA q31_t *output;
126
127 /* Allocate output buffer */
128 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
129 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
130
131 /* Copy input data to output*/
132 memcpy(output, input1, length * sizeof(q31_t));
133
134 /* Run test function */
135 zdsp_sub_q31(output, input2, output, length);
136
137 /* Validate output */
138 zassert_true(test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
139 ASSERT_MSG_SNR_LIMIT_EXCEED);
140
141 zassert_true(test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
142 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
143
144 /* Free output buffer */
145 free(output);
146 }
147
148 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31_in_place, 3, in_com1, in_com2, ref_sub, 3);
149 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31_in_place, 8, in_com1, in_com2, ref_sub, 8);
150 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31_in_place, 11, in_com1, in_com2, ref_sub, 11);
151 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31_in_place, possat, in_maxpos, in_maxneg,
152 ref_sub_possat, 9);
153 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31_in_place, negsat, in_maxneg, in_maxpos,
154 ref_sub_negsat, 9);
155 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_sub_q31_in_place, long, in_com1, in_com2, ref_sub,
156 ARRAY_SIZE(in_com1));
157
test_zdsp_mult_q31(const DSP_DATA q31_t * input1,const DSP_DATA q31_t * input2,const q31_t * ref,size_t length)158 static void test_zdsp_mult_q31(const DSP_DATA q31_t *input1, const DSP_DATA q31_t *input2,
159 const q31_t *ref, size_t length)
160 {
161 DSP_DATA q31_t *output;
162
163 /* Allocate output buffer */
164 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
165 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
166
167 /* Run test function */
168 zdsp_mult_q31(input1, input2, output, length);
169
170 /* Validate output */
171 zassert_true(
172 test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
173 ASSERT_MSG_SNR_LIMIT_EXCEED);
174
175 zassert_true(
176 test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
177 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
178
179 /* Free output buffer */
180 free(output);
181 }
182
183 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_mult_q31, 3, in_com1, in_com2, ref_mult, 3);
184 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_mult_q31, 8, in_com1, in_com2, ref_mult, 8);
185 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_mult_q31, 11, in_com1, in_com2, ref_mult, 11);
186 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_mult_q31, possat, in_maxneg2, in_maxneg2, ref_mult_possat,
187 9);
188 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_mult_q31, long, in_com1, in_com2, ref_mult,
189 ARRAY_SIZE(in_com1));
190
test_zdsp_mult_q31_in_place(const DSP_DATA q31_t * input1,const DSP_DATA q31_t * input2,const q31_t * ref,size_t length)191 static void test_zdsp_mult_q31_in_place(const DSP_DATA q31_t *input1, const DSP_DATA q31_t *input2,
192 const q31_t *ref, size_t length)
193 {
194 DSP_DATA q31_t *output;
195
196 /* Allocate output buffer */
197 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
198 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
199
200 /* Copy input data to output*/
201 memcpy(output, input1, length * sizeof(q31_t));
202
203 /* Run test function */
204 zdsp_mult_q31(output, input2, output, length);
205
206 /* Validate output */
207 zassert_true(test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
208 ASSERT_MSG_SNR_LIMIT_EXCEED);
209
210 zassert_true(test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
211 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
212
213 /* Free output buffer */
214 free(output);
215 }
216
217 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_mult_q31_in_place, 3, in_com1, in_com2, ref_mult, 3);
218 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_mult_q31_in_place, 8, in_com1, in_com2, ref_mult, 8);
219 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_mult_q31_in_place, 11, in_com1, in_com2, ref_mult, 11);
220 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_mult_q31_in_place, possat, in_maxneg2, in_maxneg2,
221 ref_mult_possat, 9);
222 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_mult_q31_in_place, long, in_com1, in_com2, ref_mult,
223 ARRAY_SIZE(in_com1));
224
test_zdsp_negate_q31(const DSP_DATA q31_t * input1,const q31_t * ref,size_t length)225 static void test_zdsp_negate_q31(const DSP_DATA q31_t *input1, const q31_t *ref, size_t length)
226 {
227 DSP_DATA q31_t *output;
228
229 /* Allocate output buffer */
230 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
231 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
232
233 /* Run test function */
234 zdsp_negate_q31(input1, output, length);
235
236 /* Validate output */
237 zassert_true(
238 test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
239 ASSERT_MSG_SNR_LIMIT_EXCEED);
240
241 zassert_true(
242 test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
243 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
244
245 /* Free output buffer */
246 free(output);
247 }
248
249 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_negate_q31, 3, in_com1, ref_negate, 3);
250 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_negate_q31, 8, in_com1, ref_negate, 8);
251 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_negate_q31, 11, in_com1, ref_negate, 11);
252 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_negate_q31, possat, in_maxneg2, ref_negate_possat, 9);
253 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_negate_q31, long, in_com1, ref_negate,
254 ARRAY_SIZE(in_com1));
255
test_zdsp_negate_q31_in_place(const DSP_DATA q31_t * input1,const q31_t * ref,size_t length)256 static void test_zdsp_negate_q31_in_place(const DSP_DATA q31_t *input1, const q31_t *ref,
257 size_t length)
258 {
259 DSP_DATA q31_t *output;
260
261 /* Allocate output buffer */
262 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
263 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
264
265 /* Copy input data to output*/
266 memcpy(output, input1, length * sizeof(q31_t));
267
268 /* Run test function */
269 zdsp_negate_q31(output, output, length);
270
271 /* Validate output */
272 zassert_true(test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
273 ASSERT_MSG_SNR_LIMIT_EXCEED);
274
275 zassert_true(test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
276 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
277
278 /* Free output buffer */
279 free(output);
280 }
281
282 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_negate_q31_in_place, 3, in_com1, ref_negate, 3);
283 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_negate_q31_in_place, 8, in_com1, ref_negate, 8);
284 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_negate_q31_in_place, 11, in_com1, ref_negate, 11);
285 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_negate_q31_in_place, possat, in_maxneg2,
286 ref_negate_possat, 9);
287 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_negate_q31_in_place, long, in_com1, ref_negate,
288 ARRAY_SIZE(in_com1));
289
test_zdsp_offset_q31(const DSP_DATA q31_t * input1,q31_t scalar,const q31_t * ref,size_t length)290 static void test_zdsp_offset_q31(const DSP_DATA q31_t *input1, q31_t scalar, const q31_t *ref,
291 size_t length)
292 {
293 DSP_DATA q31_t *output;
294
295 /* Allocate output buffer */
296 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
297 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
298
299 /* Run test function */
300 zdsp_offset_q31(input1, scalar, output, length);
301
302 /* Validate output */
303 zassert_true(
304 test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
305 ASSERT_MSG_SNR_LIMIT_EXCEED);
306
307 zassert_true(
308 test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
309 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
310
311 /* Free output buffer */
312 free(output);
313 }
314
315 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31, 0p5_3, in_com1, 0x40000000, ref_offset, 3);
316 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31, 0p5_8, in_com1, 0x40000000, ref_offset, 8);
317 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31, 0p5_11, in_com1, 0x40000000, ref_offset, 11);
318 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31, possat, in_maxpos, 0x73333333,
319 ref_offset_possat, 9);
320 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31, negsat, in_maxneg, 0x8ccccccd,
321 ref_offset_negsat, 9);
322 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31, long, in_com1, 0x40000000, ref_offset,
323 ARRAY_SIZE(in_com1));
324
test_zdsp_offset_q31_in_place(const DSP_DATA q31_t * input1,q31_t scalar,const q31_t * ref,size_t length)325 static void test_zdsp_offset_q31_in_place(const DSP_DATA q31_t *input1, q31_t scalar,
326 const q31_t *ref, size_t length)
327 {
328 DSP_DATA q31_t *output;
329
330 /* Allocate output buffer */
331 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
332 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
333
334 /* Copy input data to output*/
335 memcpy(output, input1, length * sizeof(q31_t));
336
337 /* Run test function */
338 zdsp_offset_q31(output, scalar, output, length);
339
340 /* Validate output */
341 zassert_true(test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
342 ASSERT_MSG_SNR_LIMIT_EXCEED);
343
344 zassert_true(test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
345 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
346
347 /* Free output buffer */
348 free(output);
349 }
350
351 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31_in_place, 0p5_3, in_com1, 0x40000000,
352 ref_offset, 3);
353 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31_in_place, 0p5_8, in_com1, 0x40000000,
354 ref_offset, 8);
355 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31_in_place, 0p5_11, in_com1, 0x40000000,
356 ref_offset, 11);
357 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31_in_place, possat, in_maxpos, 0x73333333,
358 ref_offset_possat, 9);
359 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31_in_place, negsat, in_maxneg, 0x8ccccccd,
360 ref_offset_negsat, 9);
361 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_offset_q31_in_place, long, in_com1, 0x40000000,
362 ref_offset, ARRAY_SIZE(in_com1));
363
test_zdsp_scale_q31(const DSP_DATA q31_t * input1,q31_t scalar,const q31_t * ref,size_t length)364 static void test_zdsp_scale_q31(const DSP_DATA q31_t *input1, q31_t scalar, const q31_t *ref,
365 size_t length)
366 {
367 DSP_DATA q31_t *output;
368
369 /* Allocate output buffer */
370 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
371 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
372
373 /* Run test function */
374 zdsp_scale_q31(input1, scalar, 0, output, length);
375
376 /* Validate output */
377 zassert_true(
378 test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
379 ASSERT_MSG_SNR_LIMIT_EXCEED);
380
381 zassert_true(
382 test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
383 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
384
385 /* Free output buffer */
386 free(output);
387 }
388
389 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_scale_q31, 0p5_3, in_com1, 0x40000000, ref_scale, 3);
390 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_scale_q31, 0p5_8, in_com1, 0x40000000, ref_scale, 8);
391 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_scale_q31, 0p5_11, in_com1, 0x40000000, ref_scale, 11);
392 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_scale_q31, possat, in_maxneg2, 0x80000000,
393 ref_scale_possat, 9);
394 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_scale_q31, long, in_com1, 0x40000000, ref_scale,
395 ARRAY_SIZE(in_com1));
396
test_zdsp_scale_q31_in_place(const DSP_DATA q31_t * input1,q31_t scalar,const q31_t * ref,size_t length)397 static void test_zdsp_scale_q31_in_place(const DSP_DATA q31_t *input1, q31_t scalar,
398 const q31_t *ref, size_t length)
399 {
400 DSP_DATA q31_t *output;
401
402 /* Allocate output buffer */
403 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
404 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
405
406 /* Copy input data to output*/
407 memcpy(output, input1, length * sizeof(q31_t));
408
409 /* Run test function */
410 zdsp_scale_q31(output, scalar, 0, output, length);
411
412 /* Validate output */
413 zassert_true(test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
414 ASSERT_MSG_SNR_LIMIT_EXCEED);
415
416 zassert_true(test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
417 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
418
419 /* Free output buffer */
420 free(output);
421 }
422
423 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_scale_q31_in_place, 0p5_3, in_com1, 0x40000000, ref_scale,
424 3);
425 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_scale_q31_in_place, 0p5_8, in_com1, 0x40000000, ref_scale,
426 8);
427 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_scale_q31_in_place, 0p5_11, in_com1, 0x40000000,
428 ref_scale, 11);
429 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_scale_q31_in_place, possat, in_maxneg2, 0x80000000,
430 ref_scale_possat, 9);
431 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_scale_q31_in_place, long, in_com1, 0x40000000, ref_scale,
432 ARRAY_SIZE(in_com1));
433
test_zdsp_dot_prod_q31(const DSP_DATA q31_t * input1,const DSP_DATA q31_t * input2,const q63_t * ref,size_t length)434 static void test_zdsp_dot_prod_q31(const DSP_DATA q31_t *input1, const DSP_DATA q31_t *input2,
435 const q63_t *ref, size_t length)
436 {
437 DSP_DATA q63_t *output;
438
439 /* Allocate output buffer */
440 output = (DSP_DATA q63_t *)malloc(length * sizeof(q63_t));
441 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
442
443 /* Run test function */
444 zdsp_dot_prod_q31(input1, input2, length, &output[0]);
445
446 /* Validate output */
447 zassert_true(
448 test_snr_error_q63(1, output, ref, SNR_ERROR_THRESH),
449 ASSERT_MSG_SNR_LIMIT_EXCEED);
450
451 zassert_true(
452 test_near_equal_q63(1, output, ref, ABS_ERROR_THRESH_Q63),
453 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
454
455 /* Free output buffer */
456 free(output);
457 }
458
459 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_dot_prod_q31, 3, in_com1, in_com2, ref_dot_prod_3, 3);
460 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_dot_prod_q31, 8, in_com1, in_com2, ref_dot_prod_4, 8);
461 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_dot_prod_q31, 11, in_com1, in_com2, ref_dot_prod_4n1, 11);
462 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_dot_prod_q31, long, in_com1, in_com2, ref_dot_prod_long,
463 ARRAY_SIZE(in_com1));
464
test_zdsp_abs_q31(const DSP_DATA q31_t * input1,const q31_t * ref,size_t length)465 static void test_zdsp_abs_q31(const DSP_DATA q31_t *input1, const q31_t *ref, size_t length)
466 {
467 DSP_DATA q31_t *output;
468
469 /* Allocate output buffer */
470 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
471 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
472
473 /* Run test function */
474 zdsp_abs_q31(input1, output, length);
475
476 /* Validate output */
477 zassert_true(
478 test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
479 ASSERT_MSG_SNR_LIMIT_EXCEED);
480
481 zassert_true(
482 test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
483 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
484
485 /* Free output buffer */
486 free(output);
487 }
488
489 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_abs_q31, 3, in_com1, ref_abs, 3);
490 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_abs_q31, 8, in_com1, ref_abs, 8);
491 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_abs_q31, 11, in_com1, ref_abs, 11);
492 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_abs_q31, long, in_com1, ref_abs, ARRAY_SIZE(in_com1));
493
test_zdsp_abs_q31_in_place(const DSP_DATA q31_t * input1,const q31_t * ref,size_t length)494 static void test_zdsp_abs_q31_in_place(const DSP_DATA q31_t *input1, const q31_t *ref,
495 size_t length)
496 {
497 DSP_DATA q31_t *output;
498
499 /* Allocate output buffer */
500 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
501 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
502
503 /* Copy input data to output*/
504 memcpy(output, input1, length * sizeof(q31_t));
505
506 /* Run test function */
507 zdsp_abs_q31(output, output, length);
508
509 /* Validate output */
510 zassert_true(test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
511 ASSERT_MSG_SNR_LIMIT_EXCEED);
512
513 zassert_true(test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
514 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
515
516 /* Free output buffer */
517 free(output);
518 }
519
520 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_abs_q31_in_place, 3, in_com1, ref_abs, 3);
521 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_abs_q31_in_place, 8, in_com1, ref_abs, 8);
522 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_abs_q31_in_place, 11, in_com1, ref_abs, 11);
523 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_abs_q31_in_place, long, in_com1, ref_abs,
524 ARRAY_SIZE(in_com1));
525
test_zdsp_shift_q31(const DSP_DATA q31_t * input1,const q31_t * ref,size_t length)526 static void test_zdsp_shift_q31(const DSP_DATA q31_t *input1, const q31_t *ref, size_t length)
527 {
528 DSP_DATA q31_t *output;
529
530 /* Allocate output buffer */
531 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
532 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
533
534 /* Run test function */
535 zdsp_shift_q31(input1, 1, output, length);
536
537 /* Validate output */
538 zassert_true(
539 test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
540 ASSERT_MSG_SNR_LIMIT_EXCEED);
541
542 zassert_true(
543 test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
544 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
545
546 /* Free output buffer */
547 free(output);
548 }
549
550 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_shift_q31, rand, in_rand, ref_shift, 9);
551 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_shift_q31, possat, in_maxpos, ref_shift_possat, 9);
552 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_shift_q31, negsat, in_maxneg, ref_shift_negsat, 9);
553
test_zdsp_shift_q31_in_place(const DSP_DATA q31_t * input1,const q31_t * ref,size_t length)554 static void test_zdsp_shift_q31_in_place(const DSP_DATA q31_t *input1, const q31_t *ref,
555 size_t length)
556 {
557 DSP_DATA q31_t *output;
558
559 /* Allocate output buffer */
560 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
561 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
562
563 /* Copy input data to output*/
564 memcpy(output, input1, length * sizeof(q31_t));
565
566 /* Run test function */
567 zdsp_shift_q31(output, 1, output, length);
568
569 /* Validate output */
570 zassert_true(test_snr_error_q31(length, output, ref, SNR_ERROR_THRESH),
571 ASSERT_MSG_SNR_LIMIT_EXCEED);
572
573 zassert_true(test_near_equal_q31(length, output, ref, ABS_ERROR_THRESH_Q31),
574 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
575
576 /* Free output buffer */
577 free(output);
578 }
579
580 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_shift_q31_in_place, rand, in_rand, ref_shift, 9);
581 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_shift_q31_in_place, possat, in_maxpos, ref_shift_possat,
582 9);
583 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_shift_q31_in_place, negsat, in_maxneg, ref_shift_negsat,
584 9);
585
test_zdsp_and_u32(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)586 static void test_zdsp_and_u32(const DSP_DATA uint32_t *input1, const DSP_DATA uint32_t *input2,
587 const uint32_t *ref, size_t length)
588 {
589 DSP_DATA uint32_t *output;
590
591 /* Allocate output buffer */
592 output = (DSP_DATA uint32_t *)malloc(length * sizeof(uint32_t));
593 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
594
595 /* Run test function */
596 zdsp_and_u32(input1, input2, output, length);
597
598 /* Validate output */
599 zassert_true(
600 test_equal_q31(length, output, ref),
601 ASSERT_MSG_INCORRECT_COMP_RESULT);
602
603 /* Free output buffer */
604 free(output);
605 }
606
607 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_and_u32, 3, in_bitwise1, in_bitwise2, ref_and, 3);
608 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_and_u32, 8, in_bitwise1, in_bitwise2, ref_and, 8);
609 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_and_u32, 11, in_bitwise1, in_bitwise2, ref_and, 11);
610
test_zdsp_and_u32_in_place(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)611 static void test_zdsp_and_u32_in_place(const DSP_DATA uint32_t *input1,
612 const DSP_DATA uint32_t *input2, const uint32_t *ref, size_t length)
613 {
614 DSP_DATA uint32_t *output;
615
616 /* Allocate output buffer */
617 output = (DSP_DATA uint32_t *)malloc(length * sizeof(uint32_t));
618 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
619
620 /* Copy input data to output*/
621 memcpy(output, input1, length * sizeof(q31_t));
622
623 /* Run test function */
624 zdsp_and_u32(output, input2, output, length);
625
626 /* Validate output */
627 zassert_true(test_equal_q31(length, output, ref), ASSERT_MSG_INCORRECT_COMP_RESULT);
628
629 /* Free output buffer */
630 free(output);
631 }
632
633 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_and_u32_in_place, 3, in_bitwise1, in_bitwise2, ref_and,
634 3);
635 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_and_u32_in_place, 8, in_bitwise1, in_bitwise2, ref_and,
636 8);
637 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_and_u32_in_place, 11, in_bitwise1, in_bitwise2, ref_and,
638 11);
639
test_zdsp_or_u32(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)640 static void test_zdsp_or_u32(const DSP_DATA uint32_t *input1, const DSP_DATA uint32_t *input2,
641 const uint32_t *ref, size_t length)
642 {
643 DSP_DATA uint32_t *output;
644
645 /* Allocate output buffer */
646 output = (DSP_DATA uint32_t *)malloc(length * sizeof(uint32_t));
647 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
648
649 /* Run test function */
650 zdsp_or_u32(input1, input2, output, length);
651
652 /* Validate output */
653 zassert_true(
654 test_equal_q31(length, output, ref),
655 ASSERT_MSG_INCORRECT_COMP_RESULT);
656
657 /* Free output buffer */
658 free(output);
659 }
660
661 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_or_u32, 3, in_bitwise1, in_bitwise2, ref_or, 3);
662 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_or_u32, 8, in_bitwise1, in_bitwise2, ref_or, 8);
663 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_or_u32, 11, in_bitwise1, in_bitwise2, ref_or, 11);
664
test_zdsp_or_u32_in_place(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)665 static void test_zdsp_or_u32_in_place(const DSP_DATA uint32_t *input1,
666 const DSP_DATA uint32_t *input2, const uint32_t *ref, size_t length)
667 {
668 DSP_DATA uint32_t *output;
669
670 /* Allocate output buffer */
671 output = (DSP_DATA uint32_t *)malloc(length * sizeof(uint32_t));
672 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
673
674 /* Copy input data to output*/
675 memcpy(output, input1, length * sizeof(q31_t));
676
677 /* Run test function */
678 zdsp_or_u32(output, input2, output, length);
679
680 /* Validate output */
681 zassert_true(test_equal_q31(length, output, ref), ASSERT_MSG_INCORRECT_COMP_RESULT);
682
683 /* Free output buffer */
684 free(output);
685 }
686
687 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_or_u32_in_place, 3, in_bitwise1, in_bitwise2, ref_or, 3);
688 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_or_u32_in_place, 8, in_bitwise1, in_bitwise2, ref_or, 8);
689 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_or_u32_in_place, 11, in_bitwise1, in_bitwise2, ref_or,
690 11);
691
test_zdsp_not_u32(const DSP_DATA uint32_t * input1,const uint32_t * ref,size_t length)692 static void test_zdsp_not_u32(const DSP_DATA uint32_t *input1, const uint32_t *ref, size_t length)
693 {
694 DSP_DATA uint32_t *output;
695
696 /* Allocate output buffer */
697 output = (DSP_DATA uint32_t *)malloc(length * sizeof(uint32_t));
698 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
699
700 /* Run test function */
701 zdsp_not_u32(input1, output, length);
702
703 /* Validate output */
704 zassert_true(
705 test_equal_q31(length, output, ref),
706 ASSERT_MSG_INCORRECT_COMP_RESULT);
707
708 /* Free output buffer */
709 free(output);
710 }
711
712 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_not_u32, 3, in_bitwise1, ref_not, 3);
713 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_not_u32, 8, in_bitwise1, ref_not, 8);
714 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_not_u32, 11, in_bitwise1, ref_not, 11);
715
test_zdsp_not_u32_in_place(const DSP_DATA uint32_t * input1,const uint32_t * ref,size_t length)716 static void test_zdsp_not_u32_in_place(const DSP_DATA uint32_t *input1, const uint32_t *ref,
717 size_t length)
718 {
719 DSP_DATA uint32_t *output;
720
721 /* Allocate output buffer */
722 output = (DSP_DATA uint32_t *)malloc(length * sizeof(uint32_t));
723 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
724
725 /* Copy input data to output*/
726 memcpy(output, input1, length * sizeof(q31_t));
727
728 /* Run test function */
729 zdsp_not_u32(output, output, length);
730
731 /* Validate output */
732 zassert_true(test_equal_q31(length, output, ref), ASSERT_MSG_INCORRECT_COMP_RESULT);
733
734 /* Free output buffer */
735 free(output);
736 }
737
738 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_not_u32_in_place, 3, in_bitwise1, ref_not, 3);
739 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_not_u32_in_place, 8, in_bitwise1, ref_not, 8);
740 DEFINE_TEST_VARIANT3(basic_math_q31, zdsp_not_u32_in_place, 11, in_bitwise1, ref_not, 11);
741
test_zdsp_xor_u32(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)742 static void test_zdsp_xor_u32(const DSP_DATA uint32_t *input1, const DSP_DATA uint32_t *input2,
743 const uint32_t *ref, size_t length)
744 {
745 DSP_DATA uint32_t *output;
746
747 /* Allocate output buffer */
748 output = (DSP_DATA uint32_t *)malloc(length * sizeof(uint32_t));
749 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
750
751 /* Run test function */
752 zdsp_xor_u32(input1, input2, output, length);
753
754 /* Validate output */
755 zassert_true(
756 test_equal_q31(length, output, ref),
757 ASSERT_MSG_INCORRECT_COMP_RESULT);
758
759 /* Free output buffer */
760 free(output);
761 }
762
763 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_xor_u32, 3, in_bitwise1, in_bitwise2, ref_xor, 3);
764 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_xor_u32, 8, in_bitwise1, in_bitwise2, ref_xor, 8);
765 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_xor_u32, 11, in_bitwise1, in_bitwise2, ref_xor, 11);
766
test_zdsp_xor_u32_in_place(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)767 static void test_zdsp_xor_u32_in_place(const DSP_DATA uint32_t *input1,
768 const DSP_DATA uint32_t *input2, const uint32_t *ref, size_t length)
769 {
770 DSP_DATA uint32_t *output;
771
772 /* Allocate output buffer */
773 output = (DSP_DATA uint32_t *)malloc(length * sizeof(uint32_t));
774 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
775
776 /* Copy input data to output*/
777 memcpy(output, input1, length * sizeof(q31_t));
778
779 /* Run test function */
780 zdsp_xor_u32(output, input2, output, length);
781
782 /* Validate output */
783 zassert_true(test_equal_q31(length, output, ref), ASSERT_MSG_INCORRECT_COMP_RESULT);
784
785 /* Free output buffer */
786 free(output);
787 }
788
789 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_xor_u32_in_place, 3, in_bitwise1, in_bitwise2, ref_xor,
790 3);
791 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_xor_u32_in_place, 8, in_bitwise1, in_bitwise2, ref_xor,
792 8);
793 DEFINE_TEST_VARIANT4(basic_math_q31, zdsp_xor_u32_in_place, 11, in_bitwise1, in_bitwise2, ref_xor,
794 11);
795
test_zdsp_clip_q31(const DSP_DATA q31_t * input,const q31_t * ref,q31_t min,q31_t max,size_t length)796 static void test_zdsp_clip_q31(const DSP_DATA q31_t *input, const q31_t *ref, q31_t min, q31_t max,
797 size_t length)
798 {
799 DSP_DATA q31_t *output;
800
801 /* Allocate output buffer */
802 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
803 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
804
805 /* Run test function */
806 zdsp_clip_q31(input, output, min, max, length);
807
808 /* Validate output */
809 zassert_true(
810 test_equal_q31(length, output, ref),
811 ASSERT_MSG_INCORRECT_COMP_RESULT);
812
813 /* Free output buffer */
814 free(output);
815 }
816
817 DEFINE_TEST_VARIANT5(basic_math_q31, zdsp_clip_q31, c0000000_f3333333, in_clip, ref_clip1,
818 0xc0000000, 0xf3333333, ARRAY_SIZE(ref_clip1));
819 DEFINE_TEST_VARIANT5(basic_math_q31, zdsp_clip_q31, c0000000_40000000, in_clip, ref_clip2,
820 0xc0000000, 0x40000000, ARRAY_SIZE(ref_clip2));
821 DEFINE_TEST_VARIANT5(basic_math_q31, zdsp_clip_q31, 0ccccccd_40000000, in_clip, ref_clip3,
822 0x0ccccccd, 0x40000000, ARRAY_SIZE(ref_clip3));
823
test_zdsp_clip_q31_in_place(const DSP_DATA q31_t * input,const q31_t * ref,q31_t min,q31_t max,size_t length)824 static void test_zdsp_clip_q31_in_place(const DSP_DATA q31_t *input, const q31_t *ref, q31_t min,
825 q31_t max, size_t length)
826 {
827 DSP_DATA q31_t *output;
828
829 /* Allocate output buffer */
830 output = (DSP_DATA q31_t *)malloc(length * sizeof(q31_t));
831 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
832
833 /* Copy input data to output*/
834 memcpy(output, input, length * sizeof(q31_t));
835
836 /* Run test function */
837 zdsp_clip_q31(output, output, min, max, length);
838
839 /* Validate output */
840 zassert_true(test_equal_q31(length, output, ref), ASSERT_MSG_INCORRECT_COMP_RESULT);
841
842 /* Free output buffer */
843 free(output);
844 }
845
846 DEFINE_TEST_VARIANT5(basic_math_q31, zdsp_clip_q31_in_place, c0000000_f3333333, in_clip, ref_clip1,
847 0xc0000000, 0xf3333333, ARRAY_SIZE(ref_clip1));
848 DEFINE_TEST_VARIANT5(basic_math_q31, zdsp_clip_q31_in_place, c0000000_40000000, in_clip, ref_clip2,
849 0xc0000000, 0x40000000, ARRAY_SIZE(ref_clip2));
850 DEFINE_TEST_VARIANT5(basic_math_q31, zdsp_clip_q31_in_place, 0ccccccd_40000000, in_clip, ref_clip3,
851 0x0ccccccd, 0x40000000, ARRAY_SIZE(ref_clip3));
852
853 ZTEST_SUITE(basic_math_q31, NULL, NULL, NULL, NULL, NULL);
854