Skip to content

ut: ztest: Port of remaining tests from test/cmocka/src/math/arithmetic#10764

Open
tmleman wants to merge 3 commits into
thesofproject:mainfrom
tmleman:topic/upstream/pr/unit_test/ztest/math/advanced_functions
Open

ut: ztest: Port of remaining tests from test/cmocka/src/math/arithmetic#10764
tmleman wants to merge 3 commits into
thesofproject:mainfrom
tmleman:topic/upstream/pr/unit_test/ztest/math/advanced_functions

Conversation

@tmleman
Copy link
Copy Markdown
Contributor

@tmleman tmleman commented May 11, 2026

This PR contains the port of the last tests from category Advanced Math Functions. Porting is being done as part of #10110.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the SOF unit test migration from CMocka to Zephyr’s ztest framework (issue #10110) by adding the remaining “Advanced Math Functions” tests (base-10 log, natural log, and square root) to the test/ztest/unit/math/advanced/functions suite.

Changes:

  • Add new ztest cases for log10_int32(), ln_int32(), and sofm_sqrt_int16().
  • Wire the new tests (and required SOF math sources) into the unit test CMake build.
  • Update the test metadata tags to reflect the newly covered functions.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
test/ztest/unit/math/advanced/functions/testcase.yaml Updates test tags to include newly added advanced-math coverage.
test/ztest/unit/math/advanced/functions/CMakeLists.txt Adds new test sources and links in required SOF math implementations.
test/ztest/unit/math/advanced/functions/test_square_root_ztest.c New ztest for fixed-point square root against reference table.
test/ztest/unit/math/advanced/functions/test_base10_logarithm_ztest.c New ztest for fixed-point base-10 log against reference table.
test/ztest/unit/math/advanced/functions/test_base_e_logarithm_ztest.c New ztest for fixed-point natural log against reference table.

/* 'THD = 20*log10(Error[max])' */
#define CMP_TOLERANCE 0.0000071279028671

/* Natural logarithm log10(X) reference table generated by matlab/Octave */
Comment on lines +50 to +78
/* testvector in Q32.0 */
static const uint32_t uv[100] = {
1ULL, 43383509ULL, 86767017ULL, 130150525ULL, 173534033ULL, 216917541ULL,
260301049ULL, 303684557ULL, 347068065ULL, 390451573ULL, 433835081ULL, 477218589ULL,
520602097ULL, 563985605ULL, 607369113ULL, 650752621ULL, 694136129ULL, 737519638ULL,
780903146ULL, 824286654ULL, 867670162ULL, 911053670ULL, 954437178ULL, 997820686ULL,
1041204194ULL, 1084587702ULL, 1127971210ULL, 1171354718ULL, 1214738226ULL, 1258121734ULL,
1301505242ULL, 1344888750ULL, 1388272258ULL, 1431655766ULL, 1475039274ULL, 1518422782ULL,
1561806290ULL, 1605189798ULL, 1648573306ULL, 1691956814ULL, 1735340322ULL, 1778723830ULL,
1822107338ULL, 1865490846ULL, 1908874354ULL, 1952257862ULL, 1995641370ULL, 2039024878ULL,
2082408386ULL, 2125791894ULL, 2169175403ULL, 2212558911ULL, 2255942419ULL, 2299325927ULL,
2342709435ULL, 2386092943ULL, 2429476451ULL, 2472859959ULL, 2516243467ULL, 2559626975ULL,
2603010483ULL, 2646393991ULL, 2689777499ULL, 2733161007ULL, 2776544515ULL, 2819928023ULL,
2863311531ULL, 2906695039ULL, 2950078547ULL, 2993462055ULL, 3036845563ULL, 3080229071ULL,
3123612579ULL, 3166996087ULL, 3210379595ULL, 3253763103ULL, 3297146611ULL, 3340530119ULL,
3383913627ULL, 3427297135ULL, 3470680643ULL, 3514064151ULL, 3557447659ULL, 3600831168ULL,
3644214676ULL, 3687598184ULL, 3730981692ULL, 3774365200ULL, 3817748708ULL, 3861132216ULL,
3904515724ULL, 3947899232ULL, 3991282740ULL, 4034666248ULL, 4078049756ULL, 4121433264ULL,
4164816772ULL, 4208200280ULL, 4251583788ULL, 4294967295ULL};

ZTEST(math_advanced_functions_suite, test_math_arithmetic_base10log_fixed)
{
double clogfxp;
double diff;
int i;

for (i = 0; i < ARRAY_SIZE(common_log10_ref_table); i++) {
clogfxp = log10_int32(uv[i]);
diff = fabs(common_log10_ref_table[i] - (double)clogfxp / (1 << 28));
Comment on lines +19 to +21
/* Test data tables from MATLAB-generated reference */
#include "log2_tables.h"

Comment on lines +87 to +92
/* testvector in Q4.12 */
static const uint32_t uv[252] = {
0U, 262U, 525U, 787U, 1049U, 1311U,
1574U, 1836U, 2098U, 2360U, 2623U, 2885U,
3147U, 3410U, 3672U, 3934U, 4196U, 4459U,
4721U, 4983U, 5246U, 5508U, 5770U, 6032U,
Comment on lines +136 to +139
double y;
double diff;

memcpy_s((void *)&u[0], sizeof(u), (void *)&uv[0], 252U * sizeof(uint32_t));
Comment on lines +141 to +143
for (i = 0; i < ARRAY_SIZE(sqrt_ref_table); i++) {
y = (double)Q_CONVERT_QTOF(sofm_sqrt_int16(u[i]), 12);
diff = fabs(sqrt_ref_table[i] - y);
tmleman added 3 commits May 11, 2026 16:43
Convert the square root math unit test from CMock/CMocka to Ztest.

Port test_math_arithmetic_sqrt_fixed from
sof/test/cmocka/src/math/arithmetic/square_root.c
originally authored by Shriram Shastry <malladi.sastry@linux.intel.com>.

Preserve the original reference data and tolerance, add the new Ztest
source to the math advanced functions suite, and build
src/math/sqrt_int16.c.

Validated with west twister on native_sim using host/llvm.

Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
Convert the base-10 logarithm math unit test from CMock/CMocka to Ztest.

Port test_math_arithmetic_base10log_fixed from
sof/test/cmocka/src/math/arithmetic/base_10_logarithm.c
originally authored by Shriram Shastry <malladi.sastry@linux.intel.com>.

Preserve the original reference data and tolerance, add the new Ztest
source to the math advanced functions suite, and build
src/math/log_10.c.

Validated with west twister on native_sim using host/llvm.

Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
Convert the natural (base-e) logarithm math unit test from CMock/CMocka
to Ztest.

Port test_math_arithmetic_base_e_log_fixed from
sof/test/cmocka/src/math/arithmetic/base_e_logarithm.c
originally authored by Shriram Shastry <malladi.sastry@linux.intel.com>.

Preserve the original reference data and tolerance, add the new Ztest
source to the math advanced functions suite, and build
src/math/log_e.c.

Validated with west twister on native_sim using host/llvm.

Signed-off-by: Tomasz Leman <tomasz.m.leman@intel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants