From 839c13bba9dfe5752d2c35de24c4a41f7b446eac Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 2 Jul 2025 16:38:43 +0200 Subject: [PATCH 1/5] fix issue 1795: Q3 overflow --- lib_com/options.h | 2 ++ lib_com/swb_bwe_com_fx.c | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib_com/options.h b/lib_com/options.h index d1b3cb63e..d82b4862e 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -111,4 +111,6 @@ #define FIX_1735_W_SHL_SAT_L /* FhG: Usage of W_shl_sat_l() */ +#define FIX_ISSUE_1795_Q3_OVERFLOW /* FhG: Q3 overflow in function WB_BWE_gain_pred_fx (EVS legacy code) BE, MR1855 */ + #endif diff --git a/lib_com/swb_bwe_com_fx.c b/lib_com/swb_bwe_com_fx.c index 11da78c46..d017d9ba6 100644 --- a/lib_com/swb_bwe_com_fx.c +++ b/lib_com/swb_bwe_com_fx.c @@ -242,8 +242,15 @@ Word16 WB_BWE_gain_pred_fx( enerL = L_deposit_l( enerL_16 ); enerL = L_shl( enerL, 6 ); /*Q6 */ +#ifdef FIX_ISSUE_1795_Q3_OVERFLOW_not + /* Here, we first square WB_fenv into Q6 format, then it is taken 3x */ + L_tmp = L_mult0( WB_fenv[0], WB_fenv[0] ); /* Q6 */ + L_tmp = L_add(L_tmp, L_add( L_tmp, L_tmp ) ); /* Q6 */ +#else + /* Here, we have not enough headroom for mult with 3, so we get some overflow */ tmp1 = i_mult_sat( 3, WB_fenv[0] ); /*Q3 */ L_tmp = L_mult0( tmp1, WB_fenv[0] ); /*Q6 */ +#endif test(); test(); -- GitLab From 01863bfdf2187dc135cf84cc75ec5f930ff6e0b3 Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 2 Jul 2025 16:48:30 +0200 Subject: [PATCH 2/5] fix clang-format --- lib_com/swb_bwe_com_fx.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib_com/swb_bwe_com_fx.c b/lib_com/swb_bwe_com_fx.c index d017d9ba6..792ff0619 100644 --- a/lib_com/swb_bwe_com_fx.c +++ b/lib_com/swb_bwe_com_fx.c @@ -241,11 +241,11 @@ Word16 WB_BWE_gain_pred_fx( tmp = extract_l( L_tmp ); enerL = L_deposit_l( enerL_16 ); - enerL = L_shl( enerL, 6 ); /*Q6 */ + enerL = L_shl( enerL, 6 ); /*Q6 */ #ifdef FIX_ISSUE_1795_Q3_OVERFLOW_not /* Here, we first square WB_fenv into Q6 format, then it is taken 3x */ L_tmp = L_mult0( WB_fenv[0], WB_fenv[0] ); /* Q6 */ - L_tmp = L_add(L_tmp, L_add( L_tmp, L_tmp ) ); /* Q6 */ + L_tmp = L_add( L_tmp, L_add( L_tmp, L_tmp ) ); /* Q6 */ #else /* Here, we have not enough headroom for mult with 3, so we get some overflow */ tmp1 = i_mult_sat( 3, WB_fenv[0] ); /*Q3 */ -- GitLab From b9cdafd5f60802c9381b934d652ffb0582245c2a Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 2 Jul 2025 17:58:17 +0200 Subject: [PATCH 3/5] correct macro setting, was inactive --- lib_com/swb_bwe_com_fx.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib_com/swb_bwe_com_fx.c b/lib_com/swb_bwe_com_fx.c index 792ff0619..d85c4fe04 100644 --- a/lib_com/swb_bwe_com_fx.c +++ b/lib_com/swb_bwe_com_fx.c @@ -242,7 +242,7 @@ Word16 WB_BWE_gain_pred_fx( enerL = L_deposit_l( enerL_16 ); enerL = L_shl( enerL, 6 ); /*Q6 */ -#ifdef FIX_ISSUE_1795_Q3_OVERFLOW_not +#ifdef FIX_ISSUE_1795_Q3_OVERFLOW /* Here, we first square WB_fenv into Q6 format, then it is taken 3x */ L_tmp = L_mult0( WB_fenv[0], WB_fenv[0] ); /* Q6 */ L_tmp = L_add( L_tmp, L_add( L_tmp, L_tmp ) ); /* Q6 */ -- GitLab From c4df00266944f7f5edb338d511d0bad1cb167350 Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 2 Jul 2025 19:02:48 +0200 Subject: [PATCH 4/5] correct macro setting, was inactive, use option B --- lib_com/swb_bwe_com_fx.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib_com/swb_bwe_com_fx.c b/lib_com/swb_bwe_com_fx.c index d85c4fe04..689dff46b 100644 --- a/lib_com/swb_bwe_com_fx.c +++ b/lib_com/swb_bwe_com_fx.c @@ -243,9 +243,8 @@ Word16 WB_BWE_gain_pred_fx( enerL = L_deposit_l( enerL_16 ); enerL = L_shl( enerL, 6 ); /*Q6 */ #ifdef FIX_ISSUE_1795_Q3_OVERFLOW - /* Here, we first square WB_fenv into Q6 format, then it is taken 3x */ + /* Here, we do not multiply L_tmp by 3 to avoid overflow */ L_tmp = L_mult0( WB_fenv[0], WB_fenv[0] ); /* Q6 */ - L_tmp = L_add( L_tmp, L_add( L_tmp, L_tmp ) ); /* Q6 */ #else /* Here, we have not enough headroom for mult with 3, so we get some overflow */ tmp1 = i_mult_sat( 3, WB_fenv[0] ); /*Q3 */ @@ -255,7 +254,13 @@ Word16 WB_BWE_gain_pred_fx( test(); test(); test(); +#ifdef FIX_ISSUE_1795_Q3_OVERFLOW +#define ONE_DIV_3 ((Word32) 0x2AAAAAAA) + /* Here, L_tmp is not pre-multiplied with 3, we multiply enerL with 1/3 */ + IF( GT_16( shr( enerL_16, 3 ), tmp ) && GT_32( Mpy_32_32(ONE_DIV_3, enerL), L_tmp ) && NE_16( prev_coder_type, UNVOICED ) && WB_fenv[0] != 0 ) +#else IF( GT_16( shr( enerL_16, 3 ), tmp ) && GT_32( enerL, L_tmp ) && NE_16( prev_coder_type, UNVOICED ) && WB_fenv[0] != 0 ) +#endif { env_var_flag = 1; move16(); -- GitLab From a832e36753f30d284c77b19fee4d928df763c613 Mon Sep 17 00:00:00 2001 From: Arthur Date: Wed, 2 Jul 2025 19:08:21 +0200 Subject: [PATCH 5/5] fix clang-format --- lib_com/swb_bwe_com_fx.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib_com/swb_bwe_com_fx.c b/lib_com/swb_bwe_com_fx.c index 689dff46b..8b134e5d6 100644 --- a/lib_com/swb_bwe_com_fx.c +++ b/lib_com/swb_bwe_com_fx.c @@ -244,7 +244,7 @@ Word16 WB_BWE_gain_pred_fx( enerL = L_shl( enerL, 6 ); /*Q6 */ #ifdef FIX_ISSUE_1795_Q3_OVERFLOW /* Here, we do not multiply L_tmp by 3 to avoid overflow */ - L_tmp = L_mult0( WB_fenv[0], WB_fenv[0] ); /* Q6 */ + L_tmp = L_mult0( WB_fenv[0], WB_fenv[0] ); /* Q6 */ #else /* Here, we have not enough headroom for mult with 3, so we get some overflow */ tmp1 = i_mult_sat( 3, WB_fenv[0] ); /*Q3 */ @@ -255,9 +255,9 @@ Word16 WB_BWE_gain_pred_fx( test(); test(); #ifdef FIX_ISSUE_1795_Q3_OVERFLOW -#define ONE_DIV_3 ((Word32) 0x2AAAAAAA) +#define ONE_DIV_3 ( (Word32) 0x2AAAAAAA ) /* Here, L_tmp is not pre-multiplied with 3, we multiply enerL with 1/3 */ - IF( GT_16( shr( enerL_16, 3 ), tmp ) && GT_32( Mpy_32_32(ONE_DIV_3, enerL), L_tmp ) && NE_16( prev_coder_type, UNVOICED ) && WB_fenv[0] != 0 ) + IF( GT_16( shr( enerL_16, 3 ), tmp ) && GT_32( Mpy_32_32( ONE_DIV_3, enerL ), L_tmp ) && NE_16( prev_coder_type, UNVOICED ) && WB_fenv[0] != 0 ) #else IF( GT_16( shr( enerL_16, 3 ), tmp ) && GT_32( enerL, L_tmp ) && NE_16( prev_coder_type, UNVOICED ) && WB_fenv[0] != 0 ) #endif -- GitLab