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/ztest.h>
9 #include <zephyr/kernel.h>
10 #include <stdlib.h>
11 #include <arm_math.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_t)2000)
18 
ZTEST(interpolation_q31,test_arm_linear_interp_q31)19 ZTEST(interpolation_q31, test_arm_linear_interp_q31)
20 {
21 	size_t index;
22 	size_t length = ARRAY_SIZE(ref_linear);
23 	q31_t *output;
24 
25 	/* Allocate output buffer */
26 	output = malloc(length * sizeof(q31_t));
27 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
28 
29 	/* Run test function */
30 	for (index = 0; index < length; index++) {
31 		/*
32 		 * FIXME: Remove the cast below once the upstream CMSIS is
33 		 *        fixed to specify the const qualifier.
34 		 */
35 		output[index] = arm_linear_interp_q31((q31_t *)in_linear_y,
36 						     in_linear_x[index],
37 						     ARRAY_SIZE(in_linear_y));
38 	}
39 
40 	/* Validate output */
41 	zassert_true(
42 		test_snr_error_q31(length, output, ref_linear,
43 			SNR_ERROR_THRESH),
44 		ASSERT_MSG_SNR_LIMIT_EXCEED);
45 
46 	zassert_true(
47 		test_near_equal_q31(length, output, ref_linear,
48 			ABS_ERROR_THRESH),
49 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
50 
51 	/* Free output buffer */
52 	free(output);
53 }
54 
ZTEST(interpolation_q31,test_arm_bilinear_interp_q31)55 ZTEST(interpolation_q31, test_arm_bilinear_interp_q31)
56 {
57 	arm_bilinear_interp_instance_q31 inst;
58 	size_t index;
59 	size_t length = ARRAY_SIZE(ref_bilinear);
60 	q31_t *output;
61 
62 	/* Initialise instance */
63 	inst.numRows = in_bilinear_config[1];
64 	inst.numCols = in_bilinear_config[0];
65 	inst.pData = (q31_t *)in_bilinear_y;
66 
67 	/* Allocate output buffer */
68 	output = malloc(length * sizeof(q31_t));
69 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
70 
71 	/* Run test function */
72 	for (index = 0; index < length; index++) {
73 		output[index] = arm_bilinear_interp_q31(
74 					&inst,
75 					in_bilinear_x[2 * index],
76 					in_bilinear_x[2 * index + 1]
77 					);
78 	}
79 
80 	/* Validate output */
81 	zassert_true(
82 		test_snr_error_q31(length, output, ref_bilinear,
83 			SNR_ERROR_THRESH),
84 		ASSERT_MSG_SNR_LIMIT_EXCEED);
85 
86 	zassert_true(
87 		test_near_equal_q31(length, output, ref_bilinear,
88 			ABS_ERROR_THRESH),
89 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
90 
91 	/* Free output buffer */
92 	free(output);
93 }
94 
95 ZTEST_SUITE(interpolation_q31, NULL, NULL, NULL, NULL, NULL);
96