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