1 /* @(#)e_fmod.c 1.3 95/01/18 */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
10 * ====================================================
13 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
17 #include "math_private.h"
19 static const float Zero[] = {0.0, -0.0,};
22 * Return the IEEE remainder and set *quo to the last n bits of the
23 * quotient, rounded to the nearest integer. We choose n=31 because
24 * we wind up computing all the integer bits of the quotient anyway as
25 * a side-effect of computing the remainder by the shift and subtract
26 * method. In practice, this is far more bits than are needed to use
27 * remquo in reduction algorithms.
30 remquof(float x, float y, int *quo)
32 int32_t n,hx,hy,hz,ix,iy,sx,i;
37 sxy = (hx ^ hy) & 0x80000000;
38 sx = hx&0x80000000; /* sign of x */
40 hy &= 0x7fffffff; /* |y| */
42 /* purge off exception values */
43 if(hy==0||hx>=0x7f800000||hy>0x7f800000) /* y=0,NaN;or x not finite */
47 goto fixup; /* |x|<|y| return x or x-y */
49 *quo = (sxy ? -1 : 1);
50 return Zero[(u_int32_t)sx>>31]; /* |x|=|y| return x*0*/
53 /* determine ix = ilogb(x) */
54 if(hx<0x00800000) { /* subnormal x */
55 for (ix = -126,i=(hx<<8); i>0; i<<=1) ix -=1;
56 } else ix = (hx>>23)-127;
58 /* determine iy = ilogb(y) */
59 if(hy<0x00800000) { /* subnormal y */
60 for (iy = -126,i=(hy<<8); i>0; i<<=1) iy -=1;
61 } else iy = (hy>>23)-127;
63 /* set up {hx,lx}, {hy,ly} and align y to x */
65 hx = 0x00800000|(0x007fffff&hx);
66 else { /* subnormal x, shift x to normal */
71 hy = 0x00800000|(0x007fffff&hy);
72 else { /* subnormal y, shift y to normal */
82 if(hz<0) hx = hx << 1;
83 else {hx = hz << 1; q++;}
87 if(hz>=0) {hx=hz;q++;}
89 /* convert back to floating value and restore the sign */
90 if(hx==0) { /* return sign(x)*0 */
92 *quo = (sxy ? -q : q);
93 return Zero[(u_int32_t)sx>>31];
95 while(hx<0x00800000) { /* normalize x */
99 if(iy>= -126) { /* normalize output */
100 hx = ((hx-0x00800000)|((iy+127)<<23));
101 } else { /* subnormal output */
106 SET_FLOAT_WORD(x,hx);
109 if (x+x>y || (x+x==y && (q & 1))) {
113 } else if (x>0.5f*y || (x==0.5f*y && (q & 1))) {
117 GET_FLOAT_WORD(hx,x);
118 SET_FLOAT_WORD(x,hx^sx);
120 *quo = (sxy ? -q : q);