1 /*
2 * CDDL HEADER START
3 *
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.
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 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
23 */
24 /*
25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26 * Use is subject to license terms.
27 */
28
29 #pragma weak __cabs = cabs
30
31 #include <math.h>
32 #include "complex_wrapper.h"
33
34 /*
35 * If C were the only standard we cared about, cabs could just call
36 * hypot. Unfortunately, various other standards say that hypot must
37 * call matherr and/or set errno to ERANGE when the result overflows.
38 * Since cabs should do neither of these things, we have to either
39 * make hypot a wrapper on another internal function or duplicate
40 * the hypot implementation here. I've chosen to do the latter.
41 */
42
43 static const double
44 zero = 0.0,
45 onep1u = 1.00000000000000022204e+00, /* 0x3ff00000 1 = 1+2**-52 */
46 twom53 = 1.11022302462515654042e-16, /* 0x3ca00000 0 = 2**-53 */
47 twom768 = 6.441148769597133308e-232, /* 2^-768 */
48 two768 = 1.552518092300708935e+231; /* 2^768 */
49
50 double
51 cabs(dcomplex z)
52 {
53 double x, y, xh, yh, w, ax, ay;
54 int i, j, nx, ny, ix, iy, iscale = 0;
55 unsigned lx, ly;
56
57 x = D_RE(z);
58 y = D_IM(z);
59
60 ix = ((int *)&x)[HIWORD] & ~0x80000000;
61 lx = ((int *)&x)[LOWORD];
62 iy = ((int *)&y)[HIWORD] & ~0x80000000;
63 ly = ((int *)&y)[LOWORD];
64
65 /* force ax = |x| ~>~ ay = |y| */
66 if (iy > ix) {
67 ax = fabs(y);
68 ay = fabs(x);
69 i = ix;
70 ix = iy;
71 iy = i;
72 i = lx;
73 lx = ly;
74 ly = i;
75 } else {
76 ax = fabs(x);
77 ay = fabs(y);
78 }
79 nx = ix >> 20;
80 ny = iy >> 20;
81 j = nx - ny;
82
83 if (nx >= 0x5f3) {
84 /* x >= 2^500 (x*x or y*y may overflow) */
85 if (nx == 0x7ff) {
86 /* inf or NaN, signal of sNaN */
87 if (((ix - 0x7ff00000) | lx) == 0)
88 return ((ax == ay)? ay : ax);
89 else if (((iy - 0x7ff00000) | ly) == 0)
90 return ((ay == ax)? ax : ay);
91 else
92 return (ax * ay);
93 } else if (j > 32) {
94 /* x >> y */
95 if (j <= 53)
96 ay *= twom53;
97 ax += ay;
98 return (ax);
99 }
100 ax *= twom768;
101 ay *= twom768;
102 iscale = 2;
103 ix -= 768 << 20;
104 iy -= 768 << 20;
105 } else if (ny < 0x23d) {
106 /* y < 2^-450 (x*x or y*y may underflow) */
107 if ((ix | lx) == 0)
108 return (ay);
109 if ((iy | ly) == 0)
110 return (ax);
111 if (j > 53) /* x >> y */
112 return (ax + ay);
113 iscale = 1;
114 ax *= two768;
115 ay *= two768;
116 if (nx == 0) {
117 if (ax == zero) /* guard subnormal flush to zero */
118 return (ax);
119 ix = ((int *)&ax)[HIWORD];
120 } else {
121 ix += 768 << 20;
122 }
123 if (ny == 0) {
124 if (ay == zero) /* guard subnormal flush to zero */
125 return (ax * twom768);
126 iy = ((int *)&ay)[HIWORD];
127 } else {
128 iy += 768 << 20;
129 }
130 j = (ix >> 20) - (iy >> 20);
131 if (j > 32) {
132 /* x >> y */
133 if (j <= 53)
134 ay *= twom53;
135 return ((ax + ay) * twom768);
136 }
137 } else if (j > 32) {
138 /* x >> y */
139 if (j <= 53)
140 ay *= twom53;
141 return (ax + ay);
142 }
143
144 /*
145 * Medium range ax and ay with max{|ax/ay|,|ay/ax|} bounded by 2^32.
146 * First check rounding mode by comparing onep1u*onep1u with onep1u
147 * + twom53. Make sure the computation is done at run-time.
148 */
149 if (((lx | ly) << 5) == 0) {
150 ay = ay * ay;
151 ax += ay / (ax + sqrt(ax * ax + ay));
152 } else if (onep1u * onep1u != onep1u + twom53) {
153 /* round-to-zero, positive, negative mode */
154 /* magic formula with less than an ulp error */
155 w = sqrt(ax * ax + ay * ay);
156 ax += ay / ((ax + w) / ay);
157 } else {
158 /* round-to-nearest mode */
159 w = ax - ay;
160 if (w > ay) {
161 ((int *)&xh)[HIWORD] = ix;
162 ((int *)&xh)[LOWORD] = 0;
163 ay = ay * ay + (ax - xh) * (ax + xh);
164 ax = sqrt(xh * xh + ay);
165 } else {
166 ax = ax + ax;
167 ((int *)&xh)[HIWORD] = ix + 0x00100000;
168 ((int *)&xh)[LOWORD] = 0;
169 ((int *)&yh)[HIWORD] = iy;
170 ((int *)&yh)[LOWORD] = 0;
171 ay = w * w + ((ax - xh) * yh + (ay - yh) * ax);
172 ax = sqrt(xh * yh + ay);
173 }
174 }
175 if (iscale > 0) {
176 if (iscale == 1)
177 ax *= twom768;
178 else
179 ax *= two768; /* must generate side effect here */
180 }
181 return (ax);
182 }
|
1 /*
2 * CDDL HEADER START
3 *
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.
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 2005 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31 #pragma weak __cabs = cabs
32
33 #include <math.h>
34 #include "complex_wrapper.h"
35
36 /*
37 * If C were the only standard we cared about, cabs could just call
38 * hypot. Unfortunately, various other standards say that hypot must
39 * call matherr and/or set errno to ERANGE when the result overflows.
40 * Since cabs should do neither of these things, we have to either
41 * make hypot a wrapper on another internal function or duplicate
42 * the hypot implementation here. I've chosen to do the latter.
43 */
44
45 static const double zero = 0.0,
46 onep1u = 1.00000000000000022204e+00, /* 0x3ff00000 1 = 1+2**-52 */
47 twom53 = 1.11022302462515654042e-16, /* 0x3ca00000 0 = 2**-53 */
48 twom768 = 6.441148769597133308e-232, /* 2^-768 */
49 two768 = 1.552518092300708935e+231; /* 2^768 */
50
51 double
52 cabs(dcomplex z)
53 {
54 double x, y, xh, yh, w, ax, ay;
55 int i, j, nx, ny, ix, iy, iscale = 0;
56 unsigned lx, ly;
57
58 x = D_RE(z);
59 y = D_IM(z);
60
61 ix = ((int *)&x)[HIWORD] & ~0x80000000;
62 lx = ((int *)&x)[LOWORD];
63 iy = ((int *)&y)[HIWORD] & ~0x80000000;
64 ly = ((int *)&y)[LOWORD];
65
66 /* force ax = |x| ~>~ ay = |y| */
67 if (iy > ix) {
68 ax = fabs(y);
69 ay = fabs(x);
70 i = ix;
71 ix = iy;
72 iy = i;
73 i = lx;
74 lx = ly;
75 ly = i;
76 } else {
77 ax = fabs(x);
78 ay = fabs(y);
79 }
80
81 nx = ix >> 20;
82 ny = iy >> 20;
83 j = nx - ny;
84
85 if (nx >= 0x5f3) {
86 /* x >= 2^500 (x*x or y*y may overflow) */
87 if (nx == 0x7ff) {
88 /* inf or NaN, signal of sNaN */
89 if (((ix - 0x7ff00000) | lx) == 0)
90 return ((ax == ay) ? ay : ax);
91 else if (((iy - 0x7ff00000) | ly) == 0)
92 return ((ay == ax) ? ax : ay);
93 else
94 return (ax * ay);
95 } else if (j > 32) {
96 /* x >> y */
97 if (j <= 53)
98 ay *= twom53;
99
100 ax += ay;
101 return (ax);
102 }
103
104 ax *= twom768;
105 ay *= twom768;
106 iscale = 2;
107 ix -= 768 << 20;
108 iy -= 768 << 20;
109 } else if (ny < 0x23d) {
110 /* y < 2^-450 (x*x or y*y may underflow) */
111 if ((ix | lx) == 0)
112 return (ay);
113
114 if ((iy | ly) == 0)
115 return (ax);
116
117 if (j > 53) /* x >> y */
118 return (ax + ay);
119
120 iscale = 1;
121 ax *= two768;
122 ay *= two768;
123
124 if (nx == 0) {
125 if (ax == zero) /* guard subnormal flush to zero */
126 return (ax);
127
128 ix = ((int *)&ax)[HIWORD];
129 } else {
130 ix += 768 << 20;
131 }
132
133 if (ny == 0) {
134 if (ay == zero) /* guard subnormal flush to zero */
135 return (ax * twom768);
136
137 iy = ((int *)&ay)[HIWORD];
138 } else {
139 iy += 768 << 20;
140 }
141
142 j = (ix >> 20) - (iy >> 20);
143
144 if (j > 32) {
145 /* x >> y */
146 if (j <= 53)
147 ay *= twom53;
148
149 return ((ax + ay) * twom768);
150 }
151 } else if (j > 32) {
152 /* x >> y */
153 if (j <= 53)
154 ay *= twom53;
155
156 return (ax + ay);
157 }
158
159 /*
160 * Medium range ax and ay with max{|ax/ay|,|ay/ax|} bounded by 2^32.
161 * First check rounding mode by comparing onep1u*onep1u with onep1u
162 * + twom53. Make sure the computation is done at run-time.
163 */
164 if (((lx | ly) << 5) == 0) {
165 ay = ay * ay;
166 ax += ay / (ax + sqrt(ax * ax + ay));
167 } else if (onep1u * onep1u != onep1u + twom53) {
168 /*
169 * round-to-zero, positive, negative mode
170 * magic formula with less than an ulp error
171 */
172 w = sqrt(ax * ax + ay * ay);
173 ax += ay / ((ax + w) / ay);
174 } else {
175 /* round-to-nearest mode */
176 w = ax - ay;
177
178 if (w > ay) {
179 ((int *)&xh)[HIWORD] = ix;
180 ((int *)&xh)[LOWORD] = 0;
181 ay = ay * ay + (ax - xh) * (ax + xh);
182 ax = sqrt(xh * xh + ay);
183 } else {
184 ax = ax + ax;
185 ((int *)&xh)[HIWORD] = ix + 0x00100000;
186 ((int *)&xh)[LOWORD] = 0;
187 ((int *)&yh)[HIWORD] = iy;
188 ((int *)&yh)[LOWORD] = 0;
189 ay = w * w + ((ax - xh) * yh + (ay - yh) * ax);
190 ax = sqrt(xh * yh + ay);
191 }
192 }
193
194 if (iscale > 0) {
195 if (iscale == 1)
196 ax *= twom768;
197 else
198 ax *= two768; /* must generate side effect here */
199 }
200
201 return (ax);
202 }
|