Commit 2bcc3f4b authored by Nicolas Roussin's avatar Nicolas Roussin
Browse files

Add back computational checks.

parent 82603e74
Loading
Loading
Loading
Loading
+102 −28
Original line number Diff line number Diff line
@@ -1363,6 +1363,42 @@ static Word64 Mpy_64_32( Word64 W_var1, Word32 L_var2 )
    return var_out;
}

#define NONBE_2157_VERIFY_ACCURACY

#ifdef NONBE_2157_VERIFY_ACCURACY
static void check(Word64 computed_fx, Word32 computed_q, Word64 expected_fx, Word32 expected_e, Word64 max_error);

static void check(Word64 computed_fx, Word32 computed_q, Word64 expected_fx, Word32 expected_e, Word64 max_error)
{
    Word32 expected_q = 31 - expected_e;
    Word32 shift = expected_q - computed_q;

    if (shift >= 0)
    {
        expected_fx >>= shift;
    } 
    else 
    {
        computed_fx >>= -shift;
    }

    Word64 error = llabs(expected_fx - computed_fx);
    if (error > max_error)
    {
        printf("\n\n");
        printf("0x%016llx\n", computed_fx);
        printf("%d\n", computed_q);
        printf("0x%016llx\n", expected_fx);
        printf("%d\n", expected_q);
        assert(false);
    }
}

#define CHECK_ACCURACY(CFX, CQ, EFX, EE, ME) check(CFX, CQ, EFX, EE, ME)
#else
#define CHECK_ACCURACY(CFX, CQ, EFX, EE, ME)
#endif

static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
    DIRAC_DEC_BIN_HANDLE hDiracDecBin,
    SPAT_PARAM_REND_COMMON_DATA_HANDLE hSpatParamRendCom,
@@ -1386,8 +1422,9 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
    Word32 ivas_total_brate;
    Word16 nchan_transport;
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
    Word16 rsh0, rsh1, exp0, exp1;
#else
    Word16 rsh0, rsh1, e0, e1;
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
    Word16 exp, exp1;
    Word32 temp;
#endif
@@ -1409,7 +1446,8 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
    set64_fx( hDiracDecBin->ChCrossRe, 0, nBins );
    set64_fx( hDiracDecBin->ChCrossIm, 0, nBins );
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
    set32_fx( hDiracDecBin->ChCrossRe_fx, 0, nBins );
    set32_fx( hDiracDecBin->ChCrossIm_fx, 0, nBins );
    set16_fx( hDiracDecBin->ChCrossRe_e, 0, nBins );
