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]>);
29 The <<fmod>> and <<fmodf>> functions compute the floating-point
30 remainder of <[x]>/<[y]> (<[x]> modulo <[y]>).
33 The <<fmod>> function returns the value
40 for the largest integer <[i]> such that, if <[y]> is nonzero, the
41 result has the same sign as <[x]> and magnitude less than the
44 <<fmod(<[x]>,0)>> returns NaN, and sets <<errno>> to <<EDOM>>.
47 <<fmod>> is ANSI C. <<fmodf>> is an extension.
52 * Return x mod y in exact arithmetic
53 * Method: shift and subtract
59 #ifndef _DOUBLE_IS_32BITS
62 static const double one
= 1.0, Zero
[] = {0.0, -0.0,};
64 static double one
= 1.0, Zero
[] = {0.0, -0.0,};
68 double fmod(double x
, double y
)
74 __int32_t n
,hx
,hy
,hz
,ix
,iy
,sx
,i
;
77 EXTRACT_WORDS(hx
,lx
,x
);
78 EXTRACT_WORDS(hy
,ly
,y
);
79 sx
= hx
&0x80000000; /* sign of x */
81 hy
&= 0x7fffffff; /* |y| */
83 /* purge off exception values */
84 if((hy
|ly
)==0||(hx
>=0x7ff00000)|| /* y=0,or x not finite */
85 ((hy
|((ly
|-ly
)>>31))>0x7ff00000)) /* or y is NaN */
88 if((hx
<hy
)||(lx
<ly
)) return x
; /* |x|<|y| return x */
90 return Zero
[(__uint32_t
)sx
>>31]; /* |x|=|y| return x*0*/
93 /* determine ix = ilogb(x) */
94 if(hx
<0x00100000) { /* subnormal x */
96 for (ix
= -1043, i
=lx
; i
>0; i
<<=1) ix
-=1;
98 for (ix
= -1022,i
=(hx
<<11); i
>0; i
<<=1) ix
-=1;
100 } else ix
= (hx
>>20)-1023;
102 /* determine iy = ilogb(y) */
103 if(hy
<0x00100000) { /* subnormal y */
105 for (iy
= -1043, i
=ly
; i
>0; i
<<=1) iy
-=1;
107 for (iy
= -1022,i
=(hy
<<11); i
>0; i
<<=1) iy
-=1;
109 } else iy
= (hy
>>20)-1023;
111 /* set up {hx,lx}, {hy,ly} and align y to x */
113 hx
= 0x00100000|(0x000fffff&hx
);
114 else { /* subnormal x, shift x to normal */
117 hx
= (hx
<<n
)|(lx
>>(32-n
));
125 hy
= 0x00100000|(0x000fffff&hy
);
126 else { /* subnormal y, shift y to normal */
129 hy
= (hy
<<n
)|(ly
>>(32-n
));
140 hz
=hx
-hy
;lz
=lx
-ly
; if(lx
<ly
) hz
-= 1;
141 if(hz
<0){hx
= hx
+hx
+(lx
>>31); lx
= lx
+lx
;}
143 if((hz
|lz
)==0) /* return sign(x)*0 */
144 return Zero
[(__uint32_t
)sx
>>31];
145 hx
= hz
+hz
+(lz
>>31); lx
= lz
+lz
;
148 hz
=hx
-hy
;lz
=lx
-ly
; if(lx
<ly
) hz
-= 1;
149 if(hz
>=0) {hx
=hz
;lx
=lz
;}
151 /* convert back to floating value and restore the sign */
152 if((hx
|lx
)==0) /* return sign(x)*0 */
153 return Zero
[(__uint32_t
)sx
>>31];
154 while(hx
<0x00100000) { /* normalize x */
155 hx
= hx
+hx
+(lx
>>31); lx
= lx
+lx
;
158 if(iy
>= -1022) { /* normalize output */
159 hx
= ((hx
-0x00100000)|((iy
+1023)<<20));
160 INSERT_WORDS(x
,hx
|sx
,lx
);
161 } else { /* subnormal output */
164 lx
= (lx
>>n
)|((__uint32_t
)hx
<<(32-n
));
167 lx
= (hx
<<(32-n
))|(lx
>>n
); hx
= sx
;
169 lx
= hx
>>(n
-32); hx
= sx
;
171 INSERT_WORDS(x
,hx
|sx
,lx
);
172 x
*= one
; /* create necessary signal */
174 return x
; /* exact output */
177 #endif /* defined(_DOUBLE_IS_32BITS) */