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_F32_CASE(n, args, func) \
21 RE_DEFINE_TEST_VARIANT3(shift_f32, func, n, __DEBRACKET args)
22
23 #define TEST_CASES_SHIFT_F32_TO_Q7 (-1.0F, 0, -128), (1.0F, 0, 127), (1.0F, 7, 1), (-1.0F, 7, -1)
24
25 #define TEST_CASES_SHIFT_F32_TO_Q15 \
26 (-1.0F, 0, -32768), (1.0F, 0, 32767), (1.0F, 15, 1), (-1.0F, 15, -1)
27
28 #define TEST_CASES_SHIFT_F32_TO_Q31 \
29 (-1.0F, 0, -2147483648), (1.0F, 0, 2147483647), (1.0F, 31, 1), (-1.0F, 31, -1)
30
test_shift_f32_to_q7(const float32_t data,const uint32_t shift,const DSP_DATA q31_t expected)31 static void test_shift_f32_to_q7(const float32_t data, const uint32_t shift,
32 const DSP_DATA q31_t expected)
33 {
34 q7_t shifted_data = Z_SHIFT_F32_TO_Q7(data, shift);
35
36 zassert_equal(shifted_data, expected,
37 "Conversion failed: %f shifted by %d = %d (expected %d)", (double)data, shift,
38 shifted_data, expected);
39 }
40
test_shift_f32_to_q15(const float32_t data,const uint32_t shift,const DSP_DATA q31_t expected)41 static void test_shift_f32_to_q15(const float32_t data, const uint32_t shift,
42 const DSP_DATA q31_t expected)
43 {
44 q15_t shifted_data = Z_SHIFT_F32_TO_Q15(data, shift);
45
46 zassert_equal(shifted_data, expected,
47 "Conversion failed: %f shifted by %d = %d (expected %d)", (double)data, shift,
48 shifted_data, expected);
49 }
50
test_shift_f32_to_q31(const float32_t data,const uint32_t shift,const DSP_DATA q31_t expected)51 static void test_shift_f32_to_q31(const float32_t data, const uint32_t shift,
52 const DSP_DATA q31_t expected)
53 {
54 q31_t shifted_data = Z_SHIFT_F32_TO_Q31(data, shift);
55
56 zassert_equal(shifted_data, expected,
57 "Conversion failed: %f shifted by %d = %d (expected %d)", (double)data, shift,
58 shifted_data, expected);
59 }
60
61 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_F32_CASE, shift_f32_to_q7, TEST_CASES_SHIFT_F32_TO_Q7)
62 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_F32_CASE, shift_f32_to_q15, TEST_CASES_SHIFT_F32_TO_Q15)
63 DEFINE_MULTIPLE_TEST_CASES(DEFINE_SHIFT_F32_CASE, shift_f32_to_q31, TEST_CASES_SHIFT_F32_TO_Q31)
64
65 ZTEST_SUITE(shift_f32, NULL, NULL, NULL, NULL, NULL);
66