xref: /freebsd/lib/msun/src/s_ctanhf.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
13daee1d6SDavid Schultz /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
43daee1d6SDavid Schultz  * Copyright (c) 2011 David Schultz
53daee1d6SDavid Schultz  * All rights reserved.
63daee1d6SDavid Schultz  *
73daee1d6SDavid Schultz  * Redistribution and use in source and binary forms, with or without
83daee1d6SDavid Schultz  * modification, are permitted provided that the following conditions
93daee1d6SDavid Schultz  * are met:
103daee1d6SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
113daee1d6SDavid Schultz  *    notice unmodified, this list of conditions, and the following
123daee1d6SDavid Schultz  *    disclaimer.
133daee1d6SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
143daee1d6SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
153daee1d6SDavid Schultz  *    documentation and/or other materials provided with the distribution.
163daee1d6SDavid Schultz  *
173daee1d6SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
183daee1d6SDavid Schultz  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
193daee1d6SDavid Schultz  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
203daee1d6SDavid Schultz  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
213daee1d6SDavid Schultz  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223daee1d6SDavid Schultz  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233daee1d6SDavid Schultz  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243daee1d6SDavid Schultz  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253daee1d6SDavid Schultz  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
263daee1d6SDavid Schultz  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273daee1d6SDavid Schultz  */
283daee1d6SDavid Schultz 
293daee1d6SDavid Schultz /*
303daee1d6SDavid Schultz  * Hyperbolic tangent of a complex argument z.  See s_ctanh.c for details.
313daee1d6SDavid Schultz  */
323daee1d6SDavid Schultz 
333daee1d6SDavid Schultz #include <complex.h>
343daee1d6SDavid Schultz #include <math.h>
353daee1d6SDavid Schultz 
363daee1d6SDavid Schultz #include "math_private.h"
373daee1d6SDavid Schultz 
383daee1d6SDavid Schultz float complex
ctanhf(float complex z)393daee1d6SDavid Schultz ctanhf(float complex z)
403daee1d6SDavid Schultz {
41e2157cd0SDimitry Andric 	float x, y;
423daee1d6SDavid Schultz 	float t, beta, s, rho, denom;
433daee1d6SDavid Schultz 	uint32_t hx, ix;
443daee1d6SDavid Schultz 
453daee1d6SDavid Schultz 	x = crealf(z);
463daee1d6SDavid Schultz 	y = cimagf(z);
473daee1d6SDavid Schultz 
483daee1d6SDavid Schultz 	GET_FLOAT_WORD(hx, x);
493daee1d6SDavid Schultz 	ix = hx & 0x7fffffff;
503daee1d6SDavid Schultz 
513daee1d6SDavid Schultz 	if (ix >= 0x7f800000) {
523daee1d6SDavid Schultz 		if (ix & 0x7fffff)
536f1b8a07SBruce Evans 			return (CMPLXF(nan_mix(x, y),
546f1b8a07SBruce Evans 			    y == 0 ? y : nan_mix(x, y)));
553daee1d6SDavid Schultz 		SET_FLOAT_WORD(x, hx - 0x40000000);
562cec876aSEd Schouten 		return (CMPLXF(x,
573daee1d6SDavid Schultz 		    copysignf(0, isinf(y) ? y : sinf(y) * cosf(y))));
583daee1d6SDavid Schultz 	}
593daee1d6SDavid Schultz 
60bc23acdcSDavid Schultz 	if (!isfinite(y))
61a7b42c4bSAlex Richardson 		return (CMPLXF(ix ? y - y : x, y - y));
62bc23acdcSDavid Schultz 
6368b433d7STijl Coosemans 	if (ix >= 0x41300000) {	/* |x| >= 11 */
643daee1d6SDavid Schultz 		float exp_mx = expf(-fabsf(x));
652cec876aSEd Schouten 		return (CMPLXF(copysignf(1, x),
663daee1d6SDavid Schultz 		    4 * sinf(y) * cosf(y) * exp_mx * exp_mx));
673daee1d6SDavid Schultz 	}
683daee1d6SDavid Schultz 
693daee1d6SDavid Schultz 	t = tanf(y);
703daee1d6SDavid Schultz 	beta = 1.0 + t * t;
713daee1d6SDavid Schultz 	s = sinhf(x);
723daee1d6SDavid Schultz 	rho = sqrtf(1 + s * s);
733daee1d6SDavid Schultz 	denom = 1 + beta * s * s;
742cec876aSEd Schouten 	return (CMPLXF((beta * rho * s) / denom, t / denom));
753daee1d6SDavid Schultz }
763daee1d6SDavid Schultz 
773daee1d6SDavid Schultz float complex
ctanf(float complex z)783daee1d6SDavid Schultz ctanf(float complex z)
793daee1d6SDavid Schultz {
803daee1d6SDavid Schultz 
8168b433d7STijl Coosemans 	z = ctanhf(CMPLXF(cimagf(z), crealf(z)));
8268b433d7STijl Coosemans 	return (CMPLXF(cimagf(z), crealf(z)));
833daee1d6SDavid Schultz }
843daee1d6SDavid Schultz 
85