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 "f32.pat"
15
16 #define SNR_ERROR_THRESH ((float32_t)120)
17 #define REL_ERROR_THRESH (5.0e-5)
18
test_zdsp_add_f32(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)19 static void test_zdsp_add_f32(const DSP_DATA uint32_t *input1, const DSP_DATA uint32_t *input2,
20 const uint32_t *ref, size_t length)
21 {
22 DSP_DATA float32_t *output;
23
24 /* Allocate output buffer */
25 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
26 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
27
28 /* Run test function */
29 zdsp_add_f32((DSP_DATA float32_t *)input1, (DSP_DATA float32_t *)input2, output, length);
30
31 /* Validate output */
32 zassert_true(
33 test_snr_error_f32(length, output, (float32_t *)ref,
34 SNR_ERROR_THRESH),
35 ASSERT_MSG_SNR_LIMIT_EXCEED);
36
37 zassert_true(
38 test_rel_error_f32(length, output, (float32_t *)ref,
39 REL_ERROR_THRESH),
40 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
41
42 /* Free output buffer */
43 free(output);
44 }
45
46 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_add_f32, 3, in_com1, in_com2, ref_add, 3);
47 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_add_f32, 8, in_com1, in_com2, ref_add, 8);
48 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_add_f32, 11, in_com1, in_com2, ref_add, 11);
49 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_add_f32, long, in_com1, in_com2, ref_add,
50 ARRAY_SIZE(in_com1));
51
test_zdsp_add_f32_in_place(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)52 static void test_zdsp_add_f32_in_place(const DSP_DATA uint32_t *input1,
53 const DSP_DATA uint32_t *input2, const uint32_t *ref, size_t length)
54 {
55 DSP_DATA float32_t *output;
56
57 /* Allocate output buffer */
58 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
59 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
60
61 /* Copy input data to output*/
62 memcpy(output, input1, length * sizeof(float32_t));
63
64 /* Run test function */
65 zdsp_add_f32(output, (DSP_DATA float32_t *)input2, output, length);
66
67 /* Validate output */
68 zassert_true(test_snr_error_f32(length, output, (float32_t *)ref, SNR_ERROR_THRESH),
69 ASSERT_MSG_SNR_LIMIT_EXCEED);
70
71 zassert_true(test_rel_error_f32(length, output, (float32_t *)ref, REL_ERROR_THRESH),
72 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
73
74 /* Free output buffer */
75 free(output);
76 }
77
78 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_add_f32_in_place, 3, in_com1, in_com2, ref_add, 3);
79 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_add_f32_in_place, 8, in_com1, in_com2, ref_add, 8);
80 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_add_f32_in_place, 11, in_com1, in_com2, ref_add, 11);
81 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_add_f32_in_place, long, in_com1, in_com2, ref_add,
82 ARRAY_SIZE(in_com1));
83
test_zdsp_sub_f32(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)84 static void test_zdsp_sub_f32(const DSP_DATA uint32_t *input1, const DSP_DATA uint32_t *input2,
85 const uint32_t *ref, size_t length)
86 {
87 DSP_DATA float32_t *output;
88
89 /* Allocate output buffer */
90 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
91 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
92
93 /* Run test function */
94 zdsp_sub_f32((DSP_DATA float32_t *)input1, (DSP_DATA float32_t *)input2, output, length);
95
96 /* Validate output */
97 zassert_true(
98 test_snr_error_f32(length, output, (float32_t *)ref,
99 SNR_ERROR_THRESH),
100 ASSERT_MSG_SNR_LIMIT_EXCEED);
101
102 zassert_true(
103 test_rel_error_f32(length, output, (float32_t *)ref,
104 REL_ERROR_THRESH),
105 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
106
107 /* Free output buffer */
108 free(output);
109 }
110
111 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_sub_f32, 3, in_com1, in_com2, ref_sub, 3);
112 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_sub_f32, 8, in_com1, in_com2, ref_sub, 8);
113 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_sub_f32, 11, in_com1, in_com2, ref_sub, 11);
114 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_sub_f32, long, in_com1, in_com2, ref_sub,
115 ARRAY_SIZE(in_com1));
116
test_zdsp_sub_f32_in_place(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)117 static void test_zdsp_sub_f32_in_place(const DSP_DATA uint32_t *input1,
118 const DSP_DATA uint32_t *input2, const uint32_t *ref, size_t length)
119 {
120 DSP_DATA float32_t *output;
121
122 /* Allocate output buffer */
123 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
124 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
125
126 /* Copy input data to output*/
127 memcpy(output, input1, length * sizeof(float32_t));
128
129 /* Run test function */
130 zdsp_sub_f32(output, (DSP_DATA float32_t *)input2, output, length);
131
132 /* Validate output */
133 zassert_true(test_snr_error_f32(length, output, (float32_t *)ref, SNR_ERROR_THRESH),
134 ASSERT_MSG_SNR_LIMIT_EXCEED);
135
136 zassert_true(test_rel_error_f32(length, output, (float32_t *)ref, REL_ERROR_THRESH),
137 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
138
139 /* Free output buffer */
140 free(output);
141 }
142
143 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_sub_f32_in_place, 3, in_com1, in_com2, ref_sub, 3);
144 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_sub_f32_in_place, 8, in_com1, in_com2, ref_sub, 8);
145 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_sub_f32_in_place, 11, in_com1, in_com2, ref_sub, 11);
146 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_sub_f32_in_place, long, in_com1, in_com2, ref_sub,
147 ARRAY_SIZE(in_com1));
148
test_zdsp_mult_f32(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)149 static void test_zdsp_mult_f32(const DSP_DATA uint32_t *input1, const DSP_DATA uint32_t *input2,
150 const uint32_t *ref, size_t length)
151 {
152 DSP_DATA float32_t *output;
153
154 /* Allocate output buffer */
155 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
156 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
157
158 /* Run test function */
159 zdsp_mult_f32((DSP_DATA float32_t *)input1, (DSP_DATA float32_t *)input2, output, length);
160
161 /* Validate output */
162 zassert_true(
163 test_snr_error_f32(length, output, (float32_t *)ref,
164 SNR_ERROR_THRESH),
165 ASSERT_MSG_SNR_LIMIT_EXCEED);
166
167 zassert_true(
168 test_rel_error_f32(length, output, (float32_t *)ref,
169 REL_ERROR_THRESH),
170 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
171
172 /* Free output buffer */
173 free(output);
174 }
175
176 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_mult_f32, 3, in_com1, in_com2, ref_mult, 3);
177 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_mult_f32, 8, in_com1, in_com2, ref_mult, 8);
178 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_mult_f32, 11, in_com1, in_com2, ref_mult, 11);
179 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_mult_f32, long, in_com1, in_com2, ref_mult,
180 ARRAY_SIZE(in_com1));
181
test_zdsp_mult_f32_in_place(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)182 static void test_zdsp_mult_f32_in_place(const DSP_DATA uint32_t *input1,
183 const DSP_DATA uint32_t *input2, const uint32_t *ref, size_t length)
184 {
185 DSP_DATA float32_t *output;
186
187 /* Allocate output buffer */
188 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
189 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
190
191 /* Copy input data to output*/
192 memcpy(output, input1, length * sizeof(float32_t));
193
194 /* Run test function */
195 zdsp_mult_f32(output, (DSP_DATA float32_t *)input2, output, length);
196
197 /* Validate output */
198 zassert_true(test_snr_error_f32(length, output, (float32_t *)ref, SNR_ERROR_THRESH),
199 ASSERT_MSG_SNR_LIMIT_EXCEED);
200
201 zassert_true(test_rel_error_f32(length, output, (float32_t *)ref, REL_ERROR_THRESH),
202 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
203
204 /* Free output buffer */
205 free(output);
206 }
207
208 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_mult_f32_in_place, 3, in_com1, in_com2, ref_mult, 3);
209 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_mult_f32_in_place, 8, in_com1, in_com2, ref_mult, 8);
210 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_mult_f32_in_place, 11, in_com1, in_com2, ref_mult, 11);
211 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_mult_f32_in_place, long, in_com1, in_com2, ref_mult,
212 ARRAY_SIZE(in_com1));
213
test_zdsp_negate_f32(const DSP_DATA uint32_t * input1,const uint32_t * ref,size_t length)214 static void test_zdsp_negate_f32(const DSP_DATA uint32_t *input1, const uint32_t *ref,
215 size_t length)
216 {
217 DSP_DATA float32_t *output;
218
219 /* Allocate output buffer */
220 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
221 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
222
223 /* Run test function */
224 zdsp_negate_f32((DSP_DATA float32_t *)input1, output, length);
225
226 /* Validate output */
227 zassert_true(
228 test_snr_error_f32(length, output, (float32_t *)ref,
229 SNR_ERROR_THRESH),
230 ASSERT_MSG_SNR_LIMIT_EXCEED);
231
232 zassert_true(
233 test_rel_error_f32(length, output, (float32_t *)ref,
234 REL_ERROR_THRESH),
235 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
236
237 /* Free output buffer */
238 free(output);
239 }
240
241 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_negate_f32, 3, in_com1, ref_negate, 3);
242 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_negate_f32, 8, in_com1, ref_negate, 8);
243 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_negate_f32, 11, in_com1, ref_negate, 11);
244 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_negate_f32, long, in_com1, ref_negate,
245 ARRAY_SIZE(in_com1));
246
test_zdsp_negate_f32_in_place(const DSP_DATA uint32_t * input1,const uint32_t * ref,size_t length)247 static void test_zdsp_negate_f32_in_place(const DSP_DATA uint32_t *input1, const uint32_t *ref,
248 size_t length)
249 {
250 DSP_DATA float32_t *output;
251
252 /* Allocate output buffer */
253 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
254 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
255
256 /* Copy input data to output*/
257 memcpy(output, input1, length * sizeof(float32_t));
258
259 /* Run test function */
260 zdsp_negate_f32(output, output, length);
261
262 /* Validate output */
263 zassert_true(test_snr_error_f32(length, output, (float32_t *)ref, SNR_ERROR_THRESH),
264 ASSERT_MSG_SNR_LIMIT_EXCEED);
265
266 zassert_true(test_rel_error_f32(length, output, (float32_t *)ref, REL_ERROR_THRESH),
267 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
268
269 /* Free output buffer */
270 free(output);
271 }
272
273 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_negate_f32_in_place, 3, in_com1, ref_negate, 3);
274 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_negate_f32_in_place, 8, in_com1, ref_negate, 8);
275 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_negate_f32_in_place, 11, in_com1, ref_negate, 11);
276 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_negate_f32_in_place, long, in_com1, ref_negate,
277 ARRAY_SIZE(in_com1));
278
test_zdsp_offset_f32(const DSP_DATA uint32_t * input1,float32_t scalar,const uint32_t * ref,size_t length)279 static void test_zdsp_offset_f32(const DSP_DATA uint32_t *input1, float32_t scalar,
280 const uint32_t *ref, size_t length)
281 {
282 DSP_DATA float32_t *output;
283
284 /* Allocate output buffer */
285 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
286 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
287
288 /* Run test function */
289 zdsp_offset_f32((DSP_DATA float32_t *)input1, scalar, output, length);
290
291 /* Validate output */
292 zassert_true(
293 test_snr_error_f32(length, output, (float32_t *)ref,
294 SNR_ERROR_THRESH),
295 ASSERT_MSG_SNR_LIMIT_EXCEED);
296
297 zassert_true(
298 test_rel_error_f32(length, output, (float32_t *)ref,
299 REL_ERROR_THRESH),
300 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
301
302 /* Free output buffer */
303 free(output);
304 }
305
306 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_offset_f32, 0p5_3, in_com1, 0.5f, ref_offset, 3);
307 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_offset_f32, 0p5_8, in_com1, 0.5f, ref_offset, 8);
308 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_offset_f32, 0p5_11, in_com1, 0.5f, ref_offset, 11);
309 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_offset_f32, long, in_com1, 0.5f, ref_offset,
310 ARRAY_SIZE(in_com1));
311
test_zdsp_offset_f32_in_place(const DSP_DATA uint32_t * input1,float32_t scalar,const uint32_t * ref,size_t length)312 static void test_zdsp_offset_f32_in_place(const DSP_DATA uint32_t *input1, float32_t scalar,
313 const uint32_t *ref, size_t length)
314 {
315 DSP_DATA float32_t *output;
316
317 /* Allocate output buffer */
318 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
319 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
320
321 /* Copy input data to output*/
322 memcpy(output, input1, length * sizeof(float32_t));
323
324 /* Run test function */
325 zdsp_offset_f32(output, scalar, output, length);
326
327 /* Validate output */
328 zassert_true(test_snr_error_f32(length, output, (float32_t *)ref, SNR_ERROR_THRESH),
329 ASSERT_MSG_SNR_LIMIT_EXCEED);
330
331 zassert_true(test_rel_error_f32(length, output, (float32_t *)ref, REL_ERROR_THRESH),
332 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
333
334 /* Free output buffer */
335 free(output);
336 }
337
338 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_offset_f32_in_place, 0p5_3, in_com1, 0.5f, ref_offset, 3);
339 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_offset_f32_in_place, 0p5_8, in_com1, 0.5f, ref_offset, 8);
340 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_offset_f32_in_place, 0p5_11, in_com1, 0.5f, ref_offset,
341 11);
342 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_offset_f32_in_place, long, in_com1, 0.5f, ref_offset,
343 ARRAY_SIZE(in_com1));
344
test_zdsp_scale_f32(const DSP_DATA uint32_t * input1,float32_t scalar,const uint32_t * ref,size_t length)345 static void test_zdsp_scale_f32(const DSP_DATA uint32_t *input1, float32_t scalar,
346 const uint32_t *ref, size_t length)
347 {
348 DSP_DATA float32_t *output;
349
350 /* Allocate output buffer */
351 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
352 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
353
354 /* Run test function */
355 zdsp_scale_f32((DSP_DATA float32_t *)input1, scalar, output, length);
356
357 /* Validate output */
358 zassert_true(
359 test_snr_error_f32(length, output, (float32_t *)ref,
360 SNR_ERROR_THRESH),
361 ASSERT_MSG_SNR_LIMIT_EXCEED);
362
363 zassert_true(
364 test_rel_error_f32(length, output, (float32_t *)ref,
365 REL_ERROR_THRESH),
366 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
367
368 /* Free output buffer */
369 free(output);
370 }
371
372 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_scale_f32, 0p5_3, in_com1, 0.5f, ref_scale, 3);
373 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_scale_f32, 0p5_8, in_com1, 0.5f, ref_scale, 8);
374 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_scale_f32, 0p5_11, in_com1, 0.5f, ref_scale, 11);
375 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_scale_f32, long, in_com1, 0.5f, ref_scale,
376 ARRAY_SIZE(in_com1));
377
test_zdsp_scale_f32_in_place(const DSP_DATA uint32_t * input1,float32_t scalar,const uint32_t * ref,size_t length)378 static void test_zdsp_scale_f32_in_place(const DSP_DATA uint32_t *input1, float32_t scalar,
379 const uint32_t *ref, size_t length)
380 {
381 DSP_DATA float32_t *output;
382
383 /* Allocate output buffer */
384 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
385 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
386
387 /* Copy input data to output*/
388 memcpy(output, input1, length * sizeof(float32_t));
389
390 /* Run test function */
391 zdsp_scale_f32(output, scalar, output, length);
392
393 /* Validate output */
394 zassert_true(test_snr_error_f32(length, output, (float32_t *)ref, SNR_ERROR_THRESH),
395 ASSERT_MSG_SNR_LIMIT_EXCEED);
396
397 zassert_true(test_rel_error_f32(length, output, (float32_t *)ref, REL_ERROR_THRESH),
398 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
399
400 /* Free output buffer */
401 free(output);
402 }
403
404 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_scale_f32_in_place, 0p5_3, in_com1, 0.5f, ref_scale, 3);
405 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_scale_f32_in_place, 0p5_8, in_com1, 0.5f, ref_scale, 8);
406 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_scale_f32_in_place, 0p5_11, in_com1, 0.5f, ref_scale, 11);
407 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_scale_f32_in_place, long, in_com1, 0.5f, ref_scale,
408 ARRAY_SIZE(in_com1));
409
test_zdsp_dot_prod_f32(const DSP_DATA uint32_t * input1,const DSP_DATA uint32_t * input2,const uint32_t * ref,size_t length)410 static void test_zdsp_dot_prod_f32(const DSP_DATA uint32_t *input1, const DSP_DATA uint32_t *input2,
411 const uint32_t *ref, size_t length)
412 {
413 DSP_DATA float32_t *output;
414
415 /* Allocate output buffer */
416 output = (DSP_DATA float32_t *)malloc(1 * sizeof(float32_t));
417 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
418
419 /* Run test function */
420 zdsp_dot_prod_f32((DSP_DATA float32_t *)input1, (DSP_DATA float32_t *)input2, length,
421 &output[0]);
422
423 /* Validate output */
424 zassert_true(
425 test_snr_error_f32(1, output, (float32_t *)ref,
426 SNR_ERROR_THRESH),
427 ASSERT_MSG_SNR_LIMIT_EXCEED);
428
429 zassert_true(
430 test_rel_error_f32(1, output, (float32_t *)ref,
431 REL_ERROR_THRESH),
432 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
433
434 /* Free output buffer */
435 free(output);
436 }
437
438 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_dot_prod_f32, 3, in_com1, in_com2, ref_dot_prod_3, 3);
439 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_dot_prod_f32, 8, in_com1, in_com2, ref_dot_prod_4, 8);
440 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_dot_prod_f32, 11, in_com1, in_com2, ref_dot_prod_4n1, 11);
441 DEFINE_TEST_VARIANT4(basic_math_f32, zdsp_dot_prod_f32, long, in_com1, in_com2, ref_dot_prod_long,
442 ARRAY_SIZE(in_com1));
443
test_zdsp_abs_f32(const DSP_DATA uint32_t * input1,const uint32_t * ref,size_t length)444 static void test_zdsp_abs_f32(const DSP_DATA uint32_t *input1, const uint32_t *ref, size_t length)
445 {
446 DSP_DATA float32_t *output;
447
448 /* Allocate output buffer */
449 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
450 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
451
452 /* Run test function */
453 zdsp_abs_f32((DSP_DATA float32_t *)input1, output, length);
454
455 /* Validate output */
456 zassert_true(
457 test_snr_error_f32(length, output, (float32_t *)ref,
458 SNR_ERROR_THRESH),
459 ASSERT_MSG_SNR_LIMIT_EXCEED);
460
461 zassert_true(
462 test_rel_error_f32(length, output, (float32_t *)ref,
463 REL_ERROR_THRESH),
464 "incorrect computation result");
465
466 /* Free output buffer */
467 free(output);
468 }
469
470 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_abs_f32, 3, in_com1, ref_abs, 3);
471 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_abs_f32, 8, in_com1, ref_abs, 8);
472 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_abs_f32, 11, in_com1, ref_abs, 11);
473 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_abs_f32, long, in_com1, ref_abs, ARRAY_SIZE(in_com1));
474
test_zdsp_abs_f32_in_place(const DSP_DATA uint32_t * input1,const uint32_t * ref,size_t length)475 static void test_zdsp_abs_f32_in_place(const DSP_DATA uint32_t *input1, const uint32_t *ref,
476 size_t length)
477 {
478 DSP_DATA float32_t *output;
479
480 /* Allocate output buffer */
481 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
482 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
483
484 /* Copy input data to output*/
485 memcpy(output, input1, length * sizeof(float32_t));
486
487 /* Run test function */
488 zdsp_abs_f32(output, output, length);
489
490 /* Validate output */
491 zassert_true(test_snr_error_f32(length, output, (float32_t *)ref, SNR_ERROR_THRESH),
492 ASSERT_MSG_SNR_LIMIT_EXCEED);
493
494 zassert_true(test_rel_error_f32(length, output, (float32_t *)ref, REL_ERROR_THRESH),
495 "incorrect computation result");
496
497 /* Free output buffer */
498 free(output);
499 }
500
501 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_abs_f32_in_place, 3, in_com1, ref_abs, 3);
502 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_abs_f32_in_place, 8, in_com1, ref_abs, 8);
503 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_abs_f32_in_place, 11, in_com1, ref_abs, 11);
504 DEFINE_TEST_VARIANT3(basic_math_f32, zdsp_abs_f32_in_place, long, in_com1, ref_abs,
505 ARRAY_SIZE(in_com1));
506
test_zdsp_clip_f32(const DSP_DATA uint32_t * input,const uint32_t * ref,float32_t min,float32_t max,size_t length)507 static void test_zdsp_clip_f32(const DSP_DATA uint32_t *input, const uint32_t *ref, float32_t min,
508 float32_t max, size_t length)
509 {
510 DSP_DATA float32_t *output;
511
512 /* Allocate output buffer */
513 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
514 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
515
516 /* Run test function */
517 zdsp_clip_f32((DSP_DATA float32_t *)input, output, min, max, length);
518
519 /* Validate output */
520 zassert_true(
521 test_snr_error_f32(length, output, (float32_t *)ref,
522 SNR_ERROR_THRESH),
523 ASSERT_MSG_SNR_LIMIT_EXCEED);
524
525 zassert_true(
526 test_rel_error_f32(length, output, (float32_t *)ref,
527 REL_ERROR_THRESH),
528 "incorrect computation result");
529
530 /* Free output buffer */
531 free(output);
532 }
533
534 DEFINE_TEST_VARIANT5(basic_math_f32, zdsp_clip_f32, m0p5_m0p1, in_clip, ref_clip1,
535 -0.5f, -0.1f, ARRAY_SIZE(ref_clip1));
536 DEFINE_TEST_VARIANT5(basic_math_f32, zdsp_clip_f32, m0p5_0p5, in_clip, ref_clip2,
537 -0.5f, 0.5f, ARRAY_SIZE(ref_clip2));
538 DEFINE_TEST_VARIANT5(basic_math_f32, zdsp_clip_f32, 0p1_0p5, in_clip, ref_clip3,
539 0.1f, 0.5f, ARRAY_SIZE(ref_clip3));
540
test_zdsp_clip_f32_in_place(const DSP_DATA uint32_t * input,const uint32_t * ref,float32_t min,float32_t max,size_t length)541 static void test_zdsp_clip_f32_in_place(const DSP_DATA uint32_t *input, const uint32_t *ref,
542 float32_t min, float32_t max, size_t length)
543 {
544 DSP_DATA float32_t *output;
545
546 /* Allocate output buffer */
547 output = (DSP_DATA float32_t *)malloc(length * sizeof(float32_t));
548 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
549
550 /* Copy input data to output*/
551 memcpy(output, input, length * sizeof(float32_t));
552
553 /* Run test function */
554 zdsp_clip_f32(output, output, min, max, length);
555
556 /* Validate output */
557 zassert_true(test_snr_error_f32(length, output, (float32_t *)ref, SNR_ERROR_THRESH),
558 ASSERT_MSG_SNR_LIMIT_EXCEED);
559
560 zassert_true(test_rel_error_f32(length, output, (float32_t *)ref, REL_ERROR_THRESH),
561 "incorrect computation result");
562
563 /* Free output buffer */
564 free(output);
565 }
566
567 DEFINE_TEST_VARIANT5(basic_math_f32, zdsp_clip_f32_in_place, m0p5_m0p1, in_clip, ref_clip1, -0.5f,
568 -0.1f, ARRAY_SIZE(ref_clip1));
569 DEFINE_TEST_VARIANT5(basic_math_f32, zdsp_clip_f32_in_place, m0p5_0p5, in_clip, ref_clip2, -0.5f,
570 0.5f, ARRAY_SIZE(ref_clip2));
571 DEFINE_TEST_VARIANT5(basic_math_f32, zdsp_clip_f32_in_place, 0p1_0p5, in_clip, ref_clip3, 0.1f,
572 0.5f, ARRAY_SIZE(ref_clip3));
573
574 ZTEST_SUITE(basic_math_f32, NULL, NULL, NULL, NULL, NULL);
575