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 "q31.pat"
15 
16 #define SNR_ERROR_THRESH	((float32_t)100)
17 #define ABS_ERROR_THRESH	((q31_t)2200)
18 
19 ZTEST_SUITE(fastmath_q31, NULL, NULL, NULL, NULL, NULL);
20 
ZTEST(fastmath_q31,test_arm_cos_q31)21 ZTEST(fastmath_q31, test_arm_cos_q31)
22 {
23 	size_t index;
24 	size_t length = ARRAY_SIZE(in_angles);
25 	q31_t *output;
26 
27 	/* Allocate output buffer */
28 	output = malloc(length * sizeof(q31_t));
29 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
30 
31 	/* Run test function */
32 	for (index = 0; index < length; index++) {
33 		output[index] = arm_cos_q31(in_angles[index]);
34 	}
35 
36 	/* Validate output */
37 	zassert_true(
38 		test_snr_error_q31(length, output, ref_cos, SNR_ERROR_THRESH),
39 		ASSERT_MSG_SNR_LIMIT_EXCEED);
40 
41 	zassert_true(
42 		test_near_equal_q31(length, output, ref_cos, ABS_ERROR_THRESH),
43 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
44 
45 	/* Free output buffer */
46 	free(output);
47 }
48 
ZTEST(fastmath_q31,test_arm_sin_q31)49 ZTEST(fastmath_q31, test_arm_sin_q31)
50 {
51 	size_t index;
52 	size_t length = ARRAY_SIZE(in_angles);
53 	q31_t *output;
54 
55 	/* Allocate output buffer */
56 	output = malloc(length * sizeof(q31_t));
57 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
58 
59 	/* Run test function */
60 	for (index = 0; index < length; index++) {
61 		output[index] = arm_sin_q31(in_angles[index]);
62 	}
63 
64 	/* Validate output */
65 	zassert_true(
66 		test_snr_error_q31(length, output, ref_sin, SNR_ERROR_THRESH),
67 		ASSERT_MSG_SNR_LIMIT_EXCEED);
68 
69 	zassert_true(
70 		test_near_equal_q31(length, output, ref_sin, ABS_ERROR_THRESH),
71 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
72 
73 	/* Free output buffer */
74 	free(output);
75 }
76 
ZTEST(fastmath_q31,test_arm_sqrt_q31)77 ZTEST(fastmath_q31, test_arm_sqrt_q31)
78 {
79 	size_t index;
80 	size_t length = ARRAY_SIZE(in_sqrt);
81 	arm_status status;
82 	q31_t *output;
83 
84 	/* Allocate output buffer */
85 	output = malloc(length * sizeof(q31_t));
86 	zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
87 
88 	/* Run test function */
89 	for (index = 0; index < length; index++) {
90 		status = arm_sqrt_q31(in_sqrt[index], &output[index]);
91 
92 		/* Validate operation status */
93 		if (in_sqrt[index] < 0) {
94 			zassert_equal(status, ARM_MATH_ARGUMENT_ERROR,
95 				"square root did fail with an input value "
96 				"of less than '0'");
97 		} else {
98 			zassert_equal(status, ARM_MATH_SUCCESS,
99 				"square root operation did not succeed");
100 		}
101 	}
102 
103 	/* Validate output */
104 	zassert_true(
105 		test_snr_error_q31(length, output, ref_sqrt, SNR_ERROR_THRESH),
106 		ASSERT_MSG_SNR_LIMIT_EXCEED);
107 
108 	zassert_true(
109 		test_near_equal_q31(length, output, ref_sqrt,
110 			ABS_ERROR_THRESH),
111 		ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
112 
113 	/* Free output buffer */
114 	free(output);
115 }
116