blob: ba1a79497fc6491935741c1fffb15be8c7255822 [file] [log] [blame]
Elliott Hughesa0ee0782013-01-30 19:06:37 -08001/*-
Elliott Hughes8da8ca42018-05-08 13:35:33 -07002 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
Elliott Hughesa0ee0782013-01-30 19:06:37 -08004 * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
Elliott Hughes8cff2f92015-08-28 20:21:43 -070030 * Float version of csinh(). See s_csinh.c for details.
Elliott Hughesa0ee0782013-01-30 19:06:37 -080031 */
32
33#include <sys/cdefs.h>
Elliott Hughesbac0ebb2021-01-26 14:17:20 -080034__FBSDID("$FreeBSD$");
Elliott Hughesa0ee0782013-01-30 19:06:37 -080035
36#include <complex.h>
37#include <math.h>
38
39#include "math_private.h"
40
41static const float huge = 0x1p127;
42
43float complex
44csinhf(float complex z)
45{
46 float x, y, h;
47 int32_t hx, hy, ix, iy;
48
49 x = crealf(z);
50 y = cimagf(z);
51
52 GET_FLOAT_WORD(hx, x);
53 GET_FLOAT_WORD(hy, y);
54
55 ix = 0x7fffffff & hx;
56 iy = 0x7fffffff & hy;
57
58 if (ix < 0x7f800000 && iy < 0x7f800000) {
59 if (iy == 0)
Elliott Hughes8cff2f92015-08-28 20:21:43 -070060 return (CMPLXF(sinhf(x), y));
61 if (ix < 0x41100000) /* |x| < 9: normal case */
62 return (CMPLXF(sinhf(x) * cosf(y), coshf(x) * sinf(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080063
64 /* |x| >= 9, so cosh(x) ~= exp(|x|) */
65 if (ix < 0x42b17218) {
66 /* x < 88.7: expf(|x|) won't overflow */
Elliott Hughes8cff2f92015-08-28 20:21:43 -070067 h = expf(fabsf(x)) * 0.5F;
68 return (CMPLXF(copysignf(h, x) * cosf(y), h * sinf(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080069 } else if (ix < 0x4340b1e7) {
70 /* x < 192.7: scale to avoid overflow */
Elliott Hughes8cff2f92015-08-28 20:21:43 -070071 z = __ldexp_cexpf(CMPLXF(fabsf(x), y), -1);
72 return (CMPLXF(crealf(z) * copysignf(1, x), cimagf(z)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080073 } else {
74 /* x >= 192.7: the result always overflows */
75 h = huge * x;
Elliott Hughes8cff2f92015-08-28 20:21:43 -070076 return (CMPLXF(h * cosf(y), h * h * sinf(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080077 }
78 }
79
Elliott Hughes8cff2f92015-08-28 20:21:43 -070080 if (ix == 0) /* && iy >= 0x7f800000 */
81 return (CMPLXF(x, y - y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080082
Elliott Hughes8cff2f92015-08-28 20:21:43 -070083 if (iy == 0) /* && ix >= 0x7f800000 */
84 return (CMPLXF(x + x, y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080085
Elliott Hughes8cff2f92015-08-28 20:21:43 -070086 if (ix < 0x7f800000) /* && iy >= 0x7f800000 */
87 return (CMPLXF(y - y, y - y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080088
Elliott Hughes8cff2f92015-08-28 20:21:43 -070089 if (ix == 0x7f800000) {
Elliott Hughesa0ee0782013-01-30 19:06:37 -080090 if (iy >= 0x7f800000)
Elliott Hughes8cff2f92015-08-28 20:21:43 -070091 return (CMPLXF(x, y - y));
92 return (CMPLXF(x * cosf(y), INFINITY * sinf(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080093 }
94
Elliott Hughesab528072018-07-24 00:01:52 +000095 return (CMPLXF(((long double)x + x) * (y - y),
96 ((long double)x * x) * (y - y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080097}
98
99float complex
100csinf(float complex z)
101{
102
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700103 z = csinhf(CMPLXF(cimagf(z), crealf(z)));
104 return (CMPLXF(cimagf(z), crealf(z)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800105}