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 REL_ERROR_THRESH (1.0e-5)
17 #define ABS_ERROR_THRESH_Q7 ((q7_t)10)
18 #define ABS_ERROR_THRESH_Q15 ((q15_t)10)
19 #define ABS_ERROR_THRESH_Q31 ((q31_t)80)
20
test_arm_copy_q31(const q31_t * input1,size_t length)21 static void test_arm_copy_q31(const q31_t *input1, size_t length)
22 {
23 q31_t *output;
24
25 /* Allocate output buffer */
26 output = malloc(length * sizeof(q31_t));
27 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
28
29 /* Run test function */
30 arm_copy_q31(input1, output, length);
31
32 /* Validate output */
33 zassert_true(
34 test_equal_q31(length, input1, output),
35 ASSERT_MSG_INCORRECT_COMP_RESULT);
36
37 /* Free output buffer */
38 free(output);
39 }
40
41 DEFINE_TEST_VARIANT2(support_q31, arm_copy_q31, 3, in_q31, 3);
42 DEFINE_TEST_VARIANT2(support_q31, arm_copy_q31, 8, in_q31, 8);
43 DEFINE_TEST_VARIANT2(support_q31, arm_copy_q31, 11, in_q31, 11);
44
test_arm_fill_q31(size_t length)45 static void test_arm_fill_q31(size_t length)
46 {
47 size_t index;
48 q31_t *output;
49 q31_t val = 0x40000000;
50
51 /* Allocate output buffer */
52 output = malloc(length * sizeof(q31_t));
53 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
54
55 /* Run test function */
56 arm_fill_q31(val, output, length);
57
58 /* Validate output */
59 for (index = 0; index < length; index++) {
60 zassert_equal(
61 output[index], val, ASSERT_MSG_INCORRECT_COMP_RESULT);
62 }
63
64 /* Free output buffer */
65 free(output);
66 }
67
68 DEFINE_TEST_VARIANT1(support_q31, arm_fill_q31, 3, 3);
69 DEFINE_TEST_VARIANT1(support_q31, arm_fill_q31, 8, 8);
70 DEFINE_TEST_VARIANT1(support_q31, arm_fill_q31, 11, 11);
71
test_arm_q31_to_float(const q31_t * input1,const uint32_t * ref,size_t length)72 static void test_arm_q31_to_float(
73 const q31_t *input1, const uint32_t *ref, size_t length)
74 {
75 float32_t *output;
76
77 /* Allocate output buffer */
78 output = malloc(length * sizeof(float32_t));
79 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
80
81 /* Run test function */
82 arm_q31_to_float(input1, output, length);
83
84 /* Validate output */
85 zassert_true(
86 test_rel_error_f32(
87 length, (float32_t *)ref, output, REL_ERROR_THRESH),
88 ASSERT_MSG_REL_ERROR_LIMIT_EXCEED);
89
90 /* Free output buffer */
91 free(output);
92 }
93
94 DEFINE_TEST_VARIANT3(support_q31, arm_q31_to_float, 7, in_q31, ref_f32, 7);
95 DEFINE_TEST_VARIANT3(support_q31, arm_q31_to_float, 16, in_q31, ref_f32, 16);
96 DEFINE_TEST_VARIANT3(support_q31, arm_q31_to_float, 17, in_q31, ref_f32, 17);
97
test_arm_q31_to_q15(const q31_t * input1,const q15_t * ref,size_t length)98 static void test_arm_q31_to_q15(
99 const q31_t *input1, const q15_t *ref, size_t length)
100 {
101 q15_t *output;
102
103 /* Allocate output buffer */
104 output = malloc(length * sizeof(q15_t));
105 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
106
107 /* Run test function */
108 arm_q31_to_q15(input1, output, length);
109
110 /* Validate output */
111 zassert_true(
112 test_near_equal_q15(length, ref, output, ABS_ERROR_THRESH_Q15),
113 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
114
115 /* Free output buffer */
116 free(output);
117 }
118
119 DEFINE_TEST_VARIANT3(support_q31, arm_q31_to_q15, 3, in_q31, ref_q15, 3);
120 DEFINE_TEST_VARIANT3(support_q31, arm_q31_to_q15, 8, in_q31, ref_q15, 8);
121 DEFINE_TEST_VARIANT3(support_q31, arm_q31_to_q15, 11, in_q31, ref_q15, 11);
122
test_arm_q31_to_q7(const q31_t * input1,const q7_t * ref,size_t length)123 static void test_arm_q31_to_q7(
124 const q31_t *input1, const q7_t *ref, size_t length)
125 {
126 q7_t *output;
127
128 /* Allocate output buffer */
129 output = malloc(length * sizeof(q7_t));
130 zassert_not_null(output, ASSERT_MSG_BUFFER_ALLOC_FAILED);
131
132 /* Run test function */
133 arm_q31_to_q7(input1, output, length);
134
135 /* Validate output */
136 zassert_true(
137 test_near_equal_q7(length, ref, output, ABS_ERROR_THRESH_Q7),
138 ASSERT_MSG_ABS_ERROR_LIMIT_EXCEED);
139
140 /* Free output buffer */
141 free(output);
142 }
143
144 DEFINE_TEST_VARIANT3(support_q31, arm_q31_to_q7, 15, in_q31, ref_q7, 15);
145 DEFINE_TEST_VARIANT3(support_q31, arm_q31_to_q7, 32, in_q31, ref_q7, 32);
146 DEFINE_TEST_VARIANT3(support_q31, arm_q31_to_q7, 33, in_q31, ref_q7, 33);
147
148 ZTEST_SUITE(support_q31, NULL, NULL, NULL, NULL, NULL);
149