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