| 1 | /* s_tanhl.c -- long double version of s_tanh.c. |
| 2 | * Conversion to long double by Ulrich Drepper, |
| 3 | * Cygnus Support, drepper@cygnus.com. |
| 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * ==================================================== |
| 8 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
| 9 | * |
| 10 | * Developed at SunPro, a Sun Microsystems, Inc. business. |
| 11 | * Permission to use, copy, modify, and distribute this |
| 12 | * software is freely granted, provided that this notice |
| 13 | * is preserved. |
| 14 | * ==================================================== |
| 15 | */ |
| 16 | |
| 17 | /* Changes for 128-bit long double contributed by |
| 18 | Stephen L. Moshier <moshier@na-net.ornl.gov> */ |
| 19 | |
| 20 | /* tanhl(x) |
| 21 | * Return the Hyperbolic Tangent of x |
| 22 | * |
| 23 | * Method : |
| 24 | * x -x |
| 25 | * e - e |
| 26 | * 0. tanhl(x) is defined to be ----------- |
| 27 | * x -x |
| 28 | * e + e |
| 29 | * 1. reduce x to non-negative by tanhl(-x) = -tanhl(x). |
| 30 | * 2. 0 <= x <= 2**-57 : tanhl(x) := x*(one+x) |
| 31 | * -t |
| 32 | * 2**-57 < x <= 1 : tanhl(x) := -----; t = expm1l(-2x) |
| 33 | * t + 2 |
| 34 | * 2 |
| 35 | * 1 <= x <= 40.0 : tanhl(x) := 1- ----- ; t=expm1l(2x) |
| 36 | * t + 2 |
| 37 | * 40.0 < x <= INF : tanhl(x) := 1. |
| 38 | * |
| 39 | * Special cases: |
| 40 | * tanhl(NaN) is NaN; |
| 41 | * only tanhl(0)=0 is exact for finite argument. |
| 42 | */ |
| 43 | |
| 44 | #include <float.h> |
| 45 | #include <math.h> |
| 46 | #include <math_private.h> |
| 47 | |
| 48 | static const _Float128 one = 1.0, two = 2.0, tiny = L(1.0e-4900); |
| 49 | |
| 50 | _Float128 |
| 51 | __tanhl (_Float128 x) |
| 52 | { |
| 53 | _Float128 t, z; |
| 54 | u_int32_t jx, ix; |
| 55 | ieee854_long_double_shape_type u; |
| 56 | |
| 57 | /* Words of |x|. */ |
| 58 | u.value = x; |
| 59 | jx = u.parts32.w0; |
| 60 | ix = jx & 0x7fffffff; |
| 61 | /* x is INF or NaN */ |
| 62 | if (ix >= 0x7fff0000) |
| 63 | { |
| 64 | /* for NaN it's not important which branch: tanhl(NaN) = NaN */ |
| 65 | if (jx & 0x80000000) |
| 66 | return one / x - one; /* tanhl(-inf)= -1; */ |
| 67 | else |
| 68 | return one / x + one; /* tanhl(+inf)=+1 */ |
| 69 | } |
| 70 | |
| 71 | /* |x| < 40 */ |
| 72 | if (ix < 0x40044000) |
| 73 | { |
| 74 | if (u.value == 0) |
| 75 | return x; /* x == +- 0 */ |
| 76 | if (ix < 0x3fc60000) /* |x| < 2^-57 */ |
| 77 | { |
| 78 | math_check_force_underflow (x); |
| 79 | return x * (one + tiny); /* tanh(small) = small */ |
| 80 | } |
| 81 | u.parts32.w0 = ix; /* Absolute value of x. */ |
| 82 | if (ix >= 0x3fff0000) |
| 83 | { /* |x| >= 1 */ |
| 84 | t = __expm1l (two * u.value); |
| 85 | z = one - two / (t + two); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | t = __expm1l (-two * u.value); |
| 90 | z = -t / (t + two); |
| 91 | } |
| 92 | /* |x| > 40, return +-1 */ |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | z = one - tiny; /* raised inexact flag */ |
| 97 | } |
| 98 | return (jx & 0x80000000) ? -z : z; |
| 99 | } |
| 100 | weak_alias (__tanhl, tanhl) |
| 101 | |