Skip to content

Commit 871e7cc

Browse files
committed
test: ztest: add base-e logarithm unit test converted from CMock
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>
1 parent 1841cca commit 871e7cc

2 files changed

Lines changed: 105 additions & 0 deletions

File tree

test/ztest/unit/math/advanced/functions/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ target_sources(app PRIVATE
3131
test_scalar_power_ztest.c
3232
test_base2_logarithm_ztest.c
3333
test_base10_logarithm_ztest.c
34+
test_base_e_logarithm_ztest.c
3435
test_exponential_ztest.c
3536
test_square_root_ztest.c
3637
${SOF_ROOT}/src/math/power.c
3738
${SOF_ROOT}/src/math/base2log.c
3839
${SOF_ROOT}/src/math/log_10.c
40+
${SOF_ROOT}/src/math/log_e.c
3941
${SOF_ROOT}/src/math/exp_fcn.c
4042
${SOF_ROOT}/src/math/sqrt_int16.c
4143
# Note: exp_fcn_hifi.c is conditionally compiled only for Xtensa HiFi platforms.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
//
3+
// Copyright(c) 2026 Intel Corporation. All rights reserved.
4+
//
5+
// These contents may have been developed with support from one or more Intel-operated
6+
// generative artificial intelligence solutions.
7+
//
8+
// Converted from CMock to Ztest
9+
//
10+
// Original test from sof/test/cmocka/src/math/arithmetic/base_e_logarithm.c
11+
// Author: Shriram Shastry <malladi.sastry@linux.intel.com>
12+
13+
#include <zephyr/ztest.h>
14+
#include <stdint.h>
15+
#include <math.h>
16+
17+
#include <sof/math/log.h>
18+
19+
/* Test data tables from MATLAB-generated reference */
20+
#include "log2_tables.h"
21+
22+
/* 'Error[max] = 0.0000164133276926,THD(-dBc) = -95.6960671942683234' */
23+
/* 'Error[max] = rms(log() - double(log_int32()))' */
24+
/* 'THD = 20*log10(Error[max])' */
25+
#define CMP_TOLERANCE 0.0000164133276926
26+
27+
/* Natural logarithm loge(X) reference table generated by matlab/Octave */
28+
/* UQ5.27 */
29+
static const double natural_log_lookup_table[] = {
30+
0.0000000000000000, 17.5855899268523359, 18.2787371074122831, 18.6842022155204468,
31+
18.9718842879722267, 19.1950278392864391, 19.3773493960803940, 19.5315000759076511,
32+
19.6650314685321739, 19.7828145041885577, 19.8881750198463827, 19.9834851996507084,
33+
20.0704965766403376, 20.1505392860869676, 20.2246472581140395, 20.2936401294912301,
34+
20.3581786505327571, 20.4188032722644479, 20.4759616860290699, 20.5300289072319480,
35+
20.5813222015588408, 20.6301123656733907, 20.6766323812583899, 20.7210841437836706,
36+
20.7636437581607112, 20.8044657526425461, 20.8436864657603671, 20.8814267937103786,
37+
20.9177944378507625, 20.9528857576336485, 20.9867873092828354, 21.0195771320810394,
38+
21.0513258303723845, 21.0820974890173112, 21.1119504521464485, 21.1409379890003279,
39+
21.1691088659487328, 21.1965078407425196, 21.2231760877918916, 21.2491515741640455,
40+
21.2744693821187845, 21.2991619946810467, 21.3232595462333343, 21.3467900436180038,
41+
21.3697795618183370, 21.3922524176471107, 21.4142313243436178, 21.4357375295432568,
42+
21.4567909387206548, 21.4774102259037889, 21.4976129332024932, 21.5174155604805932,
43+
21.5368336463203107, 21.5558818412742781, 21.5745739742703257, 21.5929231129229997,
44+
21.6109416184107097, 21.6286411954956677, 21.6460329381935921, 21.6631273715394208,
45+
21.6799344898427790, 21.6964637917813938, 21.7127243130127638, 21.7287246543415016,
46+
21.7444730112924880, 21.7599771978118319, 21.7752446699265008, 21.7902825472754031,
47+
21.8050976330453672, 21.8196964324517815, 21.8340851698895619, 21.8482698048676056,
48+
21.8622560468288185, 21.8760493689479993, 21.8896550209909755, 21.9030780413106569,
49+
21.9163232680485471, 21.9293953496040821, 21.9422987544284780, 21.9550377801946830,
50+
21.9676165623906030, 21.9800390823784895, 21.9923091749598925, 22.0044305354820757,
51+
22.0164067265188734, 22.0282411841561903, 22.0399372239099236, 22.0514980465667030,
52+
22.0629267423782807, 22.0742262976204415, 22.0853995982070579, 22.0964494343823858,
53+
22.1073785049035614, 22.1181894209970018, 22.1288847101032040, 22.1394668194234150,
54+
22.1499381192805984, 22.1603009063062437, 22.1705574064637361, 22.1807097776854185};
55+
56+
/* testvector in Q32.0 */
57+
static const uint32_t uv[100] = {
58+
1ULL, 43383509ULL, 86767017ULL, 130150525ULL, 173534033ULL, 216917541ULL,
59+
260301049ULL, 303684557ULL, 347068065ULL, 390451573ULL, 433835081ULL, 477218589ULL,
60+
520602097ULL, 563985605ULL, 607369113ULL, 650752621ULL, 694136129ULL, 737519638ULL,
61+
780903146ULL, 824286654ULL, 867670162ULL, 911053670ULL, 954437178ULL, 997820686ULL,
62+
1041204194ULL, 1084587702ULL, 1127971210ULL, 1171354718ULL, 1214738226ULL, 1258121734ULL,
63+
1301505242ULL, 1344888750ULL, 1388272258ULL, 1431655766ULL, 1475039274ULL, 1518422782ULL,
64+
1561806290ULL, 1605189798ULL, 1648573306ULL, 1691956814ULL, 1735340322ULL, 1778723830ULL,
65+
1822107338ULL, 1865490846ULL, 1908874354ULL, 1952257862ULL, 1995641370ULL, 2039024878ULL,
66+
2082408386ULL, 2125791894ULL, 2169175403ULL, 2212558911ULL, 2255942419ULL, 2299325927ULL,
67+
2342709435ULL, 2386092943ULL, 2429476451ULL, 2472859959ULL, 2516243467ULL, 2559626975ULL,
68+
2603010483ULL, 2646393991ULL, 2689777499ULL, 2733161007ULL, 2776544515ULL, 2819928023ULL,
69+
2863311531ULL, 2906695039ULL, 2950078547ULL, 2993462055ULL, 3036845563ULL, 3080229071ULL,
70+
3123612579ULL, 3166996087ULL, 3210379595ULL, 3253763103ULL, 3297146611ULL, 3340530119ULL,
71+
3383913627ULL, 3427297135ULL, 3470680643ULL, 3514064151ULL, 3557447659ULL, 3600831168ULL,
72+
3644214676ULL, 3687598184ULL, 3730981692ULL, 3774365200ULL, 3817748708ULL, 3861132216ULL,
73+
3904515724ULL, 3947899232ULL, 3991282740ULL, 4034666248ULL, 4078049756ULL, 4121433264ULL,
74+
4164816772ULL, 4208200280ULL, 4251583788ULL, 4294967295ULL};
75+
76+
/**
77+
* @brief Test natural (base-e) logarithm function with fixed-point arithmetic
78+
*
79+
* This test validates the ln_int32() function against MATLAB-generated reference
80+
* values. It tests 100 uniformly distributed input values across the full uint32_t
81+
* range, checking that the fixed-point logarithm calculation stays within acceptable
82+
* tolerance.
83+
*
84+
* Input values: Q32.0 format (unsigned 32-bit integers)
85+
* Result: UQ5.27 fixed-point format
86+
* Reference: MATLAB log() function results
87+
*/
88+
ZTEST(math_advanced_functions_suite, test_math_arithmetic_base_e_log_fixed)
89+
{
90+
int i;
91+
92+
BUILD_ASSERT(ARRAY_SIZE(uv) == ARRAY_SIZE(natural_log_lookup_table),
93+
"Test vector size must match reference table size");
94+
95+
for (i = 0; i < ARRAY_SIZE(uv); i++) {
96+
double logefxp = ln_int32(uv[i]);
97+
double diff = fabs(natural_log_lookup_table[i] - logefxp / (1 << 27));
98+
99+
zassert_true(diff <= CMP_TOLERANCE,
100+
"ln_int32(%u): diff %.16f > tolerance (expected %.16f, got %.16f)",
101+
uv[i], diff, natural_log_lookup_table[i], logefxp / (1 << 27));
102+
}
103+
}

0 commit comments

Comments
 (0)