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_Q15_CASE(n, args, func)                                                       \
21 	RE_DEFINE_TEST_VARIANT3(shift_q15, func, n, __DEBRACKET args)
22 
23 #define TEST_CORNER_CASES_SHIFT_Q15_TO_F32                                                         \
24 	(-32768, 0, -1.0F), (32767, 0, 0.999969482421875F), (32767, 15, 32767.0F),                 \
25 		(-32768, 15, -32768.0F)
26 
27 #define TEST_CORNER_CASES_SHIFT_Q15_TO_F64                                                         \
28 	(-32768, 0, -1), (32767, 0, 0.999969482421875), (32767, 15, 32767.0), (-32768, 15, -32768.0)
29 
test_shift_q15_to_f32(const q15_t data,const uint32_t shift,const float32_t expected)30 static void test_shift_q15_to_f32(const q15_t data, const uint32_t shift, const float32_t expected)
31 {
32 	const float32_t shifted_data = Z_SHIFT_Q15_TO_F32(data, shift);
33 
34 	zassert_equal(shifted_data, expected,
35 		      "Conversion failed: 0x%08x shifted by %d = %f (expected %f)", data, shift,
36 		      (double)shifted_data, (double)expected);
37 }
38 
test_shift_q15_to_f64(const q15_t data,const uint32_t shift,const float64_t expected)39 static void test_shift_q15_to_f64(const q15_t data, const uint32_t shift, const float64_t expected)
40 {
41 	const float64_t shifted_data = Z_SHIFT_Q15_TO_F64(data, shift);
42 
43 	zassert_equal(shifted_data, expected,
44 		      "Conversion failed: 0x%08x shifted by %d = %f (expected %f)", data, shift,
45 		      shifted_data, expected);
46 }
47 
48 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_Q15_CASE, shift_q15_to_f32,
49 			   TEST_CORNER_CASES_SHIFT_Q15_TO_F32)
50 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_Q15_CASE, shift_q15_to_f64,
51 			   TEST_CORNER_CASES_SHIFT_Q15_TO_F64)
52 
53 ZTEST_SUITE(shift_q15, NULL, NULL, NULL, NULL, NULL);
54