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 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27 * Use is subject to license terms.
28 */
29
30#pragma weak __cexpl = cexpl
31
32#include "libm.h"		/* expl/isinfl/iszerol/scalbnl/sincosl */
33#include "complex_wrapper.h"
34
35extern int isinfl(long double);
36extern int iszerol(long double);
37
38/* INDENT OFF */
39static const long double zero = 0.0L;
40/* INDENT ON */
41
42ldcomplex
43cexpl(ldcomplex z) {
44	ldcomplex ans;
45	long double x, y, t, c, s;
46	int n, ix, iy, hx, hy;
47
48	x = LD_RE(z);
49	y = LD_IM(z);
50	hx = HI_XWORD(x);
51	hy = HI_XWORD(y);
52	ix = hx & 0x7fffffff;
53	iy = hy & 0x7fffffff;
54	if (iszerol(y)) {	/* y = 0 */
55		LD_RE(ans) = expl(x);
56		LD_IM(ans) = y;
57	} else if (isinfl(x)) {	/* x is +-inf */
58		if (hx < 0) {
59			if (iy >= 0x7fff0000) {
60				LD_RE(ans) = zero;
61				LD_IM(ans) = zero;
62			} else {
63				sincosl(y, &s, &c);
64				LD_RE(ans) = zero * c;
65				LD_IM(ans) = zero * s;
66			}
67		} else {
68			if (iy >= 0x7fff0000) {
69				LD_RE(ans) = x;
70				LD_IM(ans) = y - y;
71			} else {
72				(void) sincosl(y, &s, &c);
73				LD_RE(ans) = x * c;
74				LD_IM(ans) = x * s;
75			}
76		}
77	} else {
78		(void) sincosl(y, &s, &c);
79		if (ix >= 0x400C62E4) {	/* |x| > 11356.52... ~ log(2**16384) */
80			t = __k_cexpl(x, &n);
81			LD_RE(ans) = scalbnl(t * c, n);
82			LD_IM(ans) = scalbnl(t * s, n);
83		} else {
84			t = expl(x);
85			LD_RE(ans) = t * c;
86			LD_IM(ans) = t * s;
87		}
88	}
89	return (ans);
90}
91