xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/experimental/tanh_3u.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-precision tanh(x) function.
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 2023-2024, Arm Limited.
5*f3087befSAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner  */
7*f3087befSAndrew Turner #include "math_config.h"
8*f3087befSAndrew Turner #include "poly_scalar_f64.h"
9*f3087befSAndrew Turner #include "test_sig.h"
10*f3087befSAndrew Turner #include "test_defs.h"
11*f3087befSAndrew Turner 
12*f3087befSAndrew Turner #define AbsMask 0x7fffffffffffffff
13*f3087befSAndrew Turner #define InvLn2 0x1.71547652b82fep0
14*f3087befSAndrew Turner #define Ln2hi 0x1.62e42fefa39efp-1
15*f3087befSAndrew Turner #define Ln2lo 0x1.abc9e3b39803fp-56
16*f3087befSAndrew Turner #define Shift 0x1.8p52
17*f3087befSAndrew Turner 
18*f3087befSAndrew Turner /* asuint64 (0x1.241bf835f9d5fp+4).  */
19*f3087befSAndrew Turner #define BoringBound 0x403241bf835f9d5f
20*f3087befSAndrew Turner /* asuint64 (0x1p-27).  */
21*f3087befSAndrew Turner #define TinyBound 0x3e40000000000000
22*f3087befSAndrew Turner #define One 0x3ff0000000000000
23*f3087befSAndrew Turner 
24*f3087befSAndrew Turner static inline double
expm1_inline(double x)25*f3087befSAndrew Turner expm1_inline (double x)
26*f3087befSAndrew Turner {
27*f3087befSAndrew Turner   /* Helper routine for calculating exp(x) - 1. Copied from expm1_2u5.c, with
28*f3087befSAndrew Turner      several simplifications:
29*f3087befSAndrew Turner      - No special-case handling for tiny or special values.
30*f3087befSAndrew Turner      - Simpler combination of p and t in final stage of the algorithm.
31*f3087befSAndrew Turner      - Use shift-and-add instead of ldexp to calculate t.  */
32*f3087befSAndrew Turner 
33*f3087befSAndrew Turner   /* Reduce argument: f in [-ln2/2, ln2/2], i is exact.  */
34*f3087befSAndrew Turner   double j = fma (InvLn2, x, Shift) - Shift;
35*f3087befSAndrew Turner   int64_t i = j;
36*f3087befSAndrew Turner   double f = fma (j, -Ln2hi, x);
37*f3087befSAndrew Turner   f = fma (j, -Ln2lo, f);
38*f3087befSAndrew Turner 
39*f3087befSAndrew Turner   /* Approximate expm1(f) using polynomial.  */
40*f3087befSAndrew Turner   double f2 = f * f;
41*f3087befSAndrew Turner   double f4 = f2 * f2;
42*f3087befSAndrew Turner   double p = fma (f2, estrin_10_f64 (f, f2, f4, f4 * f4, __expm1_poly), f);
43*f3087befSAndrew Turner 
44*f3087befSAndrew Turner   /* t = 2 ^ i.  */
45*f3087befSAndrew Turner   double t = asdouble ((uint64_t) (i + 1023) << 52);
46*f3087befSAndrew Turner   /* expm1(x) = p * t + (t - 1).  */
47*f3087befSAndrew Turner   return fma (p, t, t - 1);
48*f3087befSAndrew Turner }
49*f3087befSAndrew Turner 
50*f3087befSAndrew Turner /* Approximation for double-precision tanh(x), using a simplified version of
51*f3087befSAndrew Turner    expm1. The greatest observed error is 2.77 ULP:
52*f3087befSAndrew Turner    tanh(-0x1.c4a4ca0f9f3b7p-3) got -0x1.bd6a21a163627p-3
53*f3087befSAndrew Turner 			      want -0x1.bd6a21a163624p-3.  */
54*f3087befSAndrew Turner double
tanh(double x)55*f3087befSAndrew Turner tanh (double x)
56*f3087befSAndrew Turner {
57*f3087befSAndrew Turner   uint64_t ix = asuint64 (x);
58*f3087befSAndrew Turner   uint64_t ia = ix & AbsMask;
59*f3087befSAndrew Turner   uint64_t sign = ix & ~AbsMask;
60*f3087befSAndrew Turner 
61*f3087befSAndrew Turner   if (unlikely (ia > BoringBound))
62*f3087befSAndrew Turner     {
63*f3087befSAndrew Turner       if (ia > 0x7ff0000000000000)
64*f3087befSAndrew Turner 	return __math_invalid (x);
65*f3087befSAndrew Turner       return asdouble (One | sign);
66*f3087befSAndrew Turner     }
67*f3087befSAndrew Turner 
68*f3087befSAndrew Turner   if (unlikely (ia < TinyBound))
69*f3087befSAndrew Turner     return x;
70*f3087befSAndrew Turner 
71*f3087befSAndrew Turner   /* tanh(x) = (e^2x - 1) / (e^2x + 1).  */
72*f3087befSAndrew Turner   double q = expm1_inline (2 * x);
73*f3087befSAndrew Turner   return q / (q + 2);
74*f3087befSAndrew Turner }
75*f3087befSAndrew Turner 
76*f3087befSAndrew Turner TEST_SIG (S, D, 1, tanh, -10.0, 10.0)
77*f3087befSAndrew Turner TEST_ULP (tanh, 2.27)
78*f3087befSAndrew Turner TEST_SYM_INTERVAL (tanh, 0, TinyBound, 1000)
79*f3087befSAndrew Turner TEST_SYM_INTERVAL (tanh, TinyBound, BoringBound, 100000)
80*f3087befSAndrew Turner TEST_SYM_INTERVAL (tanh, BoringBound, inf, 1000)
81