diff --git a/test/cmocka/src/math/arithmetic/CMakeLists.txt b/test/cmocka/src/math/arithmetic/CMakeLists.txt index 721c50020ba5..6625aef444ef 100644 --- a/test/cmocka/src/math/arithmetic/CMakeLists.txt +++ b/test/cmocka/src/math/arithmetic/CMakeLists.txt @@ -1,37 +1,5 @@ # SPDX-License-Identifier: BSD-3-Clause -cmocka_test(scalar_power - scalar_power.c - ${PROJECT_SOURCE_DIR}/src/math/power.c -) - -cmocka_test(base2_logarithm - base2_logarithm.c - ${PROJECT_SOURCE_DIR}/src/math/base2log.c -) - -cmocka_test(exponential - exponential.c - ${PROJECT_SOURCE_DIR}/src/math/exp_fcn.c - ${PROJECT_SOURCE_DIR}/src/math/exp_fcn_hifi.c -) -cmocka_test(square_root - square_root.c - ${PROJECT_SOURCE_DIR}/src/math/sqrt_int16.c -) - -cmocka_test(base_10_logarithm - base_10_logarithm.c - ${PROJECT_SOURCE_DIR}/src/math/log_10.c - ${PROJECT_SOURCE_DIR}/src/math/base2log.c -) - -cmocka_test(base_e_logarithm - base_e_logarithm.c - ${PROJECT_SOURCE_DIR}/src/math/log_e.c - ${PROJECT_SOURCE_DIR}/src/math/base2log.c -) - cmocka_test(a_law_codec a_law_codec.c ${PROJECT_SOURCE_DIR}/src/math/a_law.c diff --git a/test/cmocka/src/math/arithmetic/base2_logarithm.c b/test/cmocka/src/math/arithmetic/base2_logarithm.c deleted file mode 100644 index a06e44d09b78..000000000000 --- a/test/cmocka/src/math/arithmetic/base2_logarithm.c +++ /dev/null @@ -1,73 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2021 Intel Corporation. All rights reserved. -// -// Author: Shriram Shastry - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include "log2_tables.h" -/* 'Error[max] = 0.0000236785999981,THD(-dBc) = -92.5128795787487235' */ -#define CMP_TOLERANCE 0.0000236785691029 - -/* 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}; - -static void test_math_arithmetic_base2log_fixed(void **state) -{ - (void)state; - - uint32_t u[100]; - int i; - - memcpy_s((void *)&u[0], sizeof(u), (void *)&uv[0], 100U * sizeof(uint32_t)); - for (i = 0; i < ARRAY_SIZE(log2_lookup_table); i++) { - float y = Q_CONVERT_QTOF(base2_logarithm(u[i]), 0); - float diff = fabs(log2_lookup_table[i] - (double)y / (1 << 16)); - - if (diff > CMP_TOLERANCE) { - printf("%s: diff for %.16f: value = %.16f, log2 = %.16f\n", __func__, diff, - (double)u[i], (double)y / (1 << 16)); - assert_true(diff <= CMP_TOLERANCE); - } - } -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_math_arithmetic_base2log_fixed) - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/math/arithmetic/base_10_logarithm.c b/test/cmocka/src/math/arithmetic/base_10_logarithm.c deleted file mode 100644 index 3973df31a515..000000000000 --- a/test/cmocka/src/math/arithmetic/base_10_logarithm.c +++ /dev/null @@ -1,105 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2021 Intel Corporation. All rights reserved. -// -// Author: Shriram Shastry -// -// - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include "log2_tables.h" -/* 'Error[max] = 0.0000071279028671,THD = -102.9407645424143993 ' */ -/* 'Error[max] = rms(log10() - double(log10_int32()))' */ -/* 'THD = 20*log10(Error[max])' */ -#define CMP_TOLERANCE 0.0000071279028671 -/* Natural logarithm log10(X) reference table generated by matlab/Octave */ -/* UQ4.28 */ -static const double common_log10_ref_table[] = { - 0.0000000000000000, 7.6373246662453802, 7.9383546619093615, 8.1144459209650428, - 8.2393846575733427, 8.3362946705813989, 8.4154759166290241, 8.4824227062596371, - 8.5404146532373240, 8.5915671756847054, 8.6373246662453802, 8.6787173514036056, - 8.7165059122930053, 8.7512680193222625, 8.7834527026386606, 8.8134159259684335, - 8.8414446495269665, 8.8677735882125130, 8.8925971719048302, 8.9160782677250818, - 8.9383546624098908, 8.9595439614559940, 8.9797473475226131, 8.9990525026982162, - 9.0175359083740947, 9.0352646753178405, 9.0522980146012202, 9.0686884307751292, - 9.0844826979451199, 9.0997226644895282, 9.1144459212987297, 9.1286863604025754, - 9.1424746448781171, 9.1558386064266184, 9.1688035835820649, 9.1813927108816724, - 9.1936271672907388, 9.2055263908534872, 9.2171082633890631, 9.2283892737852433, - 9.2393846580738721, 9.2501085234534361, 9.2605739571199752, 9.2707931222905753, - 9.2807773431865943, 9.2905371804656394, 9.3000824983621975, 9.3094225246070792, - 9.3185659040380759, 9.3275207466824899, 9.3362946709818218, 9.3448948427358882, - 9.3533280102652014, 9.3616005362239267, 9.3697184264391105, 9.3776873561036460, - 9.3855126936091011, 9.3931995222691196, 9.4007526601535094, 9.4081766782268659, - 9.4154759169627091, 9.4226545015843630, 9.4297163562280168, 9.4366652161756566, - 9.4435046406985137, 9.4502380233502628, 9.4568686022422757, 9.4633994693944423, - 9.4698335793932600, 9.4761737574178788, 9.4824227066886628, 9.4885830153874373, - 9.4946571630937573, 9.5006475267772306, 9.5065563863821918, 9.5123859300375031, - 9.5181382589213257, 9.5238153918078847, 9.5294192693208828, 9.5349517579159713, - 9.5404146536127215, 9.5458096854947918, 9.5511385189953373, 9.5564027589832818, - 9.5616039526647825, 9.5667435923129869, 9.5718231178381536, 9.5768439193242560, - 9.5818073388505756, 9.5867146733402073, 9.5915671761296206, 9.5963660590064990, - 9.6011124940261787, 9.6058076152298781, 9.6104525202710605, 9.6150482719557271, - 9.6195958997020572, 9.6240964009244330, 9.6285507423464711, 9.6329598611462810}; - -/* 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}; - -static void test_math_arithmetic_base10log_fixed(void **state) -{ - (void)state; - - 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)); - - if (diff > CMP_TOLERANCE) { - printf("%s: diff for %.16f: val = %16d, log10() = %.16f\n", __func__, diff, - uv[i], clogfxp / (1 << 28)); - assert_true(diff <= CMP_TOLERANCE); - } - } -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_math_arithmetic_base10log_fixed) - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/math/arithmetic/base_e_logarithm.c b/test/cmocka/src/math/arithmetic/base_e_logarithm.c deleted file mode 100644 index c2c1d2083c8b..000000000000 --- a/test/cmocka/src/math/arithmetic/base_e_logarithm.c +++ /dev/null @@ -1,104 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2021 Intel Corporation. All rights reserved. -// -// Author: Shriram Shastry - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include "log2_tables.h" -/* 'Error[max] = 0.0000164133276926,THD(-dBc) = -95.6960671942683234' */ -/* 'Error[max] = rms(log() - double(log_int32()))' */ -/* 'THD = 20*log10(Error[max])' */ -#define CMP_TOLERANCE 0.0000164133276926 - -/* Natural logarithm loge(X) reference table generated by matlab/Octave */ -/* UQ5.27 */ -static const double natural_log_lookup_table[] = { - 0.0000000000000000, 17.5855899268523359, 18.2787371074122831, 18.6842022155204468, - 18.9718842879722267, 19.1950278392864391, 19.3773493960803940, 19.5315000759076511, - 19.6650314685321739, 19.7828145041885577, 19.8881750198463827, 19.9834851996507084, - 20.0704965766403376, 20.1505392860869676, 20.2246472581140395, 20.2936401294912301, - 20.3581786505327571, 20.4188032722644479, 20.4759616860290699, 20.5300289072319480, - 20.5813222015588408, 20.6301123656733907, 20.6766323812583899, 20.7210841437836706, - 20.7636437581607112, 20.8044657526425461, 20.8436864657603671, 20.8814267937103786, - 20.9177944378507625, 20.9528857576336485, 20.9867873092828354, 21.0195771320810394, - 21.0513258303723845, 21.0820974890173112, 21.1119504521464485, 21.1409379890003279, - 21.1691088659487328, 21.1965078407425196, 21.2231760877918916, 21.2491515741640455, - 21.2744693821187845, 21.2991619946810467, 21.3232595462333343, 21.3467900436180038, - 21.3697795618183370, 21.3922524176471107, 21.4142313243436178, 21.4357375295432568, - 21.4567909387206548, 21.4774102259037889, 21.4976129332024932, 21.5174155604805932, - 21.5368336463203107, 21.5558818412742781, 21.5745739742703257, 21.5929231129229997, - 21.6109416184107097, 21.6286411954956677, 21.6460329381935921, 21.6631273715394208, - 21.6799344898427790, 21.6964637917813938, 21.7127243130127638, 21.7287246543415016, - 21.7444730112924880, 21.7599771978118319, 21.7752446699265008, 21.7902825472754031, - 21.8050976330453672, 21.8196964324517815, 21.8340851698895619, 21.8482698048676056, - 21.8622560468288185, 21.8760493689479993, 21.8896550209909755, 21.9030780413106569, - 21.9163232680485471, 21.9293953496040821, 21.9422987544284780, 21.9550377801946830, - 21.9676165623906030, 21.9800390823784895, 21.9923091749598925, 22.0044305354820757, - 22.0164067265188734, 22.0282411841561903, 22.0399372239099236, 22.0514980465667030, - 22.0629267423782807, 22.0742262976204415, 22.0853995982070579, 22.0964494343823858, - 22.1073785049035614, 22.1181894209970018, 22.1288847101032040, 22.1394668194234150, - 22.1499381192805984, 22.1603009063062437, 22.1705574064637361, 22.1807097776854185}; - -/* 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}; - -static void test_math_arithmetic_base_e_log_fixed(void **state) -{ - (void)state; - - double logefxp; - double diff; - int i; - - for (i = 0; i < ARRAY_SIZE(natural_log_lookup_table); i++) { - logefxp = ln_int32(uv[i]); - diff = fabs(natural_log_lookup_table[i] - (double)logefxp / (1 << 27)); - - if (diff > CMP_TOLERANCE) { - printf("%s: diff for %.16f: value = %16d, log() = %.16f\n", __func__, diff, - uv[i], logefxp / (1 << 27)); - assert_true(diff <= CMP_TOLERANCE); - } - } -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_math_arithmetic_base_e_log_fixed) - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/math/arithmetic/exponential.c b/test/cmocka/src/math/arithmetic/exponential.c deleted file mode 100644 index 3e44bf0c3743..000000000000 --- a/test/cmocka/src/math/arithmetic/exponential.c +++ /dev/null @@ -1,291 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -/* - * Copyright(c) 2022-2025 Intel Corporation. - * - * Author: Shriram Shastry - * Seppo Ingalsuo - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#define ULP_TOLERANCE 1.0 -#define ULP_SCALE 1.9073e-06 /* For exp() output Q13.19, 1 / 2^19 */ -#define NUMTESTSAMPLES 256 - -#define NUMTESTSAMPLES_TEST2 100 -#define ABS_DELTA_TOLERANCE_TEST2 2.0e-6 -#define REL_DELTA_TOLERANCE_TEST2 1000.0 /* rel. error is large with values near zero */ -#define NUMTESTSAMPLES_TEST3 100 -#define ABS_DELTA_TOLERANCE_TEST3 2.0e-6 -#define REL_DELTA_TOLERANCE_TEST3 10.0e-2 -#define SOFM_EXP_FIXED_ARG_MIN -11.5 -#define SOFM_EXP_FIXED_ARG_MAX 7.6245 - -#define NUMTESTSAMPLES_TEST4 100 -#define ABS_DELTA_TOLERANCE_TEST4 2.5e-5 -#define REL_DELTA_TOLERANCE_TEST4 1000.0 /* rel. error is large with values near zero */ - -/** - * Saturates input to 32 bits - * @param x Input value - * @return Saturated output value - */ -static int32_t saturate32(int64_t x) -{ - if (x < INT32_MIN) - return INT32_MIN; - else if (x > INT32_MAX) - return INT32_MAX; - - return x; -} - -/** - * Generates linearly spaced values for a vector with end points and number points in - * desired fractional Q-format for 32 bit integer. If the test values exceed int32_t - * range, the values are saturated to INT32_MIN to INT32_MAX range. - * - * @param a First value of test vector - * @param b Last value of test vector - * @param step_count Number of values in vector - * @param point Calculate n-th point of vector 0 .. step_count - 1 - * @param qformat Number of fractional bits y in Qx.y format - * @param fout Pointer to calculated test vector value, double - * @param iout Pointer to calculated test vector value, int32_t - */ -static void gen_testvector_linspace_int32(double a, double b, int step_count, int point, - int qformat, double *fout, int32_t *iout) -{ - double fstep = (b - a) / (step_count - 1); - double fvalue = a + fstep * point; - int64_t itmp; - - itmp = (int64_t)round(fvalue * (double)(1 << qformat)); - *iout = saturate32(itmp); - *fout = (double)*iout / (1 << qformat); - return; -} - -/** - * Test for sofm_exp_approx() - * @param state Cmocka internal state - */ -static void test_function_sofm_exp_approx(void **state) -{ - (void)state; - - int32_t accum; - int i; - double a_i; - double max_ulp = 0; - double ulp; - double a_tmp = -8; - double b_tmp = 8; - int32_t b_i; - - for (i = 0; i < NUMTESTSAMPLES; i++) { - gen_testvector_linspace_int32(a_tmp, b_tmp, NUMTESTSAMPLES, i, 28, &a_i, &b_i); - accum = sofm_exp_approx(b_i); - ulp = fabs(exp(a_i) - (double)accum / (1 << 19)) / ULP_SCALE; - if (ulp > max_ulp) - max_ulp = ulp; - - if (ulp > ULP_TOLERANCE) { - printf("%s: ULP for %.16f: value = %.16f, Exp = %.16f\n", __func__, - ulp, (double)b_i / (1 << 28), (double)accum / (1 << 19)); - assert_true(false); - } - } - printf("%s: Worst-case ULP: %g ULP_SCALE %g\n", __func__, max_ulp, ULP_SCALE); -} - - -/** - * Calculate reference exponent value - * @param x Input value - * @param qformat Fractional bits y in Qx.y format - * @return Saturated exponent value to match fractional format - */ -static double ref_exp(double x, int qformat) -{ - double yf; - int64_t yi; - - yf = exp(x); - yi = yf * (1 << qformat); - if (yi > INT32_MAX) - yi = INT32_MAX; - else if (yi < INT32_MIN) - yi = INT32_MIN; - - yf = (double)yi / (1 << qformat); - return yf; -} - -/** - * Calculates test exponent function and compares result to reference exponent. - * @param ivalue Fractional format input value Q5.27 - * @param iexp_value Fractional format output value Q12.20 - * @param abs_delta_max Calculated absolute error - * @param rel_delta_max Calculated relative error - * @param abs_delta_tolerance Tolerance for absolute error - * @param rel_delta_tolerance Tolerance for relative error - */ -static void test_exp_with_input_value(int32_t ivalue, int32_t *iexp_value, - double *abs_delta_max, double *rel_delta_max, - double abs_delta_tolerance, double rel_delta_tolerance) -{ - double fvalue, fexp_value, ref_exp_value; - double rel_delta, abs_delta; - double eps = 1e-9; - - *iexp_value = sofm_exp_fixed(ivalue); - fvalue = (double)ivalue / (1 << 27); /* Q5.27 */ - fexp_value = (double)*iexp_value / (1 << 20); /* Q12.20 */ - ref_exp_value = ref_exp(fvalue, 20); - abs_delta = fabs(ref_exp_value - fexp_value); - rel_delta = abs_delta / (ref_exp_value + eps); - if (abs_delta > *abs_delta_max) - *abs_delta_max = abs_delta; - - if (rel_delta > *rel_delta_max) - *rel_delta_max = rel_delta; - - if (abs_delta > abs_delta_tolerance) { - printf("%s: Absolute error %g exceeds limit %g, input %g output %g.\n", - __func__, abs_delta, abs_delta_tolerance, fvalue, fexp_value); - assert_true(false); - } - - if (rel_delta > rel_delta_tolerance) { - printf("%s: Relative error %g exceeds limit %g, input %g output %g.\n", - __func__, rel_delta, rel_delta_tolerance, fvalue, fexp_value); - assert_true(false); - } -} - -/** - * Test for sofm_exp_fixed() - * @param state Cmocka internal state - */ -static void test_function_sofm_exp_fixed(void **state) -{ - (void)state; - double rel_delta_max, abs_delta_max; - double tmp; - int32_t ivalue, iexp_value; - int i; - - /* Test max int32_t range with coarse grid */ - rel_delta_max = 0; - abs_delta_max = 0; - for (i = 0; i < NUMTESTSAMPLES_TEST2; i++) { - gen_testvector_linspace_int32(-16, 16, NUMTESTSAMPLES_TEST2, i, 27, &tmp, &ivalue); - test_exp_with_input_value(ivalue, &iexp_value, &abs_delta_max, &rel_delta_max, - ABS_DELTA_TOLERANCE_TEST2, REL_DELTA_TOLERANCE_TEST2); - } - - printf("%s: Absolute max error was %.6e (max range).\n", __func__, abs_delta_max); - printf("%s: Relative max error was %.6e (max range).\n", __func__, rel_delta_max); - - /* Test max int32_t middle range with fine grid */ - rel_delta_max = 0; - abs_delta_max = 0; - for (i = 0; i < NUMTESTSAMPLES_TEST3; i++) { - gen_testvector_linspace_int32(SOFM_EXP_FIXED_ARG_MIN, SOFM_EXP_FIXED_ARG_MAX, - NUMTESTSAMPLES_TEST3, i, 27, &tmp, &ivalue); - test_exp_with_input_value(ivalue, &iexp_value, &abs_delta_max, &rel_delta_max, - ABS_DELTA_TOLERANCE_TEST3, REL_DELTA_TOLERANCE_TEST3); - } - - printf("%s: Absolute max error was %.6e (middle).\n", __func__, abs_delta_max); - printf("%s: Relative max error was %.6e (middle).\n", __func__, rel_delta_max); -} - -/** - * Reference function for dB to linear conversion - * @param x Input value - * @param qformat Fractional bits y in Qx.y format for saturation - * @return Saturated linear value - */ -static double ref_db2lin(double x, int qformat) -{ - double fref; - int64_t iref; - - fref = pow(10, x / 20); - iref = fref * (1 << qformat); - return (double)saturate32(iref) / (1 << qformat); -} - -/** - * Test for sofm_db2lin_fixed() - * @param state Cmocka internal state - */ -static void test_function_sofm_db2lin_fixed(void **state) -{ - (void)state; - double abs_delta, rel_delta, abs_delta_max, rel_delta_max; - double fin, fout, fref; - double eps = 1e-9; - int32_t iin, iout; - int i; - - rel_delta_max = 0; - abs_delta_max = 0; - for (i = 0; i < NUMTESTSAMPLES_TEST2; i++) { - gen_testvector_linspace_int32(-128, 128, NUMTESTSAMPLES_TEST4, i, 24, &fin, &iin); - iout = sofm_db2lin_fixed(iin); - fout = (double)iout / (1 << 20); - fref = ref_db2lin(fin, 20); - abs_delta = fabs(fref - fout); - rel_delta = abs_delta / (fref + eps); - if (abs_delta > abs_delta_max) - abs_delta_max = abs_delta; - - if (rel_delta > rel_delta_max) - rel_delta_max = rel_delta; - - if (abs_delta > ABS_DELTA_TOLERANCE_TEST4) { - printf("%s: Absolute error %g exceeds limit %g, input %g output %g.\n", - __func__, abs_delta, ABS_DELTA_TOLERANCE_TEST4, fin, fout); - assert_true(false); - } - - if (rel_delta > REL_DELTA_TOLERANCE_TEST4) { - printf("%s: Relative error %g exceeds limit %g, input %g output %g.\n", - __func__, rel_delta, REL_DELTA_TOLERANCE_TEST4, fin, fout); - assert_true(false); - } - } - printf("%s: Absolute max error was %.6e.\n", __func__, abs_delta_max); - printf("%s: Relative max error was %.6e.\n", __func__, rel_delta_max); -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_function_sofm_exp_approx), - cmocka_unit_test(test_function_sofm_exp_fixed), - cmocka_unit_test(test_function_sofm_db2lin_fixed), - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/math/arithmetic/scalar_power.c b/test/cmocka/src/math/arithmetic/scalar_power.c deleted file mode 100644 index 8b23791e979e..000000000000 --- a/test/cmocka/src/math/arithmetic/scalar_power.c +++ /dev/null @@ -1,55 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2021 Intel Corporation. All rights reserved. -// -// Author: Shriram Shastry - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include "power_tables.h" -/* 'Error (max = 0.000034912111005), THD+N = -96.457180359025074' */ -#define CMP_TOLERANCE 0.0000150363575813 - -static void test_math_arithmetic_power_fixed(void **state) -{ - (void)state; - - double p; - int i; - int j; - - for (i = 0; i < ARRAY_SIZE(b); i++) { - for (j = 0; j < ARRAY_SIZE(e); j++) { - p = power_int32(b[i], e[j]); - float diff = fabsf(power_table[i][j] - (double)p / (1 << 15)); - - if (diff > CMP_TOLERANCE) { - printf("%s: diff for %.16f: base = %.16f", - __func__, diff, (double)b[i] / (1 << 25)); - printf(", exponent = %.16f, power = %.16f\n", - (double)e[j] / (1 << 29), (double)p / (1 << 15)); - assert_true(diff <= CMP_TOLERANCE); - } - } - } -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_math_arithmetic_power_fixed) - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -} diff --git a/test/cmocka/src/math/arithmetic/square_root.c b/test/cmocka/src/math/arithmetic/square_root.c deleted file mode 100644 index 6a31588c1235..000000000000 --- a/test/cmocka/src/math/arithmetic/square_root.c +++ /dev/null @@ -1,162 +0,0 @@ -// SPDX-License-Identifier: BSD-3-Clause -// -// Copyright(c) 2021 Intel Corporation. All rights reserved. -// -// Author: Shriram Shastry - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -/* 'Error[max] = 0.0003000860000000,THD(-dBc) = -87.1210823527511309' */ -#define CMP_TOLERANCE 0.0001689942 - -static const double sqrt_ref_table[] = { - 0.0000000000000000, 0.2529127196287289, 0.3580137261684250, 0.4383362543470480, - 0.5060667106469264, 0.5657458434447398, 0.6199010757774179, 0.6695089151758922, - 0.7156864056624241, 0.7590598625931949, 0.8002380017063674, 0.8392530626247365, - 0.8765332548597343, 0.9124250825410271, 0.9468285879714448, 0.9800251112854200, - 1.0121334212938529, 1.0433710015497843, 1.0735864616438677, 1.1029744939820685, - 1.1317074351394887, 1.1596234572049671, 1.1868830634270588, 1.2135304899342250, - 1.2397036881347898, 1.2652391387105444, 1.2902693214499832, 1.3148230929007141, - 1.3390178303517843, 1.3626935069009465, 1.3859648038460428, 1.4089384024417106, - 1.4314580907679415, 1.4536289448738284, 1.4754666899408471, 1.4970674458754356, - 1.5182805344369004, 1.5392012945030940, 1.5598414883410430, 1.5802893574042065, - 1.6003997303408295, 1.6202605162827983, 1.6399552204252408, 1.6593426315110451, - 1.6785061252494731, 1.6974532854396907, 1.7162624043615824, 1.7347972458979175, - 1.7531361407845656, 1.7712851751976586, 1.7893183496097054, 1.8071040368501201, - 1.8247163735084968, 1.8422265952170487, 1.8595062978852748, 1.8766268983537990, - 1.8935927121149891, 1.9104717594746068, 1.9271396388170734, 1.9436645881555799, - 1.9601125007572908, 1.9763617734046062, 1.9924785326635266, 2.0084659685628234, - 2.0243874459327196, 2.0401248429936829, 2.0557417684986605, 2.0712409474756917, - 2.0866835042418388, 2.1019545405705138, 2.1171154277400652, 2.1322257663648099, - 2.1471729232877355, 2.1620167451363552, 2.1767593459084997, 2.1914584719713490, - 2.2060043241401410, 2.2204548907543695, 2.2348120201987909, 2.2491317769308226, - 2.2633070038662453, 2.2773940013752560, 2.2914476694602910, 2.3053627188850347, - 2.3191942802134968, 2.3329438383992445, 2.3466648541067809, 2.3602543890966499, - 2.3737661268541177, 2.3872013883939496, 2.4006123079591588, 2.4138981537908761, - 2.4271112748749282, 2.4403028756693299, 2.4533737931163282, 2.4663754402969551, - 2.4793089069839604, 2.4922242356226696, 2.5050242482608827, 2.5177591878742098, - 2.5304782774210888, 2.5430857547967194, 2.5556310375326090, 2.5681150370943278, - 2.5805859466650203, 2.5929498012639969, 2.6052549809231724, 2.6175023131556161, - 2.6297390257875399, 2.6418728560436060, 2.6539512111660981, 2.6660206330081166, - 2.6779900782816579, 2.6899062628881700, 2.7017698915479462, 2.7136266381449752, - 2.7253870138018930, 2.7370968595849874, 2.7487568212739375, 2.7604117531402812, - 2.7719736453698474, 2.7834875128828940, 2.7949976241045360, 2.8064170328908711, - 2.8177901636300033, 2.8291175744390689, 2.8404427884354582, 2.8516802201728368, - 2.8628735427669523, 2.8740232715872360, 2.8851722218959477, 2.8962361080806240, - 2.9072578897476569, 2.9182798738083706, 2.9292187124939990, 2.9401168530136688, - 2.9509747462702896, 2.9618340496219568, 2.9726126187665289, 2.9833522462156559, - 2.9940941216626773, 3.0047569707257522, 3.0153821145710538, 3.0259699503836783, - 3.0365610688738007, 3.0470753139280951, 3.0575534030495688, 3.0679957066870220, - 3.0784422425351754, 3.0888139284157279, 3.0991509043809078, 3.1094927741514371, - 3.1197612338526808, 3.1299960063872287, 3.1401974211424988, 3.1504045499149789, - 3.1605400917999757, 3.1706432337342845, 3.1807142844611178, 3.1907918051402224, - 3.2007994606816590, 3.2107759235502562, 3.2207593849316032, 3.2306742112715421, - 3.2405587023112234, 3.2504131347991749, 3.2602752232365293, 3.2700702400713046, - 3.2798360048560355, 3.2895727781126838, 3.2993178153786578, 3.3089972636170311, - 3.3186484800856810, 3.3283083869662677, 3.3379037677111065, 3.3474716438306089, - 3.3570122504989461, 3.3665620793882591, 3.3760487375221642, 3.3855088128485207, - 3.3949784839156196, 3.4043859578490805, 3.4137675072784321, 3.4231233453528955, - 3.4324892457042018, 3.4417941928048226, 3.4510740515635128, 3.4603290238249023, - 3.4695944917958350, 3.4788001927748020, 3.4879815975718680, 3.4971738031409019, - 3.5063070962374359, 3.5154166604934614, 3.5245026799003858, 3.5335998818485379, - 3.5426392659640071, 3.5516556438511886, 3.5606491902811768, 3.5696542746637245, - 3.5786025882144274, 3.5875285822032135, 3.5964663647113397, 3.6053481324623839, - 3.6142080737002402, 3.6230463485511746, 3.6318967259718442, 3.6406920594682268, - 3.6494661959833250, 3.6582526566654741, 3.6669847755001657, 3.6756961500510350, - 3.6843869274616097, 3.6930903069956198, 3.7017402474207994, 3.7103700224000571, - 3.7189797723132347, 3.7276023837381045, 3.7361724230822109, 3.7447228493908598, - 3.7532863204297375, 3.7617978476886553, 3.7702901600042669, 3.7787633869263368, - 3.7872498886065071, 3.7956852559847478, 3.8041019184887777, 3.8125000000000000, - 3.8209115711273665, 3.8292727871131094, 3.8376157861196840, 3.8459724266107265, - 3.8542792776341468, 3.8625682639598748, 3.8708395003538962, 3.8791245690071618, - 3.8873605782876637, 3.8955791750874478, 3.9037804693815712, 3.9119957742180653, - 3.9201627238228260, 3.9283126944020128, 3.9364768015796816, 3.9445930655930783, - 3.9526926641056979, 3.9607756993580185, 3.9688730295891301, 3.9769231786332004, - 3.9849570653270532, 3.9930053589840071, 3.9999694823054588, 3.9999694823054588}; -/* 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, - 6295U, 6557U, 6819U, 7081U, 7344U, 7606U, - 7868U, 8131U, 8393U, 8655U, 8917U, 9180U, - 9442U, 9704U, 9966U, 10229U, 10491U, 10753U, - 11016U, 11278U, 11540U, 11802U, 12065U, 12327U, - 12589U, 12851U, 13114U, 13376U, 13638U, 13901U, - 14163U, 14425U, 14687U, 14950U, 15212U, 15474U, - 15737U, 15999U, 16261U, 16523U, 16786U, 17048U, - 17310U, 17572U, 17835U, 18097U, 18359U, 18622U, - 18884U, 19146U, 19408U, 19671U, 19933U, 20195U, - 20457U, 20720U, 20982U, 21244U, 21507U, 21769U, - 22031U, 22293U, 22556U, 22818U, 23080U, 23342U, - 23605U, 23867U, 24129U, 24392U, 24654U, 24916U, - 25178U, 25441U, 25703U, 25965U, 26228U, 26490U, - 26752U, 27014U, 27277U, 27539U, 27801U, 28063U, - 28326U, 28588U, 28850U, 29113U, 29375U, 29637U, - 29899U, 30162U, 30424U, 30686U, 30948U, 31211U, - 31473U, 31735U, 31998U, 32260U, 32522U, 32784U, - 33047U, 33309U, 33571U, 33833U, 34096U, 34358U, - 34620U, 34883U, 35145U, 35407U, 35669U, 35932U, - 36194U, 36456U, 36719U, 36981U, 37243U, 37505U, - 37768U, 38030U, 38292U, 38554U, 38817U, 39079U, - 39341U, 39604U, 39866U, 40128U, 40390U, 40653U, - 40915U, 41177U, 41439U, 41702U, 41964U, 42226U, - 42489U, 42751U, 43013U, 43275U, 43538U, 43800U, - 44062U, 44324U, 44587U, 44849U, 45111U, 45374U, - 45636U, 45898U, 46160U, 46423U, 46685U, 46947U, - 47210U, 47472U, 47734U, 47996U, 48259U, 48521U, - 48783U, 49045U, 49308U, 49570U, 49832U, 50095U, - 50357U, 50619U, 50881U, 51144U, 51406U, 51668U, - 51930U, 52193U, 52455U, 52717U, 52980U, 53242U, - 53504U, 53766U, 54029U, 54291U, 54553U, 54816U, - 55078U, 55340U, 55602U, 55865U, 56127U, 56389U, - 56651U, 56914U, 57176U, 57438U, 57701U, 57963U, - 58225U, 58487U, 58750U, 59012U, 59274U, 59536U, - 59799U, 60061U, 60323U, 60586U, 60848U, 61110U, - 61372U, 61635U, 61897U, 62159U, 62421U, 62684U, - 62946U, 63208U, 63471U, 63733U, 63995U, 64257U, - 64520U, 64782U, 65044U, 65307U, UINT16_MAX, UINT16_MAX}; -static void test_math_arithmetic_sqrt_fixed(void **state) -{ - (void)state; - - uint32_t u[252]; - int i; - double y; - double diff; - - memcpy_s((void *)&u[0], sizeof(u), (void *)&uv[0], 252U * sizeof(uint32_t)); - for (i = 0; i < ARRAY_SIZE(sqrt_ref_table); i++) { - y = Q_CONVERT_QTOF(sofm_sqrt_int16(u[i]), 12); - diff = fabs(sqrt_ref_table[i] - y); - - if (diff > CMP_TOLERANCE) { - printf("%s: diff for %.16f: reftbl = %.16f, sqrt = %.16f\n", __func__, - diff, (double)sqrt_ref_table[i], y); - assert_true(diff <= CMP_TOLERANCE); - } - } -} - -int main(void) -{ - const struct CMUnitTest tests[] = { - cmocka_unit_test(test_math_arithmetic_sqrt_fixed) - }; - - cmocka_set_message_output(CM_OUTPUT_TAP); - - return cmocka_run_group_tests(tests, NULL, NULL); -}