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