13daee1d6SDavid Schultz /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni *
43daee1d6SDavid Schultz * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
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 /*
30f0f050e0STijl Coosemans * Float version of csinh(). See s_csinh.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
38c6df46baSDavid Schultz static const float huge = 0x1p127;
39c6df46baSDavid Schultz
403daee1d6SDavid Schultz float complex
csinhf(float complex z)413daee1d6SDavid Schultz csinhf(float complex z)
423daee1d6SDavid Schultz {
43c6df46baSDavid Schultz float x, y, h;
443daee1d6SDavid Schultz int32_t hx, hy, ix, iy;
453daee1d6SDavid Schultz
463daee1d6SDavid Schultz x = crealf(z);
473daee1d6SDavid Schultz y = cimagf(z);
483daee1d6SDavid Schultz
493daee1d6SDavid Schultz GET_FLOAT_WORD(hx, x);
503daee1d6SDavid Schultz GET_FLOAT_WORD(hy, y);
513daee1d6SDavid Schultz
523daee1d6SDavid Schultz ix = 0x7fffffff & hx;
533daee1d6SDavid Schultz iy = 0x7fffffff & hy;
543daee1d6SDavid Schultz
553daee1d6SDavid Schultz if (ix < 0x7f800000 && iy < 0x7f800000) {
563daee1d6SDavid Schultz if (iy == 0)
572cec876aSEd Schouten return (CMPLXF(sinhf(x), y));
58f0f050e0STijl Coosemans if (ix < 0x41100000) /* |x| < 9: normal case */
592cec876aSEd Schouten return (CMPLXF(sinhf(x) * cosf(y), coshf(x) * sinf(y)));
60c6df46baSDavid Schultz
61c6df46baSDavid Schultz /* |x| >= 9, so cosh(x) ~= exp(|x|) */
62c6df46baSDavid Schultz if (ix < 0x42b17218) {
63c6df46baSDavid Schultz /* x < 88.7: expf(|x|) won't overflow */
64f0f050e0STijl Coosemans h = expf(fabsf(x)) * 0.5F;
652cec876aSEd Schouten return (CMPLXF(copysignf(h, x) * cosf(y), h * sinf(y)));
66c6df46baSDavid Schultz } else if (ix < 0x4340b1e7) {
67c6df46baSDavid Schultz /* x < 192.7: scale to avoid overflow */
682cec876aSEd Schouten z = __ldexp_cexpf(CMPLXF(fabsf(x), y), -1);
692cec876aSEd Schouten return (CMPLXF(crealf(z) * copysignf(1, x), cimagf(z)));
70c6df46baSDavid Schultz } else {
71c6df46baSDavid Schultz /* x >= 192.7: the result always overflows */
72c6df46baSDavid Schultz h = huge * x;
732cec876aSEd Schouten return (CMPLXF(h * cosf(y), h * h * sinf(y)));
74c6df46baSDavid Schultz }
753daee1d6SDavid Schultz }
763daee1d6SDavid Schultz
77f0f050e0STijl Coosemans if (ix == 0) /* && iy >= 0x7f800000 */
78f0f050e0STijl Coosemans return (CMPLXF(x, y - y));
793daee1d6SDavid Schultz
80f0f050e0STijl Coosemans if (iy == 0) /* && ix >= 0x7f800000 */
81f0f050e0STijl Coosemans return (CMPLXF(x + x, y));
823daee1d6SDavid Schultz
83f0f050e0STijl Coosemans if (ix < 0x7f800000) /* && iy >= 0x7f800000 */
84f0f050e0STijl Coosemans return (CMPLXF(y - y, y - y));
853daee1d6SDavid Schultz
86f0f050e0STijl Coosemans if (ix == 0x7f800000) {
873daee1d6SDavid Schultz if (iy >= 0x7f800000)
88f0f050e0STijl Coosemans return (CMPLXF(x, y - y));
892cec876aSEd Schouten return (CMPLXF(x * cosf(y), INFINITY * sinf(y)));
903daee1d6SDavid Schultz }
913daee1d6SDavid Schultz
926f1b8a07SBruce Evans return (CMPLXF(((long double)x + x) * (y - y),
936f1b8a07SBruce Evans ((long double)x * x) * (y - y)));
943daee1d6SDavid Schultz }
953daee1d6SDavid Schultz
963daee1d6SDavid Schultz float complex
csinf(float complex z)973daee1d6SDavid Schultz csinf(float complex z)
983daee1d6SDavid Schultz {
993daee1d6SDavid Schultz
100f0f050e0STijl Coosemans z = csinhf(CMPLXF(cimagf(z), crealf(z)));
101f0f050e0STijl Coosemans return (CMPLXF(cimagf(z), crealf(z)));
1023daee1d6SDavid Schultz }
103