1072a4ba8SAndrew Turner /*
2072a4ba8SAndrew Turner * Double-precision vector tanh(x) function.
3072a4ba8SAndrew Turner * Copyright (c) 2023, Arm Limited.
4072a4ba8SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5072a4ba8SAndrew Turner */
6072a4ba8SAndrew Turner
7072a4ba8SAndrew Turner #include "v_math.h"
8*5a02ffc3SAndrew Turner #include "poly_advsimd_f64.h"
9072a4ba8SAndrew Turner #include "mathlib.h"
10072a4ba8SAndrew Turner #include "pl_sig.h"
11072a4ba8SAndrew Turner #include "pl_test.h"
12072a4ba8SAndrew Turner
13*5a02ffc3SAndrew Turner static const struct data
14*5a02ffc3SAndrew Turner {
15*5a02ffc3SAndrew Turner float64x2_t poly[11];
16*5a02ffc3SAndrew Turner float64x2_t inv_ln2, ln2_hi, ln2_lo, shift;
17*5a02ffc3SAndrew Turner uint64x2_t onef;
18*5a02ffc3SAndrew Turner uint64x2_t thresh, tiny_bound;
19*5a02ffc3SAndrew Turner } data = {
20*5a02ffc3SAndrew Turner /* Generated using Remez, deg=12 in [-log(2)/2, log(2)/2]. */
21*5a02ffc3SAndrew Turner .poly = { V2 (0x1p-1), V2 (0x1.5555555555559p-3), V2 (0x1.555555555554bp-5),
22*5a02ffc3SAndrew Turner V2 (0x1.111111110f663p-7), V2 (0x1.6c16c16c1b5f3p-10),
23*5a02ffc3SAndrew Turner V2 (0x1.a01a01affa35dp-13), V2 (0x1.a01a018b4ecbbp-16),
24*5a02ffc3SAndrew Turner V2 (0x1.71ddf82db5bb4p-19), V2 (0x1.27e517fc0d54bp-22),
25*5a02ffc3SAndrew Turner V2 (0x1.af5eedae67435p-26), V2 (0x1.1f143d060a28ap-29), },
26072a4ba8SAndrew Turner
27*5a02ffc3SAndrew Turner .inv_ln2 = V2 (0x1.71547652b82fep0),
28*5a02ffc3SAndrew Turner .ln2_hi = V2 (-0x1.62e42fefa39efp-1),
29*5a02ffc3SAndrew Turner .ln2_lo = V2 (-0x1.abc9e3b39803fp-56),
30*5a02ffc3SAndrew Turner .shift = V2 (0x1.8p52),
31072a4ba8SAndrew Turner
32*5a02ffc3SAndrew Turner .onef = V2 (0x3ff0000000000000),
33*5a02ffc3SAndrew Turner .tiny_bound = V2 (0x3e40000000000000), /* asuint64 (0x1p-27). */
34*5a02ffc3SAndrew Turner /* asuint64(0x1.241bf835f9d5fp+4) - asuint64(tiny_bound). */
35*5a02ffc3SAndrew Turner .thresh = V2 (0x01f241bf835f9d5f),
36*5a02ffc3SAndrew Turner };
37072a4ba8SAndrew Turner
38*5a02ffc3SAndrew Turner static inline float64x2_t
expm1_inline(float64x2_t x,const struct data * d)39*5a02ffc3SAndrew Turner expm1_inline (float64x2_t x, const struct data *d)
40072a4ba8SAndrew Turner {
41072a4ba8SAndrew Turner /* Helper routine for calculating exp(x) - 1. Vector port of the helper from
42072a4ba8SAndrew Turner the scalar variant of tanh. */
43072a4ba8SAndrew Turner
44072a4ba8SAndrew Turner /* Reduce argument: f in [-ln2/2, ln2/2], i is exact. */
45*5a02ffc3SAndrew Turner float64x2_t j = vsubq_f64 (vfmaq_f64 (d->shift, d->inv_ln2, x), d->shift);
46*5a02ffc3SAndrew Turner int64x2_t i = vcvtq_s64_f64 (j);
47*5a02ffc3SAndrew Turner float64x2_t f = vfmaq_f64 (x, j, d->ln2_hi);
48*5a02ffc3SAndrew Turner f = vfmaq_f64 (f, j, d->ln2_lo);
49072a4ba8SAndrew Turner
50072a4ba8SAndrew Turner /* Approximate expm1(f) using polynomial. */
51*5a02ffc3SAndrew Turner float64x2_t f2 = vmulq_f64 (f, f);
52*5a02ffc3SAndrew Turner float64x2_t f4 = vmulq_f64 (f2, f2);
53*5a02ffc3SAndrew Turner float64x2_t p = vfmaq_f64 (
54*5a02ffc3SAndrew Turner f, f2, v_estrin_10_f64 (f, f2, f4, vmulq_f64 (f4, f4), d->poly));
55072a4ba8SAndrew Turner
56072a4ba8SAndrew Turner /* t = 2 ^ i. */
57*5a02ffc3SAndrew Turner float64x2_t t = vreinterpretq_f64_u64 (
58*5a02ffc3SAndrew Turner vaddq_u64 (vreinterpretq_u64_s64 (i << 52), d->onef));
59072a4ba8SAndrew Turner /* expm1(x) = p * t + (t - 1). */
60*5a02ffc3SAndrew Turner return vfmaq_f64 (vsubq_f64 (t, v_f64 (1)), p, t);
61072a4ba8SAndrew Turner }
62072a4ba8SAndrew Turner
63*5a02ffc3SAndrew Turner static float64x2_t NOINLINE VPCS_ATTR
special_case(float64x2_t x,float64x2_t y,uint64x2_t special)64*5a02ffc3SAndrew Turner special_case (float64x2_t x, float64x2_t y, uint64x2_t special)
65072a4ba8SAndrew Turner {
66072a4ba8SAndrew Turner return v_call_f64 (tanh, x, y, special);
67072a4ba8SAndrew Turner }
68072a4ba8SAndrew Turner
69072a4ba8SAndrew Turner /* Vector approximation for double-precision tanh(x), using a simplified
70*5a02ffc3SAndrew Turner version of expm1. The greatest observed error is 2.77 ULP:
71*5a02ffc3SAndrew Turner _ZGVnN2v_tanh(-0x1.c4a4ca0f9f3b7p-3) got -0x1.bd6a21a163627p-3
72*5a02ffc3SAndrew Turner want -0x1.bd6a21a163624p-3. */
V_NAME_D1(tanh)73*5a02ffc3SAndrew Turner float64x2_t VPCS_ATTR V_NAME_D1 (tanh) (float64x2_t x)
74072a4ba8SAndrew Turner {
75*5a02ffc3SAndrew Turner const struct data *d = ptr_barrier (&data);
76*5a02ffc3SAndrew Turner
77*5a02ffc3SAndrew Turner uint64x2_t ia = vreinterpretq_u64_f64 (vabsq_f64 (x));
78*5a02ffc3SAndrew Turner
79*5a02ffc3SAndrew Turner float64x2_t u = x;
80072a4ba8SAndrew Turner
81072a4ba8SAndrew Turner /* Trigger special-cases for tiny, boring and infinity/NaN. */
82*5a02ffc3SAndrew Turner uint64x2_t special = vcgtq_u64 (vsubq_u64 (ia, d->tiny_bound), d->thresh);
83*5a02ffc3SAndrew Turner #if WANT_SIMD_EXCEPT
84072a4ba8SAndrew Turner /* To trigger fp exceptions correctly, set special lanes to a neutral value.
85072a4ba8SAndrew Turner They will be fixed up later by the special-case handler. */
86072a4ba8SAndrew Turner if (unlikely (v_any_u64 (special)))
87*5a02ffc3SAndrew Turner u = v_zerofy_f64 (u, special);
88*5a02ffc3SAndrew Turner #endif
89*5a02ffc3SAndrew Turner
90*5a02ffc3SAndrew Turner u = vaddq_f64 (u, u);
91072a4ba8SAndrew Turner
92072a4ba8SAndrew Turner /* tanh(x) = (e^2x - 1) / (e^2x + 1). */
93*5a02ffc3SAndrew Turner float64x2_t q = expm1_inline (u, d);
94*5a02ffc3SAndrew Turner float64x2_t qp2 = vaddq_f64 (q, v_f64 (2));
95072a4ba8SAndrew Turner
96072a4ba8SAndrew Turner if (unlikely (v_any_u64 (special)))
97*5a02ffc3SAndrew Turner return special_case (x, vdivq_f64 (q, qp2), special);
98*5a02ffc3SAndrew Turner return vdivq_f64 (q, qp2);
99072a4ba8SAndrew Turner }
100072a4ba8SAndrew Turner
101072a4ba8SAndrew Turner PL_SIG (V, D, 1, tanh, -10.0, 10.0)
102*5a02ffc3SAndrew Turner PL_TEST_ULP (V_NAME_D1 (tanh), 2.27)
103*5a02ffc3SAndrew Turner PL_TEST_EXPECT_FENV (V_NAME_D1 (tanh), WANT_SIMD_EXCEPT)
104*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_D1 (tanh), 0, 0x1p-27, 5000)
105*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_D1 (tanh), 0x1p-27, 0x1.241bf835f9d5fp+4, 50000)
106*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_D1 (tanh), 0x1.241bf835f9d5fp+4, inf, 1000)
107