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