[vorbis-dev] Another good optimization (for PPC only, though)

Timothy J. Wood tjw at omnigroup.com
Mon Nov 27 07:52:28 PST 2000



  OK, here is the sqrt(x) = x * 1/sqrt(x) version.

  It gets about the same accuracy (the reciprocal estimate instruction gives really good accuracy for most cases), but is 12.4x faster than sqrt instead of only 8.8x.

  This doesn't end up having a lot of effect on the eventual performance since the percentage time taken here was so low already, but this is obviously better :)

-tim

#if defined(__ppc__)
// Vanilla PPC code, but since PPC has a reciprocal square root estimate instruction,
// runs *much* faster than calling sqrt().  We'll use two Newton-Raphson
// refinement steps to get bunch more precision in the 1/sqrt() value for very little cost.
// We'll then multiply 1/sqrt times the original value to get the sqrt.
// This is about 12.4 times faster than sqrt() and according to my testing (not exhaustive)
// it returns fairly accurate results (error below 1.0e-5 up to 100000.0 in 0.1 increments).
static inline float _fast_sqrt(float x)
{
    const float half = 0.5;
    const float one  = 1.0;
    float B, y0, y1;
    
    // This'll NaN if it hits frsqrte.  Handle both +0.0 and -0.0
    if (fabs(x) == 0.0)
        return x;
        
    B = x;

#ifdef __GNUC__            
    asm("frsqrte %0,%1" : "=f" (y0) : "f" (B));
#else
    y0 = __frsqrte(B);
#endif

    /* First refinement step */
    y1 = y0 + half*y0*(one - B*y0*y0);

    /* Second refinement step -- copy the output of the last step to the input of this step */
    y0 = y1;
    y1 = y0 + half*y0*(one - B*y0*y0);

    /* Get sqrt(x) from x * 1/sqrt(x) */
    return x * y1;
}

#else
static inline double _fast_sqrt(double x)
{
    return sqrt(x);
}
#endif

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'vorbis-dev-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the Vorbis-dev mailing list