1 /*
2  * Copyright (C) 2024 OWL Services LLC. All rights reserved.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdlib.h>
8 
9 #include <zephyr/ztest.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/dsp/utils.h>
12 
13 #include "common/test_common.h"
14 
15 #define DEFINE_MULTIPLE_TEST_CASES(constructor, variant, array)                                    \
16 	FOR_EACH_IDX_FIXED_ARG(constructor, (), variant, array)
17 
18 #define RE_DEFINE_TEST_VARIANT3(...) DEFINE_TEST_VARIANT3(__VA_ARGS__)
19 
20 #define DEFINE_SHIFT_F64_CASE(n, args, func)                                                       \
21 	RE_DEFINE_TEST_VARIANT3(shift_f64, func, n, __DEBRACKET args)
22 
23 #define TEST_CASES_SHIFT_F64_TO_Q7 (-1.0, 0, -128), (1.0, 0, 127), (1.0, 7, 1), (-1.0, 7, -1)
24 
25 #define TEST_CASES_SHIFT_F64_TO_Q15 (-1.0, 0, -32768), (1.0, 0, 32767), (1.0, 15, 1), (-1.0, 15, -1)
26 
27 #define TEST_CASES_SHIFT_F64_TO_Q31                                                                \
28 	(-1.0, 0, INT32_MIN), (1.0, 0, INT32_MAX), (1.0, 31, 1), (-1.0, 31, -1)
29 
test_shift_f64_to_q7(const float64_t data,const uint32_t shift,const DSP_DATA q7_t expected)30 static void test_shift_f64_to_q7(const float64_t data, const uint32_t shift,
31 				 const DSP_DATA q7_t expected)
32 {
33 	const q7_t shifted_data = Z_SHIFT_F64_TO_Q7(data, shift);
34 
35 	zassert_equal(shifted_data, expected,
36 		      "Conversion failed: %f shifted by %d = %d (expected %d)", data, shift,
37 		      shifted_data, expected);
38 }
39 
test_shift_f64_to_q15(const float64_t data,const uint32_t shift,const DSP_DATA q15_t expected)40 static void test_shift_f64_to_q15(const float64_t data, const uint32_t shift,
41 				  const DSP_DATA q15_t expected)
42 {
43 	const q15_t shifted_data = Z_SHIFT_F64_TO_Q15(data, shift);
44 
45 	zassert_equal(shifted_data, expected,
46 		      "Conversion failed: %f shifted by %d = %d (expected %d)", (double)data, shift,
47 		      shifted_data, expected);
48 }
49 
test_shift_f64_to_q31(const float64_t data,const uint32_t shift,const DSP_DATA q31_t expected)50 static void test_shift_f64_to_q31(const float64_t data, const uint32_t shift,
51 				  const DSP_DATA q31_t expected)
52 {
53 	const q31_t shifted_data = Z_SHIFT_F64_TO_Q31(data, shift);
54 
55 	zassert_equal(shifted_data, expected,
56 		      "Conversion failed: %f shifted by %d = %d (expected %d)", (double)data, shift,
57 		      shifted_data, expected);
58 }
59 
60 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_F64_CASE, shift_f64_to_q7, TEST_CASES_SHIFT_F64_TO_Q7)
61 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_F64_CASE, shift_f64_to_q15, TEST_CASES_SHIFT_F64_TO_Q15)
62 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_F64_CASE, shift_f64_to_q31, TEST_CASES_SHIFT_F64_TO_Q31)
63 
64 ZTEST_SUITE(shift_f64, NULL, NULL, NULL, NULL, NULL);
65