OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [tags/] [gnu-src/] [newlib-1.18.0/] [newlib-1.18.0-or32-1.0rc1/] [newlib/] [libm/] [math/] [ef_sqrt.c] - Diff between revs 207 and 345

Only display areas with differences | Details | Blame | View Log

Rev 207 Rev 345
/* ef_sqrtf.c -- float version of e_sqrt.c.
/* ef_sqrtf.c -- float version of e_sqrt.c.
 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
 */
 */
 
 
/*
/*
 * ====================================================
 * ====================================================
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
 *
 *
 * Developed at SunPro, a Sun Microsystems, Inc. business.
 * Developed at SunPro, a Sun Microsystems, Inc. business.
 * Permission to use, copy, modify, and distribute this
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * software is freely granted, provided that this notice
 * is preserved.
 * is preserved.
 * ====================================================
 * ====================================================
 */
 */
 
 
#include "fdlibm.h"
#include "fdlibm.h"
 
 
#ifdef __STDC__
#ifdef __STDC__
static  const float     one     = 1.0, tiny=1.0e-30;
static  const float     one     = 1.0, tiny=1.0e-30;
#else
#else
static  float   one     = 1.0, tiny=1.0e-30;
static  float   one     = 1.0, tiny=1.0e-30;
#endif
#endif
 
 
#ifdef __STDC__
#ifdef __STDC__
        float __ieee754_sqrtf(float x)
        float __ieee754_sqrtf(float x)
#else
#else
        float __ieee754_sqrtf(x)
        float __ieee754_sqrtf(x)
        float x;
        float x;
#endif
#endif
{
{
        float z;
        float z;
        __int32_t       sign = (__int32_t)0x80000000;
        __int32_t       sign = (__int32_t)0x80000000;
        __uint32_t r,hx;
        __uint32_t r,hx;
        __int32_t ix,s,q,m,t,i;
        __int32_t ix,s,q,m,t,i;
 
 
        GET_FLOAT_WORD(ix,x);
        GET_FLOAT_WORD(ix,x);
        hx = ix&0x7fffffff;
        hx = ix&0x7fffffff;
 
 
    /* take care of Inf and NaN */
    /* take care of Inf and NaN */
        if(!FLT_UWORD_IS_FINITE(hx))
        if(!FLT_UWORD_IS_FINITE(hx))
            return x*x+x;               /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
            return x*x+x;               /* sqrt(NaN)=NaN, sqrt(+inf)=+inf
                                           sqrt(-inf)=sNaN */
                                           sqrt(-inf)=sNaN */
    /* take care of zero and -ves */
    /* take care of zero and -ves */
        if(FLT_UWORD_IS_ZERO(hx)) return x;/* sqrt(+-0) = +-0 */
        if(FLT_UWORD_IS_ZERO(hx)) return x;/* sqrt(+-0) = +-0 */
        if(ix<0) return (x-x)/(x-x);             /* sqrt(-ve) = sNaN */
        if(ix<0) return (x-x)/(x-x);             /* sqrt(-ve) = sNaN */
 
 
    /* normalize x */
    /* normalize x */
        m = (ix>>23);
        m = (ix>>23);
        if(FLT_UWORD_IS_SUBNORMAL(hx)) {                /* subnormal x */
        if(FLT_UWORD_IS_SUBNORMAL(hx)) {                /* subnormal x */
            for(i=0;(ix&0x00800000L)==0;i++) ix<<=1;
            for(i=0;(ix&0x00800000L)==0;i++) ix<<=1;
            m -= i-1;
            m -= i-1;
        }
        }
        m -= 127;       /* unbias exponent */
        m -= 127;       /* unbias exponent */
        ix = (ix&0x007fffffL)|0x00800000L;
        ix = (ix&0x007fffffL)|0x00800000L;
        if(m&1) /* odd m, double x to make it even */
        if(m&1) /* odd m, double x to make it even */
            ix += ix;
            ix += ix;
        m >>= 1;        /* m = [m/2] */
        m >>= 1;        /* m = [m/2] */
 
 
    /* generate sqrt(x) bit by bit */
    /* generate sqrt(x) bit by bit */
        ix += ix;
        ix += ix;
        q = s = 0;               /* q = sqrt(x) */
        q = s = 0;               /* q = sqrt(x) */
        r = 0x01000000L;                /* r = moving bit from right to left */
        r = 0x01000000L;                /* r = moving bit from right to left */
 
 
        while(r!=0) {
        while(r!=0) {
            t = s+r;
            t = s+r;
            if(t<=ix) {
            if(t<=ix) {
                s    = t+r;
                s    = t+r;
                ix  -= t;
                ix  -= t;
                q   += r;
                q   += r;
            }
            }
            ix += ix;
            ix += ix;
            r>>=1;
            r>>=1;
        }
        }
 
 
    /* use floating add to find out rounding direction */
    /* use floating add to find out rounding direction */
        if(ix!=0) {
        if(ix!=0) {
            z = one-tiny; /* trigger inexact flag */
            z = one-tiny; /* trigger inexact flag */
            if (z>=one) {
            if (z>=one) {
                z = one+tiny;
                z = one+tiny;
                if (z>one)
                if (z>one)
                    q += 2;
                    q += 2;
                else
                else
                    q += (q&1);
                    q += (q&1);
            }
            }
        }
        }
        ix = (q>>1)+0x3f000000L;
        ix = (q>>1)+0x3f000000L;
        ix += (m <<23);
        ix += (m <<23);
        SET_FLOAT_WORD(z,ix);
        SET_FLOAT_WORD(z,ix);
        return z;
        return z;
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.