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 * ====================================================
16 <<remquo>>, <<remquof>>---remainder and part of quotient
24 double remquo(double <[x]>, double <[y]>, int *<[quo]>);
25 float remquof(float <[x]>, float <[y]>, int *<[quo]>);
28 The <<remquo>> functions compute the same remainder as the <<remainder>>
29 functions; this value is in the range -<[y]>/2 ... +<[y]>/2. In the object
30 pointed to by <<quo>> they store a value whose sign is the sign of <<x>>/<<y>>
31 and whose magnitude is congruent modulo 2**n to the magnitude of the integral
32 quotient of <<x>>/<<y>>. (That is, <<quo>> is given the n lsbs of the
33 quotient, not counting the sign.) This implementation uses n=31 if int is 32
34 bits or more, otherwise, n is 1 less than the width of int.
37 . remquo(-29.0, 3.0, &<[quo]>)
38 returns -1.0 and sets <[quo]>=10, and
39 . remquo(-98307.0, 3.0, &<[quo]>)
40 returns -0.0 and sets <[quo]>=-32769, although for 16-bit int, <[quo]>=-1. In
41 the latter case, the actual quotient of -(32769=0x8001) is reduced to -1
42 because of the 15-bit limitation for the quotient.
45 When either argument is NaN, NaN is returned. If <[y]> is 0 or <[x]> is
46 infinite (and neither is NaN), a domain error occurs (i.e. the "invalid"
47 floating point exception is raised or errno is set to EDOM), and NaN is
49 Otherwise, the <<remquo>> functions return <[x]> REM <[y]>.
52 IEEE754-2008 calls for <<remquo>>(subnormal, inf) to cause the "underflow"
53 floating-point exception. This implementation does not.
64 /* For quotient, return either all 31 bits that can from calculation (using
65 * int32_t), or as many as can fit into an int that is smaller than 32 bits. */
66 #if INT_MAX > 0x7FFFFFFFL
67 #define QUO_MASK 0x7FFFFFFF
69 #define QUO_MASK INT_MAX
72 static const double Zero
[] = {0.0, -0.0,};
75 * Return the IEEE remainder and set *quo to the last n bits of the
76 * quotient, rounded to the nearest integer. We choose n=31--if that many fit--
77 * because we wind up computing all the integer bits of the quotient anyway as
78 * a side-effect of computing the remainder by the shift and subtract
79 * method. In practice, this is far more bits than are needed to use
80 * remquo in reduction algorithms.
83 remquo(double x
, double y
, int *quo
)
85 __int32_t n
,hx
,hy
,hz
,ix
,iy
,sx
,i
;
86 __uint32_t lx
,ly
,lz
,q
,sxy
;
88 EXTRACT_WORDS(hx
,lx
,x
);
89 EXTRACT_WORDS(hy
,ly
,y
);
90 sxy
= (hx
^ hy
) & 0x80000000;
91 sx
= hx
&0x80000000; /* sign of x */
93 hy
&= 0x7fffffff; /* |y| */
95 /* purge off exception values */
96 if((hy
|ly
)==0||(hx
>=0x7ff00000)|| /* y=0,or x not finite */
97 ((hy
|((ly
|-ly
)>>31))>0x7ff00000)) { /* or y is NaN */
98 *quo
= 0; /* Not necessary, but return consistent value */
102 if((hx
<hy
)||(lx
<ly
)) {
104 goto fixup
; /* |x|<|y| return x or x-y */
107 *quo
= (sxy
? -1 : 1);
108 return Zero
[(__uint32_t
)sx
>>31]; /* |x|=|y| return x*0 */
112 /* determine ix = ilogb(x) */
113 if(hx
<0x00100000) { /* subnormal x */
115 for (ix
= -1043, i
=lx
; i
>0; i
<<=1) ix
-=1;
117 for (ix
= -1022,i
=(hx
<<11); i
>0; i
<<=1) ix
-=1;
119 } else ix
= (hx
>>20)-1023;
121 /* determine iy = ilogb(y) */
122 if(hy
<0x00100000) { /* subnormal y */
124 for (iy
= -1043, i
=ly
; i
>0; i
<<=1) iy
-=1;
126 for (iy
= -1022,i
=(hy
<<11); i
>0; i
<<=1) iy
-=1;
128 } else iy
= (hy
>>20)-1023;
130 /* set up {hx,lx}, {hy,ly} and align y to x */
132 hx
= 0x00100000|(0x000fffff&hx
);
133 else { /* subnormal x, shift x to normal */
136 hx
= (hx
<<n
)|(lx
>>(32-n
));
144 hy
= 0x00100000|(0x000fffff&hy
);
145 else { /* subnormal y, shift y to normal */
148 hy
= (hy
<<n
)|(ly
>>(32-n
));
160 hz
=hx
-hy
;lz
=lx
-ly
; if(lx
<ly
) hz
-= 1;
161 if(hz
<0){hx
= hx
+hx
+(lx
>>31); lx
= lx
+lx
;}
162 else {hx
= hz
+hz
+(lz
>>31); lx
= lz
+lz
; q
++;}
165 hz
=hx
-hy
;lz
=lx
-ly
; if(lx
<ly
) hz
-= 1;
166 if(hz
>=0) {hx
=hz
;lx
=lz
;q
++;}
168 /* convert back to floating value and restore the sign */
169 if((hx
|lx
)==0) { /* return sign(x)*0 */
171 *quo
= (sxy
? -q
: q
);
172 return Zero
[(__uint32_t
)sx
>>31];
174 while(hx
<0x00100000) { /* normalize x */
175 hx
= hx
+hx
+(lx
>>31); lx
= lx
+lx
;
178 if(iy
>= -1022) { /* normalize output */
179 hx
= ((hx
-0x00100000)|((iy
+1023)<<20));
180 } else { /* subnormal output */
183 lx
= (lx
>>n
)|((__uint32_t
)hx
<<(32-n
));
186 lx
= (hx
<<(32-n
))|(lx
>>n
); hx
= sx
;
188 lx
= hx
>>(n
-32); hx
= sx
;
192 INSERT_WORDS(x
,hx
,lx
);
195 if (x
+x
>y
|| (x
+x
==y
&& (q
& 1))) {
199 } else if (x
>0.5*y
|| (x
==0.5*y
&& (q
& 1))) {
204 SET_HIGH_WORD(x
,hx
^sx
);
206 *quo
= (sxy
? -q
: q
);