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 <ztest.h>
9 #include <zephyr.h>
10 #include <stdlib.h>
11 #include <arm_math.h>
12 #include "../../common/test_common.h"
13 
14 #include "f32.pat"
15 
16 #define SNR_ERROR_THRESH	(120)
17 #define REL_ERROR_THRESH	(1.0e-5)
18 #define ABS_ERROR_THRESH_Q7	((q7_t)10)
19 #define ABS_ERROR_THRESH_Q15	((q15_t)10)
20 #define ABS_ERROR_THRESH_Q31	((q31_t)80)
21 
test_arm_copy_f32(const uint32_t * input1,size_t length)22 static void test_arm_copy_f32(const uint32_t *input1, size_t length)
23 {
24 	float32_t *output;
25 
26 	/* Allocate output buffer */
27 	output = malloc(length * sizeof(float32_t));
28 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
29 
30 	/* Run test function */
31 	arm_copy_f32((float32_t *)input1, output, length);
32 
33 	/* Validate output */
34 	zassert_true(
35 		test_equal_f32(length, (float32_t *)input1, output),
36 		ASSERT_MSG_INCORRECT_COMP_RESULT);
37 
38 	/* Free output buffer */
39 	free(output);
40 }
41 
42 DEFINE_TEST_VARIANT2(arm_copy_f32, 3, in_f32, 3);
43 DEFINE_TEST_VARIANT2(arm_copy_f32, 8, in_f32, 8);
44 DEFINE_TEST_VARIANT2(arm_copy_f32, 11, in_f32, 11);
45 
test_arm_fill_f32(size_t length)46 static void test_arm_fill_f32(size_t length)
47 {
48 	size_t index;
49 	float32_t *output;
50 	float32_t val = 1.1;
51 
52 	/* Allocate output buffer */
53 	output = malloc(length * sizeof(float32_t));
54 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
55 
56 	/* Run test function */
57 	arm_fill_f32(val, output, length);
58 
59 	/* Validate output */
60 	for (index = 0; index < length; index++) {
61 		zassert_equal(
62 			output[index], val, ASSERT_MSG_INCORRECT_COMP_RESULT);
63 	}
64 
65 	/* Free output buffer */
66 	free(output);
67 }
68 
69 DEFINE_TEST_VARIANT1(arm_fill_f32, 3, 3);
70 DEFINE_TEST_VARIANT1(arm_fill_f32, 8, 8);
71 DEFINE_TEST_VARIANT1(arm_fill_f32, 11, 11);
72 
test_arm_float_to_q31(const uint32_t * input1,const q31_t * ref,size_t length)73 static void test_arm_float_to_q31(
74 	const uint32_t *input1, const q31_t *ref, size_t length)
75 {
76 	q31_t *output;
77 
78 	/* Allocate output buffer */
79 	output = malloc(length * sizeof(q31_t));
80 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
81 
82 	/* Run test function */
83 	arm_float_to_q31((float32_t *)input1, output, length);
84 
85 	/* Validate output */
86 	zassert_true(
87 		test_near_equal_q31(length, ref, output, ABS_ERROR_THRESH_Q31),
88 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
89 
90 	/* Free output buffer */
91 	free(output);
92 }
93 
94 DEFINE_TEST_VARIANT3(arm_float_to_q31, 3, in_f32, ref_q31, 3);
95 DEFINE_TEST_VARIANT3(arm_float_to_q31, 8, in_f32, ref_q31, 8);
96 DEFINE_TEST_VARIANT3(arm_float_to_q31, 11, in_f32, ref_q31, 11);
97 
test_arm_float_to_q15(const uint32_t * input1,const q15_t * ref,size_t length)98 static void test_arm_float_to_q15(
99 	const uint32_t *input1, const q15_t *ref, size_t length)
100 {
101 	q15_t *output;
102 
103 	/* Allocate output buffer */
104 	output = malloc(length * sizeof(q15_t));
105 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
106 
107 	/* Run test function */
108 	arm_float_to_q15((float32_t *)input1, output, length);
109 
110 	/* Validate output */
111 	zassert_true(
112 		test_near_equal_q15(length, ref, output, ABS_ERROR_THRESH_Q15),
113 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
114 
115 	/* Free output buffer */
116 	free(output);
117 }
118 
119 DEFINE_TEST_VARIANT3(arm_float_to_q15, 7, in_f32, ref_q15, 7);
120 DEFINE_TEST_VARIANT3(arm_float_to_q15, 16, in_f32, ref_q15, 16);
121 DEFINE_TEST_VARIANT3(arm_float_to_q15, 17, in_f32, ref_q15, 17);
122 
test_arm_float_to_q7(const uint32_t * input1,const q7_t * ref,size_t length)123 static void test_arm_float_to_q7(
124 	const uint32_t *input1, const q7_t *ref, size_t length)
125 {
126 	q7_t *output;
127 
128 	/* Allocate output buffer */
129 	output = malloc(length * sizeof(q7_t));
130 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
131 
132 	/* Run test function */
133 	arm_float_to_q7((float32_t *)input1, output, length);
134 
135 	/* Validate output */
136 	zassert_true(
137 		test_near_equal_q7(length, ref, output, ABS_ERROR_THRESH_Q7),
138 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
139 
140 	/* Free output buffer */
141 	free(output);
142 }
143 
144 DEFINE_TEST_VARIANT3(arm_float_to_q7, 15, in_f32, ref_q7, 15);
145 DEFINE_TEST_VARIANT3(arm_float_to_q7, 32, in_f32, ref_q7, 32);
146 DEFINE_TEST_VARIANT3(arm_float_to_q7, 33, in_f32, ref_q7, 33);
147 
test_arm_weighted_sum_f32(int ref_offset,size_t length)148 static void test_arm_weighted_sum_f32(
149 	int ref_offset, size_t length)
150 {
151 	const float32_t *val = (const float32_t *)in_weighted_sum_val;
152 	const float32_t *coeff = (const float32_t *)in_weighted_sum_coeff;
153 	const float32_t *ref = (const float32_t *)ref_weighted_sum;
154 	float32_t *output;
155 
156 	/* Allocate output buffer */
157 	output = malloc(1 * sizeof(float32_t));
158 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
159 
160 	/* Run test function */
161 	output[0] = arm_weighted_sum_f32(val, coeff, length);
162 
163 	/* Validate output */
164 	zassert_true(
165 		test_rel_error_f32(1, output, &ref[ref_offset],
166 			REL_ERROR_THRESH),
167 		ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
168 
169 	/* Free output buffer */
170 	free(output);
171 }
172 
173 DEFINE_TEST_VARIANT2(arm_weighted_sum_f32, 3, 0, 3);
174 DEFINE_TEST_VARIANT2(arm_weighted_sum_f32, 8, 1, 8);
175 DEFINE_TEST_VARIANT2(arm_weighted_sum_f32, 11, 2, 11);
176 
test_arm_sort_out(const uint32_t * input1,const uint32_t * ref,size_t length,arm_sort_alg alg,arm_sort_dir dir)177 static void test_arm_sort_out(
178 	const uint32_t *input1, const uint32_t *ref, size_t length,
179 	arm_sort_alg alg, arm_sort_dir dir)
180 {
181 	float32_t *output;
182 	arm_sort_instance_f32 inst;
183 
184 	/* Allocate output buffer */
185 	output = malloc(length * sizeof(float32_t));
186 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
187 
188 	/* Initialise sorter */
189 	arm_sort_init_f32(&inst, alg, dir);
190 
191 	/* Run test function */
192 	arm_sort_f32(&inst, (float32_t *)input1, output, length);
193 
194 	/* Validate output */
195 	zassert_true(
196 		test_equal_f32(length, output, (float32_t *)ref),
197 		ASSERT_MSG_INCORRECT_COMP_RESULT);
198 
199 	/* Free output buffer */
200 	free(output);
201 }
202 
203 DEFINE_TEST_VARIANT5(arm_sort_out, bitonic_16,
204 	in_sort_bitonic_16, ref_sort_bitonic_16, 16,
205 	ARM_SORT_BITONIC, ARM_SORT_ASCENDING);
206 
207 DEFINE_TEST_VARIANT5(arm_sort_out, bitonic_32,
208 	in_sort_bitonic_32, ref_sort_bitonic_32, 32,
209 	ARM_SORT_BITONIC, ARM_SORT_ASCENDING);
210 
211 DEFINE_TEST_VARIANT5(arm_sort_out, bubble_11,
212 	in_sort, ref_sort, 11,
213 	ARM_SORT_BUBBLE, ARM_SORT_ASCENDING);
214 
215 DEFINE_TEST_VARIANT5(arm_sort_out, heap_11,
216 	in_sort, ref_sort, 11,
217 	ARM_SORT_HEAP, ARM_SORT_ASCENDING);
218 
219 DEFINE_TEST_VARIANT5(arm_sort_out, insertion_11,
220 	in_sort, ref_sort, 11,
221 	ARM_SORT_INSERTION, ARM_SORT_ASCENDING);
222 
223 DEFINE_TEST_VARIANT5(arm_sort_out, quick_11,
224 	in_sort, ref_sort, 11,
225 	ARM_SORT_QUICK, ARM_SORT_ASCENDING);
226 
227 DEFINE_TEST_VARIANT5(arm_sort_out, selection_11,
228 	in_sort, ref_sort, 11,
229 	ARM_SORT_SELECTION, ARM_SORT_ASCENDING);
230 
test_arm_merge_sort_out(const uint32_t * input1,const uint32_t * ref,size_t length,arm_sort_dir dir)231 static void test_arm_merge_sort_out(
232 	const uint32_t *input1, const uint32_t *ref, size_t length,
233 	arm_sort_dir dir)
234 {
235 	float32_t *output;
236 	float32_t *scratch;
237 	arm_merge_sort_instance_f32 inst;
238 
239 	/* Allocate output buffer */
240 	output = malloc(length * sizeof(float32_t));
241 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
242 
243 	/* Allocate scratch buffer */
244 	scratch = malloc(length * sizeof(float32_t));
245 	zassert_not_null(scratch, ASSERT_MSG_BUFFER_ALLOC_FAILED);
246 
247 	/* Initialise sorter */
248 	arm_merge_sort_init_f32(&inst, dir, scratch);
249 
250 	/* Run test function */
251 	arm_merge_sort_f32(&inst, (float32_t *)input1, output, length);
252 
253 	/* Validate output */
254 	zassert_true(
255 		test_equal_f32(length, output, (float32_t *)ref),
256 		ASSERT_MSG_INCORRECT_COMP_RESULT);
257 
258 	/* Free output buffer */
259 	free(output);
260 	free(scratch);
261 }
262 
263 DEFINE_TEST_VARIANT4(arm_merge_sort_out, 11,
264 	in_sort, ref_sort, 11, ARM_SORT_ASCENDING);
265 
test_arm_sort_in(const uint32_t * input1,const uint32_t * ref,size_t length,arm_sort_alg alg,arm_sort_dir dir)266 static void test_arm_sort_in(
267 	const uint32_t *input1, const uint32_t *ref, size_t length,
268 	arm_sort_alg alg, arm_sort_dir dir)
269 {
270 	float32_t *output;
271 	arm_sort_instance_f32 inst;
272 
273 	/* Allocate output buffer */
274 	output = malloc(length * sizeof(float32_t));
275 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
276 
277 	/* Copy input to the output buffer */
278 	memcpy(output, input1, length * sizeof(float32_t));
279 
280 	/* Initialise sorter */
281 	arm_sort_init_f32(&inst, alg, dir);
282 
283 	/* Run test function */
284 	arm_sort_f32(&inst, output, output, length);
285 
286 	/* Validate output */
287 	zassert_true(
288 		test_equal_f32(length, output, (float32_t *)ref),
289 		ASSERT_MSG_INCORRECT_COMP_RESULT);
290 
291 	/* Free output buffer */
292 	free(output);
293 }
294 
295 DEFINE_TEST_VARIANT5(arm_sort_in, bitonic_32,
296 	in_sort_bitonic_32, ref_sort_bitonic_32, 32,
297 	ARM_SORT_BITONIC, ARM_SORT_ASCENDING);
298 
299 DEFINE_TEST_VARIANT5(arm_sort_in, bubble_11,
300 	in_sort, ref_sort, 11,
301 	ARM_SORT_BUBBLE, ARM_SORT_ASCENDING);
302 
303 DEFINE_TEST_VARIANT5(arm_sort_in, heap_11,
304 	in_sort, ref_sort, 11,
305 	ARM_SORT_HEAP, ARM_SORT_ASCENDING);
306 
307 DEFINE_TEST_VARIANT5(arm_sort_in, insertion_11,
308 	in_sort, ref_sort, 11,
309 	ARM_SORT_INSERTION, ARM_SORT_ASCENDING);
310 
311 DEFINE_TEST_VARIANT5(arm_sort_in, quick_11,
312 	in_sort, ref_sort, 11,
313 	ARM_SORT_QUICK, ARM_SORT_ASCENDING);
314 
315 DEFINE_TEST_VARIANT5(arm_sort_in, selection_11,
316 	in_sort, ref_sort, 11,
317 	ARM_SORT_SELECTION, ARM_SORT_ASCENDING);
318 
test_arm_sort_const(const uint32_t * input1,const uint32_t * ref,size_t length,arm_sort_alg alg,arm_sort_dir dir)319 static void test_arm_sort_const(
320 	const uint32_t *input1, const uint32_t *ref, size_t length,
321 	arm_sort_alg alg, arm_sort_dir dir)
322 {
323 	float32_t *output;
324 	arm_sort_instance_f32 inst;
325 
326 	/* Allocate output buffer */
327 	output = malloc(length * sizeof(float32_t));
328 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
329 
330 	/* Initialise sorter */
331 	arm_sort_init_f32(&inst, alg, dir);
332 
333 	/* Run test function */
334 	arm_sort_f32(&inst, (float32_t *)input1, output, length);
335 
336 	/* Validate output */
337 	zassert_true(
338 		test_equal_f32(length, output, (float32_t *)ref),
339 		ASSERT_MSG_INCORRECT_COMP_RESULT);
340 
341 	/* Free output buffer */
342 	free(output);
343 }
344 
345 DEFINE_TEST_VARIANT5(arm_sort_const, bitonic_16,
346 	in_sort_const, ref_sort_const, 16,
347 	ARM_SORT_BITONIC, ARM_SORT_ASCENDING);
348 
349 DEFINE_TEST_VARIANT5(arm_sort_const, bubble_16,
350 	in_sort_const, ref_sort_const, 16,
351 	ARM_SORT_BUBBLE, ARM_SORT_ASCENDING);
352 
353 DEFINE_TEST_VARIANT5(arm_sort_const, heap_16,
354 	in_sort_const, ref_sort_const, 16,
355 	ARM_SORT_HEAP, ARM_SORT_ASCENDING);
356 
357 DEFINE_TEST_VARIANT5(arm_sort_const, insertion_16,
358 	in_sort_const, ref_sort_const, 16,
359 	ARM_SORT_INSERTION, ARM_SORT_ASCENDING);
360 
361 DEFINE_TEST_VARIANT5(arm_sort_const, quick_16,
362 	in_sort_const, ref_sort_const, 16,
363 	ARM_SORT_QUICK, ARM_SORT_ASCENDING);
364 
365 DEFINE_TEST_VARIANT5(arm_sort_const, selection_16,
366 	in_sort_const, ref_sort_const, 16,
367 	ARM_SORT_SELECTION, ARM_SORT_ASCENDING);
368 
test_arm_merge_sort_const(const uint32_t * input1,const uint32_t * ref,size_t length,arm_sort_dir dir)369 static void test_arm_merge_sort_const(
370 	const uint32_t *input1, const uint32_t *ref, size_t length,
371 	arm_sort_dir dir)
372 {
373 	float32_t *output;
374 	float32_t *scratch;
375 	arm_merge_sort_instance_f32 inst;
376 
377 	/* Allocate output buffer */
378 	output = malloc(length * sizeof(float32_t));
379 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
380 
381 	/* Allocate scratch buffer */
382 	scratch = malloc(length * sizeof(float32_t));
383 	zassert_not_null(scratch, ASSERT_MSG_BUFFER_ALLOC_FAILED);
384 
385 	/* Initialise sorter */
386 	arm_merge_sort_init_f32(&inst, dir, scratch);
387 
388 	/* Run test function */
389 	arm_merge_sort_f32(&inst, (float32_t *)input1, output, length);
390 
391 	/* Validate output */
392 	zassert_true(
393 		test_equal_f32(length, output, (float32_t *)ref),
394 		ASSERT_MSG_INCORRECT_COMP_RESULT);
395 
396 	/* Free output buffer */
397 	free(output);
398 	free(scratch);
399 }
400 
401 DEFINE_TEST_VARIANT4(arm_merge_sort_const, 16,
402 	in_sort_const, ref_sort_const, 16, ARM_SORT_ASCENDING);
403 
test_support_f32(void)404 void test_support_f32(void)
405 {
406 	ztest_test_suite(support_f32,
407 		ztest_unit_test(test_arm_copy_f32_3),
408 		ztest_unit_test(test_arm_copy_f32_8),
409 		ztest_unit_test(test_arm_copy_f32_11),
410 		ztest_unit_test(test_arm_fill_f32_3),
411 		ztest_unit_test(test_arm_fill_f32_8),
412 		ztest_unit_test(test_arm_fill_f32_11),
413 		ztest_unit_test(test_arm_float_to_q31_3),
414 		ztest_unit_test(test_arm_float_to_q31_8),
415 		ztest_unit_test(test_arm_float_to_q31_11),
416 		ztest_unit_test(test_arm_float_to_q15_7),
417 		ztest_unit_test(test_arm_float_to_q15_16),
418 		ztest_unit_test(test_arm_float_to_q15_17),
419 		ztest_unit_test(test_arm_float_to_q7_15),
420 		ztest_unit_test(test_arm_float_to_q7_32),
421 		ztest_unit_test(test_arm_float_to_q7_33),
422 		ztest_unit_test(test_arm_weighted_sum_f32_3),
423 		ztest_unit_test(test_arm_weighted_sum_f32_8),
424 		ztest_unit_test(test_arm_weighted_sum_f32_11),
425 		ztest_unit_test(test_arm_sort_out_bitonic_16),
426 		ztest_unit_test(test_arm_sort_out_bitonic_32),
427 		ztest_unit_test(test_arm_sort_in_bitonic_32),
428 		ztest_unit_test(test_arm_sort_const_bitonic_16),
429 		ztest_unit_test(test_arm_sort_out_bubble_11),
430 		ztest_unit_test(test_arm_sort_in_bubble_11),
431 		ztest_unit_test(test_arm_sort_const_bubble_16),
432 		ztest_unit_test(test_arm_sort_out_heap_11),
433 		ztest_unit_test(test_arm_sort_in_heap_11),
434 		ztest_unit_test(test_arm_sort_const_heap_16),
435 		ztest_unit_test(test_arm_sort_out_insertion_11),
436 		ztest_unit_test(test_arm_sort_in_insertion_11),
437 		ztest_unit_test(test_arm_sort_const_insertion_16),
438 		ztest_unit_test(test_arm_sort_out_quick_11),
439 		ztest_unit_test(test_arm_sort_in_quick_11),
440 		ztest_unit_test(test_arm_sort_const_quick_16),
441 		ztest_unit_test(test_arm_sort_out_selection_11),
442 		ztest_unit_test(test_arm_sort_in_selection_11),
443 		ztest_unit_test(test_arm_sort_const_selection_16),
444 		ztest_unit_test(test_arm_merge_sort_out_11),
445 		ztest_unit_test(test_arm_merge_sort_const_16)
446 		);
447 
448 	ztest_run_test_suite(support_f32);
449 }
450