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