4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
29 #pragma weak __fmod = fmod
33 static const double zero
= 0.0;
36 * The following implementation assumes fast 64-bit integer arith-
37 * metic. This is fine for sparc because we build libm in v8plus
38 * mode. It's also fine for sparcv9 and amd64, although we have
39 * assembly code on amd64. For x86, it would be better to use
40 * 32-bit code, but we have assembly for x86, too.
43 fmod(double x
, double y
) {
45 long long hx
, ix
, iy
, iz
;
48 hx
= *(long long *)&x
;
49 ix
= hx
& ~0x8000000000000000ull
;
50 iy
= *(long long *)&y
& ~0x8000000000000000ull
;
52 /* handle special cases */
54 return (_SVID_libm_err(x
, y
, 27));
56 if (ix
>= 0x7ff0000000000000ll
|| iy
> 0x7ff0000000000000ll
)
57 return ((x
* y
) * zero
);
60 return ((ix
< iy
)? x
: x
* zero
);
64 * ny = true exponent of y
65 * nd = true exponent of x minus true exponent of y
66 * ix = normalized significand of x
67 * iy = normalized significand of y
72 /* y is subnormal, x could be normal or subnormal */
74 while (iy
< 0x0010000000000000ll
) {
81 while (ix
< 0x0010000000000000ll
) {
86 ix
= 0x0010000000000000ll
| (ix
& 0x000fffffffffffffll
);
89 /* both x and y are normal */
91 ix
= 0x0010000000000000ll
| (ix
& 0x000fffffffffffffll
);
92 iy
= 0x0010000000000000ll
| (iy
& 0x000fffffffffffffll
);
95 /* perform fixed point mod */
106 /* convert back to floating point and restore the sign */
109 while (ix
< 0x0010000000000000ll
) {
113 while (ix
> 0x0020000000000000ll
) { /* XXX can this ever happen? */
118 /* result is subnormal */
121 *(long long *)&w
= (hx
& 0x8000000000000000ull
) | ix
;
124 *(long long *)&w
= (hx
& 0x8000000000000000ull
) |
125 ((long long)ny
<< 52) | (ix
& 0x000fffffffffffffll
);