xref: /freebsd/lib/msun/ld128/s_cexpl.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
1046e2d5dSSteve Kargl /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3046e2d5dSSteve Kargl  *
4046e2d5dSSteve Kargl  * Copyright (c) 2019 Steven G. Kargl <kargl@FreeBSD.ORG>
5046e2d5dSSteve Kargl  * All rights reserved.
6046e2d5dSSteve Kargl  *
7046e2d5dSSteve Kargl  * Redistribution and use in source and binary forms, with or without
8046e2d5dSSteve Kargl  * modification, are permitted provided that the following conditions
9046e2d5dSSteve Kargl  * are met:
10046e2d5dSSteve Kargl  * 1. Redistributions of source code must retain the above copyright
11046e2d5dSSteve Kargl  *    notice, this list of conditions and the following disclaimer.
12046e2d5dSSteve Kargl  * 2. Redistributions in binary form must reproduce the above copyright
13046e2d5dSSteve Kargl  *    notice, this list of conditions and the following disclaimer in the
14046e2d5dSSteve Kargl  *    documentation and/or other materials provided with the distribution.
15046e2d5dSSteve Kargl  *
16046e2d5dSSteve Kargl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17046e2d5dSSteve Kargl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18046e2d5dSSteve Kargl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19046e2d5dSSteve Kargl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20046e2d5dSSteve Kargl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21046e2d5dSSteve Kargl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22046e2d5dSSteve Kargl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23046e2d5dSSteve Kargl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24046e2d5dSSteve Kargl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25046e2d5dSSteve Kargl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26046e2d5dSSteve Kargl  * SUCH DAMAGE.
27046e2d5dSSteve Kargl  */
28046e2d5dSSteve Kargl 
29046e2d5dSSteve Kargl #include <complex.h>
30046e2d5dSSteve Kargl #include <float.h>
31046e2d5dSSteve Kargl #include <math.h>
32046e2d5dSSteve Kargl 
33046e2d5dSSteve Kargl #include "fpmath.h"
34046e2d5dSSteve Kargl #include "math_private.h"
35046e2d5dSSteve Kargl #include "k_expl.h"
36046e2d5dSSteve Kargl 
37046e2d5dSSteve Kargl /* XXX cexpl() should be converted to use bits likeo src/s_cexp.c. */
38046e2d5dSSteve Kargl 
39046e2d5dSSteve Kargl static const long double
40046e2d5dSSteve Kargl cexp_ovfl = 2.27892930024498818830197576893019292e+04L,
41046e2d5dSSteve Kargl exp_ovfl = 1.13565234062941439494919310779707649e+04L;
42046e2d5dSSteve Kargl 
43046e2d5dSSteve Kargl long double complex
cexpl(long double complex z)44046e2d5dSSteve Kargl cexpl(long double complex z)
45046e2d5dSSteve Kargl {
46046e2d5dSSteve Kargl 	long double c, exp_x, s, x, y;
47046e2d5dSSteve Kargl 
48046e2d5dSSteve Kargl 	x = creall(z);
49046e2d5dSSteve Kargl 	y = cimagl(z);
50046e2d5dSSteve Kargl 
51046e2d5dSSteve Kargl 	/* cexp(x + I 0) = exp(x) + I 0 */
52046e2d5dSSteve Kargl 	if (y == 0)
53046e2d5dSSteve Kargl 		return (CMPLXL(expl(x), y));
54046e2d5dSSteve Kargl 	/* cexp(0 + I y) = cos(y) + I sin(y) */
55046e2d5dSSteve Kargl 	if (x == 0) {
56046e2d5dSSteve Kargl 		sincosl(y, &s, &c);
57046e2d5dSSteve Kargl 		return (CMPLXL(c, s));
58046e2d5dSSteve Kargl 	}
59046e2d5dSSteve Kargl 
60046e2d5dSSteve Kargl 	if (!isfinite(y)) {
61046e2d5dSSteve Kargl 		if (isfinite(x) || isnan(x)) {
62046e2d5dSSteve Kargl 			/* cexp(finite|NaN +- I Inf|NaN) = NaN + I NaN */
63046e2d5dSSteve Kargl 			return (CMPLXL(y - y, y - y));
64046e2d5dSSteve Kargl 		} else if (isinf(x) && copysignl(1.L, x) < 0) {
65046e2d5dSSteve Kargl 			/* cexp(-Inf +- I Inf|NaN) = 0 + I 0 */
66046e2d5dSSteve Kargl 			return (CMPLXL(0.0, 0.0));
67046e2d5dSSteve Kargl 		} else {
68046e2d5dSSteve Kargl 			/* cexp(+Inf +- I Inf|NaN) = Inf + I NaN */
69046e2d5dSSteve Kargl 			return (CMPLXL(x, y - y));
70046e2d5dSSteve Kargl 		}
71046e2d5dSSteve Kargl 	}
72046e2d5dSSteve Kargl 
73046e2d5dSSteve Kargl 	if (x > exp_ovfl && x < cexp_ovfl) {
74046e2d5dSSteve Kargl 		/*
75046e2d5dSSteve Kargl 		 * x is between exp_ovfl and cexp_ovfl, so we must scale to
76046e2d5dSSteve Kargl 		 * avoid overflow in exp(x).
77046e2d5dSSteve Kargl 		 */
78046e2d5dSSteve Kargl 		return (__ldexp_cexpl(z, 0));
79046e2d5dSSteve Kargl 	} else {
80046e2d5dSSteve Kargl 		/*
81046e2d5dSSteve Kargl 		 * Cases covered here:
82046e2d5dSSteve Kargl 		 *  -  x < exp_ovfl and exp(x) won't overflow (common case)
83046e2d5dSSteve Kargl 		 *  -  x > cexp_ovfl, so exp(x) * s overflows for all s > 0
84046e2d5dSSteve Kargl 		 *  -  x = +-Inf (generated by exp())
85046e2d5dSSteve Kargl 		 *  -  x = NaN (spurious inexact exception from y)
86046e2d5dSSteve Kargl 		 */
87046e2d5dSSteve Kargl 		exp_x = expl(x);
88046e2d5dSSteve Kargl 		sincosl(y, &s, &c);
89046e2d5dSSteve Kargl 		return (CMPLXL(exp_x * c, exp_x * s));
90046e2d5dSSteve Kargl 	}
91046e2d5dSSteve Kargl }
92