@@ -1420,7 +1458,8 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
    {
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
        set64_fx( hDiracDecBin->ChEne[ch], 0, nBins );
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
        set32_fx( hDiracDecBin->ChEne_fx[ch], 0, nBins );
        set16_fx( hDiracDecBin->ChEne_e[ch], 0, nBins );
#endif
@@ -1459,16 +1498,18 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
    /* Formulate input and target covariance matrices for this subframe */
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
    set64_fx( subFrameTotalEne, 0, CLDFB_NO_CHANNELS_MAX );
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
    set32_fx( subFrameTotalEne_fx, 0, CLDFB_NO_CHANNELS_MAX );
#endif

#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
    rsh0 = (Word16) ( ceil_log2( hSpatParamRendCom->subframe_nbslots[subframe] ) - 1 );
    rsh1 = (Word16) ( ceil_log2( BINAURAL_CHANNELS * hSpatParamRendCom->subframe_nbslots[subframe] ) - 1 );
    exp0 = add( sub( 63, shl( q, 1 ) ), rsh0 );
    exp1 = add( sub( 63, shl( q, 1 ) ), rsh1 );
#else
    e0 = add( sub( 63, shl( q, 1 ) ), rsh0 );
    e1 = add( sub( 63, shl( q, 1 ) ), rsh1 );
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
    exp = sub( 63, shl( q, 1 ) ); // exp for the energy (inRe_fx * inRe_fx + inIm_fx * inIm_fx) computed below
#endif

@@ -1486,7 +1527,8 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
                subFrameTotalEne[bin] = W_add( subFrameTotalEne[bin], W_shr( temp64, rsh1 ) );               // 2q - rsh1
                move64();
                move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
                Word32 instEne_fx;
                exp1 = W_norm( temp64 );
                instEne_fx = W_extract_h( W_shl( temp64, exp1 ) ); // 2q - 32 + exp1
@@ -1496,30 +1538,36 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
                move32();
                move32();
#endif
                CHECK_ACCURACY(hDiracDecBin->ChEne[ch][bin], 2 * q - rsh0, hDiracDecBin->ChEne_fx[ch][bin], hDiracDecBin->ChEne_e[ch][bin], 16);
                CHECK_ACCURACY(subFrameTotalEne[bin], 2 * q - rsh1, subFrameTotalEne_fx[bin], subFrameTotalEne_e[bin], 16);
            }
            temp64 = W_mult0_32_32( inRe_fx[0][slot][bin], inRe_fx[1][slot][bin] );                  // 2q
            temp64 = W_add( temp64, W_mult0_32_32( inIm_fx[0][slot][bin], inIm_fx[1][slot][bin] ) ); // 2q
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
            hDiracDecBin->ChCrossRe[bin] = W_add( hDiracDecBin->ChCrossRe[bin], W_shr( temp64, rsh0 ) ); // 2q - rsh0
            move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
            exp1 = W_norm( temp64 );
            temp = W_extract_h( W_shl( temp64, exp1 ) ); // // 2q - 32 + exp1
            hDiracDecBin->ChCrossRe_fx[bin] = BASOP_Util_Add_Mant32Exp( hDiracDecBin->ChCrossRe_fx[bin], hDiracDecBin->ChCrossRe_e[bin], temp, sub( exp, exp1 ), &hDiracDecBin->ChCrossRe_e[bin] );
            move32();
#endif
            CHECK_ACCURACY(hDiracDecBin->ChCrossRe[bin], 2 * q - rsh0, hDiracDecBin->ChCrossRe_fx[bin], hDiracDecBin->ChCrossRe_e[bin], 16);

            temp64 = W_mult0_32_32( inRe_fx[0][slot][bin], inIm_fx[1][slot][bin] );                  // 2q
            temp64 = W_sub( temp64, W_mult0_32_32( inIm_fx[0][slot][bin], inRe_fx[1][slot][bin] ) ); // 2q
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
            hDiracDecBin->ChCrossIm[bin] = W_add( hDiracDecBin->ChCrossIm[bin], W_shr( temp64, rsh0 ) ); // 2q - rsh0
            move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
            exp1 = W_norm( temp64 );
            temp = W_extract_h( W_shl( temp64, exp1 ) ); // // 2q - 32 + exp1
            hDiracDecBin->ChCrossIm_fx[bin] = BASOP_Util_Add_Mant32Exp( hDiracDecBin->ChCrossIm_fx[bin], hDiracDecBin->ChCrossIm_e[bin], temp, sub( exp, exp1 ), &hDiracDecBin->ChCrossIm_e[bin] );
            move32();
#endif
            CHECK_ACCURACY(hDiracDecBin->ChCrossIm[bin], 2 * q - rsh0, hDiracDecBin->ChCrossIm_fx[bin], hDiracDecBin->ChCrossIm_e[bin], 16);
        }
    }

@@ -1533,20 +1581,24 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
            subFrameTotalEne[bin] = Mpy_64_32( subFrameTotalEne[bin], lowBitRateEQ_fx[bin] );
            move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
            subFrameTotalEne_fx[bin] = Mpy_32_32( subFrameTotalEne_fx[bin], lowBitRateEQ_fx[bin] ); // exp = subFrameTotalEne_e[bin]
            move32();
