1 /* @(#)e_fmod.c 5.1 93/09/24 */ 2 /* 3 * ==================================================== 4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 5 * 6 * Developed at SunPro, a Sun Microsystems, Inc. business. 7 * Permission to use, copy, modify, and distribute this 8 * software is freely granted, provided that this notice 9 * is preserved. 10 * ==================================================== 11 */ 12 13 /* 14 * fmod(x,y) 15 * Return x mod y in exact arithmetic 16 * Method: shift and subtract 17 */ 18 19 #include "math.h" 20 #include "math_private.h" 21 22 static const double one = 1.0, Zero[] = {0.0, -0.0,}; 23 24 double 25 fmod(double x, double y) 26 { 27 int32_t n,hx,hy,hz,ix,iy,sx,i; 28 u_int32_t lx,ly,lz; 29 30 EXTRACT_WORDS(hx,lx,x); 31 EXTRACT_WORDS(hy,ly,y); 32 sx = hx&0x80000000; /* sign of x */ 33 hx ^=sx; /* |x| */ 34 hy &= 0x7fffffff; /* |y| */ 35 36 /* purge off exception values */ 37 if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */ 38 ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */ 39 return (x*y)/(x*y); 40 if(hx<=hy) { 41 if((hx<hy)||(lx<ly)) return x; /* |x|<|y| return x */ 42 if(lx==ly) 43 return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/ 44 } 45 46 /* determine ix = ilogb(x) */ 47 if(hx<0x00100000) { /* subnormal x */ 48 if(hx==0) { 49 for (ix = -1043, i=lx; i>0; i<<=1) ix -=1; 50 } else { 51 for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1; 52 } 53 } else ix = (hx>>20)-1023; 54 55 /* determine iy = ilogb(y) */ 56 if(hy<0x00100000) { /* subnormal y */ 57 if(hy==0) { 58 for (iy = -1043, i=ly; i>0; i<<=1) iy -=1; 59 } else { 60 for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1; 61 } 62 } else iy = (hy>>20)-1023; 63 64 /* set up {hx,lx}, {hy,ly} and align y to x */ 65 if(ix >= -1022) 66 hx = 0x00100000|(0x000fffff&hx); 67 else { /* subnormal x, shift x to normal */ 68 n = -1022-ix; 69 if(n<=31) { 70 hx = (hx<<n)|(lx>>(32-n)); 71 lx <<= n; 72 } else { 73 hx = lx<<(n-32); 74 lx = 0; 75 } 76 } 77 if(iy >= -1022) 78 hy = 0x00100000|(0x000fffff&hy); 79 else { /* subnormal y, shift y to normal */ 80 n = -1022-iy; 81 if(n<=31) { 82 hy = (hy<<n)|(ly>>(32-n)); 83 ly <<= n; 84 } else { 85 hy = ly<<(n-32); 86 ly = 0; 87 } 88 } 89 90 /* fix point fmod */ 91 n = ix - iy; 92 while(n--) { 93 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; 94 if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;} 95 else { 96 if((hz|lz)==0) /* return sign(x)*0 */ 97 return Zero[(u_int32_t)sx>>31]; 98 hx = hz+hz+(lz>>31); lx = lz+lz; 99 } 100 } 101 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; 102 if(hz>=0) {hx=hz;lx=lz;} 103 104 /* convert back to floating value and restore the sign */ 105 if((hx|lx)==0) /* return sign(x)*0 */ 106 return Zero[(u_int32_t)sx>>31]; 107 while(hx<0x00100000) { /* normalize x */ 108 hx = hx+hx+(lx>>31); lx = lx+lx; 109 iy -= 1; 110 } 111 if(iy>= -1022) { /* normalize output */ 112 hx = ((hx-0x00100000)|((iy+1023)<<20)); 113 INSERT_WORDS(x,hx|sx,lx); 114 } else { /* subnormal output */ 115 n = -1022 - iy; 116 if(n<=20) { 117 lx = (lx>>n)|((u_int32_t)hx<<(32-n)); 118 hx >>= n; 119 } else if (n<=31) { 120 lx = (hx<<(32-n))|(lx>>n); hx = sx; 121 } else { 122 lx = hx>>(n-32); hx = sx; 123 } 124 INSERT_WORDS(x,hx|sx,lx); 125 x *= one; /* create necessary signal */ 126 } 127 return x; /* exact output */ 128 } 129