From 661f5c89ffd512eaaacb5e12ab744a11bfd0b152 Mon Sep 17 00:00:00 2001 From: shastry Date: Tue, 5 Dec 2023 08:24:06 +0530 Subject: [PATCH] DRC: math: Rename variables and functions in accordance SOF math regulations. Rename variables and functions in the code to reflect sof math and hi-fi usage. Signed-off-by: shastry --- src/audio/drc/drc_generic.c | 9 ++++++--- src/audio/drc/drc_hifi3.c | 4 ++-- src/audio/drc/drc_hifi4.c | 4 ++-- src/audio/drc/drc_math_generic.c | 2 +- src/include/sof/math/decibels.h | 2 +- src/include/sof/math/exp_fcn.h | 5 ++++- src/math/auditory/auditory.c | 3 ++- src/math/decibels.c | 2 +- src/math/exp_fcn.c | 10 ++++------ src/math/exp_fcn_hifi.c | 17 +++++++---------- src/math/window.c | 3 ++- test/cmocka/src/math/auditory/CMakeLists.txt | 1 + test/cmocka/src/math/window/CMakeLists.txt | 1 + zephyr/CMakeLists.txt | 2 ++ 14 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/audio/drc/drc_generic.c b/src/audio/drc/drc_generic.c index 1019aeac3691..298f89d045e8 100644 --- a/src/audio/drc/drc_generic.c +++ b/src/audio/drc/drc_generic.c @@ -36,7 +36,7 @@ static int32_t knee_curveK(const struct sof_drc_params *p, int32_t x) * beta = -expf(k * linear_threshold) / k * gamma = -k * x */ - knee_exp_gamma = exp_fixed(Q_MULTSR_32X32((int64_t)x, -p->K, 31, 20, 27)); /* Q12.20 */ + knee_exp_gamma = sofm_exp_fixed(Q_MULTSR_32X32((int64_t)x, -p->K, 31, 20, 27)); /* Q12.20 */ return p->knee_alpha + Q_MULTSR_32X32((int64_t)p->knee_beta, knee_exp_gamma, 24, 20, 24); } @@ -66,8 +66,11 @@ static int32_t volume_gain(const struct sof_drc_params *p, int32_t x) * => y/x = ratio_base * x^(s - 1) * => y/x = ratio_base * e^(log(x) * (s - 1)) */ - exp_knee = exp_fixed(Q_MULTSR_32X32((int64_t)drc_log_fixed(Q_SHIFT_RND(x, 31, 26)), - (p->slope - ONE_Q30), 26, 30, 27)); /* Q12.20 */ + exp_knee = sofm_exp_fixed(Q_MULTSR_32X32 + ((int64_t)drc_log_fixed + (Q_SHIFT_RND(x, 31, 26)), + (p->slope - ONE_Q30), + 26, 30, 27)); /* Q12.20 */ y = Q_MULTSR_32X32((int64_t)p->ratio_base, exp_knee, 30, 20, 30); } diff --git a/src/audio/drc/drc_hifi3.c b/src/audio/drc/drc_hifi3.c index 0371bf6b1f9c..82f403fc435f 100644 --- a/src/audio/drc/drc_hifi3.c +++ b/src/audio/drc/drc_hifi3.c @@ -42,7 +42,7 @@ static int32_t knee_curveK(const struct sof_drc_params *p, int32_t x) * gamma = -k * x */ gamma = drc_mult_lshift(x, -p->K, drc_get_lshift(31, 20, 27)); - knee_exp_gamma = exp_fixed(gamma); + knee_exp_gamma = sofm_exp_fixed(gamma); knee_curve_k = drc_mult_lshift(p->knee_beta, knee_exp_gamma, drc_get_lshift(24, 20, 24)); knee_curve_k = AE_ADD32(knee_curve_k, p->knee_alpha); return knee_curve_k; @@ -78,7 +78,7 @@ static int32_t volume_gain(const struct sof_drc_params *p, int32_t x) tmp = AE_SRAI32R(x, 5); /* Q1.31 -> Q5.26 */ tmp = drc_log_fixed(tmp); /* Q6.26 */ tmp2 = AE_SUB32(p->slope, ONE_Q30); /* Q2.30 */ - exp_knee = exp_fixed(drc_mult_lshift(tmp, tmp2, drc_get_lshift(26, 30, 27))); + exp_knee = sofm_exp_fixed(drc_mult_lshift(tmp, tmp2, drc_get_lshift(26, 30, 27))); y = drc_mult_lshift(p->ratio_base, exp_knee, drc_get_lshift(30, 20, 30)); } diff --git a/src/audio/drc/drc_hifi4.c b/src/audio/drc/drc_hifi4.c index 93447ecabd90..304874bb668c 100644 --- a/src/audio/drc/drc_hifi4.c +++ b/src/audio/drc/drc_hifi4.c @@ -64,7 +64,7 @@ static int32_t knee_curveK(const struct sof_drc_params *p, int32_t x) * gamma = -k * x */ gamma = drc_mult_lshift(x, -p->K, LSHIFT_QX31_QY20_QZ27); - knee_exp_gamma = exp_fixed(gamma); + knee_exp_gamma = sofm_exp_fixed(gamma); knee_curve_k = drc_mult_lshift(p->knee_beta, knee_exp_gamma, LSHIFT_QX24_QY20_QZ24); knee_curve_k = AE_ADD32(knee_curve_k, p->knee_alpha); return knee_curve_k; @@ -100,7 +100,7 @@ static int32_t volume_gain(const struct sof_drc_params *p, int32_t x) tmp = AE_SRAI32R(x, 5); /* Q1.31 -> Q5.26 */ tmp = drc_log_fixed(tmp); /* Q6.26 */ tmp2 = AE_SUB32(p->slope, ONE_Q30); /* Q2.30 */ - exp_knee = exp_fixed(drc_mult_lshift(tmp, tmp2, LSHIFT_QX26_QY30_QZ27)); + exp_knee = sofm_exp_fixed(drc_mult_lshift(tmp, tmp2, LSHIFT_QX26_QY30_QZ27)); y = drc_mult_lshift(p->ratio_base, exp_knee, LSHIFT_QX30_QY20_QZ30); } diff --git a/src/audio/drc/drc_math_generic.c b/src/audio/drc/drc_math_generic.c index c1fb1171a879..cad152fc6a98 100644 --- a/src/audio/drc/drc_math_generic.c +++ b/src/audio/drc/drc_math_generic.c @@ -234,7 +234,7 @@ inline int32_t drc_pow_fixed(int32_t x, int32_t y) return 0; /* x^y = expf(y * log(x)) */ - return exp_fixed(q_mult(y, drc_log_fixed(x), 30, 26, 27)); + return sofm_exp_fixed(q_mult(y, drc_log_fixed(x), 30, 26, 27)); } #undef q_multq diff --git a/src/include/sof/math/decibels.h b/src/include/sof/math/decibels.h index a0336e9be9b9..8fceed5b2a25 100644 --- a/src/include/sof/math/decibels.h +++ b/src/include/sof/math/decibels.h @@ -16,7 +16,7 @@ #define DB2LIN_FIXED_INPUT_QY 24 #define DB2LIN_FIXED_OUTPUT_QY 20 -int32_t exp_fixed(int32_t x); /* Input is Q5.27, output is Q12.20 */ +int32_t sofm_exp_fixed(int32_t x); /* Input is Q5.27, output is Q12.20 */ int32_t db2lin_fixed(int32_t x); /* Input is Q8.24, output is Q12.20 */ #endif /* __SOF_MATH_DECIBELS_H__ */ diff --git a/src/include/sof/math/exp_fcn.h b/src/include/sof/math/exp_fcn.h index 5b3649040836..e6d9b2a1e0a6 100644 --- a/src/include/sof/math/exp_fcn.h +++ b/src/include/sof/math/exp_fcn.h @@ -31,8 +31,11 @@ #define SOFM_QUOTIENT_SCALE 0x40000000 #define SOFM_TERMS_Q23P9 0x800000 #define SOFM_LSHIFT_BITS 0x2000 +#define EXP_ONE_Q20 Q_CONVERT_FLOAT(1.0, 20) /* Use Q12.20 */ +#define EXP_TWO_Q27 Q_CONVERT_FLOAT(2.0, 27) /* Use Q5.27 */ +#define EXP_MINUS_TWO_Q27 Q_CONVERT_FLOAT(-2.0, 27) /* Use Q5.27 */ int32_t sofm_exp_int32(int32_t x); -int32_t exp_fixed(int32_t x); +int32_t sofm_exp_fixed(int32_t x); #endif diff --git a/src/math/auditory/auditory.c b/src/math/auditory/auditory.c index 57c1fe0ee3f8..cbc52e39d0ba 100644 --- a/src/math/auditory/auditory.c +++ b/src/math/auditory/auditory.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -80,7 +81,7 @@ int16_t psy_mel_to_hz(int16_t mel) return 0; exp_arg = Q_MULTSR_32X32((int64_t)mel, ONE_OVER_MELDIV_Q31, 2, 31, 27); - exp = exp_fixed(exp_arg) - ONE_Q20; + exp = sofm_exp_fixed(exp_arg) - ONE_Q20; hz = Q_MULTSR_32X32((int64_t)exp, 700, 20, 0, 0); return hz; } diff --git a/src/math/decibels.c b/src/math/decibels.c index b88e2cbf898e..7ee430154d76 100644 --- a/src/math/decibels.c +++ b/src/math/decibels.c @@ -32,5 +32,5 @@ int32_t db2lin_fixed(int32_t db) /* Q8.24 x Q5.27, result needs to be Q5.27 */ arg = (int32_t)Q_MULTSR_32X32((int64_t)db, LOG10_DIV20_Q27, 24, 27, 27); - return exp_fixed(arg); + return sofm_exp_fixed(arg); } diff --git a/src/math/exp_fcn.c b/src/math/exp_fcn.c index a81b5df59e00..dec5ea55f919 100644 --- a/src/math/exp_fcn.c +++ b/src/math/exp_fcn.c @@ -214,10 +214,8 @@ int32_t sofm_exp_int32(int32_t x) return ts; } -#define ONE_Q20 Q_CONVERT_FLOAT(1.0, 20) /* Use Q12.20 */ -#define TWO_Q27 Q_CONVERT_FLOAT(2.0, 27) /* Use Q5.27 */ -#define MINUS_TWO_Q27 Q_CONVERT_FLOAT(-2.0, 27) /* Use Q5.27 */ #define q_mult(a, b, qa, qb, qy) ((int32_t)Q_MULTSR_32X32((int64_t)(a), b, qa, qb, qy)) + /* Fixed point exponent function for approximate range -11.5 .. 7.6 * that corresponds to decibels range -100 .. +66 dB. * @@ -231,7 +229,7 @@ int32_t sofm_exp_int32(int32_t x) * Output is Q12.20, 0.0 .. +2048.0 */ -int32_t exp_fixed(int32_t x) +int32_t sofm_exp_fixed(int32_t x) { int32_t xs; int32_t y; @@ -247,7 +245,7 @@ int32_t exp_fixed(int32_t x) /* x is Q5.27 */ xs = x; - while (xs >= TWO_Q27 || xs <= MINUS_TWO_Q27) { + while (xs >= EXP_TWO_Q27 || xs <= EXP_MINUS_TWO_Q27) { xs >>= 1; n++; } @@ -256,7 +254,7 @@ int32_t exp_fixed(int32_t x) * sofm_exp_int32() output is Q9.23, while y0 is Q12.20 */ y0 = Q_SHIFT_RND(sofm_exp_int32(Q_SHIFT_LEFT(xs, 27, 28)), 23, 20); - y = ONE_Q20; + y = EXP_ONE_Q20; for (i = 0; i < (1 << n); i++) y = (int32_t)Q_MULTSR_32X32((int64_t)y, y0, 20, 20, 20); diff --git a/src/math/exp_fcn_hifi.c b/src/math/exp_fcn_hifi.c index a14133f477a5..153e474c3ce6 100644 --- a/src/math/exp_fcn_hifi.c +++ b/src/math/exp_fcn_hifi.c @@ -342,9 +342,6 @@ static inline int exp_hifi_q_shift_left(int a, int b, int c) return xt_o; } -#define ONE_Q20 exp_hifi_q_convert_float(1.0, 20) /* Use Q12.20 */ -#define TWO_Q27 exp_hifi_q_convert_float(2.0, 27) /* Use Q5.27 */ -#define MINUS_TWO_Q27 exp_hifi_q_convert_float(-2.0, 27) /* Use Q5.27 */ #define q_mult(a, b, qa, qb, qy) ((int32_t)exp_hifi_q_multsr_32x32((int64_t)(a), b, qa, qb, qy)) /* Fixed point exponent function for approximate range -11.5 .. 7.6 * that corresponds to decibels range -100 .. +66 dB. @@ -359,7 +356,7 @@ static inline int exp_hifi_q_shift_left(int a, int b, int c) * Output is Q12.20, 0.0 .. +2048.0 */ -int32_t exp_fixed(int32_t x) +int32_t sofm_exp_fixed(int32_t x) { int32_t xs; int32_t y; @@ -375,7 +372,7 @@ int32_t exp_fixed(int32_t x) /* x is Q5.27 */ xs = x; - while (xs >= TWO_Q27 || xs <= MINUS_TWO_Q27) { + while (xs >= EXP_TWO_Q27 || xs <= EXP_MINUS_TWO_Q27) { xs >>= 1; n++; } @@ -383,11 +380,11 @@ int32_t exp_fixed(int32_t x) /* sofm_exp_int32() input is Q4.28, while x1 is Q5.27 * sofm_exp_int32() output is Q9.23, while y0 is Q12.20 */ - y0 = exp_hifi_q_shift_rnd( - sofm_exp_int32( - exp_hifi_q_shift_left(xs, 27, 28)), - 23, 20); - y = ONE_Q20; + y0 = exp_hifi_q_shift_rnd(sofm_exp_int32 + (exp_hifi_q_shift_left(xs, 27, 28) + ), 23, 20 + ); + y = EXP_ONE_Q20; for (i = 0; i < (1 << n); i++) y = (int32_t)exp_hifi_q_multsr_32x32((int64_t)y, y0, 20, 20, 20); diff --git a/src/math/window.c b/src/math/window.c index 4795112ffc0f..33bbcae38bab 100644 --- a/src/math/window.c +++ b/src/math/window.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -114,7 +115,7 @@ void win_povey_16b(int16_t win[], int length) /* Calculate x^0.85 as exp(0.85 * log(x)) */ x2 = (int32_t)(ln_int32((uint32_t)x1) >> 1) - WIN_LOG_2POW31_Q26; x3 = sat_int32(Q_MULTSR_32X32((int64_t)x2, WIN_085_Q31, 26, 31, 27)); /* Q5.27 */ - x4 = exp_fixed(x3); /* Q5.27 -> Q12.20 */ + x4 = sofm_exp_fixed(x3); /* Q5.27 -> Q12.20 */ /* Convert to Q1.15 */ win[n] = sat_int16(Q_SHIFT_RND(x4, 20, 15)); diff --git a/test/cmocka/src/math/auditory/CMakeLists.txt b/test/cmocka/src/math/auditory/CMakeLists.txt index 388ea6f8bd79..2fe2de1193b7 100644 --- a/test/cmocka/src/math/auditory/CMakeLists.txt +++ b/test/cmocka/src/math/auditory/CMakeLists.txt @@ -10,4 +10,5 @@ cmocka_test(auditory ${PROJECT_SOURCE_DIR}/src/math/decibels.c ${PROJECT_SOURCE_DIR}/src/math/numbers.c ${PROJECT_SOURCE_DIR}/src/math/exp_fcn.c + ${PROJECT_SOURCE_DIR}/src/math/exp_fcn_hifi.c ) diff --git a/test/cmocka/src/math/window/CMakeLists.txt b/test/cmocka/src/math/window/CMakeLists.txt index 6e5629e8f60d..69c86aa525ec 100644 --- a/test/cmocka/src/math/window/CMakeLists.txt +++ b/test/cmocka/src/math/window/CMakeLists.txt @@ -8,4 +8,5 @@ cmocka_test(window ${PROJECT_SOURCE_DIR}/src/math/base2log.c ${PROJECT_SOURCE_DIR}/src/math/decibels.c ${PROJECT_SOURCE_DIR}/src/math/exp_fcn.c + ${PROJECT_SOURCE_DIR}/src/math/exp_fcn_hifi.c ) diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 1ae14efc29a2..171d049c1ada 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -370,6 +370,8 @@ zephyr_library_sources( ${SOF_MATH_PATH}/decibels.c ${SOF_MATH_PATH}/numbers.c ${SOF_MATH_PATH}/trig.c + ${SOF_MATH_PATH}/exp_fcn.c + ${SOF_MATH_PATH}/exp_fcn_hifi.c # SOF library - parts to transition to Zephyr over time ${SOF_LIB_PATH}/clk.c