1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Sun Microsystems, Inc.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 /* @(#)e_fmod.c 1.3 95/01/18 */
42 * ====================================================
43 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
45 * Developed at SunSoft, a Sun Microsystems, Inc. business.
46 * Permission to use, copy, modify, and distribute this
47 * software is freely granted, provided that this notice
49 * ====================================================
54 * Return x mod y in exact arithmetic
55 * Method: shift and subtract
61 static const double one
= 1.0, Zero
[] = {0.0, -0.0,};
63 static double one
= 1.0, Zero
[] = {0.0, -0.0,};
67 double __ieee754_fmod(double x
, double y
)
69 double __ieee754_fmod(x
,y
)
74 int n
,hx
,hy
,hz
,ix
,iy
,sx
,i
;
78 hx
= __HI(ux
); /* high word of x */
79 lx
= __LO(ux
); /* low word of x */
80 hy
= __HI(uy
); /* high word of y */
81 ly
= __LO(uy
); /* low word of y */
82 sx
= hx
&0x80000000; /* sign of x */
84 hy
&= 0x7fffffff; /* |y| */
86 /* purge off exception values */
87 if((hy
|ly
)==0||(hx
>=0x7ff00000)|| /* y=0,or x not finite */
88 ((hy
|((ly
|-(int)ly
)>>31))>0x7ff00000)) /* or y is NaN */
91 if((hx
<hy
)||(lx
<ly
)) return x
; /* |x|<|y| return x */
93 return Zero
[(unsigned)sx
>>31]; /* |x|=|y| return x*0*/
96 /* determine ix = ilogb(x) */
97 if(hx
<0x00100000) { /* subnormal x */
99 for (ix
= -1043, i
=lx
; i
>0; i
<<=1) ix
-=1;
101 for (ix
= -1022,i
=(hx
<<11); i
>0; i
<<=1) ix
-=1;
103 } else ix
= (hx
>>20)-1023;
105 /* determine iy = ilogb(y) */
106 if(hy
<0x00100000) { /* subnormal y */
108 for (iy
= -1043, i
=ly
; i
>0; i
<<=1) iy
-=1;
110 for (iy
= -1022,i
=(hy
<<11); i
>0; i
<<=1) iy
-=1;
112 } else iy
= (hy
>>20)-1023;
114 /* set up {hx,lx}, {hy,ly} and align y to x */
116 hx
= 0x00100000|(0x000fffff&hx
);
117 else { /* subnormal x, shift x to normal */
120 hx
= (hx
<<n
)|(lx
>>(32-n
));
128 hy
= 0x00100000|(0x000fffff&hy
);
129 else { /* subnormal y, shift y to normal */
132 hy
= (hy
<<n
)|(ly
>>(32-n
));
143 hz
=hx
-hy
;lz
=lx
-ly
; if(lx
<ly
) hz
-= 1;
144 if(hz
<0){hx
= hx
+hx
+(lx
>>31); lx
= lx
+lx
;}
146 if((hz
|lz
)==0) /* return sign(x)*0 */
147 return Zero
[(unsigned)sx
>>31];
148 hx
= hz
+hz
+(lz
>>31); lx
= lz
+lz
;
151 hz
=hx
-hy
;lz
=lx
-ly
; if(lx
<ly
) hz
-= 1;
152 if(hz
>=0) {hx
=hz
;lx
=lz
;}
154 /* convert back to floating value and restore the sign */
155 if((hx
|lx
)==0) /* return sign(x)*0 */
156 return Zero
[(unsigned)sx
>>31];
157 while(hx
<0x00100000) { /* normalize x */
158 hx
= hx
+hx
+(lx
>>31); lx
= lx
+lx
;
161 if(iy
>= -1022) { /* normalize output */
162 hx
= ((hx
-0x00100000)|((iy
+1023)<<20));
167 } else { /* subnormal output */
170 lx
= (lx
>>n
)|((unsigned)hx
<<(32-n
));
173 lx
= (hx
<<(32-n
))|(lx
>>n
); hx
= sx
;
175 lx
= hx
>>(n
-32); hx
= sx
;
181 x
*= one
; /* create necessary signal */
183 return x
; /* exact output */