Print this page
5261 libm should stop using synonyms.h
5298 fabs is 0-sized, confuses dis(1) and others
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Approved by: Gordon Ross <gwr@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/C/fmod.c
+++ new/usr/src/lib/libm/common/C/fmod.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
23 23 */
24 24 /*
25 25 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
26 26 * Use is subject to license terms.
27 27 */
28 28
29 -#pragma weak fmod = __fmod
29 +#pragma weak __fmod = fmod
30 30
31 31 #include "libm.h"
32 32
33 33 static const double zero = 0.0;
34 34
35 35 /*
36 36 * The following implementation assumes fast 64-bit integer arith-
37 37 * metic. This is fine for sparc because we build libm in v8plus
38 38 * mode. It's also fine for sparcv9 and amd64, although we have
39 39 * assembly code on amd64. For x86, it would be better to use
40 40 * 32-bit code, but we have assembly for x86, too.
41 41 */
42 42 double
43 43 fmod(double x, double y) {
44 44 double w;
45 45 long long hx, ix, iy, iz;
46 46 int nd, k, ny;
47 47
48 48 hx = *(long long *)&x;
49 49 ix = hx & ~0x8000000000000000ull;
50 50 iy = *(long long *)&y & ~0x8000000000000000ull;
51 51
52 52 /* handle special cases */
53 53 if (iy == 0ll)
54 54 return (_SVID_libm_err(x, y, 27));
55 55
56 56 if (ix >= 0x7ff0000000000000ll || iy > 0x7ff0000000000000ll)
57 57 return ((x * y) * zero);
58 58
59 59 if (ix <= iy)
60 60 return ((ix < iy)? x : x * zero);
61 61
62 62 /*
63 63 * Set:
64 64 * ny = true exponent of y
65 65 * nd = true exponent of x minus true exponent of y
66 66 * ix = normalized significand of x
67 67 * iy = normalized significand of y
68 68 */
69 69 ny = iy >> 52;
70 70 k = ix >> 52;
71 71 if (ny == 0) {
72 72 /* y is subnormal, x could be normal or subnormal */
73 73 ny = 1;
74 74 while (iy < 0x0010000000000000ll) {
75 75 ny -= 1;
76 76 iy += iy;
77 77 }
78 78 nd = k - ny;
79 79 if (k == 0) {
80 80 nd += 1;
81 81 while (ix < 0x0010000000000000ll) {
82 82 nd -= 1;
83 83 ix += ix;
84 84 }
85 85 } else {
86 86 ix = 0x0010000000000000ll | (ix & 0x000fffffffffffffll);
87 87 }
88 88 } else {
89 89 /* both x and y are normal */
90 90 nd = k - ny;
91 91 ix = 0x0010000000000000ll | (ix & 0x000fffffffffffffll);
92 92 iy = 0x0010000000000000ll | (iy & 0x000fffffffffffffll);
93 93 }
94 94
95 95 /* perform fixed point mod */
96 96 while (nd--) {
97 97 iz = ix - iy;
98 98 if (iz >= 0)
99 99 ix = iz;
100 100 ix += ix;
101 101 }
102 102 iz = ix - iy;
103 103 if (iz >= 0)
104 104 ix = iz;
105 105
106 106 /* convert back to floating point and restore the sign */
107 107 if (ix == 0ll)
108 108 return (x * zero);
109 109 while (ix < 0x0010000000000000ll) {
110 110 ix += ix;
111 111 ny -= 1;
112 112 }
113 113 while (ix > 0x0020000000000000ll) { /* XXX can this ever happen? */
114 114 ny += 1;
115 115 ix >>= 1;
116 116 }
117 117 if (ny <= 0) {
118 118 /* result is subnormal */
119 119 k = -ny + 1;
120 120 ix >>= k;
121 121 *(long long *)&w = (hx & 0x8000000000000000ull) | ix;
122 122 return (w);
123 123 }
124 124 *(long long *)&w = (hx & 0x8000000000000000ull) |
125 125 ((long long)ny << 52) | (ix & 0x000fffffffffffffll);
126 126 return (w);
127 127 }
↓ open down ↓ |
88 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX