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 __cexp = cexp 32 33 34 /* 35 * dcomplex cexp(dcomplex z); 36 * 37 * x+iy x 38 * e = e (cos(y)+i*sin(y)) 39 * 40 * Over/underflow issue 41 * -------------------- 42 * exp(x) may be huge but cos(y) or sin(y) may be tiny. So we use 43 * function __k_cexp(x,&n) to return exp(x) = __k_cexp(x,&n)*2**n. 44 * Thus if exp(x+iy) = A + Bi and t = __k_cexp(x,&n), then 45 * A = t*cos(y)*2**n, B = t*sin(y)*2**n 46 * 47 * Purge off all exceptional arguments: 48 * (x,0) --> (exp(x),0) for all x, include inf and NaN 49 * (+inf, y) --> (+inf, NaN) for inf, nan 50 * (-inf, y) --> (+-0, +-0) for y = inf, nan 51 * (x,+-inf/NaN) --> (NaN,NaN) for finite x 52 * For all other cases, return 53 * (x,y) --> exp(x)*cos(y)+i*exp(x)*sin(y)) 54 * 55 * Algorithm for out of range x and finite y 56 * 1. compute exp(x) in factor form (t=__k_cexp(x,&n))*2**n 57 * 2. compute sincos(y,&s,&c) 58 * 3. compute t*s+i*(t*c), then scale back to 2**n and return. 59 */ 60 61 #include "libm.h" /* exp/scalbn/sincos/__k_cexp */ 62 #include "complex_wrapper.h" 63 64 static const double zero = 0.0; 65 66 dcomplex 67 cexp(dcomplex z) 68 { 69 dcomplex ans; 70 double x, y, t, c, s; 71 int n, ix, iy, hx, hy, lx, ly; 72 73 x = D_RE(z); 74 y = D_IM(z); 75 hx = HI_WORD(x); 76 lx = LO_WORD(x); 77 hy = HI_WORD(y); 78 ly = LO_WORD(y); 79 ix = hx & 0x7fffffff; 80 iy = hy & 0x7fffffff; 81 82 if ((iy | ly) == 0) { /* y = 0 */ 83 D_RE(ans) = exp(x); 84 D_IM(ans) = y; 85 } else if (ISINF(ix, lx)) { /* x is +-inf */ 86 if (hx < 0) { 87 if (iy >= 0x7ff00000) { 88 D_RE(ans) = zero; 89 D_IM(ans) = zero; 90 } else { 91 sincos(y, &s, &c); 92 D_RE(ans) = zero * c; 93 D_IM(ans) = zero * s; 94 } 95 } else { 96 if (iy >= 0x7ff00000) { 97 D_RE(ans) = x; 98 D_IM(ans) = y - y; 99 } else { 100 (void) sincos(y, &s, &c); 101 D_RE(ans) = x * c; 102 D_IM(ans) = x * s; 103 } 104 } 105 } else { 106 (void) sincos(y, &s, &c); 107 108 if (ix >= 0x40862E42) { /* |x| > 709.78... ~ log(2**1024) */ 109 t = __k_cexp(x, &n); 110 D_RE(ans) = scalbn(t * c, n); 111 D_IM(ans) = scalbn(t * s, n); 112 } else { 113 t = exp(x); 114 D_RE(ans) = t * c; 115 D_IM(ans) = t * s; 116 } 117 } 118 119 return (ans); 120 }