xref: /freebsd/contrib/arm-optimized-routines/pl/math/v_tanh_3u.c (revision 072a4ba82a01476eaee33781ccd241033eefcf0b)
1*072a4ba8SAndrew Turner /*
2*072a4ba8SAndrew Turner  * Double-precision vector tanh(x) function.
3*072a4ba8SAndrew Turner  * Copyright (c) 2023, Arm Limited.
4*072a4ba8SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5*072a4ba8SAndrew Turner  */
6*072a4ba8SAndrew Turner 
7*072a4ba8SAndrew Turner #include "v_math.h"
8*072a4ba8SAndrew Turner #include "estrin.h"
9*072a4ba8SAndrew Turner #include "mathlib.h"
10*072a4ba8SAndrew Turner #include "pl_sig.h"
11*072a4ba8SAndrew Turner #include "pl_test.h"
12*072a4ba8SAndrew Turner 
13*072a4ba8SAndrew Turner #if V_SUPPORTED
14*072a4ba8SAndrew Turner 
15*072a4ba8SAndrew Turner #define AbsMask v_u64 (0x7fffffffffffffff)
16*072a4ba8SAndrew Turner #define InvLn2 v_f64 (0x1.71547652b82fep0)
17*072a4ba8SAndrew Turner #define MLn2hi v_f64 (-0x1.62e42fefa39efp-1)
18*072a4ba8SAndrew Turner #define MLn2lo v_f64 (-0x1.abc9e3b39803fp-56)
19*072a4ba8SAndrew Turner #define Shift v_f64 (0x1.8p52)
20*072a4ba8SAndrew Turner #define C(i) v_f64 (__expm1_poly[i])
21*072a4ba8SAndrew Turner 
22*072a4ba8SAndrew Turner #define BoringBound 0x403241bf835f9d5f /* asuint64 (0x1.241bf835f9d5fp+4).  */
23*072a4ba8SAndrew Turner #define TinyBound 0x3e40000000000000   /* asuint64 (0x1p-27).  */
24*072a4ba8SAndrew Turner #define One v_u64 (0x3ff0000000000000)
25*072a4ba8SAndrew Turner 
26*072a4ba8SAndrew Turner static inline v_f64_t
27*072a4ba8SAndrew Turner expm1_inline (v_f64_t x)
28*072a4ba8SAndrew Turner {
29*072a4ba8SAndrew Turner   /* Helper routine for calculating exp(x) - 1. Vector port of the helper from
30*072a4ba8SAndrew Turner      the scalar variant of tanh.  */
31*072a4ba8SAndrew Turner 
32*072a4ba8SAndrew Turner   /* Reduce argument: f in [-ln2/2, ln2/2], i is exact.  */
33*072a4ba8SAndrew Turner   v_f64_t j = v_fma_f64 (InvLn2, x, Shift) - Shift;
34*072a4ba8SAndrew Turner   v_s64_t i = v_to_s64_f64 (j);
35*072a4ba8SAndrew Turner   v_f64_t f = v_fma_f64 (j, MLn2hi, x);
36*072a4ba8SAndrew Turner   f = v_fma_f64 (j, MLn2lo, f);
37*072a4ba8SAndrew Turner 
38*072a4ba8SAndrew Turner   /* Approximate expm1(f) using polynomial.  */
39*072a4ba8SAndrew Turner   v_f64_t f2 = f * f;
40*072a4ba8SAndrew Turner   v_f64_t f4 = f2 * f2;
41*072a4ba8SAndrew Turner   v_f64_t p = v_fma_f64 (f2, ESTRIN_10 (f, f2, f4, f4 * f4, C), f);
42*072a4ba8SAndrew Turner 
43*072a4ba8SAndrew Turner   /* t = 2 ^ i.  */
44*072a4ba8SAndrew Turner   v_f64_t t = v_as_f64_u64 (v_as_u64_s64 (i << 52) + One);
45*072a4ba8SAndrew Turner   /* expm1(x) = p * t + (t - 1).  */
46*072a4ba8SAndrew Turner   return v_fma_f64 (p, t, t - 1);
47*072a4ba8SAndrew Turner }
48*072a4ba8SAndrew Turner 
49*072a4ba8SAndrew Turner static NOINLINE v_f64_t
50*072a4ba8SAndrew Turner special_case (v_f64_t x, v_f64_t y, v_u64_t special)
51*072a4ba8SAndrew Turner {
52*072a4ba8SAndrew Turner   return v_call_f64 (tanh, x, y, special);
53*072a4ba8SAndrew Turner }
54*072a4ba8SAndrew Turner 
55*072a4ba8SAndrew Turner /* Vector approximation for double-precision tanh(x), using a simplified
56*072a4ba8SAndrew Turner    version of expm1. The greatest observed error is 2.75 ULP:
57*072a4ba8SAndrew Turner    __v_tanh(-0x1.c143c3a44e087p-3) got -0x1.ba31ba4691ab7p-3
58*072a4ba8SAndrew Turner 				  want -0x1.ba31ba4691ab4p-3.  */
59*072a4ba8SAndrew Turner VPCS_ATTR v_f64_t V_NAME (tanh) (v_f64_t x)
60*072a4ba8SAndrew Turner {
61*072a4ba8SAndrew Turner   v_u64_t ix = v_as_u64_f64 (x);
62*072a4ba8SAndrew Turner   v_u64_t ia = ix & AbsMask;
63*072a4ba8SAndrew Turner 
64*072a4ba8SAndrew Turner   /* Trigger special-cases for tiny, boring and infinity/NaN.  */
65*072a4ba8SAndrew Turner   v_u64_t special = v_cond_u64 ((ia - TinyBound) > (BoringBound - TinyBound));
66*072a4ba8SAndrew Turner   v_f64_t u;
67*072a4ba8SAndrew Turner 
68*072a4ba8SAndrew Turner   /* To trigger fp exceptions correctly, set special lanes to a neutral value.
69*072a4ba8SAndrew Turner      They will be fixed up later by the special-case handler.  */
70*072a4ba8SAndrew Turner   if (unlikely (v_any_u64 (special)))
71*072a4ba8SAndrew Turner     u = v_sel_f64 (special, v_f64 (1), x) * 2;
72*072a4ba8SAndrew Turner   else
73*072a4ba8SAndrew Turner     u = x * 2;
74*072a4ba8SAndrew Turner 
75*072a4ba8SAndrew Turner   /* tanh(x) = (e^2x - 1) / (e^2x + 1).  */
76*072a4ba8SAndrew Turner   v_f64_t q = expm1_inline (u);
77*072a4ba8SAndrew Turner   v_f64_t y = q / (q + 2);
78*072a4ba8SAndrew Turner 
79*072a4ba8SAndrew Turner   if (unlikely (v_any_u64 (special)))
80*072a4ba8SAndrew Turner     return special_case (x, y, special);
81*072a4ba8SAndrew Turner   return y;
82*072a4ba8SAndrew Turner }
83*072a4ba8SAndrew Turner VPCS_ALIAS
84*072a4ba8SAndrew Turner 
85*072a4ba8SAndrew Turner PL_SIG (V, D, 1, tanh, -10.0, 10.0)
86*072a4ba8SAndrew Turner PL_TEST_ULP (V_NAME (tanh), 2.26)
87*072a4ba8SAndrew Turner PL_TEST_EXPECT_FENV_ALWAYS (V_NAME (tanh))
88*072a4ba8SAndrew Turner PL_TEST_INTERVAL (V_NAME (tanh), 0, TinyBound, 1000)
89*072a4ba8SAndrew Turner PL_TEST_INTERVAL (V_NAME (tanh), -0, -TinyBound, 1000)
90*072a4ba8SAndrew Turner PL_TEST_INTERVAL (V_NAME (tanh), TinyBound, BoringBound, 100000)
91*072a4ba8SAndrew Turner PL_TEST_INTERVAL (V_NAME (tanh), -TinyBound, -BoringBound, 100000)
92*072a4ba8SAndrew Turner PL_TEST_INTERVAL (V_NAME (tanh), BoringBound, inf, 1000)
93*072a4ba8SAndrew Turner PL_TEST_INTERVAL (V_NAME (tanh), -BoringBound, -inf, 1000)
94*072a4ba8SAndrew Turner #endif
95