1a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/*-
2a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Copyright (c) 2011 David Schultz <das@FreeBSD.ORG>
3a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * All rights reserved.
4a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes *
5a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * Redistribution and use in source and binary forms, with or without
6a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * modification, are permitted provided that the following conditions
7a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * are met:
8a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * 1. Redistributions of source code must retain the above copyright
9a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes *    notice, this list of conditions and the following disclaimer.
10a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
11a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes *    notice, this list of conditions and the following disclaimer in the
12a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes *    documentation and/or other materials provided with the distribution.
13a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes *
14a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * SUCH DAMAGE.
25a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */
26a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
27a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <sys/cdefs.h>
28a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes__FBSDID("$FreeBSD$");
29a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
30a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <complex.h>
31a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <math.h>
32a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
33a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "math_private.h"
34a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
35a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesstatic const uint32_t
36a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesexp_ovfl  = 0x40862e42,			/* high bits of MAX_EXP * ln2 ~= 710 */
37a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughescexp_ovfl = 0x4096b8e4;			/* (MAX_EXP - MIN_DENORM_EXP) * ln2 */
38a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
39a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesdouble complex
40a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughescexp(double complex z)
41a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes{
42a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	double x, y, exp_x;
43a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	uint32_t hx, hy, lx, ly;
44a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
45a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	x = creal(z);
46a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	y = cimag(z);
47a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
48a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	EXTRACT_WORDS(hy, ly, y);
49a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	hy &= 0x7fffffff;
50a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
51a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	/* cexp(x + I 0) = exp(x) + I 0 */
52a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if ((hy | ly) == 0)
53a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		return (cpack(exp(x), y));
54a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	EXTRACT_WORDS(hx, lx, x);
55a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	/* cexp(0 + I y) = cos(y) + I sin(y) */
56a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if (((hx & 0x7fffffff) | lx) == 0)
57a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		return (cpack(cos(y), sin(y)));
58a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
59a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if (hy >= 0x7ff00000) {
60a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		if (lx != 0 || (hx & 0x7fffffff) != 0x7ff00000) {
61a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes			/* cexp(finite|NaN +- I Inf|NaN) = NaN + I NaN */
62a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes			return (cpack(y - y, y - y));
63a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		} else if (hx & 0x80000000) {
64a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes			/* cexp(-Inf +- I Inf|NaN) = 0 + I 0 */
65a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes			return (cpack(0.0, 0.0));
66a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		} else {
67a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes			/* cexp(+Inf +- I Inf|NaN) = Inf + I NaN */
68a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes			return (cpack(x, y - y));
69a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		}
70a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	}
71a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes
72a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	if (hx >= exp_ovfl && hx <= cexp_ovfl) {
73a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		/*
74a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		 * x is between 709.7 and 1454.3, so we must scale to avoid
75a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		 * overflow in exp(x).
76a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		 */
77a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		return (__ldexp_cexp(z, 0));
78a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	} else {
79a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		/*
80a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		 * Cases covered here:
81a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		 *  -  x < exp_ovfl and exp(x) won't overflow (common case)
82a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		 *  -  x > cexp_ovfl, so exp(x) * s overflows for all s > 0
83a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		 *  -  x = +-Inf (generated by exp())
84a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		 *  -  x = NaN (spurious inexact exception from y)
85a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		 */
86a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		exp_x = exp(x);
87a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes		return (cpack(exp_x * cos(y), exp_x * sin(y)));
88a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes	}
89a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes}
90