s_ccoshf.c revision 226599
1226458Sdas/*-
2226458Sdas * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
3226458Sdas * All rights reserved.
4226458Sdas *
5226458Sdas * Redistribution and use in source and binary forms, with or without
6226458Sdas * modification, are permitted provided that the following conditions
7226458Sdas * are met:
8226458Sdas * 1. Redistributions of source code must retain the above copyright
9226458Sdas *    notice unmodified, this list of conditions, and the following
10226458Sdas *    disclaimer.
11226458Sdas * 2. Redistributions in binary form must reproduce the above copyright
12226458Sdas *    notice, this list of conditions and the following disclaimer in the
13226458Sdas *    documentation and/or other materials provided with the distribution.
14226458Sdas *
15226458Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16226458Sdas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17226458Sdas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18226458Sdas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19226458Sdas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20226458Sdas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21226458Sdas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22226458Sdas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23226458Sdas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24226458Sdas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25226458Sdas */
26226458Sdas
27226458Sdas/*
28226458Sdas * Hyperbolic cosine of a complex argument.  See s_ccosh.c for details.
29226458Sdas */
30226458Sdas
31226458Sdas#include <sys/cdefs.h>
32226458Sdas__FBSDID("$FreeBSD: head/lib/msun/src/s_ccoshf.c 226599 2011-10-21 06:29:32Z das $");
33226458Sdas
34226458Sdas#include <complex.h>
35226458Sdas#include <math.h>
36226458Sdas
37226458Sdas#include "math_private.h"
38226458Sdas
39226599Sdasstatic const float huge = 0x1p127;
40226599Sdas
41226458Sdasfloat complex
42226458Sdasccoshf(float complex z)
43226458Sdas{
44226599Sdas	float x, y, h;
45226458Sdas	int32_t hx, hy, ix, iy;
46226458Sdas
47226458Sdas	x = crealf(z);
48226458Sdas	y = cimagf(z);
49226458Sdas
50226458Sdas	GET_FLOAT_WORD(hx, x);
51226458Sdas	GET_FLOAT_WORD(hy, y);
52226458Sdas
53226458Sdas	ix = 0x7fffffff & hx;
54226458Sdas	iy = 0x7fffffff & hy;
55226458Sdas
56226458Sdas	if (ix < 0x7f800000 && iy < 0x7f800000) {
57226458Sdas		if (iy == 0)
58226458Sdas			return (cpackf(coshf(x), x * y));
59226599Sdas		if (ix < 0x41100000)	/* small x: normal case */
60226599Sdas			return (cpackf(coshf(x) * cosf(y), sinhf(x) * sinf(y)));
61226599Sdas
62226599Sdas		/* |x| >= 9, so cosh(x) ~= exp(|x|) */
63226599Sdas		if (ix < 0x42b17218) {
64226599Sdas			/* x < 88.7: expf(|x|) won't overflow */
65226599Sdas			h = expf(fabsf(x)) * 0.5f;
66226599Sdas			return (cpackf(h * cosf(y), copysignf(h, x) * sinf(y)));
67226599Sdas		} else if (ix < 0x4340b1e7) {
68226599Sdas			/* x < 192.7: scale to avoid overflow */
69226599Sdas			z = __ldexp_cexpf(cpackf(fabsf(x), y), -1);
70226599Sdas			return (cpackf(crealf(z), cimagf(z) * copysignf(1, x)));
71226599Sdas		} else {
72226599Sdas			/* x >= 192.7: the result always overflows */
73226599Sdas			h = huge * x;
74226599Sdas			return (cpackf(h * h * cosf(y), h * sinf(y)));
75226599Sdas		}
76226458Sdas	}
77226458Sdas
78226458Sdas	if (ix == 0 && iy >= 0x7f800000)
79226458Sdas		return (cpackf(y - y, copysignf(0, x * (y - y))));
80226458Sdas
81226458Sdas	if (iy == 0 && ix >= 0x7f800000) {
82226458Sdas		if ((hx & 0x7fffff) == 0)
83226458Sdas			return (cpackf(x * x, copysignf(0, x) * y));
84226458Sdas		return (cpackf(x * x, copysignf(0, (x + x) * y)));
85226458Sdas	}
86226458Sdas
87226458Sdas	if (ix < 0x7f800000 && iy >= 0x7f800000)
88226458Sdas		return (cpackf(y - y, x * (y - y)));
89226458Sdas
90226458Sdas	if (ix >= 0x7f800000 && (hx & 0x7fffff) == 0) {
91226458Sdas		if (iy >= 0x7f800000)
92226458Sdas			return (cpackf(x * x, x * (y - y)));
93226458Sdas		return (cpackf((x * x) * cosf(y), x * sinf(y)));
94226458Sdas	}
95226458Sdas
96226458Sdas	return (cpackf((x * x) * (y - y), (x + x) * (y - y)));
97226458Sdas}
98226458Sdas
99226458Sdasfloat complex
100226458Sdasccosf(float complex z)
101226458Sdas{
102226458Sdas
103226458Sdas	return (ccoshf(cpackf(-cimagf(z), crealf(z))));
104226458Sdas}
105