#endif
            CHECK_ACCURACY(subFrameTotalEne[bin], 2 * q - rsh1, subFrameTotalEne_fx[bin], subFrameTotalEne_e[bin], 16);
        }
        FOR( ; bin < nBins; bin++ )
        {
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
            subFrameTotalEne[bin] = Mpy_64_32( subFrameTotalEne[bin], lowBitRateEQ_fx[lastEqBin] );
            move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
            subFrameTotalEne_fx[bin] = Mpy_32_32( subFrameTotalEne_fx[bin], lowBitRateEQ_fx[lastEqBin] ); // exp = subFrameTotalEne_e[bin]
            move32();
#endif
            CHECK_ACCURACY(subFrameTotalEne[bin], 2 * q - rsh1, subFrameTotalEne_fx[bin], subFrameTotalEne_e[bin], 16);
        }
    }

@@ -1557,13 +1609,15 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
        Word32 tempRe, tempIm;
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
        Word64 subFrameSumEne[CLDFB_NO_CHANNELS_MAX];
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
        Word32 subFrameSumEne_fx[CLDFB_NO_CHANNELS_MAX];
        Word16 subFrameSumEne_e[CLDFB_NO_CHANNELS_MAX];
#endif
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
        set64_fx( subFrameSumEne, 0, CLDFB_NO_CHANNELS_MAX );
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
        set32_fx( subFrameSumEne_fx, 0, CLDFB_NO_CHANNELS_MAX );
        set16_fx( subFrameSumEne_e, 0, CLDFB_NO_CHANNELS_MAX );
#endif
@@ -1577,12 +1631,14 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
                subFrameSumEne[bin] = W_add( subFrameSumEne[bin], W_shr( temp64, rsh1 ) );
                move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
                exp1 = W_norm( temp64 );
                temp = W_extract_h( W_shl( temp64, exp1 ) ); // 2q + exp1
                subFrameSumEne_fx[bin] = BASOP_Util_Add_Mant32Exp( subFrameSumEne_fx[bin], subFrameSumEne_e[bin], temp, sub( exp /* 63 - 2q */, exp1 ) /*31 - (2q + exp1 - 32)*/, &subFrameSumEne_e[bin] );
                move32();
#endif
                CHECK_ACCURACY(subFrameSumEne[bin], 2 * q - rsh1, subFrameSumEne_fx[bin], subFrameSumEne_e[bin], 16);
            }
        }
        FOR( bin = 0; bin < nBins; bin++ )
@@ -1590,7 +1646,9 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
            subFrameTotalEne[bin] = W_max( subFrameSumEne[bin], subFrameTotalEne[bin] );
            move64();
#else
#endif

#ifdef NONBE_2157_VERIFY_ACCURACY
            subFrameTotalEne_e[bin] = sub( subFrameTotalEne_e[bin], 1 );
            move16();
            temp = L_shl_sat( subFrameTotalEne_fx[bin], sub( subFrameTotalEne_e[bin], subFrameSumEne_e[bin] ) ); // subFrameSumEne_e[bin]
@@ -1602,6 +1660,7 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
                move16();
            }
#endif
            CHECK_ACCURACY(subFrameTotalEne[bin], 2 * q - rsh1, subFrameTotalEne_fx[bin], subFrameTotalEne_e[bin], 16);
        }
    }

@@ -1645,7 +1704,8 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
        {
            IIReneLimiter_fx[bin] = L_shl( IIReneLimiter_fx[bin], exp ); /*Q31*/
        }
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
        num = BASOP_Util_Add_Mant32Exp( hDiracDecBin->ChEne_fx[0][bin], hDiracDecBin->ChEne_e[0][bin], hDiracDecBin->ChEne_fx[1][bin], hDiracDecBin->ChEne_e[1][bin], &num_e );
        num = Mpy_32_32( num, IIReneLimiterFactor_fx ); /*Q = (31 - num_e + 26 - 31) = (26 - num_e)*/
        den_e = 0;
