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_f16.h>
12 #include "../../common/test_common.h"
13 
14 #include "f16.pat"
15 
16 #define SNR_ERROR_THRESH	(120)
17 
18 #define ABS_ERROR_THRESH_F16	(1.0e-5)
19 #define REL_ERROR_THRESH_F16	(1.0e-5)
20 
21 #define ABS_ERROR_THRESH_F32	(1.0e-3)
22 #define REL_ERROR_THRESH_F32	(1.0e-3)
23 
24 #define ABS_ERROR_THRESH_Q7	((q15_t)10)
25 #define ABS_ERROR_THRESH_Q15	((q15_t)10)
26 #define ABS_ERROR_THRESH_Q31	((q31_t)80)
27 
28 #define ABS_ERROR_THRESH_WS	(1.0e-1)
29 #define REL_ERROR_THRESH_WS	(5.0e-3)
30 
test_arm_copy_f16(const uint16_t * input1,size_t length)31 static void test_arm_copy_f16(const uint16_t *input1, size_t length)
32 {
33 	float16_t *output;
34 
35 	/* Allocate output buffer */
36 	output = malloc(length * sizeof(float16_t));
37 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
38 
39 	/* Run test function */
40 	arm_copy_f16((float16_t *)input1, output, length);
41 
42 	/* Validate output */
43 	zassert_true(
44 		test_equal_f16(length, (float16_t *)input1, output),
45 		ASSERT_MSG_INCORRECT_COMP_RESULT);
46 
47 	/* Free output buffer */
48 	free(output);
49 }
50 
51 DEFINE_TEST_VARIANT2(support_f16, arm_copy_f16, 7, ref_f16, 7);
52 DEFINE_TEST_VARIANT2(support_f16, arm_copy_f16, 16, ref_f16, 16);
53 DEFINE_TEST_VARIANT2(support_f16, arm_copy_f16, 23, ref_f16, 23);
54 
test_arm_fill_f16(size_t length)55 static void test_arm_fill_f16(size_t length)
56 {
57 	size_t index;
58 	float16_t *output;
59 	float16_t val = 1.1;
60 
61 	/* Allocate output buffer */
62 	output = malloc(length * sizeof(float16_t));
63 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
64 
65 	/* Run test function */
66 	arm_fill_f16(val, output, length);
67 
68 	/* Validate output */
69 	for (index = 0; index < length; index++) {
70 		zassert_equal(
71 			output[index], val, ASSERT_MSG_INCORRECT_COMP_RESULT);
72 	}
73 
74 	/* Free output buffer */
75 	free(output);
76 }
77 
78 DEFINE_TEST_VARIANT1(support_f16, arm_fill_f16, 7, 7);
79 DEFINE_TEST_VARIANT1(support_f16, arm_fill_f16, 16, 16);
80 DEFINE_TEST_VARIANT1(support_f16, arm_fill_f16, 23, 23);
81 
test_arm_f16_to_q15(const uint16_t * input1,const q15_t * ref,size_t length)82 static void test_arm_f16_to_q15(
83 	const uint16_t *input1, const q15_t *ref, size_t length)
84 {
85 	q15_t *output;
86 
87 	/* Allocate output buffer */
88 	output = malloc(length * sizeof(q15_t));
89 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
90 
91 	/* Run test function */
92 	arm_f16_to_q15((float16_t *)input1, output, length);
93 
94 	/* Validate output */
95 	zassert_true(
96 		test_near_equal_q15(length, ref, output, ABS_ERROR_THRESH_Q15),
97 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
98 
99 	/* Free output buffer */
100 	free(output);
101 }
102 
103 DEFINE_TEST_VARIANT3(support_f16, arm_f16_to_q15, 7, ref_f16, ref_q15, 7);
104 DEFINE_TEST_VARIANT3(support_f16, arm_f16_to_q15, 16, ref_f16, ref_q15, 16);
105 DEFINE_TEST_VARIANT3(support_f16, arm_f16_to_q15, 23, ref_f16, ref_q15, 23);
106 
test_arm_f16_to_float(const uint16_t * input1,const uint32_t * ref,size_t length)107 static void test_arm_f16_to_float(
108 	const uint16_t *input1, const uint32_t *ref, size_t length)
109 {
110 	float32_t *output;
111 
112 	/* Allocate output buffer */
113 	output = malloc(length * sizeof(float32_t));
114 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
115 
116 	/* Run test function */
117 	arm_f16_to_float((float16_t *)input1, output, length);
118 
119 	/* Validate output */
120 	zassert_true(
121 		test_rel_error_f32(length, (float32_t *)ref, output,
122 			REL_ERROR_THRESH_F32),
123 		ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
124 
125 	/* Free output buffer */
126 	free(output);
127 }
128 
129 DEFINE_TEST_VARIANT3(support_f16, arm_f16_to_float, 7, ref_f16, ref_f32, 7);
130 DEFINE_TEST_VARIANT3(support_f16, arm_f16_to_float, 16, ref_f16, ref_f32, 16);
131 DEFINE_TEST_VARIANT3(support_f16, arm_f16_to_float, 23, ref_f16, ref_f32, 23);
132 
test_arm_q15_to_f16(const q15_t * input1,const uint16_t * ref,size_t length)133 static void test_arm_q15_to_f16(
134 	const q15_t *input1, const uint16_t *ref, size_t length)
135 {
136 	float16_t *output;
137 
138 	/* Allocate output buffer */
139 	output = malloc(length * sizeof(float16_t));
140 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
141 
142 	/* Run test function */
143 	arm_q15_to_f16(input1, output, length);
144 
145 	/* Validate output */
146 	zassert_true(
147 		test_rel_error_f16(length, (float16_t *)ref, output,
148 			REL_ERROR_THRESH_F16),
149 		ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
150 
151 	/* Free output buffer */
152 	free(output);
153 }
154 
155 DEFINE_TEST_VARIANT3(support_f16, arm_q15_to_f16, 7, ref_q15, ref_f16, 7);
156 DEFINE_TEST_VARIANT3(support_f16, arm_q15_to_f16, 16, ref_q15, ref_f16, 16);
157 DEFINE_TEST_VARIANT3(support_f16, arm_q15_to_f16, 23, ref_q15, ref_f16, 23);
158 
test_arm_float_to_f16(const uint32_t * input1,const uint16_t * ref,size_t length)159 static void test_arm_float_to_f16(
160 	const uint32_t *input1, const uint16_t *ref, size_t length)
161 {
162 	float16_t *output;
163 
164 	/* Allocate output buffer */
165 	output = malloc(length * sizeof(float16_t));
166 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
167 
168 	/* Run test function */
169 	arm_float_to_f16((float32_t *)input1, output, length);
170 
171 	/* Validate output */
172 	zassert_true(
173 		test_rel_error_f16(length, (float16_t *)ref, output,
174 			REL_ERROR_THRESH_F16),
175 		ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
176 
177 	/* Free output buffer */
178 	free(output);
179 }
180 
181 DEFINE_TEST_VARIANT3(support_f16, arm_float_to_f16, 7, ref_f32, ref_f16, 7);
182 DEFINE_TEST_VARIANT3(support_f16, arm_float_to_f16, 16, ref_f32, ref_f16, 16);
183 DEFINE_TEST_VARIANT3(support_f16, arm_float_to_f16, 23, ref_f32, ref_f16, 23);
184 
test_arm_weighted_average_f16(int ref_offset,size_t length)185 static void test_arm_weighted_average_f16(
186 	int ref_offset, size_t length)
187 {
188 	const float16_t *val = (const float16_t *)in_weighted_sum_val;
189 	const float16_t *coeff = (const float16_t *)in_weighted_sum_coeff;
190 	const float16_t *ref = (const float16_t *)ref_weighted_sum;
191 	float16_t *output;
192 
193 	/* Allocate output buffer */
194 	output = malloc(1 * sizeof(float16_t));
195 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
196 
197 	/* Run test function */
198 	output[0] = arm_weighted_average_f16(val, coeff, length);
199 
200 	/* Validate output */
201 	zassert_true(
202 		test_close_error_f16(1, output, &ref[ref_offset],
203 			ABS_ERROR_THRESH_WS, REL_ERROR_THRESH_WS),
204 		ASSERT_MSG_ERROR_LIMIT_EXCEED);
205 
206 	/* Free output buffer */
207 	free(output);
208 }
209 
210 DEFINE_TEST_VARIANT2(support_f16, arm_weighted_average_f16, 7, 0, 7);
211 DEFINE_TEST_VARIANT2(support_f16, arm_weighted_average_f16, 16, 1, 16);
212 DEFINE_TEST_VARIANT2(support_f16, arm_weighted_average_f16, 23, 2, 23);
213 
214 ZTEST_SUITE(support_f16, NULL, NULL, NULL, NULL, NULL);
215