diff --git a/src/include/sof/math/log.h b/src/include/sof/math/log.h new file mode 100644 index 000000000000..1cee628ee2bb --- /dev/null +++ b/src/include/sof/math/log.h @@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2021 Intel Corporation. All rights reserved. + * + * Author: Shriram Shastry + * + */ + +#ifndef __SOF_MATH_BASE_TWO_LOGARITHM_H__ +#define __SOF_MATH_BASE_TWO_LOGARITHM_H__ + +/* Include Files */ +#include +#include + +/* Function Declarations */ +int32_t base2_logarithm(uint32_t u); + +#endif diff --git a/src/include/sof/math/power.h b/src/include/sof/math/power.h new file mode 100644 index 000000000000..1acd59811dbd --- /dev/null +++ b/src/include/sof/math/power.h @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2021 Intel Corporation. All rights reserved. + * + * Author: Shriram Shastry + * + */ +#ifndef __POWER_SCALAR_FUNCTION_H__ +#define __POWER_SCALAR_FUNCTION_H__ + +/* Include Files */ +#include +#include + +/* Function Declarations */ +int32_t power_scalar_function(int32_t b, int32_t e); + +#endif diff --git a/src/math/CMakeLists.txt b/src/math/CMakeLists.txt index 359a857be495..d2a18ef495d4 100644 --- a/src/math/CMakeLists.txt +++ b/src/math/CMakeLists.txt @@ -4,7 +4,7 @@ if(BUILD_LIBRARY) return() endif() -add_local_sources(sof numbers.c trig.c decibels.c iir_df2t_generic.c iir_df2t_hifi3.c) +add_local_sources(sof numbers.c trig.c decibels.c iir_df2t_generic.c iir_df2t_hifi3.c power.c log.c) if(CONFIG_MATH_FIR) add_local_sources(sof fir_generic.c fir_hifi2ep.c fir_hifi3.c) diff --git a/src/math/log.c b/src/math/log.c new file mode 100644 index 000000000000..51145d876528 --- /dev/null +++ b/src/math/log.c @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2021 Intel Corporation. All rights reserved. +// +// Author: Shriram Shastry +// +// + +/* Include Files */ +#include + +/** + * Base-2 logarithm + * + * Y = (U) computes the base-2 logarithm of + * U using lookup table. + * + * Arguments : uint32_t u[Q31.1] + * Return Type : int32_t y[Q16.16] + */ +int32_t base2_logarithm(uint32_t u) +{ + static const int32_t iv1[129] = { + 0, 736, 1466, 2190, 2909, 3623, 4331, 5034, 5732, 6425, 7112, 7795, + 8473, 9146, 9814, 10477, 11136, 11791, 12440, 13086, 13727, 14363, 14996, 15624, + 16248, 16868, 17484, 18096, 18704, 19308, 19909, 20505, 21098, 21687, 22272, 22854, + 23433, 24007, 24579, 25146, 25711, 26272, 26830, 27384, 27936, 28484, 29029, 29571, + 30109, 30645, 31178, 31707, 32234, 32758, 33279, 33797, 34312, 34825, 35334, 35841, + 36346, 36847, 37346, 37842, 38336, 38827, 39316, 39802, 40286, 40767, 41246, 41722, + 42196, 42667, 43137, 43603, 44068, 44530, 44990, 45448, 45904, 46357, 46809, 47258, + 47705, 48150, 48593, 49034, 49472, 49909, 50344, 50776, 51207, 51636, 52063, 52488, + 52911, 53332, 53751, 54169, 54584, 54998, 55410, 55820, 56229, 56635, 57040, 57443, + 57845, 58245, 58643, 59039, 59434, 59827, 60219, 60609, 60997, 61384, 61769, 62152, + 62534, 62915, 63294, 63671, 64047, 64421, 64794, 65166, 65536}; + static const int8_t iv[256] = { + 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, + 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + uint32_t v; + uint32_t x; + int8_t Num_left_shifts; + int8_t shift_factor; + uint8_t slice_temp; + + shift_factor = iv[(u >> 24)]; + v = u << (uint64_t)shift_factor; + Num_left_shifts = shift_factor; + shift_factor = iv[(v >> 24)]; + v <<= (uint64_t)shift_factor; + Num_left_shifts += shift_factor; + shift_factor = iv[(v >> 24)]; + v <<= (uint64_t)shift_factor; + Num_left_shifts += shift_factor; + shift_factor = iv[(v >> 24)]; + Num_left_shifts += shift_factor; + x = v << (uint64_t)shift_factor; + slice_temp = (uint8_t)((v << (uint64_t)shift_factor) >> 24); + /* Interpolate between points. */ + /* The upper byte was used for the index into lookup table */ + /* The remaining bits make up the fraction between points. */ + return (((31 - Num_left_shifts) << 16) + + iv1[(uint8_t)((x >> 24) + 129) - 1]) + + ((((x & 16777215) * + (int64_t)(int32_t) + (iv1[(uint8_t)(slice_temp + 130) - 1] - + iv1[(uint8_t)(slice_temp - 127) - 1])) >> + 8) >> 16); +} + diff --git a/src/math/power.c b/src/math/power.c new file mode 100644 index 000000000000..73c9b9a312ef --- /dev/null +++ b/src/math/power.c @@ -0,0 +1,81 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2021 Intel Corporation. All rights reserved. +// +// Author: Shriram Shastry +// +// + + /* Include Files */ +#include + +/* Function Declarations */ +static int32_t div_s32s64(int64_t denominator); + +/* Function Definitions */ +/** + * Arguments : int64_t denominator + * + * Return Type : int32_t + */ +static inline int32_t div_s32s64(int64_t denominator) +{ + uint64_t b_denominator; + int32_t quotient; + + if (denominator == 0) { + quotient = INT32_MAX; + } else { + if (denominator < 0) + b_denominator = ~(uint64_t)denominator + 1; + else + b_denominator = (uint64_t)denominator; + + b_denominator = 1125899906842624ULL / b_denominator; + if (denominator < 0L) + quotient = (int32_t)-(int64_t)b_denominator; + else + quotient = (int32_t)b_denominator; + } + return quotient; +} + +/** + * Arguments : int32_t b + * base = [-1 to 2^5];Q7.25 + * + * : int32_t e + * exponent = [-1 to 3];Q3.29 + * + * Return Type : int32_t + * power = [-32768 to +32768];Q17.15 + */ +int32_t power_scalar_function(int32_t b, int32_t e) +{ + int32_t i; + int32_t k; + int32_t multiplier; + int32_t p; + + p = 32768; + + if (e < 0) { + if (e <= INT32_MIN) + e = INT32_MAX; + else + e = -e; + + if (b == 0) + multiplier = INT32_MAX; + else + multiplier = div_s32s64((int64_t)b); + + } else { + multiplier = b; + } + i = e >> 29; + for (k = 0; k < i; k++) + p = (int32_t)(((int64_t)p * (int64_t)multiplier) >> 25); + + return p; +} diff --git a/test/cmocka/include/log2_tables.h b/test/cmocka/include/log2_tables.h new file mode 100644 index 000000000000..1be36951c940 --- /dev/null +++ b/test/cmocka/include/log2_tables.h @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2021 Intel Corporation. All rights reserved. + * + * Author: Shriram Shastry + */ + +/* Reference table generated by Matlab log2() */ +static const double log2_lookup_table[] = { + 0.0000000000000000, 24.3706434451000007, 25.3706434117999997, 25.9556059015000002, + 26.3706433951999983, 26.6925714867999986, 26.9556058904000011, 27.1779983101000013, + 27.3706433868999994, 27.5405683874000005, 27.6925714800999998, 27.8300750032999993, + 27.9556058848000006, 28.0710831017999993, 28.1779983053999992, 28.2775339786000011, + 28.3706433826999991, 28.4581062236999998, 28.5405683836999984, 28.6185708954999996, + 28.6925714768000013, 28.7629608045000005, 28.8300750002000008, 28.8942053375000008, + 28.9556058820999986, 29.0144995723000001, 29.0710831005999992, 29.1255308843999998, + 29.1779983041999991, 29.2286243771000009, 29.2775339774999992, 29.3248396922000012, + 29.3706433816999990, 29.4150375010000005, 29.4581062228000015, 29.4999263983999995, + 29.5405683828000001, 29.5800967469000007, 29.6185708946000013, 29.6560455999999988, + 29.6925714760000012, 29.7281953855999994, 29.7629608037000004, 29.7969081355999990, + 29.8300749994999990, 29.8624964770999988, 29.8942053367999989, 29.9252322323999991, + 29.9556058814000004, 29.9853532246999990, 30.0144995702999999, 30.0430687224999993, + 30.0710830985999991, 30.0985638350000002, 30.1255308825999997, 30.1520030938999994, + 30.1779983023999989, 30.2035333944999991, 30.2286243753999990, 30.2532864295999993, + 30.2775339758000008, 30.3013807178000008, 30.3248396906000011, 30.3479233037000000, + 30.3706433800999989, 30.3930111931000013, 30.4150374994000003, 30.4367325704999985, + 30.4581062212999996, 30.4791678367999985, 30.4999263969000012, 30.5203904994999995, + 30.5405683814000000, 30.5604679387999987, 30.5800967455999988, 30.5994620708000014, + 30.6185708938000012, 30.6374299209999990, 30.6560455991000005, 30.6744241283999983, + 30.6925714750999994, 30.7104933831000011, 30.7281953847999993, 30.7456828115000000, + 30.7629608029000003, 30.7800343163000001, 30.7969081347999989, 30.8135868759999987, + 30.8300749986999989, 30.8463768109999990, 30.8624964764000005, 30.8784380202000008, + 30.8942053361000006, 30.9098021910999989, 30.9252322317000008, 30.9404989882999999, + 30.9556058806999985, 30.9705562221000008, 30.9853532240000007, 30.9999999997000018}; +/* testvector in Q31.1 */ +static const uint32_t vector_table[100] = { + 2U, 43383510U, 86767018U, 130150526U, 173534034U, 216917542U, + 260301050U, 303684558U, 347068066U, 390451574U, 433835082U, 477218590U, + 520602098U, 563985606U, 607369114U, 650752622U, 694136130U, 737519638U, + 780903146U, 824286654U, 867670162U, 911053670U, 954437178U, 997820686U, + 1041204194U, 1084587703U, 1127971211U, 1171354719U, 1214738227U, 1258121735U, + 1301505243U, 1344888751U, 1388272259U, 1431655767U, 1475039275U, 1518422783U, + 1561806291U, 1605189799U, 1648573307U, 1691956815U, 1735340323U, 1778723831U, + 1822107339U, 1865490847U, 1908874355U, 1952257863U, 1995641371U, 2039024879U, + 2082408387U, 2125791895U, 2169175403U, 2212558911U, 2255942419U, 2299325927U, + 2342709435U, 2386092943U, 2429476451U, 2472859959U, 2516243467U, 2559626975U, + 2603010483U, 2646393991U, 2689777499U, 2733161007U, 2776544515U, 2819928023U, + 2863311531U, 2906695039U, 2950078547U, 2993462055U, 3036845563U, 3080229071U, + 3123612579U, 3166996087U, 3210379595U, 3253763104U, 3297146612U, 3340530120U, + 3383913628U, 3427297136U, 3470680644U, 3514064152U, 3557447660U, 3600831168U, + 3644214676U, 3687598184U, 3730981692U, 3774365200U, 3817748708U, 3861132216U, + 3904515724U, 3947899232U, 3991282740U, 4034666248U, 4078049756U, 4121433264U, + 4164816772U, 4208200280U, 4251583788U, 4294967295U}; diff --git a/test/cmocka/include/power_tables.h b/test/cmocka/include/power_tables.h new file mode 100644 index 000000000000..1b495fece6ee --- /dev/null +++ b/test/cmocka/include/power_tables.h @@ -0,0 +1,155 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2021 Intel Corporation. All rights reserved. + * + * Author: Shriram Shastry + */ + +/* Include Files */ +#include + +/* Reference table generated by Matlab power() */ +static const double power_table[64][6] = { + {-1.0000000000000000, 1.0000000000000000, -1.0000000000000000 + , -1.0000000000000000, 1.0000000000000000, -1.0000000000000000}, + {-0.5000000000000000, 0.2500000000000000, -0.1250000000000000 + , -2.0000000000000000, 4.0000000000000000, -8.0000000000000000}, + {-0.3333333333333333, 0.1111111111111111, -0.0370370370370370 + , -3.0000000000000000, 9.0000000000000000, -27.0000000000000000}, + {-0.2500000000000000, 0.0625000000000000, -0.0156250000000000 + , -4.0000000000000000, 16.0000000000000000, -64.0000000000000000}, + {-0.2000000000000000, 0.0400000000000000, -0.0080000000000000 + , -5.0000000000000000, 25.0000000000000000, -125.0000000000000000}, + {-0.1666666666666667, 0.0277777777777778, -0.0046296296296296 + , -6.0000000000000000, 36.0000000000000000, -216.0000000000000000}, + {-0.1428571428571428, 0.0204081632653061, -0.0029154518950437 + , -7.0000000000000000, 49.0000000000000000, -343.0000000000000000}, + {-0.1250000000000000, 0.0156250000000000, -0.0019531250000000 + , -8.0000000000000000, 64.0000000000000000, -512.0000000000000000}, + {-0.1111111111111111, 0.0123456790123457, -0.0013717421124829 + , -9.0000000000000000, 81.0000000000000000, -729.0000000000000000}, + {-0.1000000000000000, 0.0100000000000000, -0.0010000000000000 + , -10.0000000000000000, 100.0000000000000000, -1000.0000000000000000}, + {-0.0909090909090909, 0.0082644628099174, -0.0007513148009016 + , -11.0000000000000000, 121.0000000000000000, -1331.0000000000000000}, + {-0.0833333333333333, 0.0069444444444444, -0.0005787037037037 + , -12.0000000000000000, 144.0000000000000000, -1728.0000000000000000}, + {-0.0769230769230769, 0.0059171597633136, -0.0004551661356395 + , -13.0000000000000000, 169.0000000000000000, -2197.0000000000000000}, + {-0.0714285714285714, 0.0051020408163265, -0.0003644314868805 + , -14.0000000000000000, 196.0000000000000000, -2744.0000000000000000}, + {-0.0666666666666667, 0.0044444444444444, -0.0002962962962963 + , -15.0000000000000000, 225.0000000000000000, -3375.0000000000000000}, + {-0.0625000000000000, 0.0039062500000000, -0.0002441406250000 + , -16.0000000000000000, 256.0000000000000000, -4096.0000000000000000}, + {-0.0588235294117647, 0.0034602076124567, -0.0002035416242622 + , -17.0000000000000000, 289.0000000000000000, -4913.0000000000000000}, + {-0.0555555555555556, 0.0030864197530864, -0.0001714677640604 + , -18.0000000000000000, 324.0000000000000000, -5832.0000000000000000}, + {-0.0526315789473684, 0.0027700831024931, -0.0001457938474996 + , -19.0000000000000000, 361.0000000000000000, -6859.0000000000000000}, + {-0.0500000000000000, 0.0025000000000000, -0.0001250000000000 + , -20.0000000000000000, 400.0000000000000000, -8000.0000000000000000}, + {-0.0476190476190476, 0.0022675736961451, -0.0001079796998164 + , -21.0000000000000000, 441.0000000000000000, -9261.0000000000000000}, + {-0.0454545454545455, 0.0020661157024793, -0.0000939143501127 + , -22.0000000000000000, 484.0000000000000000, -10648.0000000000000000}, + {-0.0434782608695652, 0.0018903591682420, -0.0000821895290540 + , -23.0000000000000000, 529.0000000000000000, -12167.0000000000000000}, + {-0.0416666666666667, 0.0017361111111111, -0.0000723379629630 + , -24.0000000000000000, 576.0000000000000000, -13824.0000000000000000}, + {-0.0400000000000000, 0.0016000000000000, -0.0000640000000000 + , -25.0000000000000000, 625.0000000000000000, -15625.0000000000000000}, + {-0.0384615384615385, 0.0014792899408284, -0.0000568957669549 + , -26.0000000000000000, 676.0000000000000000, -17576.0000000000000000}, + {-0.0370370370370370, 0.0013717421124829, -0.0000508052634253 + , -27.0000000000000000, 729.0000000000000000, -19683.0000000000000000}, + {-0.0357142857142857, 0.0012755102040816, -0.0000455539358601 + , -28.0000000000000000, 784.0000000000000000, -21952.0000000000000000}, + {-0.0344827586206897, 0.0011890606420927, -0.0000410020911066 + , -29.0000000000000000, 841.0000000000000000, -24389.0000000000000000}, + {-0.0333333333333333, 0.0011111111111111, -0.0000370370370370 + , -30.0000000000000000, 900.0000000000000000, -27000.0000000000000000}, + {-0.0322580645161290, 0.0010405827263267, -0.0000335671847202 + , -31.0000000000000000, 961.0000000000000000, -29791.0000000000000000}, + {-0.0312500000000000, 0.0009765625000000, -0.0000305175781250 + , -32.0000000000000000, 1024.0000000000000000, -32768.0000000000000000}, + {1.0000000000000000, 1.0000000000000000, 1.0000000000000000 + , 1.0000000000000000, 1.0000000000000000, 1.0000000000000000}, + {0.5000000000000000, 0.2500000000000000, 0.1250000000000000 + , 2.0000000000000000, 4.0000000000000000, 8.0000000000000000}, + {0.3333333333333333, 0.1111111111111111, 0.0370370370370370 + , 3.0000000000000000, 9.0000000000000000, 27.0000000000000000}, + {0.2500000000000000, 0.0625000000000000, 0.0156250000000000 + , 4.0000000000000000, 16.0000000000000000, 64.0000000000000000}, + {0.2000000000000000, 0.0400000000000000, 0.0080000000000000 + , 5.0000000000000000, 25.0000000000000000, 125.0000000000000000}, + {0.1666666666666667, 0.0277777777777778, 0.0046296296296296 + , 6.0000000000000000, 36.0000000000000000, 216.0000000000000000}, + {0.1428571428571428, 0.0204081632653061, 0.0029154518950437 + , 7.0000000000000000, 49.0000000000000000, 343.0000000000000000}, + {0.1250000000000000, 0.0156250000000000, 0.0019531250000000 + , 8.0000000000000000, 64.0000000000000000, 512.0000000000000000}, + {0.1111111111111111, 0.0123456790123457, 0.0013717421124829 + , 9.0000000000000000, 81.0000000000000000, 729.0000000000000000}, + {0.1000000000000000, 0.0100000000000000, 0.0010000000000000 + , 10.0000000000000000, 100.0000000000000000, 1000.0000000000000000}, + {0.0909090909090909, 0.0082644628099174, 0.0007513148009016 + , 11.0000000000000000, 121.0000000000000000, 1331.0000000000000000}, + {0.0833333333333333, 0.0069444444444444, 0.0005787037037037 + , 12.0000000000000000, 144.0000000000000000, 1728.0000000000000000}, + {0.0769230769230769, 0.0059171597633136, 0.0004551661356395 + , 13.0000000000000000, 169.0000000000000000, 2197.0000000000000000}, + {0.0714285714285714, 0.0051020408163265, 0.0003644314868805 + , 14.0000000000000000, 196.0000000000000000, 2744.0000000000000000}, + {0.0666666666666667, 0.0044444444444444, 0.0002962962962963 + , 15.0000000000000000, 225.0000000000000000, 3375.0000000000000000}, + {0.0625000000000000, 0.0039062500000000, 0.0002441406250000 + , 16.0000000000000000, 256.0000000000000000, 4096.0000000000000000}, + {0.0588235294117647, 0.0034602076124567, 0.0002035416242622 + , 17.0000000000000000, 289.0000000000000000, 4913.0000000000000000}, + {0.0555555555555556, 0.0030864197530864, 0.0001714677640604 + , 18.0000000000000000, 324.0000000000000000, 5832.0000000000000000}, + {0.0526315789473684, 0.0027700831024931, 0.0001457938474996 + , 19.0000000000000000, 361.0000000000000000, 6859.0000000000000000}, + {0.0500000000000000, 0.0025000000000000, 0.0001250000000000 + , 20.0000000000000000, 400.0000000000000000, 8000.0000000000000000}, + {0.0476190476190476, 0.0022675736961451, 0.0001079796998164 + , 21.0000000000000000, 441.0000000000000000, 9261.0000000000000000}, + {0.0454545454545455, 0.0020661157024793, 0.0000939143501127 + , 22.0000000000000000, 484.0000000000000000, 10648.0000000000000000}, + {0.0434782608695652, 0.0018903591682420, 0.0000821895290540 + , 23.0000000000000000, 529.0000000000000000, 12167.0000000000000000}, + {0.0416666666666667, 0.0017361111111111, 0.0000723379629630 + , 24.0000000000000000, 576.0000000000000000, 13824.0000000000000000}, + {0.0400000000000000, 0.0016000000000000, 0.0000640000000000 + , 25.0000000000000000, 625.0000000000000000, 15625.0000000000000000}, + {0.0384615384615385, 0.0014792899408284, 0.0000568957669549 + , 26.0000000000000000, 676.0000000000000000, 17576.0000000000000000}, + {0.0370370370370370, 0.0013717421124829, 0.0000508052634253 + , 27.0000000000000000, 729.0000000000000000, 19683.0000000000000000}, + {0.0357142857142857, 0.0012755102040816, 0.0000455539358601 + , 28.0000000000000000, 784.0000000000000000, 21952.0000000000000000}, + {0.0344827586206897, 0.0011890606420927, 0.0000410020911066 + , 29.0000000000000000, 841.0000000000000000, 24389.0000000000000000}, + {0.0333333333333333, 0.0011111111111111, 0.0000370370370370 + , 30.0000000000000000, 900.0000000000000000, 27000.0000000000000000}, + {0.0322580645161290, 0.0010405827263267, 0.0000335671847202 + , 31.0000000000000000, 961.0000000000000000, 29791.0000000000000000}, + {0.0312500000000000, 0.0009765625000000, 0.0000305175781250 + , 32.0000000000000000, 1024.0000000000000000, 32768.0000000000000000}}; +/* base testvector */ +static const int32_t b[64] = { + -33554432, -67108864, -100663296, -134217728, -167772160, -201326592, -234881024, + -268435456, -301989888, -335544320, -369098752, -402653184, -436207616, -469762048, + -503316480, -536870912, -570425344, -603979776, -637534208, -671088640, -704643072, + -738197504, -771751936, -805306368, -838860800, -872415232, -905969664, -939524096, + -973078528, -1006632960, -1040187392, -1073741824, 33554432, 67108864, 100663296, + 134217728, 167772160, 201326592, 234881024, 268435456, 301989888, 335544320, + 369098752, 402653184, 436207616, 469762048, 503316480, 536870912, 570425344, + 603979776, 637534208, 671088640, 704643072, 738197504, 771751936, 805306368, + 838860800, 872415232, 905969664, 939524096, 973078528, 1006632960, 1040187392, + 1073741824}; +/* exponent testvector */ +static const int32_t e[6] = {-536870912, -1073741824, -1610612736, + 536870912, 1073741824, 1610612736}; diff --git a/test/cmocka/src/math/CMakeLists.txt b/test/cmocka/src/math/CMakeLists.txt index 779439690abd..9ef10a428803 100644 --- a/test/cmocka/src/math/CMakeLists.txt +++ b/test/cmocka/src/math/CMakeLists.txt @@ -2,6 +2,7 @@ add_subdirectory(numbers) add_subdirectory(trig) +add_subdirectory(arithmetic) # FFT needs maths is WIP for xtensa GCC if(XCC AND NOT BUILD_UNIT_TESTS_HOST) diff --git a/test/cmocka/src/math/arithmetic/CMakeLists.txt b/test/cmocka/src/math/arithmetic/CMakeLists.txt new file mode 100644 index 000000000000..f4a1b1d5b3a1 --- /dev/null +++ b/test/cmocka/src/math/arithmetic/CMakeLists.txt @@ -0,0 +1,11 @@ +# 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/log.c +) diff --git a/test/cmocka/src/math/arithmetic/base2_logarithm.c b/test/cmocka/src/math/arithmetic/base2_logarithm.c new file mode 100644 index 000000000000..5f842dcda358 --- /dev/null +++ b/test/cmocka/src/math/arithmetic/base2_logarithm.c @@ -0,0 +1,59 @@ +// 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 "log2_tables.h" +/* 'Error[max] = 0.0000236785999981,THD(-dBc) = -92.5128795787487235' */ +#define CMP_TOLERANCE 0.0000236785999981 + +static void test_math_arithmetic_base2log_fixed(void **state) +{ + (void)state; + + double u; + double v; + int b_i; + int i; + + for (i = 0; i < ARRAY_SIZE(log2_lookup_table); i++) { + u = ((double)vector_table[i] / (1 << 1)); + v = fabs(u); + /* GitHub macro Q_CONVERT_FLOAT is inaccurate, so replaced with below */ + u = (v >= 0.5) ? floor(u + 0.5) : 0.0; + b_i = (int)u; + + float y = Q_CONVERT_QTOF(base2_logarithm(b_i), 16); + float diff = fabs(log2_lookup_table[i] - y); + + if (diff > CMP_TOLERANCE) { + printf("%s: diff for %.16f: value = %.16f, log2 = %.16f\n", __func__, diff, + (double)vector_table[i] / (1 << 1), (double)y); + } + + 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/scalar_power.c b/test/cmocka/src/math/arithmetic/scalar_power.c new file mode 100644 index 000000000000..f07460ab28cd --- /dev/null +++ b/test/cmocka/src/math/arithmetic/scalar_power.c @@ -0,0 +1,56 @@ +// 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 = -89.1404782100538' */ +#define CMP_TOLERANCE 0.000034912111005 + +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_scalar_function(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 ,exponent = %.16f," + "power = %.16f\n", __func__, diff, + (double)b[i] / (1 << 25), + (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); +}