1*412f47f9SXin Li /*
2*412f47f9SXin Li * Double-precision vector tanh(x) function.
3*412f47f9SXin Li * Copyright (c) 2023, Arm Limited.
4*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5*412f47f9SXin Li */
6*412f47f9SXin Li
7*412f47f9SXin Li #include "v_math.h"
8*412f47f9SXin Li #include "poly_advsimd_f64.h"
9*412f47f9SXin Li #include "mathlib.h"
10*412f47f9SXin Li #include "pl_sig.h"
11*412f47f9SXin Li #include "pl_test.h"
12*412f47f9SXin Li
13*412f47f9SXin Li static const struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li float64x2_t poly[11];
16*412f47f9SXin Li float64x2_t inv_ln2, ln2_hi, ln2_lo, shift;
17*412f47f9SXin Li uint64x2_t onef;
18*412f47f9SXin Li uint64x2_t thresh, tiny_bound;
19*412f47f9SXin Li } data = {
20*412f47f9SXin Li /* Generated using Remez, deg=12 in [-log(2)/2, log(2)/2]. */
21*412f47f9SXin Li .poly = { V2 (0x1p-1), V2 (0x1.5555555555559p-3), V2 (0x1.555555555554bp-5),
22*412f47f9SXin Li V2 (0x1.111111110f663p-7), V2 (0x1.6c16c16c1b5f3p-10),
23*412f47f9SXin Li V2 (0x1.a01a01affa35dp-13), V2 (0x1.a01a018b4ecbbp-16),
24*412f47f9SXin Li V2 (0x1.71ddf82db5bb4p-19), V2 (0x1.27e517fc0d54bp-22),
25*412f47f9SXin Li V2 (0x1.af5eedae67435p-26), V2 (0x1.1f143d060a28ap-29), },
26*412f47f9SXin Li
27*412f47f9SXin Li .inv_ln2 = V2 (0x1.71547652b82fep0),
28*412f47f9SXin Li .ln2_hi = V2 (-0x1.62e42fefa39efp-1),
29*412f47f9SXin Li .ln2_lo = V2 (-0x1.abc9e3b39803fp-56),
30*412f47f9SXin Li .shift = V2 (0x1.8p52),
31*412f47f9SXin Li
32*412f47f9SXin Li .onef = V2 (0x3ff0000000000000),
33*412f47f9SXin Li .tiny_bound = V2 (0x3e40000000000000), /* asuint64 (0x1p-27). */
34*412f47f9SXin Li /* asuint64(0x1.241bf835f9d5fp+4) - asuint64(tiny_bound). */
35*412f47f9SXin Li .thresh = V2 (0x01f241bf835f9d5f),
36*412f47f9SXin Li };
37*412f47f9SXin Li
38*412f47f9SXin Li static inline float64x2_t
expm1_inline(float64x2_t x,const struct data * d)39*412f47f9SXin Li expm1_inline (float64x2_t x, const struct data *d)
40*412f47f9SXin Li {
41*412f47f9SXin Li /* Helper routine for calculating exp(x) - 1. Vector port of the helper from
42*412f47f9SXin Li the scalar variant of tanh. */
43*412f47f9SXin Li
44*412f47f9SXin Li /* Reduce argument: f in [-ln2/2, ln2/2], i is exact. */
45*412f47f9SXin Li float64x2_t j = vsubq_f64 (vfmaq_f64 (d->shift, d->inv_ln2, x), d->shift);
46*412f47f9SXin Li int64x2_t i = vcvtq_s64_f64 (j);
47*412f47f9SXin Li float64x2_t f = vfmaq_f64 (x, j, d->ln2_hi);
48*412f47f9SXin Li f = vfmaq_f64 (f, j, d->ln2_lo);
49*412f47f9SXin Li
50*412f47f9SXin Li /* Approximate expm1(f) using polynomial. */
51*412f47f9SXin Li float64x2_t f2 = vmulq_f64 (f, f);
52*412f47f9SXin Li float64x2_t f4 = vmulq_f64 (f2, f2);
53*412f47f9SXin Li float64x2_t p = vfmaq_f64 (
54*412f47f9SXin Li f, f2, v_estrin_10_f64 (f, f2, f4, vmulq_f64 (f4, f4), d->poly));
55*412f47f9SXin Li
56*412f47f9SXin Li /* t = 2 ^ i. */
57*412f47f9SXin Li float64x2_t t = vreinterpretq_f64_u64 (
58*412f47f9SXin Li vaddq_u64 (vreinterpretq_u64_s64 (i << 52), d->onef));
59*412f47f9SXin Li /* expm1(x) = p * t + (t - 1). */
60*412f47f9SXin Li return vfmaq_f64 (vsubq_f64 (t, v_f64 (1)), p, t);
61*412f47f9SXin Li }
62*412f47f9SXin Li
63*412f47f9SXin Li static float64x2_t NOINLINE VPCS_ATTR
special_case(float64x2_t x,float64x2_t y,uint64x2_t special)64*412f47f9SXin Li special_case (float64x2_t x, float64x2_t y, uint64x2_t special)
65*412f47f9SXin Li {
66*412f47f9SXin Li return v_call_f64 (tanh, x, y, special);
67*412f47f9SXin Li }
68*412f47f9SXin Li
69*412f47f9SXin Li /* Vector approximation for double-precision tanh(x), using a simplified
70*412f47f9SXin Li version of expm1. The greatest observed error is 2.77 ULP:
71*412f47f9SXin Li _ZGVnN2v_tanh(-0x1.c4a4ca0f9f3b7p-3) got -0x1.bd6a21a163627p-3
72*412f47f9SXin Li want -0x1.bd6a21a163624p-3. */
V_NAME_D1(tanh)73*412f47f9SXin Li float64x2_t VPCS_ATTR V_NAME_D1 (tanh) (float64x2_t x)
74*412f47f9SXin Li {
75*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
76*412f47f9SXin Li
77*412f47f9SXin Li uint64x2_t ia = vreinterpretq_u64_f64 (vabsq_f64 (x));
78*412f47f9SXin Li
79*412f47f9SXin Li float64x2_t u = x;
80*412f47f9SXin Li
81*412f47f9SXin Li /* Trigger special-cases for tiny, boring and infinity/NaN. */
82*412f47f9SXin Li uint64x2_t special = vcgtq_u64 (vsubq_u64 (ia, d->tiny_bound), d->thresh);
83*412f47f9SXin Li #if WANT_SIMD_EXCEPT
84*412f47f9SXin Li /* To trigger fp exceptions correctly, set special lanes to a neutral value.
85*412f47f9SXin Li They will be fixed up later by the special-case handler. */
86*412f47f9SXin Li if (unlikely (v_any_u64 (special)))
87*412f47f9SXin Li u = v_zerofy_f64 (u, special);
88*412f47f9SXin Li #endif
89*412f47f9SXin Li
90*412f47f9SXin Li u = vaddq_f64 (u, u);
91*412f47f9SXin Li
92*412f47f9SXin Li /* tanh(x) = (e^2x - 1) / (e^2x + 1). */
93*412f47f9SXin Li float64x2_t q = expm1_inline (u, d);
94*412f47f9SXin Li float64x2_t qp2 = vaddq_f64 (q, v_f64 (2));
95*412f47f9SXin Li
96*412f47f9SXin Li if (unlikely (v_any_u64 (special)))
97*412f47f9SXin Li return special_case (x, vdivq_f64 (q, qp2), special);
98*412f47f9SXin Li return vdivq_f64 (q, qp2);
99*412f47f9SXin Li }
100*412f47f9SXin Li
101*412f47f9SXin Li PL_SIG (V, D, 1, tanh, -10.0, 10.0)
102*412f47f9SXin Li PL_TEST_ULP (V_NAME_D1 (tanh), 2.27)
103*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_D1 (tanh), WANT_SIMD_EXCEPT)
104*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (tanh), 0, 0x1p-27, 5000)
105*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (tanh), 0x1p-27, 0x1.241bf835f9d5fp+4, 50000)
106*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (tanh), 0x1.241bf835f9d5fp+4, inf, 1000)
107