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 __FBSDID("$FreeBSD: src/lib/msun/src/s_remquof.c,v 1.1 2005/03/25 04:40:44 das Exp $");
16 #include "math_private.h"
18 static const float Zero
[] = {0.0, -0.0,};
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.
29 remquof(float x
, float y
, int *quo
)
31 int32_t n
,hx
,hy
,hz
,ix
,iy
,sx
,i
;
36 sxy
= (hx
^ hy
) & 0x80000000;
37 sx
= hx
&0x80000000; /* sign of x */
39 hy
&= 0x7fffffff; /* |y| */
41 /* purge off exception values */
42 if(hy
==0||hx
>=0x7f800000||hy
>0x7f800000) /* y=0,NaN;or x not finite */
46 goto fixup
; /* |x|<|y| return x or x-y */
48 *quo
= (sxy
? -1 : 1);
49 return Zero
[(uint32_t)sx
>>31]; /* |x|=|y| return x*0*/
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;
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;
62 /* set up {hx,lx}, {hy,ly} and align y to x */
64 hx
= 0x00800000|(0x007fffff&hx
);
65 else { /* subnormal x, shift x to normal */
70 hy
= 0x00800000|(0x007fffff&hy
);
71 else { /* subnormal y, shift y to normal */
81 if(hz
<0) hx
= hx
<< 1;
82 else {hx
= hz
<< 1; q
++;}
86 if(hz
>=0) {hx
=hz
;q
++;}
88 /* convert back to floating value and restore the sign */
89 if(hx
==0) { /* return sign(x)*0 */
91 *quo
= (sxy
? -q
: q
);
92 return Zero
[(uint32_t)sx
>>31];
94 while(hx
<0x00800000) { /* normalize x */
98 if(iy
>= -126) { /* normalize output */
99 hx
= ((hx
-0x00800000)|((iy
+127)<<23));
100 } else { /* subnormal output */
105 SET_FLOAT_WORD(x
,hx
);
108 if (x
+x
>y
|| (x
+x
==y
&& (q
& 1))) {
112 } else if (x
>0.5f
*y
|| (x
==0.5f
*y
&& (q
& 1))) {
116 GET_FLOAT_WORD(hx
,x
);
117 SET_FLOAT_WORD(x
,hx
^sx
);
119 *quo
= (sxy
? -q
: q
);