From 5757f2cca83d6d60451a822d77d51abab61c9e17 Mon Sep 17 00:00:00 2001 From: malenov Date: Wed, 27 Nov 2024 11:11:48 +0100 Subject: [PATCH 1/5] fix discpreancy within the Copy() function depending on memory locations of x[] and y[] --- lib_com/options.h | 1 + lib_com/tools_fx.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib_com/options.h b/lib_com/options.h index 0ae5cf8ca..03a1b1e92 100644 --- a/lib_com/options.h +++ b/lib_com/options.h @@ -84,4 +84,5 @@ #define NONBE_1233_HQ_CLASSIFIER_DIV_BY_ZERO /* Eri: issue 1233: Address possible division by zero in hf_spectrum_sparseness() */ #define FIX_ISSUE_1062_AND_1068_TON_ENE_EST_FX #define FIX_1054_IF_ELSE_CMPLX /* VA: Fix 1054 incorrect counting of complexity when ELSE-IF sequence is encoutered in two functions */ +#define FIX_1052_COPY_CMPLX_DISCREPANCY /* VA: modify IF-ELSE statements used in Copy*() functions to avoid dependency on x[] and y[] in RAM */ #endif diff --git a/lib_com/tools_fx.c b/lib_com/tools_fx.c index ba3e9c85b..100072926 100644 --- a/lib_com/tools_fx.c +++ b/lib_com/tools_fx.c @@ -486,7 +486,12 @@ void Copy( move16(); } } + +#ifdef FIX_IF_ELSE_CMPLX_DISCREPANCY + IF( y >= x ) +#else ELSE +#endif { FOR( i = L - 1; i >= 0; i-- ) { @@ -515,7 +520,12 @@ void Copy64( move64(); } } + +#ifdef FIX_IF_ELSE_CMPLX_DISCREPANCY + IF( y >= x ) +#else ELSE +#endif { FOR( i = L - 1; i >= 0; i-- ) { @@ -559,7 +569,12 @@ void Copy_pword( move16(); } } + +#ifdef FIX_IF_ELSE_CMPLX_DISCREPANCY + IF( y >= x ) +#else ELSE +#endif { FOR( i = L - 1; i >= 0; i-- ) { @@ -590,7 +605,12 @@ void Copy32( move32(); } } + +#ifdef FIX_IF_ELSE_CMPLX_DISCREPANCY + IF( y >= x ) +#else ELSE +#endif { FOR( i = L - 1; i >= 0; i-- ) { -- GitLab From b63fcd9df45466572b925bbd021000bafb9d1ec1 Mon Sep 17 00:00:00 2001 From: Vladimir Malenovsky Date: Wed, 4 Dec 2024 11:11:40 +0100 Subject: [PATCH 2/5] modify IF-ELSE statements in Copy*() functions to avoid differences in complexity --- lib_com/tools_fx.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib_com/tools_fx.c b/lib_com/tools_fx.c index 100072926..99b942f15 100644 --- a/lib_com/tools_fx.c +++ b/lib_com/tools_fx.c @@ -487,7 +487,7 @@ void Copy( } } -#ifdef FIX_IF_ELSE_CMPLX_DISCREPANCY +#ifdef FIX_1052_COPY_CMPLX_DISCREPANCY IF( y >= x ) #else ELSE @@ -521,7 +521,7 @@ void Copy64( } } -#ifdef FIX_IF_ELSE_CMPLX_DISCREPANCY +#ifdef FIX_1052_COPY_CMPLX_DISCREPANCY IF( y >= x ) #else ELSE @@ -570,7 +570,7 @@ void Copy_pword( } } -#ifdef FIX_IF_ELSE_CMPLX_DISCREPANCY +#ifdef FIX_1052_COPY_CMPLX_DISCREPANCY IF( y >= x ) #else ELSE @@ -606,7 +606,7 @@ void Copy32( } } -#ifdef FIX_IF_ELSE_CMPLX_DISCREPANCY +#ifdef FIX_1052_COPY_CMPLX_DISCREPANCY IF( y >= x ) #else ELSE -- GitLab From 809e0d6985c4b2855319659e201dbf6cd1d5ba16 Mon Sep 17 00:00:00 2001 From: malenov Date: Wed, 4 Dec 2024 11:23:12 +0100 Subject: [PATCH 3/5] more elegant solution propose by Arthur --- lib_com/tools_fx.c | 48 ++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/lib_com/tools_fx.c b/lib_com/tools_fx.c index 99b942f15..fc573201f 100644 --- a/lib_com/tools_fx.c +++ b/lib_com/tools_fx.c @@ -485,20 +485,24 @@ void Copy( y[i] = x[i]; move16(); } - } #ifdef FIX_1052_COPY_CMPLX_DISCREPANCY - IF( y >= x ) -#else - ELSE + return; #endif + } + +#ifndef FIX_1052_COPY_CMPLX_DISCREPANCY + ELSE { +#endif FOR( i = L - 1; i >= 0; i-- ) { y[i] = x[i]; move16(); } +#ifndef FIX_1052_COPY_CMPLX_DISCREPANCY } +#endif } /*-------------------------------------------------------------------* * Copy64: @@ -519,20 +523,24 @@ void Copy64( y[i] = x[i]; move64(); } - } #ifdef FIX_1052_COPY_CMPLX_DISCREPANCY - IF( y >= x ) -#else - ELSE + return; #endif + } + +#ifndef FIX_1052_COPY_CMPLX_DISCREPANCY + ELSE { +#endif FOR( i = L - 1; i >= 0; i-- ) { y[i] = x[i]; move64(); } +#ifndef FIX_1052_COPY_CMPLX_DISCREPANCY } +#endif } void set64_fx( @@ -568,14 +576,16 @@ void Copy_pword( move16(); move16(); } - } #ifdef FIX_1052_COPY_CMPLX_DISCREPANCY - IF( y >= x ) -#else - ELSE + return; #endif + } + +#ifndef FIX_1052_COPY_CMPLX_DISCREPANCY + ELSE { +#endif FOR( i = L - 1; i >= 0; i-- ) { y[i].v.im = x[i].v.im; @@ -583,7 +593,9 @@ void Copy_pword( move16(); move16(); } +#ifndef FIX_1052_COPY_CMPLX_DISCREPANCY } +#endif } /*-------------------------------------------------------------------* * Copy32: @@ -604,20 +616,24 @@ void Copy32( y[i] = x[i]; move32(); } - } #ifdef FIX_1052_COPY_CMPLX_DISCREPANCY - IF( y >= x ) -#else - ELSE + return; #endif + } + +#ifndef FIX_1052_COPY_CMPLX_DISCREPANCY + ELSE { +#endif FOR( i = L - 1; i >= 0; i-- ) { y[i] = x[i]; move32(); } +#ifndef FIX_1052_COPY_CMPLX_DISCREPANCY } +#endif } void set8_fx( -- GitLab From 58a18b532b6febd2aca5841f9378ecb639675917 Mon Sep 17 00:00:00 2001 From: malenov Date: Mon, 9 Dec 2024 12:39:37 +0100 Subject: [PATCH 4/5] - insert explanation - fix indentation - add return --- lib_com/tools_fx.c | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/lib_com/tools_fx.c b/lib_com/tools_fx.c index fc573201f..fe07680f8 100644 --- a/lib_com/tools_fx.c +++ b/lib_com/tools_fx.c @@ -469,6 +469,7 @@ Word32 sum2_fx_mod( /* o : sum of all squared vector element * Copy: * * Copy vector x[] to y[] + * *-------------------------------------------------------------------*/ void Copy( const Word16 x[], /* i : input vector */ @@ -487,6 +488,7 @@ void Copy( } #ifdef FIX_1052_COPY_CMPLX_DISCREPANCY + /* Location of x and y may differ depending on platform/memory allocation. Since IF and ELSE has different complexity count, the early return is used instead of ELSE to ensure the same complexity number regardless of x and y memory addresses. */ return; #endif } @@ -495,11 +497,13 @@ void Copy( ELSE { #endif - FOR( i = L - 1; i >= 0; i-- ) - { - y[i] = x[i]; - move16(); - } + FOR( i = L - 1; i >= 0; i-- ) + { + y[i] = x[i]; + move16(); + } + + return; #ifndef FIX_1052_COPY_CMPLX_DISCREPANCY } #endif @@ -525,6 +529,7 @@ void Copy64( } #ifdef FIX_1052_COPY_CMPLX_DISCREPANCY + /* Location of x and y may differ depending on platform/memory allocation. Since IF and ELSE has different complexity count, the early return is used instead of ELSE to ensure the same complexity number regardless of x and y memory addresses. */ return; #endif } @@ -533,11 +538,13 @@ void Copy64( ELSE { #endif - FOR( i = L - 1; i >= 0; i-- ) - { - y[i] = x[i]; - move64(); - } + FOR( i = L - 1; i >= 0; i-- ) + { + y[i] = x[i]; + move64(); + } + + return; #ifndef FIX_1052_COPY_CMPLX_DISCREPANCY } #endif @@ -578,6 +585,7 @@ void Copy_pword( } #ifdef FIX_1052_COPY_CMPLX_DISCREPANCY + /* Location of x and y may differ depending on platform/memory allocation. Since IF and ELSE has different complexity count, the early return is used instead of ELSE to ensure the same complexity number regardless of x and y memory addresses. */ return; #endif } @@ -586,13 +594,15 @@ void Copy_pword( ELSE { #endif - FOR( i = L - 1; i >= 0; i-- ) - { - y[i].v.im = x[i].v.im; - y[i].v.re = x[i].v.re; - move16(); - move16(); - } + FOR( i = L - 1; i >= 0; i-- ) + { + y[i].v.im = x[i].v.im; + y[i].v.re = x[i].v.re; + move16(); + move16(); + } + + return; #ifndef FIX_1052_COPY_CMPLX_DISCREPANCY } #endif -- GitLab From abf8a1f4359e411bdad697d6154248fd692ae2af Mon Sep 17 00:00:00 2001 From: Vladimir Malenovsky Date: Mon, 9 Dec 2024 15:16:25 +0100 Subject: [PATCH 5/5] clang format --- lib_com/tools_fx.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/lib_com/tools_fx.c b/lib_com/tools_fx.c index fe07680f8..b6cb03469 100644 --- a/lib_com/tools_fx.c +++ b/lib_com/tools_fx.c @@ -469,7 +469,7 @@ Word32 sum2_fx_mod( /* o : sum of all squared vector element * Copy: * * Copy vector x[] to y[] - * + * *-------------------------------------------------------------------*/ void Copy( const Word16 x[], /* i : input vector */ @@ -497,13 +497,13 @@ void Copy( ELSE { #endif - FOR( i = L - 1; i >= 0; i-- ) - { - y[i] = x[i]; - move16(); - } + FOR( i = L - 1; i >= 0; i-- ) + { + y[i] = x[i]; + move16(); + } - return; + return; #ifndef FIX_1052_COPY_CMPLX_DISCREPANCY } #endif @@ -538,13 +538,13 @@ void Copy64( ELSE { #endif - FOR( i = L - 1; i >= 0; i-- ) - { - y[i] = x[i]; - move64(); - } + FOR( i = L - 1; i >= 0; i-- ) + { + y[i] = x[i]; + move64(); + } - return; + return; #ifndef FIX_1052_COPY_CMPLX_DISCREPANCY } #endif @@ -594,15 +594,15 @@ void Copy_pword( ELSE { #endif - FOR( i = L - 1; i >= 0; i-- ) - { - y[i].v.im = x[i].v.im; - y[i].v.re = x[i].v.re; - move16(); - move16(); - } + FOR( i = L - 1; i >= 0; i-- ) + { + y[i].v.im = x[i].v.im; + y[i].v.re = x[i].v.re; + move16(); + move16(); + } - return; + return; #ifndef FIX_1052_COPY_CMPLX_DISCREPANCY } #endif -- GitLab