2 /* @(#)z_fmod.c 1.0 98/08/13 */
4 * ====================================================
5 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
7 * Developed at SunPro, a Sun Microsystems, Inc. business.
8 * Permission to use, copy, modify, and distribute this
9 * software is freely granted, provided that this notice
11 * ====================================================
16 <<fmod>>, <<fmodf>>---floating-point remainder (modulo)
25 double fmod(double <[x]>, double <[y]>)
26 float fmodf(float <[x]>, float <[y]>)
30 double fmod(<[x]>, <[y]>)
31 double (<[x]>, <[y]>);
33 float fmodf(<[x]>, <[y]>)
37 The <<fmod>> and <<fmodf>> functions compute the floating-point
38 remainder of <[x]>/<[y]> (<[x]> modulo <[y]>).
41 The <<fmod>> function returns the value
48 for the largest integer <[i]> such that, if <[y]> is nonzero, the
49 result has the same sign as <[x]> and magnitude less than the
52 <<fmod(<[x]>,0)>> returns NaN, and sets <<errno>> to <<EDOM>>.
54 You can modify error treatment for these functions using <<matherr>>.
57 <<fmod>> is ANSI C. <<fmodf>> is an extension.
62 * Return x mod y in exact arithmetic
63 * Method: shift and subtract
69 #ifndef _DOUBLE_IS_32BITS
72 static const double one
= 1.0, Zero
[] = {0.0, -0.0,};
74 static double one
= 1.0, Zero
[] = {0.0, -0.0,};
78 double fmod(double x
, double y
)
84 __int32_t n
,hx
,hy
,hz
,ix
,iy
,sx
,i
;
87 EXTRACT_WORDS(hx
,lx
,x
);
88 EXTRACT_WORDS(hy
,ly
,y
);
89 sx
= hx
&0x80000000; /* sign of x */
91 hy
&= 0x7fffffff; /* |y| */
93 /* purge off exception values */
94 if((hy
|ly
)==0||(hx
>=0x7ff00000)|| /* y=0,or x not finite */
95 ((hy
|((ly
|-ly
)>>31))>0x7ff00000)) /* or y is NaN */
98 if((hx
<hy
)||(lx
<ly
)) return x
; /* |x|<|y| return x */
100 return Zero
[(__uint32_t
)sx
>>31]; /* |x|=|y| return x*0*/
103 /* determine ix = ilogb(x) */
104 if(hx
<0x00100000) { /* subnormal x */
106 for (ix
= -1043, i
=lx
; i
>0; i
<<=1) ix
-=1;
108 for (ix
= -1022,i
=(hx
<<11); i
>0; i
<<=1) ix
-=1;
110 } else ix
= (hx
>>20)-1023;
112 /* determine iy = ilogb(y) */
113 if(hy
<0x00100000) { /* subnormal y */
115 for (iy
= -1043, i
=ly
; i
>0; i
<<=1) iy
-=1;
117 for (iy
= -1022,i
=(hy
<<11); i
>0; i
<<=1) iy
-=1;
119 } else iy
= (hy
>>20)-1023;
121 /* set up {hx,lx}, {hy,ly} and align y to x */
123 hx
= 0x00100000|(0x000fffff&hx
);
124 else { /* subnormal x, shift x to normal */
127 hx
= (hx
<<n
)|(lx
>>(32-n
));
135 hy
= 0x00100000|(0x000fffff&hy
);
136 else { /* subnormal y, shift y to normal */
139 hy
= (hy
<<n
)|(ly
>>(32-n
));
150 hz
=hx
-hy
;lz
=lx
-ly
; if(lx
<ly
) hz
-= 1;
151 if(hz
<0){hx
= hx
+hx
+(lx
>>31); lx
= lx
+lx
;}
153 if((hz
|lz
)==0) /* return sign(x)*0 */
154 return Zero
[(__uint32_t
)sx
>>31];
155 hx
= hz
+hz
+(lz
>>31); lx
= lz
+lz
;
158 hz
=hx
-hy
;lz
=lx
-ly
; if(lx
<ly
) hz
-= 1;
159 if(hz
>=0) {hx
=hz
;lx
=lz
;}
161 /* convert back to floating value and restore the sign */
162 if((hx
|lx
)==0) /* return sign(x)*0 */
163 return Zero
[(__uint32_t
)sx
>>31];
164 while(hx
<0x00100000) { /* normalize x */
165 hx
= hx
+hx
+(lx
>>31); lx
= lx
+lx
;
168 if(iy
>= -1022) { /* normalize output */
169 hx
= ((hx
-0x00100000)|((iy
+1023)<<20));
170 INSERT_WORDS(x
,hx
|sx
,lx
);
171 } else { /* subnormal output */
174 lx
= (lx
>>n
)|((__uint32_t
)hx
<<(32-n
));
177 lx
= (hx
<<(32-n
))|(lx
>>n
); hx
= sx
;
179 lx
= hx
>>(n
-32); hx
= sx
;
181 INSERT_WORDS(x
,hx
|sx
,lx
);
182 x
*= one
; /* create necessary signal */
184 return x
; /* exact output */
187 #endif /* defined(_DOUBLE_IS_32BITS) */