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