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>
5262 libm needs to be carefully unifdef'd
5268 libm doesn't need to hide symbols which are already local
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Reviewed by: Igor Kozhukhov <ikozhukhov@gmail.com>
Reviewed by: Gordon Ross <gwr@nexenta.com>
Approved by: Gordon Ross <gwr@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/C/hypot.c
+++ new/usr/src/lib/libm/common/C/hypot.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 *
19 19 * CDDL HEADER END
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 -#if defined(ELFOBJ)
31 -#pragma weak hypot = __hypot
32 -#endif
30 +#pragma weak __hypot = hypot
33 31
34 32 /* INDENT OFF */
35 33 /*
36 34 * Hypot(x, y)
37 35 * by K.C. Ng for SUN 4.0 libm, updated 3/11/2003.
38 36 * Method :
39 37 * A. When rounding is rounded-to-nearest:
40 38 * If z = x * x + y * y has error less than sqrt(2) / 2 ulp than
41 39 * sqrt(z) has error less than 1 ulp.
42 40 * So, compute sqrt(x*x+y*y) with some care as follows:
43 41 * Assume x > y > 0;
44 42 * 1. Check whether save and set rounding to round-to-nearest
45 43 * 2. if x > 2y use
46 44 * xh*xh+(y*y+((x-xh)*(x+xh))) for x*x+y*y
47 45 * where xh = x with lower 32 bits cleared; else
48 46 * 3. if x <= 2y use
49 47 * x2h*yh+((x-y)*(x-y)+(x2h*(y-yh)+(x2-x2h)*y))
50 48 * where x2 = 2*x, x2h = 2x with lower 32 bits cleared, yh = y with
51 49 * lower 32 bits chopped.
52 50 *
53 51 * B. When rounding is not rounded-to-nearest:
54 52 * The following (magic) formula will yield an error less than 1 ulp.
55 53 * z = sqrt(x * x + y * y)
56 54 * hypot(x, y) = x + (y / ((x + z) / y))
57 55 *
58 56 * NOTE: DO NOT remove parenthsis!
59 57 *
60 58 * Special cases:
61 59 * hypot(x, y) is INF if x or y is +INF or -INF; else
62 60 * hypot(x, y) is NAN if x or y is NAN.
63 61 *
64 62 * Accuracy:
65 63 * hypot(x, y) returns sqrt(x^2+y^2) with error less than 1 ulps
66 64 * (units in the last place)
67 65 */
68 66
69 67 #include "libm.h"
70 68
71 69 static const double
72 70 zero = 0.0,
73 71 onep1u = 1.00000000000000022204e+00, /* 0x3ff00000 1 = 1+2**-52 */
74 72 twom53 = 1.11022302462515654042e-16, /* 0x3ca00000 0 = 2**-53 */
75 73 twom768 = 6.441148769597133308e-232, /* 2^-768 */
76 74 two768 = 1.552518092300708935e+231; /* 2^768 */
77 75
78 76 /* INDENT ON */
79 77
80 78 double
81 79 hypot(double x, double y) {
82 80 double xh, yh, w, ax, ay;
83 81 int i, j, nx, ny, ix, iy, iscale = 0;
84 82 unsigned lx, ly;
85 83
86 84 ix = ((int *) &x)[HIWORD] & ~0x80000000;
87 85 lx = ((int *) &x)[LOWORD];
88 86 iy = ((int *) &y)[HIWORD] & ~0x80000000;
89 87 ly = ((int *) &y)[LOWORD];
90 88 /*
91 89 * Force ax = |x| ~>~ ay = |y|
92 90 */
93 91 if (iy > ix) {
94 92 ax = fabs(y);
95 93 ay = fabs(x);
96 94 i = ix;
97 95 ix = iy;
98 96 iy = i;
99 97 i = lx;
100 98 lx = ly;
101 99 ly = i;
102 100 } else {
103 101 ax = fabs(x);
104 102 ay = fabs(y);
105 103 }
106 104 nx = ix >> 20;
107 105 ny = iy >> 20;
108 106 j = nx - ny;
109 107 /*
110 108 * x >= 2^500 (x*x or y*y may overflow)
111 109 */
112 110 if (nx >= 0x5f3) {
113 111 if (nx == 0x7ff) { /* inf or NaN, signal of sNaN */
114 112 if (((ix - 0x7ff00000) | lx) == 0)
115 113 return (ax == ay ? ay : ax);
116 114 else if (((iy - 0x7ff00000) | ly) == 0)
117 115 return (ay == ax ? ax : ay);
118 116 else
119 117 return (ax * ay); /* + -> * for Cheetah */
120 118 } else if (j > 32) { /* x >> y */
121 119 if (j <= 53)
122 120 ay *= twom53;
123 121 ax += ay;
124 122 if (((int *) &ax)[HIWORD] == 0x7ff00000)
125 123 ax = _SVID_libm_err(x, y, 4);
126 124 return (ax);
127 125 }
128 126 ax *= twom768;
129 127 ay *= twom768;
130 128 iscale = 2;
131 129 ix -= 768 << 20;
132 130 iy -= 768 << 20;
133 131 }
134 132 /*
135 133 * y < 2^-450 (x*x or y*y may underflow)
136 134 */
137 135 else if (ny < 0x23d) {
138 136 if ((ix | lx) == 0)
139 137 return (ay);
140 138 if ((iy | ly) == 0)
141 139 return (ax);
142 140 if (j > 53) /* x >> y */
143 141 return (ax + ay);
144 142 iscale = 1;
145 143 ax *= two768;
146 144 ay *= two768;
147 145 if (nx == 0) {
148 146 if (ax == zero) /* guard subnormal flush to zero */
149 147 return (ax);
150 148 ix = ((int *) &ax)[HIWORD];
151 149 } else
152 150 ix += 768 << 20;
153 151 if (ny == 0) {
154 152 if (ay == zero) /* guard subnormal flush to zero */
155 153 return (ax * twom768);
156 154 iy = ((int *) &ay)[HIWORD];
157 155 } else
158 156 iy += 768 << 20;
159 157 j = (ix >> 20) - (iy >> 20);
160 158 if (j > 32) { /* x >> y */
161 159 if (j <= 53)
162 160 ay *= twom53;
163 161 return ((ax + ay) * twom768);
164 162 }
165 163 } else if (j > 32) { /* x >> y */
166 164 if (j <= 53)
167 165 ay *= twom53;
168 166 return (ax + ay);
169 167 }
170 168 /*
171 169 * Medium range ax and ay with max{|ax/ay|,|ay/ax|} bounded by 2^32
172 170 * First check rounding mode by comparing onep1u*onep1u with onep1u+twom53.
173 171 * Make sure the computation is done at run-time.
174 172 */
175 173 if (((lx | ly) << 5) == 0) {
176 174 ay = ay * ay;
177 175 ax += ay / (ax + sqrt(ax * ax + ay));
178 176 } else
179 177 if (onep1u * onep1u != onep1u + twom53) {
180 178 /* round-to-zero, positive, negative mode */
181 179 /* magic formula with less than an ulp error */
182 180 w = sqrt(ax * ax + ay * ay);
183 181 ax += ay / ((ax + w) / ay);
184 182 } else {
185 183 /* round-to-nearest mode */
186 184 w = ax - ay;
187 185 if (w > ay) {
188 186 ((int *) &xh)[HIWORD] = ix;
189 187 ((int *) &xh)[LOWORD] = 0;
190 188 ay = ay * ay + (ax - xh) * (ax + xh);
191 189 ax = sqrt(xh * xh + ay);
192 190 } else {
193 191 ax = ax + ax;
194 192 ((int *) &xh)[HIWORD] = ix + 0x00100000;
195 193 ((int *) &xh)[LOWORD] = 0;
196 194 ((int *) &yh)[HIWORD] = iy;
197 195 ((int *) &yh)[LOWORD] = 0;
198 196 ay = w * w + ((ax - xh) * yh + (ay - yh) * ax);
199 197 ax = sqrt(xh * yh + ay);
200 198 }
201 199 }
202 200 if (iscale > 0) {
203 201 if (iscale == 1)
204 202 ax *= twom768;
205 203 else {
206 204 ax *= two768; /* must generate side effect here */
207 205 if (((int *) &ax)[HIWORD] == 0x7ff00000)
208 206 ax = _SVID_libm_err(x, y, 4);
209 207 }
210 208 }
211 209 return (ax);
212 210 }
↓ open down ↓ |
170 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX