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 "cdefs-compat.h" 14 //__FBSDID("$FreeBSD: src/lib/msun/src/s_remquof.c,v 1.1 2005/03/25 04:40:44 das Exp $"); 15 16 #include <openlibm.h> 17 18 #include "math_private.h" 19 20 static const float Zero[] = {0.0, -0.0,}; 21 22 /* 23 * Return the IEEE remainder and set *quo to the last n bits of the 24 * quotient, rounded to the nearest integer. We choose n=31 because 25 * we wind up computing all the integer bits of the quotient anyway as 26 * a side-effect of computing the remainder by the shift and subtract 27 * method. In practice, this is far more bits than are needed to use 28 * remquo in reduction algorithms. 29 */ 30 DLLEXPORT float 31 remquof(float x, float y, int *quo) 32 { 33 int32_t n,hx,hy,hz,ix,iy,sx,i; 34 u_int32_t q,sxy; 35 36 GET_FLOAT_WORD(hx,x); 37 GET_FLOAT_WORD(hy,y); 38 sxy = (hx ^ hy) & 0x80000000; 39 sx = hx&0x80000000; /* sign of x */ 40 hx ^=sx; /* |x| */ 41 hy &= 0x7fffffff; /* |y| */ 42 43 /* purge off exception values */ 44 if(hy==0||hx>=0x7f800000||hy>0x7f800000) /* y=0,NaN;or x not finite */ 45 return (x*y)/(x*y); 46 if(hx<hy) { 47 q = 0; 48 goto fixup; /* |x|<|y| return x or x-y */ 49 } else if(hx==hy) { 50 *quo = 1; 51 return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/ 52 } 53 54 /* determine ix = ilogb(x) */ 55 if(hx<0x00800000) { /* subnormal x */ 56 for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1; 57 } else ix = (hx>>23)-127; 58 59 /* determine iy = ilogb(y) */ 60 if(hy<0x00800000) { /* subnormal y */ 61 for (iy = -126,i=(hy<<8); i>0; i<<=1) iy -=1; 62 } else iy = (hy>>23)-127; 63 64 /* set up {hx,lx}, {hy,ly} and align y to x */ 65 if(ix >= -126) 66 hx = 0x00800000|(0x007fffff&hx); 67 else { /* subnormal x, shift x to normal */ 68 n = -126-ix; 69 hx <<= n; 70 } 71 if(iy >= -126) 72 hy = 0x00800000|(0x007fffff&hy); 73 else { /* subnormal y, shift y to normal */ 74 n = -126-iy; 75 hy <<= n; 76 } 77 78 /* fix point fmod */ 79 n = ix - iy; 80 q = 0; 81 while(n--) { 82 hz=hx-hy; 83 if(hz<0) hx = hx << 1; 84 else {hx = hz << 1; q++;} 85 q <<= 1; 86 } 87 hz=hx-hy; 88 if(hz>=0) {hx=hz;q++;} 89 90 /* convert back to floating value and restore the sign */ 91 if(hx==0) { /* return sign(x)*0 */ 92 *quo = (sxy ? -q : q); 93 return Zero[(u_int32_t)sx>>31]; 94 } 95 while(hx<0x00800000) { /* normalize x */ 96 hx <<= 1; 97 iy -= 1; 98 } 99 if(iy>= -126) { /* normalize output */ 100 hx = ((hx-0x00800000)|((iy+127)<<23)); 101 } else { /* subnormal output */ 102 n = -126 - iy; 103 hx >>= n; 104 } 105 fixup: 106 SET_FLOAT_WORD(x,hx); 107 y = fabsf(y); 108 if (y < 0x1p-125f) { 109 if (x+x>y || (x+x==y && (q & 1))) { 110 q++; 111 x-=y; 112 } 113 } else if (x>0.5f*y || (x==0.5f*y && (q & 1))) { 114 q++; 115 x-=y; 116 } 117 GET_FLOAT_WORD(hx,x); 118 SET_FLOAT_WORD(x,hx^sx); 119 q &= 0x7fffffff; 120 *quo = (sxy ? -q : q); 121 return x; 122 } 123