1 /* Adapted for Newlib, 2009. (Allow for int < 32 bits; return *quo=0 during
2 * errors to make test scripts easier.) */
3 /* @(#)e_fmod.c 1.3 95/01/18 */
5 * ====================================================
6 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8 * Developed at SunSoft, a Sun Microsystems, Inc. business.
9 * Permission to use, copy, modify, and distribute this
10 * software is freely granted, provided that this notice
12 * ====================================================
18 /* For quotient, return either all 31 bits that can from calculation (using
19 * int32_t), or as many as can fit into an int that is smaller than 32 bits. */
20 #if INT_MAX > 0x7FFFFFFFL
21 #define QUO_MASK 0x7FFFFFFF
23 #define QUO_MASK INT_MAX
26 static const float Zero
[] = {0.0, -0.0,};
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--if that many fit--
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.
37 remquof(float x
, float y
, int *quo
)
39 __int32_t n
,hx
,hy
,hz
,ix
,iy
,sx
,i
;
44 sxy
= (hx
^ hy
) & 0x80000000;
45 sx
= hx
&0x80000000; /* sign of x */
47 hy
&= 0x7fffffff; /* |y| */
49 /* purge off exception values */
50 if(hy
==0||hx
>=0x7f800000||hy
>0x7f800000) { /* y=0,NaN;or x not finite */
51 *quo
= 0; /* Not necessary, but return consistent value */
56 goto fixup
; /* |x|<|y| return x or x-y */
59 return Zero
[(__uint32_t
)sx
>>31]; /* |x|=|y| return x*0*/
62 /* determine ix = ilogb(x) */
63 if(hx
<0x00800000) { /* subnormal x */
64 for (ix
= -126,i
=(hx
<<8); i
>0; i
<<=1) ix
-=1;
65 } else ix
= (hx
>>23)-127;
67 /* determine iy = ilogb(y) */
68 if(hy
<0x00800000) { /* subnormal y */
69 for (iy
= -126,i
=(hy
<<8); i
>0; i
<<=1) iy
-=1;
70 } else iy
= (hy
>>23)-127;
72 /* set up {hx,lx}, {hy,ly} and align y to x */
74 hx
= 0x00800000|(0x007fffff&hx
);
75 else { /* subnormal x, shift x to normal */
80 hy
= 0x00800000|(0x007fffff&hy
);
81 else { /* subnormal y, shift y to normal */
91 if(hz
<0) hx
= hx
<< 1;
92 else {hx
= hz
<< 1; q
++;}
96 if(hz
>=0) {hx
=hz
;q
++;}
98 /* convert back to floating value and restore the sign */
99 if(hx
==0) { /* return sign(x)*0 */
100 *quo
= (sxy
? -q
: q
);
101 return Zero
[(__uint32_t
)sx
>>31];
103 while(hx
<0x00800000) { /* normalize x */
107 if(iy
>= -126) { /* normalize output */
108 hx
= ((hx
-0x00800000)|((iy
+127)<<23));
109 } else { /* subnormal output */
114 SET_FLOAT_WORD(x
,hx
);
117 if (x
+x
>y
|| (x
+x
==y
&& (q
& 1))) {
121 } else if (x
>0.5f
*y
|| (x
==0.5f
*y
&& (q
& 1))) {
125 GET_FLOAT_WORD(hx
,x
);
126 SET_FLOAT_WORD(x
,hx
^sx
);
128 *quo
= (sxy
? -q
: q
);