xref: /freebsd/lib/msun/src/s_cexpf.c (revision 4ce31585dc8e569e2c63c7253931d8286a64d98c)
1f3732b5aSDavid Schultz /*-
2f3732b5aSDavid Schultz  * Copyright (c) 2011 David Schultz <das@FreeBSD.ORG>
3f3732b5aSDavid Schultz  * All rights reserved.
4f3732b5aSDavid Schultz  *
5f3732b5aSDavid Schultz  * Redistribution and use in source and binary forms, with or without
6f3732b5aSDavid Schultz  * modification, are permitted provided that the following conditions
7f3732b5aSDavid Schultz  * are met:
8f3732b5aSDavid Schultz  * 1. Redistributions of source code must retain the above copyright
9f3732b5aSDavid Schultz  *    notice, this list of conditions and the following disclaimer.
10f3732b5aSDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
11f3732b5aSDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
12f3732b5aSDavid Schultz  *    documentation and/or other materials provided with the distribution.
13f3732b5aSDavid Schultz  *
14f3732b5aSDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15f3732b5aSDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16f3732b5aSDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17f3732b5aSDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18f3732b5aSDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19f3732b5aSDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20f3732b5aSDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21f3732b5aSDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22f3732b5aSDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23f3732b5aSDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24f3732b5aSDavid Schultz  * SUCH DAMAGE.
25f3732b5aSDavid Schultz  */
26f3732b5aSDavid Schultz 
27f3732b5aSDavid Schultz #include <sys/cdefs.h>
28f3732b5aSDavid Schultz __FBSDID("$FreeBSD$");
29f3732b5aSDavid Schultz 
30f3732b5aSDavid Schultz #include <complex.h>
31f3732b5aSDavid Schultz #include <math.h>
32f3732b5aSDavid Schultz 
33f3732b5aSDavid Schultz #include "math_private.h"
34f3732b5aSDavid Schultz 
35f3732b5aSDavid Schultz static const uint32_t
36f3732b5aSDavid Schultz exp_ovfl  = 0x42b17218,		/* MAX_EXP * ln2 ~= 88.722839355 */
37f3732b5aSDavid Schultz cexp_ovfl = 0x43400074,		/* (MAX_EXP - MIN_DENORM_EXP) * ln2 */
38f3732b5aSDavid Schultz k         = 235;		/* constant for reduction */
39f3732b5aSDavid Schultz 
40f3732b5aSDavid Schultz static const float
41f3732b5aSDavid Schultz kln2      =  162.88958740f;	/* k * ln2 */
42f3732b5aSDavid Schultz 
43f3732b5aSDavid Schultz float complex
44f3732b5aSDavid Schultz cexpf(float complex z)
45f3732b5aSDavid Schultz {
46f3732b5aSDavid Schultz 	float x, y, exp_x;
47f3732b5aSDavid Schultz 	uint32_t hx, hy;
48f3732b5aSDavid Schultz 	int scale;
49f3732b5aSDavid Schultz 
50f3732b5aSDavid Schultz 	x = crealf(z);
51f3732b5aSDavid Schultz 	y = cimagf(z);
52f3732b5aSDavid Schultz 
53f3732b5aSDavid Schultz 	GET_FLOAT_WORD(hy, y);
54f3732b5aSDavid Schultz 	hy &= 0x7fffffff;
55f3732b5aSDavid Schultz 
56f3732b5aSDavid Schultz 	/* cexp(x + I 0) = exp(x) + I 0 */
57f3732b5aSDavid Schultz 	if (hy == 0)
58f3732b5aSDavid Schultz 		return (cpackf(expf(x), y));
59f3732b5aSDavid Schultz 	GET_FLOAT_WORD(hx, x);
60*4ce31585SDavid Schultz 	/* cexp(0 + I y) = cos(y) + I sin(y) */
61*4ce31585SDavid Schultz 	if ((hx & 0x7fffffff) == 0)
62*4ce31585SDavid Schultz 		return (cpackf(cosf(y), sinf(y)));
63*4ce31585SDavid Schultz 
64f3732b5aSDavid Schultz 	if (hy >= 0x7f800000) {
65f3732b5aSDavid Schultz 		if ((hx & 0x7fffffff) != 0x7f800000) {
66f3732b5aSDavid Schultz 			/* cexp(finite|NaN +- I Inf|NaN) = NaN + I NaN */
67f3732b5aSDavid Schultz 			return (cpackf(y - y, y - y));
68f3732b5aSDavid Schultz 		} else if (hx & 0x80000000) {
69f3732b5aSDavid Schultz 			/* cexp(-Inf +- I Inf|NaN) = 0 + I 0 */
70f3732b5aSDavid Schultz 			return (cpackf(0.0, 0.0));
71f3732b5aSDavid Schultz 		} else {
72f3732b5aSDavid Schultz 			/* cexp(+Inf +- I Inf|NaN) = Inf + I NaN */
73f3732b5aSDavid Schultz 			return (cpackf(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 88.7 and 192, so we must scale to avoid
80f3732b5aSDavid Schultz 		 * overflow in expf(x).  We use exp(x) = exp(x - kln2) * 2**k,
81f3732b5aSDavid Schultz 		 * carefully chosen to minimize |exp(kln2) - 2**k|.  We also
82f3732b5aSDavid Schultz 		 * scale the exponent of exp(x) to MANT_DIG to avoid loss of
83f3732b5aSDavid Schultz 		 * accuracy due to underflow if sin(y) is tiny.
84f3732b5aSDavid Schultz 		 */
85f3732b5aSDavid Schultz 		exp_x = expf(x - kln2);
86f3732b5aSDavid Schultz 		GET_FLOAT_WORD(hx, exp_x);
87f3732b5aSDavid Schultz 		SET_FLOAT_WORD(exp_x, (hx & 0x7fffff) | ((0x7f + 23) << 23));
88f3732b5aSDavid Schultz 		scale = (hx >> 23) - (0x7f + 23) + k;
89f3732b5aSDavid Schultz 		return (cpackf(scalbnf(cosf(y) * exp_x, scale),
90f3732b5aSDavid Schultz 			scalbnf(sinf(y) * exp_x, scale)));
91f3732b5aSDavid Schultz 	} else {
92f3732b5aSDavid Schultz 		/*
93f3732b5aSDavid Schultz 		 * Cases covered here:
94f3732b5aSDavid Schultz 		 *  -  x < exp_ovfl and exp(x) won't overflow (common case)
95f3732b5aSDavid Schultz 		 *  -  x > cexp_ovfl, so exp(x) * s overflows for all s > 0
96f3732b5aSDavid Schultz 		 *  -  x = +-Inf (generated by exp())
97f3732b5aSDavid Schultz 		 *  -  x = NaN (spurious inexact exception from y)
98f3732b5aSDavid Schultz 		 */
99f3732b5aSDavid Schultz 		exp_x = expf(x);
100f3732b5aSDavid Schultz 		return (cpackf(exp_x * cosf(y), exp_x * sinf(y)));
101f3732b5aSDavid Schultz 	}
102f3732b5aSDavid Schultz }
103