Back to index

glibc  2.9
e_fmodl.c
Go to the documentation of this file.
00001 /* e_fmodl.c -- long double version of e_fmod.c.
00002  * Conversion to IEEE quad long double by Jakub Jelinek, jj@ultra.linux.cz.
00003  */
00004 /*
00005  * ====================================================
00006  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
00007  *
00008  * Developed at SunPro, a Sun Microsystems, Inc. business.
00009  * Permission to use, copy, modify, and distribute this
00010  * software is freely granted, provided that this notice 
00011  * is preserved.
00012  * ====================================================
00013  */
00014 
00015 /* 
00016  * __ieee754_fmodl(x,y)
00017  * Return x mod y in exact arithmetic
00018  * Method: shift and subtract
00019  */
00020 
00021 #include "math.h"
00022 #include "math_private.h"
00023 
00024 #ifdef __STDC__
00025 static const long double one = 1.0, Zero[] = {0.0, -0.0,};
00026 #else
00027 static long double one = 1.0, Zero[] = {0.0, -0.0,};
00028 #endif
00029 
00030 #ifdef __STDC__
00031        long double __ieee754_fmodl(long double x, long double y)
00032 #else
00033        long double __ieee754_fmodl(x,y)
00034        long double x,y;
00035 #endif
00036 {
00037        int64_t n,hx,hy,hz,ix,iy,sx,i;
00038        u_int64_t lx,ly,lz;
00039 
00040        GET_LDOUBLE_WORDS64(hx,lx,x);
00041        GET_LDOUBLE_WORDS64(hy,ly,y);
00042        sx = hx&0x8000000000000000ULL;            /* sign of x */
00043        hx ^=sx;                           /* |x| */
00044        hy &= 0x7fffffffffffffffLL;        /* |y| */
00045 
00046     /* purge off exception values */
00047        if((hy|ly)==0||(hx>=0x7fff000000000000LL)|| /* y=0,or x not finite */
00048          ((hy|((ly|-ly)>>63))>0x7fff000000000000LL))    /* or y is NaN */
00049            return (x*y)/(x*y);
00050        if(hx<=hy) {
00051            if((hx<hy)||(lx<ly)) return x; /* |x|<|y| return x */
00052            if(lx==ly) 
00053               return Zero[(u_int64_t)sx>>63];    /* |x|=|y| return x*0*/
00054        }
00055 
00056     /* determine ix = ilogb(x) */
00057        if(hx<0x0001000000000000LL) {      /* subnormal x */
00058            if(hx==0) {
00059               for (ix = -16431, i=lx; i>0; i<<=1) ix -=1;
00060            } else {
00061               for (ix = -16382, i=hx<<15; i>0; i<<=1) ix -=1;
00062            }
00063        } else ix = (hx>>48)-0x3fff;
00064 
00065     /* determine iy = ilogb(y) */
00066        if(hy<0x0001000000000000LL) {      /* subnormal y */
00067            if(hy==0) {
00068               for (iy = -16431, i=ly; i>0; i<<=1) iy -=1;
00069            } else {
00070               for (iy = -16382, i=hy<<15; i>0; i<<=1) iy -=1;
00071            }
00072        } else iy = (hy>>48)-0x3fff;
00073 
00074     /* set up {hx,lx}, {hy,ly} and align y to x */
00075        if(ix >= -16382) 
00076            hx = 0x0001000000000000LL|(0x0000ffffffffffffLL&hx);
00077        else {        /* subnormal x, shift x to normal */
00078            n = -16382-ix;
00079            if(n<=63) {
00080                hx = (hx<<n)|(lx>>(64-n));
00081                lx <<= n;
00082            } else {
00083               hx = lx<<(n-64);
00084               lx = 0;
00085            }
00086        }
00087        if(iy >= -16382) 
00088            hy = 0x0001000000000000LL|(0x0000ffffffffffffLL&hy);
00089        else {        /* subnormal y, shift y to normal */
00090            n = -16382-iy;
00091            if(n<=63) {
00092                hy = (hy<<n)|(ly>>(64-n));
00093                ly <<= n;
00094            } else {
00095               hy = ly<<(n-64);
00096               ly = 0;
00097            }
00098        }
00099 
00100     /* fix point fmod */
00101        n = ix - iy;
00102        while(n--) {
00103            hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
00104            if(hz<0){hx = hx+hx+(lx>>63); lx = lx+lx;}
00105            else {
00106               if((hz|lz)==0)              /* return sign(x)*0 */
00107                   return Zero[(u_int64_t)sx>>63];
00108               hx = hz+hz+(lz>>63); lx = lz+lz;
00109            }
00110        }
00111        hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1;
00112        if(hz>=0) {hx=hz;lx=lz;}
00113 
00114     /* convert back to floating value and restore the sign */
00115        if((hx|lx)==0)                     /* return sign(x)*0 */
00116            return Zero[(u_int64_t)sx>>63];       
00117        while(hx<0x0001000000000000LL) {   /* normalize x */
00118            hx = hx+hx+(lx>>63); lx = lx+lx;
00119            iy -= 1;
00120        }
00121        if(iy>= -16382) {    /* normalize output */
00122            hx = ((hx-0x0001000000000000LL)|((iy+16383)<<48));
00123            SET_LDOUBLE_WORDS64(x,hx|sx,lx);
00124        } else {             /* subnormal output */
00125            n = -16382 - iy;
00126            if(n<=48) {
00127               lx = (lx>>n)|((u_int64_t)hx<<(64-n));
00128               hx >>= n;
00129            } else if (n<=63) {
00130               lx = (hx<<(64-n))|(lx>>n); hx = sx;
00131            } else {
00132               lx = hx>>(n-64); hx = sx;
00133            }
00134            SET_LDOUBLE_WORDS64(x,hx|sx,lx);
00135            x *= one;        /* create necessary signal */
00136        }
00137        return x;            /* exact output */
00138 }