1 /*
2  * Copyright 2024 NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <stdlib.h>
9 
10 #include "math_ops.h"
11 #include "input.h"
12 
13 static int start, stop;
14 
test_near_equal_q15(uint32_t length,const int16_t * in_a,const int16_t * in_b,int16_t threshold)15 static inline int test_near_equal_q15(uint32_t length, const int16_t *in_a,
16 				      const int16_t *in_b, int16_t threshold)
17 {
18 	int i;
19 
20 	for (i = 0; i < length; i++) {
21 		if (abs(in_a[i] - in_b[i]) > threshold) {
22 			return 0;
23 		}
24 	}
25 
26 	return 1;
27 }
28 
test_vec_sum_int16_op(void)29 void test_vec_sum_int16_op(void)
30 {
31 	int16_t output[VEC_LENGTH];
32 	int ret = 1; /* test passed successfully */
33 
34 	printk("[Library Test] == Vector Sum test  ==\r\n");
35 	start = k_cycle_get_32();
36 
37 	/* Run test function */
38 	vec_sum_int16(in_a, in_b, output, VEC_LENGTH);
39 	stop = k_cycle_get_32();
40 
41 	/* Validate output */
42 	ret = test_near_equal_q15(VEC_LENGTH, output, ref_add, ABS_ERROR_THRESH_Q15);
43 
44 	printk("[Library Test] Vector Sum takes %d cycles\r\n", stop - start);
45 	printk("[Library Test] == Vector Sum test end with %d ==\r\n\r\n", ret);
46 }
47 
test_power_int16_op(void)48 void test_power_int16_op(void)
49 {
50 	int64_t output;
51 	int ret = 1; /* test passed successfully */
52 
53 	printk("[Library Test] == Vector power sum test  ==\r\n");
54 	start = k_cycle_get_32();
55 
56 	/* Run test function */
57 	vec_power_int16(in_a, &output, 0, VEC_LENGTH);
58 	stop = k_cycle_get_32();
59 
60 	/* Validate output */
61 	if (output != ref_power_16[0]) {
62 		printk("[Library Test] Mismatch: expected %lld result %lld\r\n",
63 		       ref_power_16[0], output);
64 		ret = 0; /* test failed */
65 	}
66 
67 	printk("[Library Test] Vector power sum takes %d cycles\r\n", stop - start);
68 	printk("[Library Test] == Vector power sum test end with %d ==\r\n\r\n", ret);
69 }
70 
test_power_int32_op(void)71 void test_power_int32_op(void)
72 {
73 	int64_t output;
74 
75 	printk("[Library Test] == Vector power sum test  ==\r\n");
76 	start = k_cycle_get_32();
77 
78 	/* Run test function */
79 	vec_power_int32(fir_in_ref, &output, RSH, FIR_LENGTH);
80 	stop = k_cycle_get_32();
81 
82 	printk("[Library Test] Vector power sum takes %d cycles\r\n", stop - start);
83 	printk("[Library Test] == Vector power sum test end ==\r\n\r\n");
84 }
85 
test_fft_op(void)86 void test_fft_op(void)
87 {
88 	int i;
89 	int32_t fft_in[FFT_LENGTH];
90 
91 	/* Create input */
92 	for (i = 0; i < FFT_LENGTH; i++)
93 		fft_in[i] = FFT_LENGTH * (1 + i % 2); /* only real part */
94 
95 	printk("[Library Test] == Fast Fourier Transform on Real Data test  ==\r\n");
96 	start = k_cycle_get_32();
97 
98 	/* Run test function */
99 	fft_real32(fft_in, fft_out, FFT_LENGTH);
100 	stop = k_cycle_get_32();
101 
102 	printk("[Library Test] Fast Fourier Transform on Real Data takes %d cycles\r\n",
103 	       stop - start);
104 	printk("[Library Test] == Fast Fourier Transform on Real Data test end ==\r\n\r\n");
105 }
106 
test_iir_op(void)107 void test_iir_op(void)
108 {
109 	printk("[Library Test] == Bi-quad Real Block IIR test  ==\r\n");
110 	start = k_cycle_get_32();
111 
112 	/* Run test function */
113 	real_block_iir_32(IIR_M, coef_sos, coef_g, iir_in, iir_out, IIR_LENGTH);
114 	stop = k_cycle_get_32();
115 
116 	printk("[Library Test] Bi-quad Real Block IIR takes %d cycles\r\n", stop - start);
117 	printk("[Library Test] == Bi-quad Real Block IIR end ==\r\n\r\n");
118 }
119 
test_fir_blms_op(void)120 void test_fir_blms_op(void)
121 {
122 	int i;
123 	int32_t fir_coef[FIR_M];
124 	int32_t fir_in[FIR_LENGTH];
125 	int32_t fir_ref[FIR_LENGTH + FIR_M];
126 
127 	for (i = 0; i < FIR_M; i++)
128 		fir_coef[i] = fir_coef_ref[i];
129 	for (i = 0; i < FIR_LENGTH; i++)
130 		fir_in[i] = fir_in_ref[i];
131 	for (i = 0; i < FIR_LENGTH + FIR_M; i++)
132 		fir_ref[i] = fir_ref_ref[i];
133 
134 	printk("[Library Test] == Least Mean Square (LMS) Filter for Real Data test  ==\r\n");
135 	start = k_cycle_get_32();
136 
137 	/* Run test function */
138 	lms_iir_32(fir_err, fir_coef, fir_in, fir_ref, MU, FIR_LENGTH, FIR_M);
139 	stop = k_cycle_get_32();
140 
141 	printk("[Library Test] Least Mean Square (LMS) Filter for Real Data test takes %d cycles\r\n",
142 	       stop - start);
143 	printk("[Library Test] == Least Mean Square (LMS) Filter for Real Data test end ==\r\n\r\n");
144 }
145