xref: /reactos/sdk/lib/crt/math/libm_sse2/tanhf.c (revision 9e8ed3f8)
1 
2 /*******************************************************************************
3 MIT License
4 -----------
5 
6 Copyright (c) 2002-2019 Advanced Micro Devices, Inc.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this Software and associated documentaon files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 THE SOFTWARE.
25 *******************************************************************************/
26 
27 #include "libm.h"
28 #include "libm_util.h"
29 
30 #define USE_HANDLE_ERRORF
31 #define USE_SPLITEXPF
32 #define USE_SCALEFLOAT_2
33 #define USE_VALF_WITH_FLAGS
34 #include "libm_inlines.h"
35 #undef USE_SPLITEXPF
36 #undef USE_SCALEFLOAT_2
37 #undef USE_VALF_WITH_FLAGS
38 #undef USE_HANDLE_ERRORF
39 
40 #include "libm_errno.h"
41 
42 #ifdef _MSC_VER
43 // Disable "C4163: not available as intrinsic function" warning that older
44 // compilers may issue here.
45 #pragma warning(disable:4163)
46 #pragma function(tanhf)
47 #endif
48 
tanhf(float x)49 float tanhf(float x)
50 {
51   /*
52     The definition of tanh(x) is sinh(x)/cosh(x), which is also equivalent
53     to the following three formulae:
54     1.  (exp(x) - exp(-x))/(exp(x) + exp(-x))
55     2.  (1 - (2/(exp(2*x) + 1 )))
56     3.  (exp(2*x) - 1)/(exp(2*x) + 1)
57     but computationally, some formulae are better on some ranges.
58   */
59   static const float
60     thirtytwo_by_log2 =  4.6166240692e+01F, /* 0x4238aa3b */
61     log2_by_32_lead =  2.1659851074e-02F, /* 0x3cb17000 */
62     log2_by_32_tail =  9.9831822808e-07F, /* 0x3585fdf4 */
63     large_threshold = 10.0F; /* 0x41200000 */
64 
65   unsigned int ux, aux;
66   float y, z, p, z1, z2, xneg;
67   int m;
68 
69   /* Special cases */
70 
71   GET_BITS_SP32(x, ux);
72   aux = ux & ~SIGNBIT_SP32;
73   if (aux < 0x39000000) /* |x| small enough that tanh(x) = x */
74     {
75       if (aux == 0)
76         return x; /* with no inexact */
77       else
78         return valf_with_flags(x, AMD_F_INEXACT);
79     }
80   else if (aux > 0x7f800000) /* |x| is NaN */
81   {
82       unsigned int ufx;
83       GET_BITS_SP32(x, ufx);
84       return _handle_errorf("tanhf", OP_TANH, ufx|0x00400000, _DOMAIN, 0,
85                            EDOM, x, 0.0F, 1);
86   }
87 //    return x + x;
88 
89   xneg = 1.0F - 2.0F * (aux != ux);
90 
91   y = xneg * x;
92 
93   if (y > large_threshold)
94     {
95       /* If x is large then exp(-x) is negligible and
96          formula 1 reduces to plus or minus 1.0 */
97       z = 1.0F;
98     }
99   else if (y <= 1.0F)
100     {
101       float y2;
102       y2 = y*y;
103 
104       if (y < 0.9F)
105         {
106           /* Use a [2,1] Remez approximation on [0,0.9]. */
107           z = y + y*y2*
108             (-0.28192806108402678e0F +
109              (-0.14628356048797849e-2F +
110               0.4891631088530669873e-4F*y2)*y2)/
111             (0.845784192581041099e0F +
112              0.3427017942262751343e0F*y2);
113         }
114       else
115         {
116           /* Use a [2,1] Remez approximation on [0.9,1]. */
117           z = y + y*y2*
118             (-0.24069858695196524e0F +
119              (-0.12325644183611929e-2F +
120               0.3827534993599483396e-4F*y2)*y2)/
121             (0.72209738473684982e0F +
122              0.292529068698052819e0F*y2);
123         }
124     }
125   else
126     {
127       /* Compute p = exp(2*y) + 1. The code is basically inlined
128          from exp_amd. */
129 
130       splitexpf(2*y, 1.0F, thirtytwo_by_log2, log2_by_32_lead,
131 	       log2_by_32_tail, &m, &z1, &z2);
132       p = scaleFloat_2(z1 + z2, m) + 1.0F;
133       /* Now reconstruct tanh from p. */
134       z = (1.0F - 2.0F/p);
135     }
136 
137   return xneg * z;
138 }
139