1 /*
2  * Copyright (c) 2020 Stephanos Ioannidis <root@stephanos.io>
3  * Copyright (C) 2010-2020 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 "f32.pat"
15 
16 #define SNR_ERROR_THRESH	((float32_t)120)
17 #define REL_ERROR_THRESH	(1.0e-6)
18 #define ABS_ERROR_THRESH	(1.0e-5)
19 
20 ZTEST_SUITE(fastmath_f32, NULL, NULL, NULL, NULL, NULL);
21 
ZTEST(fastmath_f32,test_arm_cos_f32)22 ZTEST(fastmath_f32, test_arm_cos_f32)
23 {
24 	size_t index;
25 	size_t length = ARRAY_SIZE(in_angles);
26 	float32_t *output;
27 
28 	/* Allocate output buffer */
29 	output = malloc(length * sizeof(float32_t));
30 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
31 
32 	/* Run test function */
33 	for (index = 0; index < length; index++) {
34 		output[index] = arm_cos_f32(((float32_t *)in_angles)[index]);
35 	}
36 
37 	/* Validate output */
38 	zassert_true(
39 		test_snr_error_f32(length, output, (float32_t *)ref_cos,
40 			SNR_ERROR_THRESH),
41 		ASSERT_MSG_SNR_LIMIT_EXCEED);
42 
43 	zassert_true(
44 		test_close_error_f32(length, output, (float32_t *)ref_cos,
45 			ABS_ERROR_THRESH, REL_ERROR_THRESH),
46 		ASSERT_MSG_ERROR_LIMIT_EXCEED);
47 
48 	/* Free output buffer */
49 	free(output);
50 }
51 
ZTEST(fastmath_f32,test_arm_sin_f32)52 ZTEST(fastmath_f32, test_arm_sin_f32)
53 {
54 	size_t index;
55 	size_t length = ARRAY_SIZE(in_angles);
56 	float32_t *output;
57 
58 	/* Allocate output buffer */
59 	output = malloc(length * sizeof(float32_t));
60 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
61 
62 	/* Run test function */
63 	for (index = 0; index < length; index++) {
64 		output[index] = arm_sin_f32(((float32_t *)in_angles)[index]);
65 	}
66 
67 	/* Validate output */
68 	zassert_true(
69 		test_snr_error_f32(length, output, (float32_t *)ref_sin,
70 			SNR_ERROR_THRESH),
71 		ASSERT_MSG_SNR_LIMIT_EXCEED);
72 
73 	zassert_true(
74 		test_close_error_f32(length, output, (float32_t *)ref_sin,
75 			ABS_ERROR_THRESH, REL_ERROR_THRESH),
76 		ASSERT_MSG_ERROR_LIMIT_EXCEED);
77 
78 	/* Free output buffer */
79 	free(output);
80 }
81 
ZTEST(fastmath_f32,test_arm_sqrt_f32)82 ZTEST(fastmath_f32, test_arm_sqrt_f32)
83 {
84 	size_t index;
85 	size_t length = ARRAY_SIZE(in_sqrt);
86 	arm_status status;
87 	float32_t *output;
88 
89 	/* Allocate output buffer */
90 	output = malloc(length * sizeof(float32_t));
91 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
92 
93 	/* Run test function */
94 	for (index = 0; index < length; index++) {
95 		status = arm_sqrt_f32(
96 			((float32_t *)in_sqrt)[index], &output[index]);
97 
98 		/* Validate operation status */
99 		if (((float32_t *)in_sqrt)[index] < 0.0f) {
100 			zassert_equal(status, ARM_MATH_ARGUMENT_ERROR,
101 				"square root did fail with an input value "
102 				"of '0'");
103 		} else {
104 			zassert_equal(status, ARM_MATH_SUCCESS,
105 				"square root operation did not succeed");
106 		}
107 	}
108 
109 	/* Validate output */
110 	zassert_true(
111 		test_snr_error_f32(length, output, (float32_t *)ref_sqrt,
112 			SNR_ERROR_THRESH),
113 		ASSERT_MSG_SNR_LIMIT_EXCEED);
114 
115 	zassert_true(
116 		test_close_error_f32(length, output, (float32_t *)ref_sqrt,
117 			ABS_ERROR_THRESH, REL_ERROR_THRESH),
118 		ASSERT_MSG_ERROR_LIMIT_EXCEED);
119 
120 	/* Free output buffer */
121 	free(output);
122 }
123 
test_arm_vlog_f32(const uint32_t * input1,const uint32_t * ref,size_t length)124 static void test_arm_vlog_f32(
125 	const uint32_t *input1, const uint32_t *ref, size_t length)
126 {
127 	float32_t *output;
128 
129 	/* Allocate output buffer */
130 	output = malloc(length * sizeof(float32_t));
131 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
132 
133 	/* Run test function */
134 	arm_vlog_f32((float32_t *)input1, output, length);
135 
136 	/* Validate output */
137 	zassert_true(
138 		test_snr_error_f32(length, output, (float32_t *)ref,
139 			SNR_ERROR_THRESH),
140 		ASSERT_MSG_SNR_LIMIT_EXCEED);
141 
142 	zassert_true(
143 		test_close_error_f32(length, output, (float32_t *)ref,
144 			ABS_ERROR_THRESH, REL_ERROR_THRESH),
145 		ASSERT_MSG_ERROR_LIMIT_EXCEED);
146 
147 	/* Free output buffer */
148 	free(output);
149 }
150 
151 DEFINE_TEST_VARIANT3(fastmath_f32, arm_vlog_f32, all, in_log, ref_log, 25);
152 DEFINE_TEST_VARIANT3(fastmath_f32, arm_vlog_f32, 3, in_log, ref_log, 3);
153 DEFINE_TEST_VARIANT3(fastmath_f32, arm_vlog_f32, 8, in_log, ref_log, 8);
154 DEFINE_TEST_VARIANT3(fastmath_f32, arm_vlog_f32, 11, in_log, ref_log, 11);
155 
test_arm_vexp_f32(const uint32_t * input1,const uint32_t * ref,size_t length)156 static void test_arm_vexp_f32(
157 	const uint32_t *input1, const uint32_t *ref, size_t length)
158 {
159 	float32_t *output;
160 
161 	/* Allocate output buffer */
162 	output = malloc(length * sizeof(float32_t));
163 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
164 
165 	/* Run test function */
166 	arm_vexp_f32((float32_t *)input1, output, length);
167 
168 	/* Validate output */
169 	zassert_true(
170 		test_snr_error_f32(length, output, (float32_t *)ref,
171 			SNR_ERROR_THRESH),
172 		ASSERT_MSG_SNR_LIMIT_EXCEED);
173 
174 	zassert_true(
175 		test_close_error_f32(length, output, (float32_t *)ref,
176 			ABS_ERROR_THRESH, REL_ERROR_THRESH),
177 		ASSERT_MSG_ERROR_LIMIT_EXCEED);
178 
179 	/* Free output buffer */
180 	free(output);
181 }
182 
183 DEFINE_TEST_VARIANT3(fastmath_f32, arm_vexp_f32, all, in_exp, ref_exp, 52);
184 DEFINE_TEST_VARIANT3(fastmath_f32, arm_vexp_f32, 3, in_exp, ref_exp, 3);
185 DEFINE_TEST_VARIANT3(fastmath_f32, arm_vexp_f32, 8, in_exp, ref_exp, 8);
186 DEFINE_TEST_VARIANT3(fastmath_f32, arm_vexp_f32, 11, in_exp, ref_exp, 11);
187