Skip to content

Commit

Permalink
DRC: math: Rename variables and functions in accordance SOF math regu…
Browse files Browse the repository at this point in the history
…lations.

Rename variables and functions in the code to reflect
sof math and hi-fi usage.

Signed-off-by: shastry <[email protected]>
  • Loading branch information
ShriramShastry committed Dec 7, 2023
1 parent d8944c1 commit a3519e1
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 30 deletions.
9 changes: 6 additions & 3 deletions src/audio/drc/drc_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
4 changes: 2 additions & 2 deletions src/audio/drc/drc_hifi3.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down
4 changes: 2 additions & 2 deletions src/audio/drc/drc_hifi4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/audio/drc/drc_math_generic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/include/sof/math/decibels.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__ */
5 changes: 4 additions & 1 deletion src/include/sof/math/exp_fcn.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ endif()

add_local_sources_ifdef(CONFIG_SQRT_FIXED sof sqrt_int16.c)

add_local_sources_ifdef(CONFIG_MATH_EXP sof exp_fcn.c exp_fcn_hifi.c)
if(CONFIG_MATH_EXP)
add_local_sources(sof exp_fcn.c exp_fcn_hifi.c)
endif()

if(CONFIG_MATH_DECIBELS)
add_local_sources(sof decibels.c)
Expand Down
2 changes: 1 addition & 1 deletion src/math/auditory/auditory.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/math/decibels.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
10 changes: 4 additions & 6 deletions src/math/exp_fcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
Expand All @@ -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++;
}
Expand All @@ -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);

Expand Down
17 changes: 7 additions & 10 deletions src/math/exp_fcn_hifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -375,19 +372,19 @@ 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++;
}

/* 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);

Expand Down
2 changes: 1 addition & 1 deletion src/math/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,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));
Expand Down
1 change: 1 addition & 0 deletions test/cmocka/src/math/auditory/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
1 change: 1 addition & 0 deletions test/cmocka/src/math/window/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit a3519e1

Please sign in to comment.