Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 1 | /*- |
Elliott Hughes | c1e46b6 | 2023-07-19 14:06:31 -0700 | [diff] [blame] | 2 | * SPDX-License-Identifier: BSD-2-Clause |
Elliott Hughes | 8da8ca4 | 2018-05-08 13:35:33 -0700 | [diff] [blame] | 3 | * |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 4 | * 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 Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 30 | * Float version of csinh(). See s_csinh.c for details. |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 31 | */ |
| 32 | |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 33 | #include <complex.h> |
| 34 | #include <math.h> |
| 35 | |
| 36 | #include "math_private.h" |
| 37 | |
| 38 | static const float huge = 0x1p127; |
| 39 | |
| 40 | float complex |
| 41 | csinhf(float complex z) |
| 42 | { |
| 43 | float x, y, h; |
| 44 | int32_t hx, hy, ix, iy; |
| 45 | |
| 46 | x = crealf(z); |
| 47 | y = cimagf(z); |
| 48 | |
| 49 | GET_FLOAT_WORD(hx, x); |
| 50 | GET_FLOAT_WORD(hy, y); |
| 51 | |
| 52 | ix = 0x7fffffff & hx; |
| 53 | iy = 0x7fffffff & hy; |
| 54 | |
| 55 | if (ix < 0x7f800000 && iy < 0x7f800000) { |
| 56 | if (iy == 0) |
Elliott Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 57 | return (CMPLXF(sinhf(x), y)); |
| 58 | if (ix < 0x41100000) /* |x| < 9: normal case */ |
| 59 | return (CMPLXF(sinhf(x) * cosf(y), coshf(x) * sinf(y))); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 60 | |
| 61 | /* |x| >= 9, so cosh(x) ~= exp(|x|) */ |
| 62 | if (ix < 0x42b17218) { |
| 63 | /* x < 88.7: expf(|x|) won't overflow */ |
Elliott Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 64 | h = expf(fabsf(x)) * 0.5F; |
| 65 | return (CMPLXF(copysignf(h, x) * cosf(y), h * sinf(y))); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 66 | } else if (ix < 0x4340b1e7) { |
| 67 | /* x < 192.7: scale to avoid overflow */ |
Elliott Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 68 | z = __ldexp_cexpf(CMPLXF(fabsf(x), y), -1); |
| 69 | return (CMPLXF(crealf(z) * copysignf(1, x), cimagf(z))); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 70 | } else { |
| 71 | /* x >= 192.7: the result always overflows */ |
| 72 | h = huge * x; |
Elliott Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 73 | return (CMPLXF(h * cosf(y), h * h * sinf(y))); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Elliott Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 77 | if (ix == 0) /* && iy >= 0x7f800000 */ |
| 78 | return (CMPLXF(x, y - y)); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 79 | |
Elliott Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 80 | if (iy == 0) /* && ix >= 0x7f800000 */ |
| 81 | return (CMPLXF(x + x, y)); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 82 | |
Elliott Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 83 | if (ix < 0x7f800000) /* && iy >= 0x7f800000 */ |
| 84 | return (CMPLXF(y - y, y - y)); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 85 | |
Elliott Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 86 | if (ix == 0x7f800000) { |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 87 | if (iy >= 0x7f800000) |
Elliott Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 88 | return (CMPLXF(x, y - y)); |
| 89 | return (CMPLXF(x * cosf(y), INFINITY * sinf(y))); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Elliott Hughes | ab52807 | 2018-07-24 00:01:52 +0000 | [diff] [blame] | 92 | return (CMPLXF(((long double)x + x) * (y - y), |
| 93 | ((long double)x * x) * (y - y))); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | float complex |
| 97 | csinf(float complex z) |
| 98 | { |
| 99 | |
Elliott Hughes | 8cff2f9 | 2015-08-28 20:21:43 -0700 | [diff] [blame] | 100 | z = csinhf(CMPLXF(cimagf(z), crealf(z))); |
| 101 | return (CMPLXF(cimagf(z), crealf(z))); |
Elliott Hughes | a0ee078 | 2013-01-30 19:06:37 -0800 | [diff] [blame] | 102 | } |