@@ -1672,7 +1732,8 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
        move64();
        move64();
        move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
        hDiracDecBin->ChCrossRe_fx[bin] = Mpy_32_32( hDiracDecBin->ChCrossRe_fx[bin], qualityBasedSmFactor_fx );
        hDiracDecBin->ChCrossIm_fx[bin] = Mpy_32_32( hDiracDecBin->ChCrossIm_fx[bin], qualityBasedSmFactor_fx );
        move32();
@@ -1680,16 +1741,20 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
        move32();
        move32();
#endif
        CHECK_ACCURACY(hDiracDecBin->ChCrossRe[bin], 2 * q - rsh0, hDiracDecBin->ChCrossRe_fx[bin], hDiracDecBin->ChCrossRe_e[bin], 16);
        CHECK_ACCURACY(hDiracDecBin->ChCrossIm[bin], 2 * q - rsh0, hDiracDecBin->ChCrossIm_fx[bin], hDiracDecBin->ChCrossIm_e[bin], 16);

        FOR( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
        {
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
            hDiracDecBin->ChEne[ch][bin] = Mpy_64_32( hDiracDecBin->ChEne[ch][bin], qualityBasedSmFactor_fx );
            move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
            hDiracDecBin->ChEne_fx[ch][bin] = Mpy_32_32( hDiracDecBin->ChEne_fx[ch][bin], qualityBasedSmFactor_fx );
            move32();
#endif
            CHECK_ACCURACY(hDiracDecBin->ChEne[ch][bin], 2 * q - rsh0, hDiracDecBin->ChEne_fx[ch][bin], hDiracDecBin->ChEne_e[ch][bin], 16);
        }

#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
@@ -1697,22 +1762,27 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
        hDiracDecBin->ChCrossIm[bin] = W_add( hDiracDecBin->ChCrossIm[bin], Mpy_64_32( hDiracDecBin->ChCrossImPrev[bin], IIReneLimiter_fx[bin] ) );
        move64();
        move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
        hDiracDecBin->ChCrossRe_fx[bin] = BASOP_Util_Add_Mant32Exp( hDiracDecBin->ChCrossRe_fx[bin], hDiracDecBin->ChCrossRe_e[bin], Mpy_32_32( hDiracDecBin->ChCrossRePrev_fx[bin], IIReneLimiter_fx[bin] ), hDiracDecBin->ChCrossRePrev_e[bin], &hDiracDecBin->ChCrossRe_e[bin] );
        hDiracDecBin->ChCrossIm_fx[bin] = BASOP_Util_Add_Mant32Exp( hDiracDecBin->ChCrossIm_fx[bin], hDiracDecBin->ChCrossIm_e[bin], Mpy_32_32( hDiracDecBin->ChCrossImPrev_fx[bin], IIReneLimiter_fx[bin] ), hDiracDecBin->ChCrossImPrev_e[bin], &hDiracDecBin->ChCrossIm_e[bin] );
        move32();
        move32();
#endif
        CHECK_ACCURACY(hDiracDecBin->ChCrossRe[bin], 2 * q - rsh0, hDiracDecBin->ChCrossRe_fx[bin], hDiracDecBin->ChCrossRe_e[bin], 4096); // WARNING
        CHECK_ACCURACY(hDiracDecBin->ChCrossIm[bin], 2 * q - rsh0, hDiracDecBin->ChCrossIm_fx[bin], hDiracDecBin->ChCrossIm_e[bin], 4096); // WARNING

        FOR( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
        {
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
            hDiracDecBin->ChEne[ch][bin] = W_add( hDiracDecBin->ChEne[ch][bin], Mpy_64_32( hDiracDecBin->ChEnePrev[ch][bin], IIReneLimiter_fx[bin] ) );
            move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
            hDiracDecBin->ChEne_fx[ch][bin] = BASOP_Util_Add_Mant32Exp( hDiracDecBin->ChEne_fx[ch][bin], hDiracDecBin->ChEne_e[ch][bin], Mpy_32_32( hDiracDecBin->ChEnePrev_fx[ch][bin], IIReneLimiter_fx[bin] ), hDiracDecBin->ChEnePrev_e[ch][bin], &hDiracDecBin->ChEne_e[ch][bin] );
            move32();
#endif
            CHECK_ACCURACY(hDiracDecBin->ChEne[ch][bin], 2 * q - rsh0, hDiracDecBin->ChEne_fx[ch][bin], hDiracDecBin->ChEne_e[ch][bin], 4096); // WARNING
        }

        /* Store energy values and coefficients for next round */
@@ -1721,7 +1791,8 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
        move64();
        hDiracDecBin->ChCrossImPrev[bin] = hDiracDecBin->ChCrossIm[bin];
        move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
        hDiracDecBin->ChCrossRePrev_fx[bin] = hDiracDecBin->ChCrossRe_fx[bin];
        move32();
        hDiracDecBin->ChCrossImPrev_fx[bin] = hDiracDecBin->ChCrossIm_fx[bin];
@@ -1737,7 +1808,8 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
            hDiracDecBin->ChEnePrev[ch][bin] = hDiracDecBin->ChEne[ch][bin];
            move64();
#else
#endif
#ifdef NONBE_2157_VERIFY_ACCURACY
            hDiracDecBin->ChEnePrev_fx[ch][bin] = hDiracDecBin->ChEne_fx[ch][bin];
            move32();
            hDiracDecBin->ChEnePrev_e[ch][bin] = hDiracDecBin->ChEne_e[ch][bin];
@@ -1746,25 +1818,27 @@ static void ivas_dirac_dec_binaural_formulate_input_covariance_matrices_fx(
        }

#ifdef NONBE_2157_IVAS_DIRAC_DEC_BINAURAL_FORMULATE_INPUT_COVARIANCE_MATRICES
#ifndef NONBE_2157_VERIFY_ACCURACY
        Word16 shl;
        for ( ch = 0; ch < BINAURAL_CHANNELS; ch++ )
        {
            shl = W_norm( hDiracDecBin->ChEne[ch][bin] );
            hDiracDecBin->ChEne_fx[ch][bin] = W_extract_h( W_shl( hDiracDecBin->ChEne[ch][bin], shl ) );
            hDiracDecBin->ChEne_e[ch][bin] = sub( exp0, shl );
            hDiracDecBin->ChEne_e[ch][bin] = sub( e0, shl );
        }

        shl = W_norm( hDiracDecBin->ChCrossRe[bin] );
        hDiracDecBin->ChCrossRe_fx[bin] = W_extract_h( W_shl( hDiracDecBin->ChCrossRe[bin], shl ) );
        hDiracDecBin->ChCrossRe_e[bin] = sub( exp0, shl );
        hDiracDecBin->ChCrossRe_e[bin] = sub( e0, shl );

        shl = W_norm( hDiracDecBin->ChCrossIm[bin] );
        hDiracDecBin->ChCrossIm_fx[bin] = W_extract_h( W_shl( hDiracDecBin->ChCrossIm[bin], shl ) );
        hDiracDecBin->ChCrossIm_e[bin] = sub( exp0, shl );
        hDiracDecBin->ChCrossIm_e[bin] = sub( e0, shl );

        shl = W_norm( subFrameTotalEne[bin] );
        subFrameTotalEne_fx[bin] = W_extract_h( W_shl( subFrameTotalEne[bin], shl ) );
        subFrameTotalEne_e[bin] = sub( exp1, shl );
        subFrameTotalEne_e[bin] = sub( e1, shl );
#endif
#endif
    }