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