Print this page
5261 libm should stop using synonyms.h
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/complex/cpow.c
+++ new/usr/src/lib/libm/common/complex/cpow.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 -#pragma weak cpow = __cpow
30 +#pragma weak __cpow = cpow
31 31
32 32 /* INDENT OFF */
33 33 /*
34 34 * dcomplex cpow(dcomplex z);
35 35 *
36 36 * z**w analytically equivalent to
37 37 *
38 38 * cpow(z,w) = cexp(w clog(z))
39 39 *
40 40 * Let z = x+iy, w = u+iv.
41 41 * Since
42 42 * _________
43 43 * / 2 2 -1 y
44 44 * log(x+iy) = log(\/ x + y ) + i tan (---)
45 45 * x
46 46 *
47 47 * 1 2 2 -1 y
48 48 * = --- log(x + y ) + i tan (---)
49 49 * 2 x
50 50 * u 2 2 -1 y
51 51 * (u+iv)* log(x+iy) = --- log(x + y ) - v tan (---) + (1)
52 52 * 2 x
53 53 *
54 54 * v 2 2 -1 y
55 55 * i * [ --- log(x + y ) + u tan (---) ] (2)
56 56 * 2 x
57 57 *
58 58 * = r + i q
59 59 *
60 60 * Therefore,
61 61 * w r+iq r
62 62 * z = e = e (cos(q)+i*sin(q))
63 63 * _______
64 64 * / 2 2
65 65 * r \/ x + y -v*atan2(y,x)
66 66 * Here e can be expressed as: u * e
67 67 *
68 68 * Special cases (in the order of appearance):
69 69 * 1. (anything) ** 0 is 1
70 70 * 2. (anything) ** 1 is itself
71 71 * 3. When v = 0, y = 0:
72 72 * If x is finite and negative, and u is finite, then
73 73 * x ** u = exp(u*pi i) * pow(|x|, u);
74 74 * otherwise,
75 75 * x ** u = pow(x, u);
76 76 * 4. When v = 0, x = 0 or |x| = |y| or x is inf or y is inf:
77 77 * (x + y i) ** u = r * exp(q i)
78 78 * where
79 79 * r = hypot(x,y) ** u
80 80 * q = u * atan2pi(y, x)
81 81 *
82 82 * 5. otherwise, z**w is NAN if any x, y, u, v is a Nan or inf
83 83 *
84 84 * Note: many results of special cases are obtained in terms of
85 85 * polar coordinate. In the conversion from polar to rectangle:
86 86 * r exp(q i) = r * cos(q) + r * sin(q) i,
87 87 * we regard r * 0 is 0 except when r is a NaN.
88 88 */
89 89 /* INDENT ON */
90 90
91 91 #include "libm.h" /* atan2/exp/fabs/hypot/log/pow/scalbn */
92 92 /* atan2pi/exp2/sincos/sincospi/__k_clog_r/__k_atan2 */
93 93 #include "complex_wrapper.h"
94 94
95 95 extern void sincospi(double, double *, double *);
96 96
97 97 static const double
98 98 huge = 1e300,
99 99 tiny = 1e-300,
100 100 invln2 = 1.44269504088896338700e+00,
101 101 ln2hi = 6.93147180369123816490e-01, /* 0x3fe62e42, 0xfee00000 */
102 102 ln2lo = 1.90821492927058770002e-10, /* 0x3dea39ef, 0x35793c76 */
103 103 one = 1.0,
104 104 zero = 0.0;
105 105
106 106 static const int hiinf = 0x7ff00000;
107 107 extern double atan2pi(double, double);
108 108
109 109 /*
110 110 * Assuming |t[0]| > |t[1]| and |t[2]| > |t[3]|, sum4fp subroutine
111 111 * compute t[0] + t[1] + t[2] + t[3] into two double fp numbers.
112 112 */
113 113 static double
114 114 sum4fp(double ta[], double *w) {
115 115 double t1, t2, t3, t4, w1, w2, t;
116 116 t1 = ta[0]; t2 = ta[1]; t3 = ta[2]; t4 = ta[3];
117 117 /*
118 118 * Rearrange ti so that |t1| >= |t2| >= |t3| >= |t4|
119 119 */
120 120 if (fabs(t4) > fabs(t1)) {
121 121 t = t1; t1 = t3; t3 = t;
122 122 t = t2; t2 = t4; t4 = t;
123 123 } else if (fabs(t3) > fabs(t1)) {
124 124 t = t1; t1 = t3;
125 125 if (fabs(t4) > fabs(t2)) {
126 126 t3 = t4; t4 = t2; t2 = t;
127 127 } else {
128 128 t3 = t2; t2 = t;
129 129 }
130 130 } else if (fabs(t3) > fabs(t2)) {
131 131 t = t2; t2 = t3;
132 132 if (fabs(t4) > fabs(t2)) {
133 133 t3 = t4; t4 = t;
134 134 } else
135 135 t3 = t;
136 136 }
137 137 /* summing r = t1 + t2 + t3 + t4 to w1 + w2 */
138 138 w1 = t3 + t4;
139 139 w2 = t4 - (w1 - t3);
140 140 t = t2 + w1;
141 141 w2 += w1 - (t - t2);
142 142 w1 = t + w2;
143 143 w2 += t - w1;
144 144 t = t1 + w1;
145 145 w2 += w1 - (t - t1);
146 146 w1 = t + w2;
147 147 *w = w2 - (w1 - t);
148 148 return (w1);
149 149 }
150 150
151 151 dcomplex
152 152 cpow(dcomplex z, dcomplex w) {
153 153 dcomplex ans;
154 154 double x, y, u, v, t, c, s, r, x2, y2;
155 155 double b[4], t1, t2, t3, t4, w1, w2, u1, v1, x1, y1;
156 156 int ix, iy, hx, lx, hy, ly, hv, hu, iu, iv, lu, lv;
157 157 int i, j, k;
158 158
159 159 x = D_RE(z);
160 160 y = D_IM(z);
161 161 u = D_RE(w);
162 162 v = D_IM(w);
163 163 hx = ((int *) &x)[HIWORD];
164 164 lx = ((int *) &x)[LOWORD];
165 165 hy = ((int *) &y)[HIWORD];
166 166 ly = ((int *) &y)[LOWORD];
167 167 hu = ((int *) &u)[HIWORD];
168 168 lu = ((int *) &u)[LOWORD];
169 169 hv = ((int *) &v)[HIWORD];
170 170 lv = ((int *) &v)[LOWORD];
171 171 ix = hx & 0x7fffffff;
172 172 iy = hy & 0x7fffffff;
173 173 iu = hu & 0x7fffffff;
174 174 iv = hv & 0x7fffffff;
175 175
176 176 j = 0;
177 177 if ((iv | lv) == 0) { /* z**(real) */
178 178 if (((hu - 0x3ff00000) | lu) == 0) { /* z ** 1 = z */
179 179 D_RE(ans) = x;
180 180 D_IM(ans) = y;
181 181 } else if ((iu | lu) == 0) { /* z ** 0 = 1 */
182 182 D_RE(ans) = one;
183 183 D_IM(ans) = zero;
184 184 } else if ((iy | ly) == 0) { /* (real)**(real) */
185 185 D_IM(ans) = zero;
186 186 if (hx < 0 && ix < hiinf && iu < hiinf) {
187 187 /* -x ** u is exp(i*pi*u)*pow(x,u) */
188 188 r = pow(-x, u);
189 189 sincospi(u, &s, &c);
190 190 D_RE(ans) = (c == zero)? c: c * r;
191 191 D_IM(ans) = (s == zero)? s: s * r;
192 192 } else
193 193 D_RE(ans) = pow(x, u);
194 194 } else if (((ix | lx) == 0) || ix >= hiinf || iy >= hiinf) {
195 195 if (isnan(x) || isnan(y) || isnan(u))
196 196 D_RE(ans) = D_IM(ans) = x + y + u;
197 197 else {
198 198 if ((ix | lx) == 0)
199 199 r = fabs(y);
200 200 else
201 201 r = fabs(x) + fabs(y);
202 202 t = atan2pi(y, x);
203 203 sincospi(t * u, &s, &c);
204 204 D_RE(ans) = (c == zero)? c: c * r;
205 205 D_IM(ans) = (s == zero)? s: s * r;
206 206 }
207 207 } else if (((ix - iy) | (lx - ly)) == 0) { /* |x| = |y| */
208 208 if (hx >= 0) {
209 209 t = (hy >= 0)? 0.25 : -0.25;
210 210 sincospi(t * u, &s, &c);
211 211 } else if ((lu & 3) == 0) {
212 212 t = (hy >= 0)? 0.75 : -0.75;
213 213 sincospi(t * u, &s, &c);
214 214 } else {
215 215 r = (hy >= 0)? u : -u;
216 216 t = -0.25 * r;
217 217 w1 = r + t;
218 218 w2 = t - (w1 - r);
219 219 sincospi(w1, &t1, &t2);
220 220 sincospi(w2, &t3, &t4);
221 221 s = t1 * t4 + t3 * t2;
222 222 c = t2 * t4 - t1 * t3;
223 223 }
224 224 if (ix < 0x3fe00000) /* |x| < 1/2 */
225 225 r = pow(fabs(x + x), u) * exp2(-0.5 * u);
226 226 else if (ix >= 0x3ff00000 || iu < 0x408ff800)
227 227 /* |x| >= 1 or |u| < 1023 */
228 228 r = pow(fabs(x), u) * exp2(0.5 * u);
229 229 else /* special treatment */
230 230 j = 2;
231 231 if (j == 0) {
232 232 D_RE(ans) = (c == zero)? c: c * r;
233 233 D_IM(ans) = (s == zero)? s: s * r;
234 234 }
235 235 } else
236 236 j = 1;
237 237 if (j == 0)
238 238 return (ans);
239 239 }
240 240 if (iu >= hiinf || iv >= hiinf || ix >= hiinf || iy >= hiinf) {
241 241 /*
242 242 * non-zero imag part(s) with inf component(s) yields NaN
243 243 */
244 244 t = fabs(x) + fabs(y) + fabs(u) + fabs(v);
245 245 D_RE(ans) = D_IM(ans) = t - t;
246 246 } else {
247 247 k = 0; /* no scaling */
248 248 if (iu > 0x7f000000 || iv > 0x7f000000) {
249 249 u *= .0009765625; /* scale 2**-10 to avoid overflow */
250 250 v *= .0009765625;
251 251 k = 1; /* scale by 2**-10 */
252 252 }
253 253 /*
254 254 * Use similated higher precision arithmetic to compute:
255 255 * r = u * log(hypot(x, y)) - v * atan2(y, x)
256 256 * q = u * atan2(y, x) + v * log(hypot(x, y))
257 257 */
258 258 t1 = __k_clog_r(x, y, &t2);
259 259 t3 = __k_atan2(y, x, &t4);
260 260 x1 = t1;
261 261 y1 = t3;
262 262 u1 = u;
263 263 v1 = v;
264 264 ((int *) &u1)[LOWORD] &= 0xf8000000;
265 265 ((int *) &v1)[LOWORD] &= 0xf8000000;
266 266 ((int *) &x1)[LOWORD] &= 0xf8000000;
267 267 ((int *) &y1)[LOWORD] &= 0xf8000000;
268 268 x2 = t2 - (x1 - t1); /* log(hypot(x,y)) = x1 + x2 */
269 269 y2 = t4 - (y1 - t3); /* atan2(y,x) = y1 + y2 */
270 270 /* compute q = u * atan2(y, x) + v * log(hypot(x, y)) */
271 271 if (j != 2) {
272 272 b[0] = u1 * y1;
273 273 b[1] = (u - u1) * y1 + u * y2;
274 274 if (j == 1) { /* v = 0 */
275 275 w1 = b[0] + b[1];
276 276 w2 = b[1] - (w1 - b[0]);
277 277 } else {
278 278 b[2] = v1 * x1;
279 279 b[3] = (v - v1) * x1 + v * x2;
280 280 w1 = sum4fp(b, &w2);
281 281 }
282 282 sincos(w1, &t1, &t2);
283 283 sincos(w2, &t3, &t4);
284 284 s = t1 * t4 + t3 * t2;
285 285 c = t2 * t4 - t1 * t3;
286 286 if (k == 1)
287 287 /*
288 288 * square (cos(q) + i sin(q)) k times to get
289 289 * (cos(2^k * q + i sin(2^k * q)
290 290 */
291 291 for (i = 0; i < 10; i++) {
292 292 t1 = s * c;
293 293 c = (c + s) * (c - s);
294 294 s = t1 + t1;
295 295 }
296 296 }
297 297 /* compute r = u * (t1, t2) - v * (t3, t4) */
298 298 b[0] = u1 * x1;
299 299 b[1] = (u - u1) * x1 + u * x2;
300 300 if (j == 1) { /* v = 0 */
301 301 w1 = b[0] + b[1];
302 302 w2 = b[1] - (w1 - b[0]);
303 303 } else {
304 304 b[2] = -v1 * y1;
305 305 b[3] = (v1 - v) * y1 - v * y2;
306 306 w1 = sum4fp(b, &w2);
307 307 }
308 308 /* check over/underflow for exp(w1 + w2) */
309 309 if (k && fabs(w1) < 1000.0) {
310 310 w1 *= 1024; w2 *= 1024; k = 0;
311 311 }
312 312 hx = ((int *) &w1)[HIWORD];
313 313 lx = ((int *) &w1)[LOWORD];
314 314 ix = hx & 0x7fffffff;
315 315 /* compute exp(w1 + w2) */
316 316 if (ix < 0x3c900000) /* exp(tiny < 2**-54) = 1 */
317 317 r = one;
318 318 else if (ix >= 0x40880000) /* overflow/underflow */
319 319 r = (hx < 0)? tiny * tiny : huge * huge;
320 320 else { /* compute exp(w1 + w2) */
321 321 k = (int) (invln2 * w1 + ((hx >= 0)? 0.5 : -0.5));
322 322 t1 = (double) k;
323 323 t2 = w1 - t1 * ln2hi;
324 324 t3 = w2 - t1 * ln2lo;
325 325 r = exp(t2 + t3);
326 326 }
327 327 if (c != zero) c *= r;
328 328 if (s != zero) s *= r;
329 329 if (k != 0) {
330 330 c = scalbn(c, k);
331 331 s = scalbn(s, k);
332 332 }
333 333 D_RE(ans) = c;
334 334 D_IM(ans) = s;
335 335 }
336 336 return (ans);
337 337 }
↓ open down ↓ |
297 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX