blob: f0508909b57901ffcfc6203bf351a75408d24477 [file] [log] [blame]
Elliott Hughesa0ee0782013-01-30 19:06:37 -08001/*-
2 * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27/*
Elliott Hughes8cff2f92015-08-28 20:21:43 -070028 * Float version of csinh(). See s_csinh.c for details.
Elliott Hughesa0ee0782013-01-30 19:06:37 -080029 */
30
31#include <sys/cdefs.h>
Elliott Hughes8cff2f92015-08-28 20:21:43 -070032__FBSDID("$FreeBSD: head/lib/msun/src/s_csinhf.c 284426 2015-06-15 20:16:53Z tijl $");
Elliott Hughesa0ee0782013-01-30 19:06:37 -080033
34#include <complex.h>
35#include <math.h>
36
37#include "math_private.h"
38
39static const float huge = 0x1p127;
40
41float complex
42csinhf(float complex z)
43{
44 float x, y, h;
45 int32_t hx, hy, ix, iy;
46
47 x = crealf(z);
48 y = cimagf(z);
49
50 GET_FLOAT_WORD(hx, x);
51 GET_FLOAT_WORD(hy, y);
52
53 ix = 0x7fffffff & hx;
54 iy = 0x7fffffff & hy;
55
56 if (ix < 0x7f800000 && iy < 0x7f800000) {
57 if (iy == 0)
Elliott Hughes8cff2f92015-08-28 20:21:43 -070058 return (CMPLXF(sinhf(x), y));
59 if (ix < 0x41100000) /* |x| < 9: normal case */
60 return (CMPLXF(sinhf(x) * cosf(y), coshf(x) * sinf(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080061
62 /* |x| >= 9, so cosh(x) ~= exp(|x|) */
63 if (ix < 0x42b17218) {
64 /* x < 88.7: expf(|x|) won't overflow */
Elliott Hughes8cff2f92015-08-28 20:21:43 -070065 h = expf(fabsf(x)) * 0.5F;
66 return (CMPLXF(copysignf(h, x) * cosf(y), h * sinf(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080067 } else if (ix < 0x4340b1e7) {
68 /* x < 192.7: scale to avoid overflow */
Elliott Hughes8cff2f92015-08-28 20:21:43 -070069 z = __ldexp_cexpf(CMPLXF(fabsf(x), y), -1);
70 return (CMPLXF(crealf(z) * copysignf(1, x), cimagf(z)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080071 } else {
72 /* x >= 192.7: the result always overflows */
73 h = huge * x;
Elliott Hughes8cff2f92015-08-28 20:21:43 -070074 return (CMPLXF(h * cosf(y), h * h * sinf(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080075 }
76 }
77
Elliott Hughes8cff2f92015-08-28 20:21:43 -070078 if (ix == 0) /* && iy >= 0x7f800000 */
79 return (CMPLXF(x, y - y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080080
Elliott Hughes8cff2f92015-08-28 20:21:43 -070081 if (iy == 0) /* && ix >= 0x7f800000 */
82 return (CMPLXF(x + x, y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080083
Elliott Hughes8cff2f92015-08-28 20:21:43 -070084 if (ix < 0x7f800000) /* && iy >= 0x7f800000 */
85 return (CMPLXF(y - y, y - y));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080086
Elliott Hughes8cff2f92015-08-28 20:21:43 -070087 if (ix == 0x7f800000) {
Elliott Hughesa0ee0782013-01-30 19:06:37 -080088 if (iy >= 0x7f800000)
Elliott Hughes8cff2f92015-08-28 20:21:43 -070089 return (CMPLXF(x, y - y));
90 return (CMPLXF(x * cosf(y), INFINITY * sinf(y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080091 }
92
Elliott Hughes8cff2f92015-08-28 20:21:43 -070093 return (CMPLXF((x + x) * (y - y), (x * x) * (y - y)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -080094}
95
96float complex
97csinf(float complex z)
98{
99
Elliott Hughes8cff2f92015-08-28 20:21:43 -0700100 z = csinhf(CMPLXF(cimagf(z), crealf(z)));
101 return (CMPLXF(cimagf(z), crealf(z)));
Elliott Hughesa0ee0782013-01-30 19:06:37 -0800102}