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 "q15.pat"
15
16 #define SNR_ERROR_THRESH ((float32_t)70)
17 #define ABS_ERROR_THRESH ((q15_t)10)
18
19 ZTEST_SUITE(fastmath_q15, NULL, NULL, NULL, NULL, NULL);
20
ZTEST(fastmath_q15,test_arm_cos_q15)21 ZTEST(fastmath_q15, test_arm_cos_q15)
22 {
23 size_t index;
24 size_t length = ARRAY_SIZE(in_angles);
25 q15_t *output;
26
27 /* Allocate output buffer */
28 output = malloc(length * sizeof(q15_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_q15(in_angles[index]);
34 }
35
36 /* Validate output */
37 zassert_true(
38 test_snr_error_q15(length, output, ref_cos, SNR_ERROR_THRESH),
39 ASSERT_MSG_SNR_LIMIT_EXCEED);
40
41 zassert_true(
42 test_near_equal_q15(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_q15,test_arm_sin_q15)49 ZTEST(fastmath_q15, test_arm_sin_q15)
50 {
51 size_t index;
52 size_t length = ARRAY_SIZE(in_angles);
53 q15_t *output;
54
55 /* Allocate output buffer */
56 output = malloc(length * sizeof(q15_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_q15(in_angles[index]);
62 }
63
64 /* Validate output */
65 zassert_true(
66 test_snr_error_q15(length, output, ref_sin, SNR_ERROR_THRESH),
67 ASSERT_MSG_SNR_LIMIT_EXCEED);
68
69 zassert_true(
70 test_near_equal_q15(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_q15,test_arm_sqrt_q15)77 ZTEST(fastmath_q15, test_arm_sqrt_q15)
78 {
79 size_t index;
80 size_t length = ARRAY_SIZE(in_sqrt);
81 arm_status status;
82 q15_t *output;
83
84 /* Allocate output buffer */
85 output = malloc(length * sizeof(q15_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_q15(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_q15(length, output, ref_sqrt, SNR_ERROR_THRESH),
106 ASSERT_MSG_SNR_LIMIT_EXCEED);
107
108 zassert_true(
109 test_near_equal_q15(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
ZTEST(fastmath_q15,test_arm_divide_q15)117 ZTEST(fastmath_q15, test_arm_divide_q15)
118 {
119 size_t index;
120 size_t length = ARRAY_SIZE(ref_divide);
121 arm_status status;
122 q15_t *output;
123 uint16_t *shift;
124
125 /* Allocate output buffer */
126 output = malloc(length * sizeof(q15_t));
127 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
128
129 shift = malloc(length * sizeof(uint16_t));
130 zassert_not_null(shift, ASSERT_MSG_BUFFER_ALLOC_FAILED);
131
132 /* Run test function */
133 for (index = 0; index < length; index++) {
134 status = arm_divide_q15(
135 in_divide_num[index], in_divide_den[index],
136 &output[index], &shift[index]);
137 }
138
139 /* Validate output */
140 zassert_true(
141 test_snr_error_q15(length, output, ref_divide, SNR_ERROR_THRESH),
142 ASSERT_MSG_SNR_LIMIT_EXCEED);
143
144 zassert_true(
145 test_near_equal_q15(length, output, ref_divide,
146 ABS_ERROR_THRESH),
147 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
148
149 zassert_true(
150 memcmp(shift, ref_divide_shift,
151 length * sizeof(uint16_t)) == 0,
152 ASSERT_MSG_INCORRECT_COMP_RESULT);
153
154 /* Free output buffer */
155 free(output);
156 free(shift);
157 }
158