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