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_Q31_CASE(n, args, func)                                                       \
21 	RE_DEFINE_TEST_VARIANT3(shift_q31, func, n, __DEBRACKET args)
22 
23 #define TEST_CORNER_CASES_SHIFT_Q31_TO_F32                                                         \
24 	(2147483647, 0, 1.0F), (-2147483648, 0, -1.0F), (-2147483648, 31, -2147483648.0F),         \
25 		(2147483647, 31, 2147483647.0F)
26 
27 #define TEST_CORNER_CASES_SHIFT_Q31_TO_F64                                                         \
28 	(2147483647, 0, 0.9999999995343387), (-2147483648, 0, -1.0),                               \
29 		(-2147483648, 31, -2147483648.0), (2147483647, 31, 2147483647.0)
30 
test_shift_q31_to_f32(const q31_t data,const uint32_t shift,const float32_t expected)31 static void test_shift_q31_to_f32(const q31_t data, const uint32_t shift, const float32_t expected)
32 {
33 	const float32_t shifted_data = Z_SHIFT_Q31_TO_F32(data, shift);
34 
35 	zassert_equal(shifted_data, expected,
36 		      "Conversion failed: 0x%08x shifted by %d = %f (expected %f)", data, shift,
37 		      (double)shifted_data, (double)expected);
38 }
39 
test_shift_q31_to_f64(const q31_t data,const uint32_t shift,const float64_t expected)40 static void test_shift_q31_to_f64(const q31_t data, const uint32_t shift, const float64_t expected)
41 {
42 	const float64_t shifted_data = Z_SHIFT_Q31_TO_F64(data, shift);
43 
44 	zassert_equal(shifted_data, expected,
45 		      "Conversion failed: 0x%08x shifted by %d = %f (expected %f)", data, shift,
46 		      shifted_data, expected);
47 }
48 
49 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_Q31_CASE, shift_q31_to_f32,
50 			   TEST_CORNER_CASES_SHIFT_Q31_TO_F32)
51 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_Q31_CASE, shift_q31_to_f64,
52 			   TEST_CORNER_CASES_SHIFT_Q31_TO_F64)
53 
54 ZTEST_SUITE(shift_q31, NULL, NULL, NULL, NULL, NULL);
55