From 385485aa70f08a8eafcd21658018b02b934d26a6 Mon Sep 17 00:00:00 2001 From: Tomasz Leman Date: Tue, 5 Aug 2025 12:47:11 +0200 Subject: [PATCH] test: ztest: Add math basic trigonometry unit tests converted from CMock This patch converts 9 existing math trigonometry unit tests from CMock/Unity to Zephyr's Ztest framework, maintaining the same test coverage and functionality: - test_sin_32b_fixed: 32-bit fixed-point sine function - test_sin_16b_fixed: 16-bit fixed-point sine function - test_cos_32b_fixed: 32-bit fixed-point cosine function - test_cos_16b_fixed: 16-bit fixed-point cosine function - test_asin_32b_fixed: 32-bit fixed-point arcsine function - test_asin_16b_fixed: 16-bit fixed-point arcsine function - test_acos_32b_fixed: 32-bit fixed-point arccosine function - test_acos_16b_fixed: 16-bit fixed-point arccosine function - test_sin_lut_16b_fixed: 16-bit sine lookup table function Original tests converted from sof/test/cmocka/src/math/trig/ authored by: - Slawomir Blauciak - Shriram Shastry - Seppo Ingalsuo The converted tests validate the same trigonometric functions from src/math/trig.c and src/math/lut_trig.c as the original CMock tests, ensuring no regression in test coverage during the migration to Ztest framework. Reference tables and tolerance values are preserved to maintain identical test accuracy and validation criteria. This is part of the broader SOF unit test migration from CMock to Zephyr Ztest framework, establishing the foundation for math/basic/trigonometry tests in the new directory structure. Signed-off-by: Tomasz Leman --- .../math/basic/trigonometry/CMakeLists.txt | 28 ++ .../unit/math/basic/trigonometry/prj.conf | 1 + .../math/basic/trigonometry/testcase.yaml | 24 ++ .../math/basic/trigonometry/trig_tables.h | 371 ++++++++++++++++++ .../unit/math/basic/trigonometry/trig_test.c | 225 +++++++++++ 5 files changed, 649 insertions(+) create mode 100644 test/ztest/unit/math/basic/trigonometry/CMakeLists.txt create mode 100644 test/ztest/unit/math/basic/trigonometry/prj.conf create mode 100644 test/ztest/unit/math/basic/trigonometry/testcase.yaml create mode 100644 test/ztest/unit/math/basic/trigonometry/trig_tables.h create mode 100644 test/ztest/unit/math/basic/trigonometry/trig_test.c diff --git a/test/ztest/unit/math/basic/trigonometry/CMakeLists.txt b/test/ztest/unit/math/basic/trigonometry/CMakeLists.txt new file mode 100644 index 000000000000..b5387a998aee --- /dev/null +++ b/test/ztest/unit/math/basic/trigonometry/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(test_math_trigonometry) + +set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../../..") + +target_include_directories(app PRIVATE + ${SOF_ROOT}/zephyr/include + ${SOF_ROOT}/src/include +) + +# Define SOF-specific configurations for unit testing +target_compile_definitions(app PRIVATE + -DCONFIG_SOF_LOG_LEVEL=CONFIG_LOG_DEFAULT_LEVEL + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +target_sources(app PRIVATE + trig_test.c + ${SOF_ROOT}/src/math/trig.c + ${SOF_ROOT}/src/math/lut_trig.c +) + +# Link math library for standard math functions +target_link_libraries(app PRIVATE m) diff --git a/test/ztest/unit/math/basic/trigonometry/prj.conf b/test/ztest/unit/math/basic/trigonometry/prj.conf new file mode 100644 index 000000000000..9467c2926896 --- /dev/null +++ b/test/ztest/unit/math/basic/trigonometry/prj.conf @@ -0,0 +1 @@ +CONFIG_ZTEST=y diff --git a/test/ztest/unit/math/basic/trigonometry/testcase.yaml b/test/ztest/unit/math/basic/trigonometry/testcase.yaml new file mode 100644 index 000000000000..037fbcaac456 --- /dev/null +++ b/test/ztest/unit/math/basic/trigonometry/testcase.yaml @@ -0,0 +1,24 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2025 Intel Corporation. All rights reserved. +# +# Math basic trigonometry unit tests converted from CMock to Ztest +# +# These contents may have been developed with support from one or more Intel-operated +# generative artificial intelligence solutions. +# + +common: + tags: + - SOF + - unit_test + - math + - trigonometry + integration_platforms: + - native_sim + arch_exclude: xtensa # Test is for host builds only + +tests: + sof.unit.math.trigonometry: + platform_allow: + - native_sim diff --git a/test/ztest/unit/math/basic/trigonometry/trig_tables.h b/test/ztest/unit/math/basic/trigonometry/trig_tables.h new file mode 100644 index 000000000000..33405231cf48 --- /dev/null +++ b/test/ztest/unit/math/basic/trigonometry/trig_tables.h @@ -0,0 +1,371 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2025 Intel Corporation. All rights reserved. + */ + +#ifndef __SOF_ZTEST_TRIG_TABLES_H__ +#define __SOF_ZTEST_TRIG_TABLES_H__ + +/* Reference table generated by sin(), gcc-4.3.2 */ +static const float sin_ref_table[] = { + 0.0000000000, 0.0174524064, 0.0348994967, 0.0523359562, + 0.0697564737, 0.0871557427, 0.1045284633, 0.1218693434, + 0.1391731010, 0.1564344650, 0.1736481777, 0.1908089954, + 0.2079116908, 0.2249510543, 0.2419218956, 0.2588190451, + 0.2756373558, 0.2923717047, 0.3090169944, 0.3255681545, + 0.3420201433, 0.3583679495, 0.3746065934, 0.3907311285, + 0.4067366431, 0.4226182617, 0.4383711468, 0.4539904997, + 0.4694715628, 0.4848096202, 0.5000000000, 0.5150380749, + 0.5299192642, 0.5446390350, 0.5591929035, 0.5735764364, + 0.5877852523, 0.6018150232, 0.6156614753, 0.6293203910, + 0.6427876097, 0.6560590290, 0.6691306064, 0.6819983601, + 0.6946583705, 0.7071067812, 0.7193398003, 0.7313537016, + 0.7431448255, 0.7547095802, 0.7660444431, 0.7771459615, + 0.7880107536, 0.7986355100, 0.8090169944, 0.8191520443, + 0.8290375726, 0.8386705679, 0.8480480962, 0.8571673007, + 0.8660254038, 0.8746197071, 0.8829475929, 0.8910065242, + 0.8987940463, 0.9063077870, 0.9135454576, 0.9205048535, + 0.9271838546, 0.9335804265, 0.9396926208, 0.9455185756, + 0.9510565163, 0.9563047560, 0.9612616959, 0.9659258263, + 0.9702957263, 0.9743700648, 0.9781476007, 0.9816271834, + 0.9848077530, 0.9876883406, 0.9902680687, 0.9925461516, + 0.9945218954, 0.9961946981, 0.9975640503, 0.9986295348, + 0.9993908270, 0.9998476952, 1.0000000000, 0.9998476952, + 0.9993908270, 0.9986295348, 0.9975640503, 0.9961946981, + 0.9945218954, 0.9925461516, 0.9902680687, 0.9876883406, + 0.9848077530, 0.9816271834, 0.9781476007, 0.9743700648, + 0.9702957263, 0.9659258263, 0.9612616959, 0.9563047560, + 0.9510565163, 0.9455185756, 0.9396926208, 0.9335804265, + 0.9271838546, 0.9205048535, 0.9135454576, 0.9063077870, + 0.8987940463, 0.8910065242, 0.8829475929, 0.8746197071, + 0.8660254038, 0.8571673007, 0.8480480962, 0.8386705679, + 0.8290375726, 0.8191520443, 0.8090169944, 0.7986355100, + 0.7880107536, 0.7771459615, 0.7660444431, 0.7547095802, + 0.7431448255, 0.7313537016, 0.7193398003, 0.7071067812, + 0.6946583705, 0.6819983601, 0.6691306064, 0.6560590290, + 0.6427876097, 0.6293203910, 0.6156614753, 0.6018150232, + 0.5877852523, 0.5735764364, 0.5591929035, 0.5446390350, + 0.5299192642, 0.5150380749, 0.5000000000, 0.4848096202, + 0.4694715628, 0.4539904997, 0.4383711468, 0.4226182617, + 0.4067366431, 0.3907311285, 0.3746065934, 0.3583679495, + 0.3420201433, 0.3255681545, 0.3090169944, 0.2923717047, + 0.2756373558, 0.2588190451, 0.2419218956, 0.2249510543, + 0.2079116908, 0.1908089954, 0.1736481777, 0.1564344650, + 0.1391731010, 0.1218693434, 0.1045284633, 0.0871557427, + 0.0697564737, 0.0523359562, 0.0348994967, 0.0174524064, + 0.0000000000, -0.0174524064, -0.0348994967, -0.0523359562, + -0.0697564737, -0.0871557427, -0.1045284633, -0.1218693434, + -0.1391731010, -0.1564344650, -0.1736481777, -0.1908089954, + -0.2079116908, -0.2249510543, -0.2419218956, -0.2588190451, + -0.2756373558, -0.2923717047, -0.3090169944, -0.3255681545, + -0.3420201433, -0.3583679495, -0.3746065934, -0.3907311285, + -0.4067366431, -0.4226182617, -0.4383711468, -0.4539904997, + -0.4694715628, -0.4848096202, -0.5000000000, -0.5150380749, + -0.5299192642, -0.5446390350, -0.5591929035, -0.5735764364, + -0.5877852523, -0.6018150232, -0.6156614753, -0.6293203910, + -0.6427876097, -0.6560590290, -0.6691306064, -0.6819983601, + -0.6946583705, -0.7071067812, -0.7193398003, -0.7313537016, + -0.7431448255, -0.7547095802, -0.7660444431, -0.7771459615, + -0.7880107536, -0.7986355100, -0.8090169944, -0.8191520443, + -0.8290375726, -0.8386705679, -0.8480480962, -0.8571673007, + -0.8660254038, -0.8746197071, -0.8829475929, -0.8910065242, + -0.8987940463, -0.9063077870, -0.9135454576, -0.9205048535, + -0.9271838546, -0.9335804265, -0.9396926208, -0.9455185756, + -0.9510565163, -0.9563047560, -0.9612616959, -0.9659258263, + -0.9702957263, -0.9743700648, -0.9781476007, -0.9816271834, + -0.9848077530, -0.9876883406, -0.9902680687, -0.9925461516, + -0.9945218954, -0.9961946981, -0.9975640503, -0.9986295348, + -0.9993908270, -0.9998476952, -1.0000000000, -0.9998476952, + -0.9993908270, -0.9986295348, -0.9975640503, -0.9961946981, + -0.9945218954, -0.9925461516, -0.9902680687, -0.9876883406, + -0.9848077530, -0.9816271834, -0.9781476007, -0.9743700648, + -0.9702957263, -0.9659258263, -0.9612616959, -0.9563047560, + -0.9510565163, -0.9455185756, -0.9396926208, -0.9335804265, + -0.9271838546, -0.9205048535, -0.9135454576, -0.9063077870, + -0.8987940463, -0.8910065242, -0.8829475929, -0.8746197071, + -0.8660254038, -0.8571673007, -0.8480480962, -0.8386705679, + -0.8290375726, -0.8191520443, -0.8090169944, -0.7986355100, + -0.7880107536, -0.7771459615, -0.7660444431, -0.7547095802, + -0.7431448255, -0.7313537016, -0.7193398003, -0.7071067812, + -0.6946583705, -0.6819983601, -0.6691306064, -0.6560590290, + -0.6427876097, -0.6293203910, -0.6156614753, -0.6018150232, + -0.5877852523, -0.5735764364, -0.5591929035, -0.5446390350, + -0.5299192642, -0.5150380749, -0.5000000000, -0.4848096202, + -0.4694715628, -0.4539904997, -0.4383711468, -0.4226182617, + -0.4067366431, -0.3907311285, -0.3746065934, -0.3583679495, + -0.3420201433, -0.3255681545, -0.3090169944, -0.2923717047, + -0.2756373558, -0.2588190451, -0.2419218956, -0.2249510543, + -0.2079116908, -0.1908089954, -0.1736481777, -0.1564344650, + -0.1391731010, -0.1218693434, -0.1045284633, -0.0871557427, + -0.0697564737, -0.0523359562, -0.0348994967, -0.0174524064 +}; + +/* Reference table generated by cos(), gcc-4.3.2 */ +static const float cos_ref_table[] = { + 1.000000000000000, 0.999847695156391, 0.999390827019096, 0.998629534754574, + 0.997564050259824, 0.996194698091746, 0.994521895368273, 0.992546151641322, + 0.99026806874157, 0.987688340595138, 0.984807753012208, 0.981627183447664, + 0.978147600733806, 0.974370064785235, 0.970295726275996, 0.965925826289068, + 0.961261695938319, 0.956304755963035, 0.951056516295154, 0.945518575599317, + 0.939692620785908, 0.933580426497202, 0.927183854566787, 0.92050485345244, + 0.913545457642601, 0.90630778703665, 0.898794046299167, 0.891006524188368, + 0.882947592858927, 0.874619707139396, 0.866025403784439, 0.857167300702112, + 0.848048096156426, 0.838670567945424, 0.829037572555042, 0.819152044288992, + 0.809016994374947, 0.798635510047293, 0.788010753606722, 0.777145961456971, + 0.766044443118978, 0.754709580222772, 0.743144825477394, 0.731353701619171, + 0.719339800338651, 0.707106781186548, 0.694658370458997, 0.681998360062498, + 0.669130606358858, 0.656059028990507, 0.642787609686539, 0.629320391049838, + 0.615661475325658, 0.601815023152048, 0.587785252292473, 0.573576436351046, + 0.559192903470747, 0.544639035015027, 0.529919264233205, 0.515038074910054, + 0.500000000000000, 0.484809620246337, 0.469471562785891, 0.453990499739547, + 0.438371146789077, 0.422618261740699, 0.4067366430758, 0.390731128489274, + 0.374606593415912, 0.3583679495453, 0.342020143325669, 0.325568154457157, + 0.309016994374947, 0.292371704722737, 0.275637355816999, 0.258819045102521, + 0.241921895599668, 0.224951054343865, 0.207911690817759, 0.190808995376545, + 0.17364817766693, 0.156434465040231, 0.139173100960066, 0.121869343405147, + 0.104528463267653, 0.0871557427476581, 0.0697564737441255, 0.052335956242944, + 0.0348994967025011, 0.0174524064372834, 6.12323399573677e-17, -0.0174524064372835, + -0.0348994967025007, -0.0523359562429436, -0.0697564737441253, -0.0871557427476582, + -0.104528463267653, -0.121869343405147, -0.139173100960065, -0.156434465040231, + -0.17364817766693, -0.190808995376545, -0.207911690817759, -0.224951054343865, + -0.241921895599668, -0.258819045102521, -0.275637355816999, -0.292371704722737, + -0.309016994374947, -0.325568154457156, -0.342020143325669, -0.3583679495453, + -0.374606593415912, -0.390731128489274, -0.4067366430758, -0.422618261740699, + -0.438371146789078, -0.453990499739547, -0.469471562785891, -0.484809620246337, + -0.500000000000000, -0.515038074910054, -0.529919264233205, -0.544639035015027, + -0.559192903470747, -0.573576436351046, -0.587785252292473, -0.601815023152048, + -0.615661475325658, -0.629320391049837, -0.642787609686539, -0.656059028990507, + -0.669130606358858, -0.681998360062498, -0.694658370458997, -0.707106781186547, + -0.719339800338651, -0.73135370161917, -0.743144825477394, -0.754709580222772, + -0.766044443118978, -0.777145961456971, -0.788010753606722, -0.798635510047293, + -0.809016994374947, -0.819152044288992, -0.829037572555042, -0.838670567945424, + -0.848048096156426, -0.857167300702112, -0.866025403784439, -0.874619707139396, + -0.882947592858927, -0.891006524188368, -0.898794046299167, -0.90630778703665, + -0.913545457642601, -0.92050485345244, -0.927183854566787, -0.933580426497202, + -0.939692620785908, -0.945518575599317, -0.951056516295154, -0.956304755963035, + -0.961261695938319, -0.965925826289068, -0.970295726275996, -0.974370064785235, + -0.978147600733806, -0.981627183447664, -0.984807753012208, -0.987688340595138, + -0.99026806874157, -0.992546151641322, -0.994521895368273, -0.996194698091746, + -0.997564050259824, -0.998629534754574, -0.999390827019096, -0.999847695156391, + -1.000000000000000, -0.999847695156391, -0.999390827019096, -0.998629534754574, + -0.997564050259824, -0.996194698091746, -0.994521895368273, -0.992546151641322, + -0.99026806874157, -0.987688340595138, -0.984807753012208, -0.981627183447664, + -0.978147600733806, -0.974370064785235, -0.970295726275997, -0.965925826289068, + -0.961261695938319, -0.956304755963036, -0.951056516295154, -0.945518575599317, + -0.939692620785908, -0.933580426497202, -0.927183854566787, -0.92050485345244, + -0.913545457642601, -0.90630778703665, -0.898794046299167, -0.891006524188368, + -0.882947592858927, -0.874619707139396, -0.866025403784439, -0.857167300702112, + -0.848048096156426, -0.838670567945424, -0.829037572555042, -0.819152044288992, + -0.809016994374947, -0.798635510047293, -0.788010753606722, -0.777145961456971, + -0.766044443118978, -0.754709580222772, -0.743144825477394, -0.731353701619171, + -0.719339800338651, -0.707106781186548, -0.694658370458997, -0.681998360062499, + -0.669130606358858, -0.656059028990508, -0.642787609686539, -0.629320391049837, + -0.615661475325658, -0.601815023152048, -0.587785252292473, -0.573576436351046, + -0.559192903470747, -0.544639035015027, -0.529919264233205, -0.515038074910054, + -0.500000000000000, -0.484809620246337, -0.469471562785891, -0.453990499739547, + -0.438371146789078, -0.4226182617407, -0.4067366430758, -0.390731128489274, + -0.374606593415912, -0.358367949545301, -0.342020143325669, -0.325568154457157, + -0.309016994374948, -0.292371704722737, -0.275637355816999, -0.258819045102521, + -0.241921895599668, -0.224951054343865, -0.20791169081776, -0.190808995376545, + -0.17364817766693, -0.156434465040231, -0.139173100960065, -0.121869343405147, + -0.104528463267653, -0.0871557427476582, -0.0697564737441256, -0.0523359562429443, + -0.0348994967025016, -0.0174524064372835, -1.83697019872103e-16, 0.0174524064372831, + 0.0348994967025013, 0.0523359562429439, 0.0697564737441252, 0.0871557427476579, + 0.104528463267653, 0.121869343405148, 0.139173100960065, 0.156434465040231, + 0.17364817766693, 0.190808995376544, 0.207911690817759, 0.224951054343865, + 0.241921895599667, 0.258819045102521, 0.275637355816999, 0.292371704722737, + 0.309016994374947, 0.325568154457156, 0.342020143325668, 0.3583679495453, + 0.374606593415912, 0.390731128489273, 0.406736643075801, 0.4226182617407, + 0.438371146789077, 0.453990499739547, 0.46947156278589, 0.484809620246337, + 0.500000000000000, 0.515038074910054, 0.529919264233205, 0.544639035015027, + 0.559192903470746, 0.573576436351046, 0.587785252292473, 0.601815023152048, + 0.615661475325659, 0.629320391049838, 0.642787609686539, 0.656059028990507, + 0.669130606358858, 0.681998360062498, 0.694658370458997, 0.707106781186547, + 0.719339800338651, 0.731353701619171, 0.743144825477394, 0.754709580222772, + 0.766044443118978, 0.777145961456971, 0.788010753606722, 0.798635510047293, + 0.809016994374947, 0.819152044288992, 0.829037572555041, 0.838670567945424, + 0.848048096156425, 0.857167300702112, 0.866025403784438, 0.874619707139396, + 0.882947592858927, 0.891006524188368, 0.898794046299167, 0.90630778703665, + 0.913545457642601, 0.92050485345244, 0.927183854566787, 0.933580426497202, + 0.939692620785908, 0.945518575599317, 0.951056516295154, 0.956304755963036, + 0.961261695938319, 0.965925826289068, 0.970295726275996, 0.974370064785235, + 0.978147600733806, 0.981627183447664, 0.984807753012208, 0.987688340595138, + 0.99026806874157, 0.992546151641322, 0.994521895368273, 0.996194698091746, + 0.997564050259824, 0.998629534754574, 0.999390827019096, 0.999847695156391, + 1 +}; + +/* Reference table generated by asin(), gcc-4.3.2 */ +static const float asin_ref_table[] = { + -1.5707963258028030, -1.4292567726224661, -1.3704614471644163, -1.3252307642251253, + -1.2870021797716618, -1.2532358597964048, -1.2226302791386843, -1.1944128256291151, + -1.1680804640054703, -1.1432840377092361, -1.1197695024311543, -1.0973451361060143, + -1.0758621711283922, -1.0552022922784090, -1.0352696590125561, -1.0159852746874094, + -0.9972832072526217, -0.9791076704859734, -0.9614110030233860, -0.9441520925611258, + -0.9272952042520046, -0.9108089841902256, -0.8946658074855804, -0.8788411282002926, + -0.8633130993694067, -0.8480620700865984, -0.8330703470855951, -0.8183219302445650, + -0.8038023039698601, -0.7894981894642115, -0.7753974795341492, -0.7614890411496162, + -0.7477626111358404, -0.7342087719589472, -0.7208187356591225, -0.7075844295322895, + -0.6944982521235943, -0.6815531998872757, -0.6687426939606667, -0.6560605876147747, + -0.6435010936111212, -0.6310588326305151, -0.6187286712229252, -0.6065058410167694, + -0.5943857878446579, -0.5823642127215862, -0.5704370923340321, -0.5586005486547947, + -0.5468509383499622, -0.5351847745478153, -0.5235987640917301, -0.5120897386223078, + -0.5006546974182129, -0.4892907701432705, -0.4779951833188534, -0.4667653329670429, + -0.4555986635386944, -0.4444927759468555, -0.4334453158080578, -0.4224540572613478, + -0.4115168377757072, -0.4006315693259239, -0.3897962793707848, -0.3790090084075928, + -0.3682678826153278, -0.3575710840523243, -0.3469168916344643, -0.3363035582005978, + -0.3257294744253159, -0.3151930235326290, -0.3046926259994507, -0.2942268401384354, + -0.2837941013276577, -0.2733930107206106, -0.2630221955478191, -0.2526802383363247, + -0.2423658333718777, -0.2320776768028736, -0.2218144722282887, -0.2115749418735504, + -0.2013579159975052, -0.1911621354520321, -0.1809864528477192, -0.1708296611905098, + -0.1606906354427338, -0.1505682766437531, -0.1404614131897688, -0.1303689777851105, + -0.1202898658812046, -0.1102230437099934, -0.1001674197614193, -0.0901219304651022, + -0.0800855793058872, -0.0700572803616524, -0.0600360594689846, -0.0500208474695683, + -0.0400106683373451, -0.0300045013427734, -0.0200013294816017, -0.0100001543760300, + 0.0000000000000000, 0.0100001543760300, 0.0200013294816017, 0.0300045013427734, + 0.0400106683373451, 0.0500208474695683, 0.0600360594689846, 0.0700572803616524, + 0.0800855793058872, 0.0901219304651022, 0.1001674197614193, 0.1102230437099934, + 0.1202898658812046, 0.1303689777851105, 0.1404614131897688, 0.1505682766437531, + 0.1606906354427338, 0.1708296611905098, 0.1809864528477192, 0.1911621354520321, + 0.2013579159975052, 0.2115749418735504, 0.2218144722282887, 0.2320776768028736, + 0.2423658333718777, 0.2526802383363247, 0.2630221955478191, 0.2733930107206106, + 0.2837941013276577, 0.2942268401384354, 0.3046926259994507, 0.3151930235326290, + 0.3257294744253159, 0.3363035582005978, 0.3469168916344643, 0.3575710840523243, + 0.3682678826153278, 0.3790090084075928, 0.3897962793707848, 0.4006315693259239, + 0.4115168377757072, 0.4224540572613478, 0.4334453158080578, 0.4444927759468555, + 0.4555986635386944, 0.4667653329670429, 0.4779951833188534, 0.4892907701432705, + 0.5006546974182129, 0.5120897386223078, 0.5235987640917301, 0.5351847745478153, + 0.5468509383499622, 0.5586005486547947, 0.5704370923340321, 0.5823642127215862, + 0.5943857878446579, 0.6065058410167694, 0.6187286712229252, 0.6310588326305151, + 0.6435010936111212, 0.6560605876147747, 0.6687426939606667, 0.6815531998872757, + 0.6944982521235943, 0.7075844295322895, 0.7208187356591225, 0.7342087719589472, + 0.7477626111358404, 0.7614890411496162, 0.7753974795341492, 0.7894981894642115, + 0.8038023039698601, 0.8183219302445650, 0.8330703470855951, 0.8480620700865984, + 0.8633130993694067, 0.8788411282002926, 0.8946658074855804, 0.9108089841902256, + 0.9272952042520046, 0.9441520925611258, 0.9614110030233860, 0.9791076704859734, + 0.9972832072526217, 1.0159852746874094, 1.0352696590125561, 1.0552022922784090, + 1.0758621711283922, 1.0973451361060143, 1.1197695024311543, 1.1432840377092361, + 1.1680804640054703, 1.1944128256291151, 1.2226302791386843, 1.2532358597964048, + 1.2870021797716618, 1.3252307642251253, 1.3704614471644163, 1.4292567726224661, + 1.5707963258028030, +}; + +/* Reference table generated by acos(), gcc-4.3.2 */ +static const float acos_ref_table[] = { + 3.1415926534682512, 3.0000531002879143, 2.9412577748298645, 2.8960270918905735, + 2.8577985074371099, 2.8240321874618530, 2.7934266068041325, 2.7652091532945633, + 2.7388767916709185, 2.7140803653746843, 2.6905658300966024, 2.6681414637714624, + 2.6466584987938404, 2.6259986199438572, 2.6060659866780043, 2.5867816023528576, + 2.5680795349180698, 2.5499039981514215, 2.5322073306888342, 2.5149484202265739, + 2.4980915319174528, 2.4816053118556738, 2.4654621351510286, 2.4496374558657408, + 2.4341094270348549, 2.4188583977520466, 2.4038666747510433, 2.3891182579100132, + 2.3745986316353083, 2.3602945171296597, 2.3461938071995974, 2.3322853688150644, + 2.3185589388012886, 2.3050050996243954, 2.2916150633245707, 2.2783807571977377, + 2.2652945797890425, 2.2523495275527239, 2.2395390216261148, 2.2268569152802229, + 2.2142974212765694, 2.2018551602959633, 2.1895249988883734, 2.1773021686822176, + 2.1651821155101061, 2.1531605403870344, 2.1412334199994802, 2.1293968763202429, + 2.1176472660154104, 2.1059811022132635, 2.0943950917571783, 2.0828860662877560, + 2.0714510250836611, 2.0600870978087187, 2.0487915109843016, 2.0375616606324911, + 2.0263949912041426, 2.0152891036123037, 2.0042416434735060, 1.9932503849267960, + 1.9823131654411554, 1.9714278969913721, 1.9605926070362329, 1.9498053360730410, + 1.9390642102807760, 1.9283674117177725, 1.9177132192999125, 1.9070998858660460, + 1.8965258020907640, 1.8859893511980772, 1.8754889536648989, 1.8650231678038836, + 1.8545904289931059, 1.8441893383860588, 1.8338185232132673, 1.8234765660017729, + 1.8131621610373259, 1.8028740044683218, 1.7926107998937368, 1.7823712695389986, + 1.7721542436629534, 1.7619584631174803, 1.7517827805131674, 1.7416259888559580, + 1.7314869631081820, 1.7213646043092012, 1.7112577408552170, 1.7011653054505587, + 1.6910861935466528, 1.6810193713754416, 1.6709637474268675, 1.6609182581305504, + 1.6508819069713354, 1.6408536080271006, 1.6308323871344328, 1.6208171751350164, + 1.6108069960027933, 1.6008008290082216, 1.5907976571470499, 1.5807964820414782, + 1.5707963258028030, 1.5607961714267731, 1.5507949963212013, 1.5407918244600296, + 1.5307856574654579, 1.5207754783332348, 1.5107602663338184, 1.5007390454411507, + 1.4907107464969158, 1.4806743953377008, 1.4706289060413837, 1.4605732820928097, + 1.4505064599215984, 1.4404273480176926, 1.4303349126130342, 1.4202280491590500, + 1.4101056903600693, 1.3999666646122932, 1.3898098729550838, 1.3796341903507710, + 1.3694384098052979, 1.3592213839292526, 1.3489818535745144, 1.3387186489999294, + 1.3284304924309254, 1.3181160874664783, 1.3077741302549839, 1.2974033150821924, + 1.2870022244751453, 1.2765694856643677, 1.2661036998033524, 1.2556033022701740, + 1.2450668513774872, 1.2344927676022053, 1.2238794341683388, 1.2132252417504787, + 1.2025284431874752, 1.1917872473952103, 1.1810000464320183, 1.1701647564768791, + 1.1592794880270958, 1.1483422685414553, 1.1373510099947453, 1.1263035498559475, + 1.1151976622641087, 1.1040309928357601, 1.0928011424839497, 1.0815055556595325, + 1.0701416283845901, 1.0587065871804953, 1.0471975617110729, 1.0356115512549877, + 1.0239453874528408, 1.0121957771480083, 1.0003592334687710, 0.9884321130812168, + 0.9764105379581451, 0.9642904847860336, 0.9520676545798779, 0.9397374931722879, + 0.9272952321916819, 0.9147357381880283, 0.9020536318421364, 0.8892431259155273, + 0.8762980736792088, 0.8632118962705135, 0.8499775901436806, 0.8365875538438559, + 0.8230337146669626, 0.8093072846531868, 0.7953988462686539, 0.7812981363385916, + 0.7669940218329430, 0.7524743955582380, 0.7377259787172079, 0.7227342557162046, + 0.7074832264333963, 0.6919551976025105, 0.6761305183172226, 0.6599873416125774, + 0.6435011215507984, 0.6266442332416773, 0.6093853227794170, 0.5916886553168297, + 0.5735131185501814, 0.5548110511153936, 0.5355266667902470, 0.5155940335243940, + 0.4949341546744108, 0.4734511896967888, 0.4510268233716488, 0.4275122880935669, + 0.4027158617973328, 0.3763835001736879, 0.3481660466641188, 0.3175604660063982, + 0.2837941460311413, 0.2455655615776777, 0.2003348786383867, 0.1415395531803370, + 0.0000000000000000 +}; + +/* Reference degree table generated for acos(),asin() in range of [-1:1e-2:1]*/ +static const double degree_table[] = { + -57.2957795262336731, -56.7228217124938965, -56.1498639285564423, -55.5769061148166656, + -55.0039483308792114, -54.4309905469417572, -53.8580327332019806, -53.2850749492645264, + -52.7121171653270721, -52.1391593515872955, -51.5662015676498413, -50.9932437539100647, + -50.4202859699726105, -49.8473281860351562, -49.2743703722953796, -48.7014125883579254, + -48.1284548044204712, -47.5554969906806946, -46.9825392067432404, -46.4095813930034637, + -45.8366236090660095, -45.2636658251285553, -44.6907080113887787, -44.1177502274513245, + -43.5447924435138702, -42.9718346297740936, -42.3988768458366394, -41.8259190320968628, + -41.2529612481594086, -40.6800034642219543, -40.1070456504821777, -39.5340878665447235, + -38.9611300826072693, -38.3881722688674927, -37.8152144849300385, -37.2422566711902618, + -36.6692988872528076, -36.0963411033153534, -35.5233832895755768, -34.9504255056381226, + -34.3774677217006683, -33.8045099079608917, -33.2315521240234375, -32.6585943102836609, + -32.0856365263462067, -31.5126787424087524, -30.9397209286689758, -30.3667631447315216, + -29.7938053607940674, -29.2208475470542908, -28.6478897631168365, -28.0749319493770599, + -27.5019741654396057, -26.9290163815021515, -26.3560585677623749, -25.7831007838249207, + -25.2101429998874664, -24.6371851861476898, -24.0642274022102356, -23.4912695884704590, + -22.9183118045330048, -22.3453540205955505, -21.7723962068557739, -21.1994384229183197, + -20.6264806389808655, -20.0535228252410889, -19.4805650413036346, -18.9076072275638580, + -18.3346494436264038, -17.7616916596889496, -17.1887338459491730, -16.6157760620117188, + -16.0428182780742645, -15.4698604643344879, -14.8969026803970337, -14.3239448666572571, + -13.7509870827198029, -13.1780292987823486, -12.6050714850425720, -12.0321137011051178, + -11.4591559171676636, -10.8861981034278870, -10.3132403194904327, -9.7402825057506561, + -9.1673247218132019, -8.5943669378757477, -8.0214091241359711, -7.4484513401985168, + -6.8754935562610626, -6.3025357425212860, -5.7295779585838318, -5.1566201448440552, + -4.5836623609066010, -4.0107045769691467, -3.4377467632293701, -2.8647889792919159, + -2.2918311953544617, -1.7188733816146851, -1.1459155976772308, -0.5729577839374542, + 0.0000000000000000, 0.5729577839374542, 1.1459155976772308, 1.7188733816146851, + 2.2918311953544617, 2.8647889792919159, 3.4377467632293701, 4.0107045769691467, + 4.5836623609066010, 5.1566201448440552, 5.7295779585838318, 6.3025357425212860, + 6.8754935562610626, 7.4484513401985168, 8.0214091241359711, 8.5943669378757477, + 9.1673247218132019, 9.7402825057506561, 10.3132403194904327, 10.8861981034278870, + 11.4591559171676636, 12.0321137011051178, 12.6050714850425720, 13.1780292987823486, + 13.7509870827198029, 14.3239448666572571, 14.8969026803970337, 15.4698604643344879, + 16.0428182780742645, 16.6157760620117188, 17.1887338459491730, 17.7616916596889496, + 18.3346494436264038, 18.9076072275638580, 19.4805650413036346, 20.0535228252410889, + 20.6264806389808655, 21.1994384229183197, 21.7723962068557739, 22.3453540205955505, + 22.9183118045330048, 23.4912695884704590, 24.0642274022102356, 24.6371851861476898, + 25.2101429998874664, 25.7831007838249207, 26.3560585677623749, 26.9290163815021515, + 27.5019741654396057, 28.0749319493770599, 28.6478897631168365, 29.2208475470542908, + 29.7938053607940674, 30.3667631447315216, 30.9397209286689758, 31.5126787424087524, + 32.0856365263462067, 32.6585943102836609, 33.2315521240234375, 33.8045099079608917, + 34.3774677217006683, 34.9504255056381226, 35.5233832895755768, 36.0963411033153534, + 36.6692988872528076, 37.2422566711902618, 37.8152144849300385, 38.3881722688674927, + 38.9611300826072693, 39.5340878665447235, 40.1070456504821777, 40.6800034642219543, + 41.2529612481594086, 41.8259190320968628, 42.3988768458366394, 42.9718346297740936, + 43.5447924435138702, 44.1177502274513245, 44.6907080113887787, 45.2636658251285553, + 45.8366236090660095, 46.4095813930034637, 46.9825392067432404, 47.5554969906806946, + 48.1284548044204712, 48.7014125883579254, 49.2743703722953796, 49.8473281860351562, + 50.4202859699726105, 50.9932437539100647, 51.5662015676498413, 52.1391593515872955, + 52.7121171653270721, 53.2850749492645264, 53.8580327332019806, 54.4309905469417572, + 55.0039483308792114, 55.5769061148166656, 56.1498639285564423, 56.7228217124938965, + 57.2957795262336731 +}; + +#define CMP_TOLERANCE_32B 0.0000000611175871f +#define CMP_TOLERANCE_16B 0.000065f +#define CMP_TOLERANCE_ASIN_32B 0.000000068141916f +#define CMP_TOLERANCE_ACOS_32B 0.000000060077032f +#define CMP_TOLERANCE_ASIN_16B 0.0001152158f +#define CMP_TOLERANCE_ACOS_16B 0.0001196862f +#define CMP_TOLERANCE_SIN 3.1e-5f + +#endif /* __SOF_ZTEST_TRIG_TABLES_H__ */ diff --git a/test/ztest/unit/math/basic/trigonometry/trig_test.c b/test/ztest/unit/math/basic/trigonometry/trig_test.c new file mode 100644 index 000000000000..61cb19ef6142 --- /dev/null +++ b/test/ztest/unit/math/basic/trigonometry/trig_test.c @@ -0,0 +1,225 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2025 Intel Corporation. All rights reserved. +// +// These contents may have been developed with support from one or more Intel-operated +// generative artificial intelligence solutions. +// +// Converted from CMock to Ztest +// +// Original tests from sof/test/cmocka/src/math/trig/: +// - sin_32b_fixed.c (Author: Slawomir Blauciak ) +// - sin_16b_fixed.c (Author: Shriram Shastry ) +// - cos_32b_fixed.c (Author: Shriram Shastry ) +// - cos_16b_fixed.c (Author: Shriram Shastry ) +// - asin_32b_fixed.c (Author: Shriram Shastry ) +// - asin_16b_fixed.c (Author: Shriram Shastry ) +// - acos_32b_fixed.c (Author: Shriram Shastry ) +// - acos_16b_fixed.c (Author: Shriram Shastry ) +// - lut_sin_16b_fixed.c (Authors: Slawomir Blauciak , +// Seppo Ingalsuo ) + +#define _USE_MATH_DEFINES +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "trig_tables.h" + +/* Define M_PI if not available */ +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +/* Conversion factor from degrees to radians (PI/180) */ +#define DEGREES_TO_RADIANS 0.017453292519943295 + +/* + * Helper function for rounding double values to nearest integer + * Implements custom rounding: round to 0 if absolute value < 0.5, otherwise round normally + */ +static inline int32_t round_to_nearest_int(double value) +{ + double abs_value = fabs(value); + + return (int32_t) (abs_value >= 0.5) ? floor(value + 0.5) : 0.0; +} + +/* Test sin_32b_fixed function */ +ZTEST(trigonometry, test_sin_32b_fixed) +{ + int theta; + float delta; + double rad; + int32_t sin_val; + + for (theta = 0; theta < ARRAY_SIZE(sin_ref_table); ++theta) { + rad = M_PI * ((double)theta / 180.0); + sin_val = sin_fixed_32b(Q_CONVERT_FLOAT(rad, 28)); + delta = fabsf(sin_ref_table[theta] - Q_CONVERT_QTOF(sin_val, 31)); + zassert_true(delta <= CMP_TOLERANCE_32B, + "sin_32b_fixed failed for angle %d", theta); + } +} + +/* Test sin_16b_fixed function */ +ZTEST(trigonometry, test_sin_16b_fixed) +{ + int theta; + float delta; + double rad; + int16_t sin_val; + + for (theta = 0; theta < ARRAY_SIZE(sin_ref_table); ++theta) { + rad = M_PI * ((double)theta / 180.0); + sin_val = sin_fixed_16b(Q_CONVERT_FLOAT(rad, 28)); + delta = fabsf(sin_ref_table[theta] - Q_CONVERT_QTOF(sin_val, 15)); + zassert_true(delta <= CMP_TOLERANCE_16B, + "sin_16b_fixed failed for angle %d", theta); + } +} + +/* Test cos_32b_fixed function */ +ZTEST(trigonometry, test_cos_32b_fixed) +{ + int theta; + float delta; + double rad; + int32_t cos_val; + + for (theta = 0; theta < ARRAY_SIZE(cos_ref_table); ++theta) { + rad = M_PI * ((double)theta / 180.0); + cos_val = cos_fixed_32b(Q_CONVERT_FLOAT(rad, 28)); + delta = fabsf(cos_ref_table[theta] - Q_CONVERT_QTOF(cos_val, 31)); + zassert_true(delta <= CMP_TOLERANCE_32B, + "cos_32b_fixed failed for angle %d", theta); + } +} + +/* Test cos_16b_fixed function */ +ZTEST(trigonometry, test_cos_16b_fixed) +{ + int theta; + float delta; + double rad; + int16_t cos_val; + + for (theta = 0; theta < ARRAY_SIZE(cos_ref_table); ++theta) { + rad = M_PI * ((double)theta / 180.0); + cos_val = cos_fixed_16b(Q_CONVERT_FLOAT(rad, 28)); + delta = fabsf(cos_ref_table[theta] - Q_CONVERT_QTOF(cos_val, 15)); + zassert_true(delta <= CMP_TOLERANCE_16B, + "cos_16b_fixed failed for angle %d", theta); + } +} + +/* Test asin_32b_fixed function */ +ZTEST(trigonometry, test_asin_32b_fixed) +{ + int index; + float delta; + double u; + int32_t asin_val, input_val; + + for (index = 0; index < ARRAY_SIZE(degree_table); ++index) { + /* Convert degree to input value in Q2.30 format like original test */ + /* angleInRadians = PI/180 * angleInDegrees & const Q2.30 format */ + u = (DEGREES_TO_RADIANS * degree_table[index] * 0x40000000); + input_val = round_to_nearest_int(u); + + asin_val = asin_fixed_32b(input_val); + delta = fabsf(asin_ref_table[index] - Q_CONVERT_QTOF(asin_val, 29)); + zassert_true(delta <= CMP_TOLERANCE_ASIN_32B, + "asin_32b_fixed failed for index %d", index); + } +} + +/* Test asin_16b_fixed function */ +ZTEST(trigonometry, test_asin_16b_fixed) +{ + int index; + float delta; + double u; + int16_t asin_val; + int32_t input_val; + + for (index = 0; index < ARRAY_SIZE(degree_table); ++index) { + /* Convert degree to input value in Q2.30 format like original test */ + /* angleInRadians = PI/180 * angleInDegrees & const Q2.30 format */ + u = (DEGREES_TO_RADIANS * degree_table[index] * 0x40000000); + input_val = round_to_nearest_int(u); + + asin_val = asin_fixed_16b(input_val); + delta = fabsf(asin_ref_table[index] - Q_CONVERT_QTOF(asin_val, 13)); + zassert_true(delta <= CMP_TOLERANCE_ASIN_16B, + "asin_16b_fixed failed for index %d", index); + } +} + +/* Test acos_32b_fixed function */ +ZTEST(trigonometry, test_acos_32b_fixed) +{ + int index; + float delta; + double u; + int32_t acos_val, input_val; + + for (index = 0; index < ARRAY_SIZE(degree_table); ++index) { + /* Convert degree to input value in Q2.30 format like original test */ + /* angleInRadians = PI/180 * angleInDegrees & const Q2.30 format */ + u = (DEGREES_TO_RADIANS * degree_table[index] * 0x40000000); + input_val = round_to_nearest_int(u); + + acos_val = acos_fixed_32b(input_val); + delta = fabsf(acos_ref_table[index] - Q_CONVERT_QTOF(acos_val, 29)); + zassert_true(delta <= CMP_TOLERANCE_ACOS_32B, + "acos_32b_fixed failed for index %d", index); + } +} + +/* Test acos_16b_fixed function */ +ZTEST(trigonometry, test_acos_16b_fixed) +{ + int index; + float delta; + double u; + int16_t acos_val; + int32_t input_val; + + for (index = 0; index < ARRAY_SIZE(degree_table); ++index) { + /* Convert degree to input value in Q2.30 format like original test */ + /* angleInRadians = PI/180 * angleInDegrees & const Q2.30 format */ + u = (DEGREES_TO_RADIANS * degree_table[index] * 0x40000000); + input_val = round_to_nearest_int(u); + + acos_val = acos_fixed_16b(input_val); + delta = fabsf(acos_ref_table[index] - Q_CONVERT_QTOF(acos_val, 13)); + zassert_true(delta <= CMP_TOLERANCE_ACOS_16B, + "acos_16b_fixed failed for index %d", index); + } +} + +/* Test sin lookup table function */ +ZTEST(trigonometry, test_sin_lut_16b_fixed) +{ + int theta; + float delta; + double rad; + int16_t sin_val; + + for (theta = 0; theta < ARRAY_SIZE(sin_ref_table); ++theta) { + rad = M_PI * (double)theta / 180.0; + sin_val = sofm_lut_sin_fixed_16b(Q_CONVERT_FLOAT(rad, 28)); + delta = fabsf(sin_ref_table[theta] - Q_CONVERT_QTOF(sin_val, 15)); + zassert_true(delta <= CMP_TOLERANCE_SIN, + "sin_lut_16b_fixed failed for angle %d", theta); + } +} + +ZTEST_SUITE(trigonometry, NULL, NULL, NULL, NULL, NULL);