]>
Commit | Line | Data |
---|---|---|
6e953631 UD |
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 | #if defined(LIBM_SCCS) && !defined(lint) | |
18 | static char rcsid[] = "$NetBSD: $"; | |
19 | #endif | |
20 | ||
21 | /* tanhl(x) | |
22 | * Return the Hyperbolic Tangent of x | |
23 | * | |
24 | * Method : | |
25 | * x -x | |
26 | * e - e | |
27 | * 0. tanhl(x) is defined to be ----------- | |
28 | * x -x | |
29 | * e + e | |
30 | * 1. reduce x to non-negative by tanhl(-x) = -tanhl(x). | |
31 | * 2. 0 <= x <= 2**-55 : tanhl(x) := x*(one+x) | |
32 | * -t | |
33 | * 2**-55 < x <= 1 : tanhl(x) := -----; t = expm1l(-2x) | |
34 | * t + 2 | |
35 | * 2 | |
36 | * 1 <= x <= 23.0 : tanhl(x) := 1- ----- ; t=expm1l(2x) | |
37 | * t + 2 | |
38 | * 23.0 < x <= INF : tanhl(x) := 1. | |
39 | * | |
40 | * Special cases: | |
41 | * tanhl(NaN) is NaN; | |
42 | * only tanhl(0)=0 is exact for finite argument. | |
43 | */ | |
44 | ||
37d83a08 | 45 | #include <float.h> |
1ed0291c RH |
46 | #include <math.h> |
47 | #include <math_private.h> | |
8f5b00d3 | 48 | #include <math-underflow.h> |
86f9568a | 49 | #include <libm-alias-ldouble.h> |
6e953631 | 50 | |
cccda09f | 51 | static const long double one=1.0, two=2.0, tiny = 1.0e-4900L; |
6e953631 | 52 | |
8db21882 | 53 | long double __tanhl(long double x) |
6e953631 UD |
54 | { |
55 | long double t,z; | |
cccda09f | 56 | int32_t se; |
24ab7723 | 57 | uint32_t j0,j1,ix; |
6e953631 UD |
58 | |
59 | /* High word of |x|. */ | |
60 | GET_LDOUBLE_WORDS(se,j0,j1,x); | |
61 | ix = se&0x7fff; | |
62 | ||
63 | /* x is INF or NaN */ | |
64 | if(ix==0x7fff) { | |
c57abfa7 UD |
65 | /* for NaN it's not important which branch: tanhl(NaN) = NaN */ |
66 | if (se&0x8000) return one/x-one; /* tanhl(-inf)= -1; */ | |
67 | else return one/x+one; /* tanhl(+inf)=+1 */ | |
6e953631 UD |
68 | } |
69 | ||
70 | /* |x| < 23 */ | |
cccda09f | 71 | if (ix < 0x4003 || (ix == 0x4003 && j0 < 0xb8000000u)) {/* |x|<23 */ |
c57abfa7 UD |
72 | if ((ix|j0|j1) == 0) |
73 | return x; /* x == +- 0 */ | |
9c3bb910 | 74 | if (ix<0x3fc8) /* |x|<2**-55 */ |
37d83a08 | 75 | { |
d96164c3 | 76 | math_check_force_underflow (x); |
20f421e1 | 77 | return x*(one+tiny); /* tanh(small) = small */ |
37d83a08 | 78 | } |
6e953631 UD |
79 | if (ix>=0x3fff) { /* |x|>=1 */ |
80 | t = __expm1l(two*fabsl(x)); | |
81 | z = one - two/(t+two); | |
82 | } else { | |
83 | t = __expm1l(-two*fabsl(x)); | |
84 | z= -t/(t+two); | |
85 | } | |
86 | /* |x| > 23, return +-1 */ | |
87 | } else { | |
88 | z = one - tiny; /* raised inexact flag */ | |
89 | } | |
9c3bb910 | 90 | return (se&0x8000)? -z: z; |
6e953631 | 91 | } |
86f9568a | 92 | libm_alias_ldouble (__tanh, tanh) |