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_Q7_CASE(n, args, func)                                                        \
21 	RE_DEFINE_TEST_VARIANT3(shift_q7, func, n, __DEBRACKET args)
22 
23 #define TEST_CORNER_CASES_SHIFT_Q7_TO_F32                                                          \
24 	(-128, 0, -1.0F), (127, 0, 0.9921875F), (127, 7, 127.0F), (-128, 7, -128.0F)
25 
26 #define TEST_CORNER_CASES_SHIFT_Q7_TO_F64                                                          \
27 	(-128, 0, -1.0), (127, 0, 0.9921875), (127, 7, 127.0), (-128, 7, -128.0)
28 
test_shift_q7_to_f32(const q7_t data,const uint32_t shift,const float32_t expected)29 static void test_shift_q7_to_f32(const q7_t data, const uint32_t shift, const float32_t expected)
30 {
31 	const float32_t shifted_data = Z_SHIFT_Q7_TO_F32(data, shift);
32 
33 	zassert_equal(shifted_data, expected,
34 		      "Conversion failed: 0x%08x shifted by %d = %f (expected %f)", data, shift,
35 		      (double)shifted_data, (double)expected);
36 }
37 
test_shift_q7_to_f64(const q7_t data,const uint32_t shift,const float64_t expected)38 static void test_shift_q7_to_f64(const q7_t data, const uint32_t shift, const float64_t expected)
39 {
40 	const float64_t shifted_data = Z_SHIFT_Q7_TO_F64(data, shift);
41 
42 	zassert_equal(shifted_data, expected,
43 		      "Conversion failed: 0x%08x shifted by %d = %f (expected %f)", data, shift,
44 		      shifted_data, expected);
45 }
46 
47 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_Q7_CASE, shift_q7_to_f32, TEST_CORNER_CASES_SHIFT_Q7_TO_F32)
48 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_Q7_CASE, shift_q7_to_f64, TEST_CORNER_CASES_SHIFT_Q7_TO_F64)
49 
50 ZTEST_SUITE(shift_q7, NULL, NULL, NULL, NULL, NULL);
51