125c28e83SPiotr Jasiukajtis /*
225c28e83SPiotr Jasiukajtis * CDDL HEADER START
325c28e83SPiotr Jasiukajtis *
425c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the
525c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License").
625c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License.
725c28e83SPiotr Jasiukajtis *
825c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
1025c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions
1125c28e83SPiotr Jasiukajtis * and limitations under the License.
1225c28e83SPiotr Jasiukajtis *
1325c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
1425c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
1625c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
1725c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
1825c28e83SPiotr Jasiukajtis *
1925c28e83SPiotr Jasiukajtis * CDDL HEADER END
2025c28e83SPiotr Jasiukajtis */
2125c28e83SPiotr Jasiukajtis
2225c28e83SPiotr Jasiukajtis /*
2325c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
2425c28e83SPiotr Jasiukajtis */
2525c28e83SPiotr Jasiukajtis /*
2625c28e83SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2725c28e83SPiotr Jasiukajtis * Use is subject to license terms.
2825c28e83SPiotr Jasiukajtis */
2925c28e83SPiotr Jasiukajtis
30*ddc0e0b5SRichard Lowe #pragma weak __cexp = cexp
3125c28e83SPiotr Jasiukajtis
3225c28e83SPiotr Jasiukajtis /* INDENT OFF */
3325c28e83SPiotr Jasiukajtis /*
3425c28e83SPiotr Jasiukajtis * dcomplex cexp(dcomplex z);
3525c28e83SPiotr Jasiukajtis *
3625c28e83SPiotr Jasiukajtis * x+iy x
3725c28e83SPiotr Jasiukajtis * e = e (cos(y)+i*sin(y))
3825c28e83SPiotr Jasiukajtis *
3925c28e83SPiotr Jasiukajtis * Over/underflow issue
4025c28e83SPiotr Jasiukajtis * --------------------
4125c28e83SPiotr Jasiukajtis * exp(x) may be huge but cos(y) or sin(y) may be tiny. So we use
4225c28e83SPiotr Jasiukajtis * function __k_cexp(x,&n) to return exp(x) = __k_cexp(x,&n)*2**n.
4325c28e83SPiotr Jasiukajtis * Thus if exp(x+iy) = A + Bi and t = __k_cexp(x,&n), then
4425c28e83SPiotr Jasiukajtis * A = t*cos(y)*2**n, B = t*sin(y)*2**n
4525c28e83SPiotr Jasiukajtis *
4625c28e83SPiotr Jasiukajtis * Purge off all exceptional arguments:
4725c28e83SPiotr Jasiukajtis * (x,0) --> (exp(x),0) for all x, include inf and NaN
4825c28e83SPiotr Jasiukajtis * (+inf, y) --> (+inf, NaN) for inf, nan
4925c28e83SPiotr Jasiukajtis * (-inf, y) --> (+-0, +-0) for y = inf, nan
5025c28e83SPiotr Jasiukajtis * (x,+-inf/NaN) --> (NaN,NaN) for finite x
5125c28e83SPiotr Jasiukajtis * For all other cases, return
5225c28e83SPiotr Jasiukajtis * (x,y) --> exp(x)*cos(y)+i*exp(x)*sin(y))
5325c28e83SPiotr Jasiukajtis *
5425c28e83SPiotr Jasiukajtis * Algorithm for out of range x and finite y
5525c28e83SPiotr Jasiukajtis * 1. compute exp(x) in factor form (t=__k_cexp(x,&n))*2**n
5625c28e83SPiotr Jasiukajtis * 2. compute sincos(y,&s,&c)
5725c28e83SPiotr Jasiukajtis * 3. compute t*s+i*(t*c), then scale back to 2**n and return.
5825c28e83SPiotr Jasiukajtis */
5925c28e83SPiotr Jasiukajtis /* INDENT ON */
6025c28e83SPiotr Jasiukajtis
6125c28e83SPiotr Jasiukajtis #include "libm.h" /* exp/scalbn/sincos/__k_cexp */
6225c28e83SPiotr Jasiukajtis #include "complex_wrapper.h"
6325c28e83SPiotr Jasiukajtis
6425c28e83SPiotr Jasiukajtis static const double zero = 0.0;
6525c28e83SPiotr Jasiukajtis
6625c28e83SPiotr Jasiukajtis dcomplex
cexp(dcomplex z)6725c28e83SPiotr Jasiukajtis cexp(dcomplex z) {
6825c28e83SPiotr Jasiukajtis dcomplex ans;
6925c28e83SPiotr Jasiukajtis double x, y, t, c, s;
7025c28e83SPiotr Jasiukajtis int n, ix, iy, hx, hy, lx, ly;
7125c28e83SPiotr Jasiukajtis
7225c28e83SPiotr Jasiukajtis x = D_RE(z);
7325c28e83SPiotr Jasiukajtis y = D_IM(z);
7425c28e83SPiotr Jasiukajtis hx = HI_WORD(x);
7525c28e83SPiotr Jasiukajtis lx = LO_WORD(x);
7625c28e83SPiotr Jasiukajtis hy = HI_WORD(y);
7725c28e83SPiotr Jasiukajtis ly = LO_WORD(y);
7825c28e83SPiotr Jasiukajtis ix = hx & 0x7fffffff;
7925c28e83SPiotr Jasiukajtis iy = hy & 0x7fffffff;
8025c28e83SPiotr Jasiukajtis if ((iy | ly) == 0) { /* y = 0 */
8125c28e83SPiotr Jasiukajtis D_RE(ans) = exp(x);
8225c28e83SPiotr Jasiukajtis D_IM(ans) = y;
8325c28e83SPiotr Jasiukajtis } else if (ISINF(ix, lx)) { /* x is +-inf */
8425c28e83SPiotr Jasiukajtis if (hx < 0) {
8525c28e83SPiotr Jasiukajtis if (iy >= 0x7ff00000) {
8625c28e83SPiotr Jasiukajtis D_RE(ans) = zero;
8725c28e83SPiotr Jasiukajtis D_IM(ans) = zero;
8825c28e83SPiotr Jasiukajtis } else {
8925c28e83SPiotr Jasiukajtis sincos(y, &s, &c);
9025c28e83SPiotr Jasiukajtis D_RE(ans) = zero * c;
9125c28e83SPiotr Jasiukajtis D_IM(ans) = zero * s;
9225c28e83SPiotr Jasiukajtis }
9325c28e83SPiotr Jasiukajtis } else {
9425c28e83SPiotr Jasiukajtis if (iy >= 0x7ff00000) {
9525c28e83SPiotr Jasiukajtis D_RE(ans) = x;
9625c28e83SPiotr Jasiukajtis D_IM(ans) = y - y;
9725c28e83SPiotr Jasiukajtis } else {
9825c28e83SPiotr Jasiukajtis (void) sincos(y, &s, &c);
9925c28e83SPiotr Jasiukajtis D_RE(ans) = x * c;
10025c28e83SPiotr Jasiukajtis D_IM(ans) = x * s;
10125c28e83SPiotr Jasiukajtis }
10225c28e83SPiotr Jasiukajtis }
10325c28e83SPiotr Jasiukajtis } else {
10425c28e83SPiotr Jasiukajtis (void) sincos(y, &s, &c);
10525c28e83SPiotr Jasiukajtis if (ix >= 0x40862E42) { /* |x| > 709.78... ~ log(2**1024) */
10625c28e83SPiotr Jasiukajtis t = __k_cexp(x, &n);
10725c28e83SPiotr Jasiukajtis D_RE(ans) = scalbn(t * c, n);
10825c28e83SPiotr Jasiukajtis D_IM(ans) = scalbn(t * s, n);
10925c28e83SPiotr Jasiukajtis } else {
11025c28e83SPiotr Jasiukajtis t = exp(x);
11125c28e83SPiotr Jasiukajtis D_RE(ans) = t * c;
11225c28e83SPiotr Jasiukajtis D_IM(ans) = t * s;
11325c28e83SPiotr Jasiukajtis }
11425c28e83SPiotr Jasiukajtis }
11525c28e83SPiotr Jasiukajtis return (ans);
11625c28e83SPiotr Jasiukajtis }
117