1 /*- 2 * ==================================================== 3 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 4 * 5 * Developed at SunSoft, a Sun Microsystems, Inc. business. 6 * Permission to use, copy, modify, and distribute this 7 * software is freely granted, provided that this notice 8 * is preserved. 9 * ==================================================== 10 */ 11 12 #include <float.h> 13 14 #include "math.h" 15 #include "math_private.h" 16 17 static const double Zero[] = {0.0, -0.0,}; 18 19 /* 20 * Return the IEEE remainder and set *quo to the last n bits of the 21 * quotient, rounded to the nearest integer. We choose n=31 because 22 * we wind up computing all the integer bits of the quotient anyway as 23 * a side-effect of computing the remainder by the shift and subtract 24 * method. In practice, this is far more bits than are needed to use 25 * remquo in reduction algorithms. 26 */ 27 double 28 remquo(double x, double y, int *quo) 29 { 30 int32_t n,hx,hy,hz,ix,iy,sx,i; 31 u_int32_t lx,ly,lz,q,sxy; 32 33 EXTRACT_WORDS(hx,lx,x); 34 EXTRACT_WORDS(hy,ly,y); 35 sxy = (hx ^ hy) & 0x80000000; 36 sx = hx&0x80000000; /* sign of x */ 37 hx ^=sx; /* |x| */ 38 hy &= 0x7fffffff; /* |y| */ 39 40 /* purge off exception values */ 41 if((hy|ly)==0||(hx>=0x7ff00000)|| /* y=0,or x not finite */ 42 ((hy|((ly|-ly)>>31))>0x7ff00000)) /* or y is NaN */ 43 return nan_mix_op(x, y, *)/nan_mix_op(x, y, *); 44 if(hx<=hy) { 45 if((hx<hy)||(lx<ly)) { 46 q = 0; 47 goto fixup; /* |x|<|y| return x or x-y */ 48 } 49 if(lx==ly) { 50 *quo = (sxy ? -1 : 1); 51 return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/ 52 } 53 } 54 55 /* determine ix = ilogb(x) */ 56 if(hx<0x00100000) { /* subnormal x */ 57 if(hx==0) { 58 for (ix = -1043, i=lx; i>0; i<<=1) ix -=1; 59 } else { 60 for (ix = -1022,i=(hx<<11); i>0; i<<=1) ix -=1; 61 } 62 } else ix = (hx>>20)-1023; 63 64 /* determine iy = ilogb(y) */ 65 if(hy<0x00100000) { /* subnormal y */ 66 if(hy==0) { 67 for (iy = -1043, i=ly; i>0; i<<=1) iy -=1; 68 } else { 69 for (iy = -1022,i=(hy<<11); i>0; i<<=1) iy -=1; 70 } 71 } else iy = (hy>>20)-1023; 72 73 /* set up {hx,lx}, {hy,ly} and align y to x */ 74 if(ix >= -1022) 75 hx = 0x00100000|(0x000fffff&hx); 76 else { /* subnormal x, shift x to normal */ 77 n = -1022-ix; 78 if(n<=31) { 79 hx = (hx<<n)|(lx>>(32-n)); 80 lx <<= n; 81 } else { 82 hx = lx<<(n-32); 83 lx = 0; 84 } 85 } 86 if(iy >= -1022) 87 hy = 0x00100000|(0x000fffff&hy); 88 else { /* subnormal y, shift y to normal */ 89 n = -1022-iy; 90 if(n<=31) { 91 hy = (hy<<n)|(ly>>(32-n)); 92 ly <<= n; 93 } else { 94 hy = ly<<(n-32); 95 ly = 0; 96 } 97 } 98 99 /* fix point fmod */ 100 n = ix - iy; 101 q = 0; 102 while(n--) { 103 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; 104 if(hz<0){hx = hx+hx+(lx>>31); lx = lx+lx;} 105 else {hx = hz+hz+(lz>>31); lx = lz+lz; q++;} 106 q <<= 1; 107 } 108 hz=hx-hy;lz=lx-ly; if(lx<ly) hz -= 1; 109 if(hz>=0) {hx=hz;lx=lz;q++;} 110 111 /* convert back to floating value and restore the sign */ 112 if((hx|lx)==0) { /* return sign(x)*0 */ 113 q &= 0x7fffffff; 114 *quo = (sxy ? -q : q); 115 return Zero[(u_int32_t)sx>>31]; 116 } 117 while(hx<0x00100000) { /* normalize x */ 118 hx = hx+hx+(lx>>31); lx = lx+lx; 119 iy -= 1; 120 } 121 if(iy>= -1022) { /* normalize output */ 122 hx = ((hx-0x00100000)|((iy+1023)<<20)); 123 } else { /* subnormal output */ 124 n = -1022 - iy; 125 if(n<=20) { 126 lx = (lx>>n)|((u_int32_t)hx<<(32-n)); 127 hx >>= n; 128 } else if (n<=31) { 129 lx = (hx<<(32-n))|(lx>>n); hx = 0; 130 } else { 131 lx = hx>>(n-32); hx = 0; 132 } 133 } 134 fixup: 135 INSERT_WORDS(x,hx,lx); 136 y = fabs(y); 137 if (y < 0x1p-1021) { 138 if (x+x>y || (x+x==y && (q & 1))) { 139 q++; 140 x-=y; 141 } 142 } else if (x>0.5*y || (x==0.5*y && (q & 1))) { 143 q++; 144 x-=y; 145 } 146 GET_HIGH_WORD(hx,x); 147 SET_HIGH_WORD(x,hx^sx); 148 q &= 0x7fffffff; 149 *quo = (sxy ? -q : q); 150 return x; 151 } 152 153 #if LDBL_MANT_DIG == 53 154 __weak_reference(remquo, remquol); 155 #endif 156