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 "q7.pat"
15 
16 #define SNR_ERROR_THRESH	((float32_t)20)
17 #define ABS_ERROR_THRESH_Q7	((q7_t)20)
18 #define ABS_ERROR_THRESH_Q31	((q31_t)(1 << 15))
19 
test_arm_max_q7(const q7_t * input1,int ref_index,size_t length)20 static void test_arm_max_q7(
21 	const q7_t *input1, int ref_index, size_t length)
22 {
23 	q7_t val;
24 	uint32_t index;
25 
26 	/* Run test function */
27 	arm_max_q7(input1, length, &val, &index);
28 
29 	/* Validate output */
30 	zassert_equal(val, ref_max_val[ref_index],
31 		ASSERT_MSG_INCORRECT_COMP_RESULT);
32 
33 	zassert_equal(index, ref_max_idx[ref_index],
34 		ASSERT_MSG_INCORRECT_COMP_RESULT);
35 }
36 
37 DEFINE_TEST_VARIANT3(statistics_q7, arm_max_q7, 15, in_com1, 0, 15);
38 DEFINE_TEST_VARIANT3(statistics_q7, arm_max_q7, 32, in_com1, 1, 32);
39 DEFINE_TEST_VARIANT3(statistics_q7, arm_max_q7, 47, in_com1, 2, 47);
40 DEFINE_TEST_VARIANT3(statistics_q7, arm_max_q7, max, in_max_maxidx, 3, 280);
41 
test_arm_min_q7(const q7_t * input1,int ref_index,size_t length)42 static void test_arm_min_q7(
43 	const q7_t *input1, int ref_index, size_t length)
44 {
45 	q7_t val;
46 	uint32_t index;
47 
48 	/* Run test function */
49 	arm_min_q7(input1, length, &val, &index);
50 
51 	/* Validate output */
52 	zassert_equal(val, ref_min_val[ref_index],
53 		ASSERT_MSG_INCORRECT_COMP_RESULT);
54 
55 	zassert_equal(index, ref_min_idx[ref_index],
56 		ASSERT_MSG_INCORRECT_COMP_RESULT);
57 }
58 
59 DEFINE_TEST_VARIANT3(statistics_q7, arm_min_q7, 15, in_com1, 0, 15);
60 DEFINE_TEST_VARIANT3(statistics_q7, arm_min_q7, 32, in_com1, 1, 32);
61 DEFINE_TEST_VARIANT3(statistics_q7, arm_min_q7, 47, in_com1, 2, 47);
62 DEFINE_TEST_VARIANT3(statistics_q7, arm_min_q7, max, in_min_maxidx, 3, 280);
63 
test_arm_absmax_q7(const q7_t * input1,int ref_index,size_t length)64 static void test_arm_absmax_q7(
65 	const q7_t *input1, int ref_index, size_t length)
66 {
67 	q7_t val;
68 	uint32_t index;
69 
70 	/* Run test function */
71 	arm_absmax_q7(input1, length, &val, &index);
72 
73 	/* Validate output */
74 	zassert_equal(val, ref_absmax_val[ref_index],
75 		ASSERT_MSG_INCORRECT_COMP_RESULT);
76 
77 	zassert_equal(index, ref_absmax_idx[ref_index],
78 		ASSERT_MSG_INCORRECT_COMP_RESULT);
79 }
80 
81 DEFINE_TEST_VARIANT3(statistics_q7, arm_absmax_q7, 15, in_absminmax, 0, 15);
82 DEFINE_TEST_VARIANT3(statistics_q7, arm_absmax_q7, 32, in_absminmax, 1, 32);
83 DEFINE_TEST_VARIANT3(statistics_q7, arm_absmax_q7, 47, in_absminmax, 2, 47);
84 DEFINE_TEST_VARIANT3(statistics_q7, arm_absmax_q7, max, in_absmax_maxidx, 3, 280);
85 
test_arm_absmin_q7(const q7_t * input1,int ref_index,size_t length)86 static void test_arm_absmin_q7(
87 	const q7_t *input1, int ref_index, size_t length)
88 {
89 	q7_t val;
90 	uint32_t index;
91 
92 	/* Run test function */
93 	arm_absmin_q7(input1, length, &val, &index);
94 
95 	/* Validate output */
96 	zassert_equal(val, ref_absmin_val[ref_index],
97 		ASSERT_MSG_INCORRECT_COMP_RESULT);
98 
99 	zassert_equal(index, ref_absmin_idx[ref_index],
100 		ASSERT_MSG_INCORRECT_COMP_RESULT);
101 }
102 
103 DEFINE_TEST_VARIANT3(statistics_q7, arm_absmin_q7, 15, in_absminmax, 0, 15);
104 DEFINE_TEST_VARIANT3(statistics_q7, arm_absmin_q7, 32, in_absminmax, 1, 32);
105 DEFINE_TEST_VARIANT3(statistics_q7, arm_absmin_q7, 47, in_absminmax, 2, 47);
106 DEFINE_TEST_VARIANT3(statistics_q7, arm_absmin_q7, max, in_absmin_maxidx, 3, 280);
107 
test_arm_mean_q7(const q7_t * input1,int ref_index,size_t length)108 static void test_arm_mean_q7(
109 	const q7_t *input1, int ref_index, size_t length)
110 {
111 	q7_t ref[1];
112 	q7_t *output;
113 
114 	/* Load reference */
115 	ref[0] = ref_mean[ref_index];
116 
117 	/* Allocate output buffer */
118 	output = malloc(1 * sizeof(q7_t));
119 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
120 
121 	/* Run test function */
122 	arm_mean_q7(input1, length, &output[0]);
123 
124 	/* Validate output */
125 	zassert_true(
126 		test_snr_error_q7(1, output, ref, SNR_ERROR_THRESH),
127 		ASSERT_MSG_SNR_LIMIT_EXCEED);
128 
129 	zassert_true(
130 		test_near_equal_q7(1, output, ref, ABS_ERROR_THRESH_Q7),
131 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
132 
133 	/* Free output buffer */
134 	free(output);
135 }
136 
137 DEFINE_TEST_VARIANT3(statistics_q7, arm_mean_q7, 15, in_com2, 0, 15);
138 DEFINE_TEST_VARIANT3(statistics_q7, arm_mean_q7, 32, in_com2, 1, 32);
139 DEFINE_TEST_VARIANT3(statistics_q7, arm_mean_q7, 47, in_com2, 2, 47);
140 
test_arm_power_q7(const q7_t * input1,int ref_index,size_t length)141 static void test_arm_power_q7(
142 	const q7_t *input1, int ref_index, size_t length)
143 {
144 	q31_t ref[1];
145 	q31_t *output;
146 
147 	/* Load reference */
148 	ref[0] = ref_power[ref_index];
149 
150 	/* Allocate output buffer */
151 	output = malloc(1 * sizeof(q31_t));
152 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
153 
154 	/* Run test function */
155 	arm_power_q7(input1, length, &output[0]);
156 
157 	/* Validate output */
158 	zassert_true(
159 		test_snr_error_q31(1, output, ref, SNR_ERROR_THRESH),
160 		ASSERT_MSG_SNR_LIMIT_EXCEED);
161 
162 	zassert_true(
163 		test_near_equal_q31(1, output, ref, ABS_ERROR_THRESH_Q31),
164 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
165 
166 	/* Free output buffer */
167 	free(output);
168 }
169 
170 DEFINE_TEST_VARIANT3(statistics_q7, arm_power_q7, 15, in_com1, 0, 15);
171 DEFINE_TEST_VARIANT3(statistics_q7, arm_power_q7, 32, in_com1, 1, 32);
172 DEFINE_TEST_VARIANT3(statistics_q7, arm_power_q7, 47, in_com1, 2, 47);
173 
174 ZTEST_SUITE(statistics_q7, NULL, NULL, NULL, NULL, NULL);
175