Commit 3d6a0b86 authored by thomas dettbarn's avatar thomas dettbarn
Browse files

Code cleanup in the HouseholderReduction(), biDiagonalReduction_Left() and...

Code cleanup in the HouseholderReduction(), biDiagonalReduction_Left() and biDiagonalReduction_Right() functions. Removed one more redundant mathematical operation.
parent 874a25d4
Loading
Loading
Loading
Loading
Loading
+77 −75
Original line number Diff line number Diff line
@@ -58,8 +58,8 @@

static float GivensRotation( const float x, const float z );
#ifdef NONBE_SVD_OPTIMIZATION
static void biDiagonalReductionLeft( float singularVectors[][MAX_OUTPUT_CHANNELS], float singularValues[MAX_OUTPUT_CHANNELS], const int16_t nChannelsL, const int16_t nChannelsC, const int16_t currChannel );
static void biDiagonalReductionRight( float singularVectors[][MAX_OUTPUT_CHANNELS], float secDiag[MAX_OUTPUT_CHANNELS], const int16_t nChannelsL, const int16_t nChannelsC, const int16_t currChannel, float *g );
static void biDiagonalReductionLeft( float singularVectors[][MAX_OUTPUT_CHANNELS], const int16_t nChannelsL, const int16_t nChannelsC, const int16_t currChannel, float *g );
static void biDiagonalReductionRight( float singularVectors[][MAX_OUTPUT_CHANNELS], const int16_t nChannelsL, const int16_t nChannelsC, const int16_t currChannel, float *g );
#else
static void biDiagonalReductionLeft( float singularVectors[][MAX_OUTPUT_CHANNELS], float singularValues[MAX_OUTPUT_CHANNELS], float secDiag[MAX_OUTPUT_CHANNELS], const int16_t nChannelsL, const int16_t nChannelsC, const int16_t currChannel, float *sig_x, float *g );
static void biDiagonalReductionRight( float singularVectors[][MAX_OUTPUT_CHANNELS], float secDiag[MAX_OUTPUT_CHANNELS], const int16_t nChannelsL, const int16_t nChannelsC, const int16_t currChannel, float *sig_x, float *g );
@@ -491,17 +491,22 @@ static void HouseholderReduction(
    float *eps_x )
{
    int16_t nCh;
    float g = 0.0f;
#ifndef NONBE_SVD_OPTIMIZATION
#ifdef NONBE_SVD_OPTIMIZATION
    float g_left = 0.0f;
    float g_right = 0.0f;
#else 
    float sig_x = 0.0f;
    float g = 0.0f;
#endif

    /* Bidiagonal Reduction for every channel */
    for ( nCh = 0; nCh < nChannelsC; nCh++ ) /* nChannelsC */
    {
#ifdef NONBE_SVD_OPTIMIZATION
        biDiagonalReductionLeft( singularVectors_Left, singularValues, nChannelsL, nChannelsC, nCh );
        biDiagonalReductionRight( singularVectors_Left, secDiag, nChannelsL, nChannelsC, nCh, &g );
        secDiag[nCh] = g_right; /* from the previous channel */
        biDiagonalReductionLeft( singularVectors_Left, nChannelsL, nChannelsC, nCh, &g_left );
        singularValues[nCh] = g_left;
        biDiagonalReductionRight( singularVectors_Left, nChannelsL, nChannelsC, nCh, &g_right );
#else
        biDiagonalReductionLeft( singularVectors_Left, singularValues, secDiag, nChannelsL, nChannelsC, nCh, &sig_x, &g );
        biDiagonalReductionRight( singularVectors_Left, secDiag, nChannelsL, nChannelsC, nCh, &sig_x, &g );
@@ -518,25 +523,24 @@ static void HouseholderReduction(
}


#ifdef NONBE_SVD_OPTIMIZATION
/*-------------------------------------------------------------------------
 * biDiagonalReductionLeft()
 *
 *
 *-------------------------------------------------------------------------*/

#ifdef NONBE_SVD_OPTIMIZATION
static void biDiagonalReductionLeft(
    float singularVectors[][MAX_OUTPUT_CHANNELS],
    float singularValues[MAX_OUTPUT_CHANNELS],
    const int16_t nChannelsL,
    const int16_t nChannelsC,
    const int16_t currChannel )
    const int16_t currChannel,
    float *g )
{
    int16_t iCh, jCh;
    float norm_x, f, r, g;
    float norm_x, f, r;

    /* Setting values to 0 */
    g = 0.0f;
    ( *g ) = 0.0f;

    if ( currChannel < nChannelsL ) /* i <= m */
    {
@@ -549,9 +553,9 @@ static void biDiagonalReductionLeft(
        }
        if ( ( norm_x ) ) /*(fabsf(*sig_x) > EPSILON * fabsf(*sig_x)) { */
        {
            g = -( singularVectors[currChannel][currChannel] >= 0 ? 1 : ( -1 ) ) * sqrtf( norm_x );
            r = g * singularVectors[currChannel][currChannel] - norm_x;
            singularVectors[currChannel][currChannel] = ( singularVectors[currChannel][currChannel] - g );
            ( *g ) = -( singularVectors[currChannel][currChannel] >= 0 ? 1 : ( -1 ) ) * sqrtf( norm_x );
            r = ( *g ) * singularVectors[currChannel][currChannel] - norm_x;
            singularVectors[currChannel][currChannel] = ( singularVectors[currChannel][currChannel] - ( *g ) );

            for ( iCh = currChannel + 1; iCh < nChannelsC; iCh++ ) /* nChannelsC */
            {
@@ -570,8 +574,60 @@ static void biDiagonalReductionLeft(
                }
            }
        }
    }

    return;
}

/*-------------------------------------------------------------------------
 * biDiagonalReductionRight()
 *
 *
 *-------------------------------------------------------------------------*/
static void biDiagonalReductionRight(
    float singularVectors[][MAX_OUTPUT_CHANNELS],
    const int16_t nChannelsL,
    const int16_t nChannelsC,
    const int16_t currChannel,
    float *g )
{
    int16_t iCh, jCh, idx;
    float norm_x, r;

    /* Setting values to 0 */
    ( *g ) = 0.0f;

    if ( currChannel < nChannelsL && currChannel != ( nChannelsC - 1 ) ) /* i <=m && i !=n */
    {
        idx = currChannel + 1;

        norm_x = 0.0f;

        for ( jCh = idx; jCh < nChannelsC; jCh++ ) /*nChannelsC */
        {
            norm_x += ( singularVectors[currChannel][jCh] * singularVectors[currChannel][jCh] );
        }

        singularValues[currChannel] = g;
        if ( norm_x ) /*(fabsf(*sig_x) > EPSILON * fabsf(*sig_x)) { */
        {
            ( *g ) = -( singularVectors[currChannel][idx] >= 0 ? 1 : ( -1 ) ) * sqrtf( norm_x );
            r = ( *g ) * singularVectors[currChannel][idx] - norm_x;
            singularVectors[currChannel][idx] = ( singularVectors[currChannel][idx] - ( *g ) );

            for ( iCh = currChannel + 1; iCh < nChannelsL; iCh++ ) /*  nChannelsL */
            {
                norm_x = 0.0f;
                for ( jCh = idx; jCh < nChannelsC; jCh++ ) /* nChannelsC */
                {
                    norm_x += ( singularVectors[iCh][jCh] * singularVectors[currChannel][jCh] );
                }
                norm_x /= r;
                for ( jCh = idx; jCh < nChannelsC; jCh++ ) /*  nChannelsC */
                {
                    singularVectors[iCh][jCh] += ( norm_x * singularVectors[currChannel][jCh] );
                }
            }
        }
    }

    return;
@@ -579,6 +635,11 @@ static void biDiagonalReductionLeft(

#else

/*-------------------------------------------------------------------------
 * biDiagonalReductionLeft()
 *
 *
 *-------------------------------------------------------------------------*/
static void biDiagonalReductionLeft(
    float singularVectors[][MAX_OUTPUT_CHANNELS],
    float singularValues[MAX_OUTPUT_CHANNELS],
@@ -657,65 +718,6 @@ static void biDiagonalReductionLeft(
 *
 *-------------------------------------------------------------------------*/
#ifdef NONBE_SVD_OPTIMIZATION
static void biDiagonalReductionRight(
    float singularVectors[][MAX_OUTPUT_CHANNELS],
    float secDiag[MAX_OUTPUT_CHANNELS],
    const int16_t nChannelsL,
    const int16_t nChannelsC,
    const int16_t currChannel,
    float *g )
{
    int16_t iCh, jCh, idx;
    float norm_x, r;
    float abs_x;

    secDiag[currChannel] = *g;

    /* Setting values to 0 */
    ( *g ) = 0.0f;

    if ( currChannel < nChannelsL && currChannel != ( nChannelsC - 1 ) ) /* i <=m && i !=n */
    {
        idx = currChannel + 1;

        norm_x = 0.0f;
        abs_x = 0.0f;

        for ( jCh = idx; jCh < nChannelsC; jCh++ ) /*nChannelsC */
        {
            norm_x += ( singularVectors[currChannel][jCh] * singularVectors[currChannel][jCh] );
            abs_x += fabs( singularVectors[currChannel][jCh] );
        }

        if ( norm_x ) /*(fabsf(*sig_x) > EPSILON * fabsf(*sig_x)) { */
        {
            ( *g ) = -( singularVectors[currChannel][idx] >= 0 ? 1 : ( -1 ) ) * sqrtf( norm_x );
            r = ( *g ) * singularVectors[currChannel][idx] - norm_x;
            singularVectors[currChannel][idx] = ( singularVectors[currChannel][idx] - ( *g ) );

            for ( iCh = currChannel + 1; iCh < nChannelsL; iCh++ ) /*  nChannelsL */
            {
                norm_x = 0.0f;
                for ( jCh = idx; jCh < nChannelsC; jCh++ ) /* nChannelsC */
                {
                    norm_x += ( singularVectors[iCh][jCh] * singularVectors[currChannel][jCh] );
                }
                norm_x /= r;
                for ( jCh = idx; jCh < nChannelsC; jCh++ ) /*  nChannelsC */
                {
                    singularVectors[iCh][jCh] += ( norm_x * singularVectors[currChannel][jCh] );
                }
            }
            for ( jCh = idx; jCh < nChannelsC; jCh++ ) /* nChannelsC */
            {
                secDiag[jCh] = ( singularVectors[currChannel][jCh] * abs_x ) / maxWithSign( r );
            }
        }
    }

    return;
}


#else
static void biDiagonalReductionRight(