OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libm/] [math/] [ef_fmod.c] - Diff between revs 1010 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 1010 Rev 1765
/* ef_fmod.c -- float version of e_fmod.c.
/* ef_fmod.c -- float version of e_fmod.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.
 * ====================================================
 * ====================================================
 */
 */
 
 
/*
/*
 * __ieee754_fmodf(x,y)
 * __ieee754_fmodf(x,y)
 * Return x mod y in exact arithmetic
 * Return x mod y in exact arithmetic
 * Method: shift and subtract
 * Method: shift and subtract
 */
 */
 
 
#include "fdlibm.h"
#include "fdlibm.h"
 
 
#ifdef __STDC__
#ifdef __STDC__
static const float one = 1.0, Zero[] = {0.0, -0.0,};
static const float one = 1.0, Zero[] = {0.0, -0.0,};
#else
#else
static float one = 1.0, Zero[] = {0.0, -0.0,};
static float one = 1.0, Zero[] = {0.0, -0.0,};
#endif
#endif
 
 
#ifdef __STDC__
#ifdef __STDC__
        float __ieee754_fmodf(float x, float y)
        float __ieee754_fmodf(float x, float y)
#else
#else
        float __ieee754_fmodf(x,y)
        float __ieee754_fmodf(x,y)
        float x,y ;
        float x,y ;
#endif
#endif
{
{
        __int32_t n,hx,hy,hz,ix,iy,sx,i;
        __int32_t n,hx,hy,hz,ix,iy,sx,i;
 
 
        GET_FLOAT_WORD(hx,x);
        GET_FLOAT_WORD(hx,x);
        GET_FLOAT_WORD(hy,y);
        GET_FLOAT_WORD(hy,y);
        sx = hx&0x80000000;             /* sign of x */
        sx = hx&0x80000000;             /* sign of x */
        hx ^=sx;                /* |x| */
        hx ^=sx;                /* |x| */
        hy &= 0x7fffffff;       /* |y| */
        hy &= 0x7fffffff;       /* |y| */
 
 
    /* purge off exception values */
    /* purge off exception values */
        if(FLT_UWORD_IS_ZERO(hy)||
        if(FLT_UWORD_IS_ZERO(hy)||
           !FLT_UWORD_IS_FINITE(hx)||
           !FLT_UWORD_IS_FINITE(hx)||
           FLT_UWORD_IS_NAN(hy))
           FLT_UWORD_IS_NAN(hy))
            return (x*y)/(x*y);
            return (x*y)/(x*y);
        if(hx<hy) return x;                     /* |x|<|y| return x */
        if(hx<hy) return x;                     /* |x|<|y| return x */
        if(hx==hy)
        if(hx==hy)
            return Zero[(__uint32_t)sx>>31];    /* |x|=|y| return x*0*/
            return Zero[(__uint32_t)sx>>31];    /* |x|=|y| return x*0*/
 
 
    /* Note: y cannot be zero if we reach here. */
    /* Note: y cannot be zero if we reach here. */
 
 
    /* determine ix = ilogb(x) */
    /* determine ix = ilogb(x) */
        if(FLT_UWORD_IS_SUBNORMAL(hx)) {        /* subnormal x */
        if(FLT_UWORD_IS_SUBNORMAL(hx)) {        /* subnormal x */
            for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
            for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
        } else ix = (hx>>23)-127;
        } else ix = (hx>>23)-127;
 
 
    /* determine iy = ilogb(y) */
    /* determine iy = ilogb(y) */
        if(FLT_UWORD_IS_SUBNORMAL(hy)) {        /* subnormal y */
        if(FLT_UWORD_IS_SUBNORMAL(hy)) {        /* subnormal y */
            for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1;
            for (iy = -126,i=(hy<<8); i>=0; i<<=1) iy -=1;
        } else iy = (hy>>23)-127;
        } else iy = (hy>>23)-127;
 
 
    /* set up {hx,lx}, {hy,ly} and align y to x */
    /* set up {hx,lx}, {hy,ly} and align y to x */
        if(ix >= -126)
        if(ix >= -126)
            hx = 0x00800000|(0x007fffff&hx);
            hx = 0x00800000|(0x007fffff&hx);
        else {          /* subnormal x, shift x to normal */
        else {          /* subnormal x, shift x to normal */
            n = -126-ix;
            n = -126-ix;
            hx = hx<<n;
            hx = hx<<n;
        }
        }
        if(iy >= -126)
        if(iy >= -126)
            hy = 0x00800000|(0x007fffff&hy);
            hy = 0x00800000|(0x007fffff&hy);
        else {          /* subnormal y, shift y to normal */
        else {          /* subnormal y, shift y to normal */
            n = -126-iy;
            n = -126-iy;
            hy = hy<<n;
            hy = hy<<n;
        }
        }
 
 
    /* fix point fmod */
    /* fix point fmod */
        n = ix - iy;
        n = ix - iy;
        while(n--) {
        while(n--) {
            hz=hx-hy;
            hz=hx-hy;
            if(hz<0){hx = hx+hx;}
            if(hz<0){hx = hx+hx;}
            else {
            else {
                if(hz==0)                /* return sign(x)*0 */
                if(hz==0)                /* return sign(x)*0 */
                    return Zero[(__uint32_t)sx>>31];
                    return Zero[(__uint32_t)sx>>31];
                hx = hz+hz;
                hx = hz+hz;
            }
            }
        }
        }
        hz=hx-hy;
        hz=hx-hy;
        if(hz>=0) {hx=hz;}
        if(hz>=0) {hx=hz;}
 
 
    /* convert back to floating value and restore the sign */
    /* convert back to floating value and restore the sign */
        if(hx==0)                        /* return sign(x)*0 */
        if(hx==0)                        /* return sign(x)*0 */
            return Zero[(__uint32_t)sx>>31];
            return Zero[(__uint32_t)sx>>31];
        while(hx<0x00800000) {          /* normalize x */
        while(hx<0x00800000) {          /* normalize x */
            hx = hx+hx;
            hx = hx+hx;
            iy -= 1;
            iy -= 1;
        }
        }
        if(iy>= -126) {         /* normalize output */
        if(iy>= -126) {         /* normalize output */
            hx = ((hx-0x00800000)|((iy+127)<<23));
            hx = ((hx-0x00800000)|((iy+127)<<23));
            SET_FLOAT_WORD(x,hx|sx);
            SET_FLOAT_WORD(x,hx|sx);
        } else {                /* subnormal output */
        } else {                /* subnormal output */
            /* If denormals are not supported, this code will generate a
            /* If denormals are not supported, this code will generate a
               zero representation.  */
               zero representation.  */
            n = -126 - iy;
            n = -126 - iy;
            hx >>= n;
            hx >>= n;
            SET_FLOAT_WORD(x,hx|sx);
            SET_FLOAT_WORD(x,hx|sx);
            x *= one;           /* create necessary signal */
            x *= one;           /* create necessary signal */
        }
        }
        return x;               /* exact output */
        return x;               /* exact output */
}
}
 
 

powered by: WebSVN 2.1.0

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