11.1Schristos/*-
21.1Schristos * SPDX-License-Identifier: BSD-2-Clause
31.1Schristos *
41.1Schristos * Copyright (c) 2019 Steven G. Kargl <kargl@FreeBSD.ORG>
51.1Schristos * All rights reserved.
61.1Schristos *
71.1Schristos * Redistribution and use in source and binary forms, with or without
81.1Schristos * modification, are permitted provided that the following conditions
91.1Schristos * are met:
101.1Schristos * 1. Redistributions of source code must retain the above copyright
111.1Schristos *    notice, this list of conditions and the following disclaimer.
121.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
131.1Schristos *    notice, this list of conditions and the following disclaimer in the
141.1Schristos *    documentation and/or other materials provided with the distribution.
151.1Schristos *
161.1Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
171.1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
181.1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
191.1Schristos * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
201.1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
211.1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221.1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
231.1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
241.1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Schristos * SUCH DAMAGE.
271.1Schristos */
281.1Schristos
291.1Schristos#include <sys/cdefs.h>
301.1Schristos#include <complex.h>
311.1Schristos#include <float.h>
321.1Schristos#include <math.h>
331.1Schristos
341.1Schristos#include "math_private.h"
351.1Schristos#include "k_expl.h"
361.1Schristos
371.1Schristos/* XXX cexpl() should be converted to use bits likeo src/s_cexp.c. */
381.1Schristos
391.1Schristosstatic const long double
401.1Schristoscexp_ovfl = 2.27892930024498818830197576893019292e+04L,
411.1Schristosexp_ovfl = 1.13565234062941439494919310779707649e+04L;
421.1Schristos
431.1Schristoslong double complex
441.1Schristoscexpl(long double complex z)
451.1Schristos{
461.1Schristos	long double c, exp_x, s, x, y;
471.1Schristos
481.1Schristos	x = creall(z);
491.1Schristos	y = cimagl(z);
501.1Schristos
511.1Schristos	/* cexp(x + I 0) = exp(x) + I 0 */
521.1Schristos	if (y == 0)
531.1Schristos		return (CMPLXL(expl(x), y));
541.1Schristos	/* cexp(0 + I y) = cos(y) + I sin(y) */
551.1Schristos	if (x == 0) {
561.1Schristos		sincosl(y, &s, &c);
571.1Schristos		return (CMPLXL(c, s));
581.1Schristos	}
591.1Schristos
601.1Schristos	if (!isfinite(y)) {
611.1Schristos		if (isfinite(x) || isnan(x)) {
621.1Schristos			/* cexp(finite|NaN +- I Inf|NaN) = NaN + I NaN */
631.1Schristos			return (CMPLXL(y - y, y - y));
641.1Schristos		} else if (isinf(x) && copysignl(1.L, x) < 0) {
651.1Schristos			/* cexp(-Inf +- I Inf|NaN) = 0 + I 0 */
661.1Schristos			return (CMPLXL(0.0, 0.0));
671.1Schristos		} else {
681.1Schristos			/* cexp(+Inf +- I Inf|NaN) = Inf + I NaN */
691.1Schristos			return (CMPLXL(x, y - y));
701.1Schristos		}
711.1Schristos	}
721.1Schristos
731.1Schristos	if (x > exp_ovfl && x < cexp_ovfl) {
741.1Schristos		/*
751.1Schristos		 * x is between exp_ovfl and cexp_ovfl, so we must scale to
761.1Schristos		 * avoid overflow in exp(x).
771.1Schristos		 */
781.1Schristos		return (__ldexp_cexpl(z, 0));
791.1Schristos	} else {
801.1Schristos		/*
811.1Schristos		 * Cases covered here:
821.1Schristos		 *  -  x < exp_ovfl and exp(x) won't overflow (common case)
831.1Schristos		 *  -  x > cexp_ovfl, so exp(x) * s overflows for all s > 0
841.1Schristos		 *  -  x = +-Inf (generated by exp())
851.1Schristos		 *  -  x = NaN (spurious inexact exception from y)
861.1Schristos		 */
871.1Schristos		exp_x = expl(x);
881.1Schristos		sincosl(y, &s, &c);
891.1Schristos		return (CMPLXL(exp_x * c, exp_x * s));
901.1Schristos	}
911.1Schristos}
92