2.9
[glibc/nacl-glibc.git] / sysdeps / ieee754 / ldbl-128 / s_tanhl.c
blobfcbf300a10d9a52847cb7c56bde49a45d6d6b5c3
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 */
6 /*
7 * ====================================================
8 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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 * ====================================================
17 /* Changes for 128-bit long double contributed by
18 Stephen L. Moshier <moshier@na-net.ornl.gov> */
20 /* tanhl(x)
21 * Return the Hyperbolic Tangent of x
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.
39 * Special cases:
40 * tanhl(NaN) is NaN;
41 * only tanhl(0)=0 is exact for finite argument.
44 #include "math.h"
45 #include "math_private.h"
47 #ifdef __STDC__
48 static const long double one = 1.0, two = 2.0, tiny = 1.0e-4900L;
49 #else
50 static long double one = 1.0, two = 2.0, tiny = 1.0e-4900L;
51 #endif
53 #ifdef __STDC__
54 long double
55 __tanhl (long double x)
56 #else
57 long double
58 __tanhl (x)
59 long double x;
60 #endif
62 long double t, z;
63 u_int32_t jx, ix;
64 ieee854_long_double_shape_type u;
66 /* Words of |x|. */
67 u.value = x;
68 jx = u.parts32.w0;
69 ix = jx & 0x7fffffff;
70 /* x is INF or NaN */
71 if (ix >= 0x7fff0000)
73 /* for NaN it's not important which branch: tanhl(NaN) = NaN */
74 if (jx & 0x80000000)
75 return one / x - one; /* tanhl(-inf)= -1; */
76 else
77 return one / x + one; /* tanhl(+inf)=+1 */
80 /* |x| < 40 */
81 if (ix < 0x40044000)
83 if (u.value == 0)
84 return x; /* x == +- 0 */
85 if (ix < 0x3fc60000) /* |x| < 2^-57 */
86 return x * (one + tiny); /* tanh(small) = small */
87 u.parts32.w0 = ix; /* Absolute value of x. */
88 if (ix >= 0x3fff0000)
89 { /* |x| >= 1 */
90 t = __expm1l (two * u.value);
91 z = one - two / (t + two);
93 else
95 t = __expm1l (-two * u.value);
96 z = -t / (t + two);
98 /* |x| > 40, return +-1 */
100 else
102 z = one - tiny; /* raised inexact flag */
104 return (jx & 0x80000000) ? -z : z;
106 weak_alias (__tanhl, tanhl)