1*5a02ffc3SAndrew Turner /*
2*5a02ffc3SAndrew Turner * Double-precision SVE tanh(x) function.
3*5a02ffc3SAndrew Turner * Copyright (c) 2023, Arm Limited.
4*5a02ffc3SAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
5*5a02ffc3SAndrew Turner */
6*5a02ffc3SAndrew Turner
7*5a02ffc3SAndrew Turner #include "sv_math.h"
8*5a02ffc3SAndrew Turner #include "poly_sve_f64.h"
9*5a02ffc3SAndrew Turner #include "mathlib.h"
10*5a02ffc3SAndrew Turner #include "pl_sig.h"
11*5a02ffc3SAndrew Turner #include "pl_test.h"
12*5a02ffc3SAndrew Turner
13*5a02ffc3SAndrew Turner static const struct data
14*5a02ffc3SAndrew Turner {
15*5a02ffc3SAndrew Turner float64_t poly[11];
16*5a02ffc3SAndrew Turner float64_t inv_ln2, ln2_hi, ln2_lo, shift;
17*5a02ffc3SAndrew Turner uint64_t thresh, tiny_bound;
18*5a02ffc3SAndrew Turner } data = {
19*5a02ffc3SAndrew Turner /* Generated using Remez, deg=12 in [-log(2)/2, log(2)/2]. */
20*5a02ffc3SAndrew Turner .poly = { 0x1p-1, 0x1.5555555555559p-3, 0x1.555555555554bp-5,
21*5a02ffc3SAndrew Turner 0x1.111111110f663p-7, 0x1.6c16c16c1b5f3p-10,
22*5a02ffc3SAndrew Turner 0x1.a01a01affa35dp-13, 0x1.a01a018b4ecbbp-16,
23*5a02ffc3SAndrew Turner 0x1.71ddf82db5bb4p-19, 0x1.27e517fc0d54bp-22,
24*5a02ffc3SAndrew Turner 0x1.af5eedae67435p-26, 0x1.1f143d060a28ap-29, },
25*5a02ffc3SAndrew Turner
26*5a02ffc3SAndrew Turner .inv_ln2 = 0x1.71547652b82fep0,
27*5a02ffc3SAndrew Turner .ln2_hi = -0x1.62e42fefa39efp-1,
28*5a02ffc3SAndrew Turner .ln2_lo = -0x1.abc9e3b39803fp-56,
29*5a02ffc3SAndrew Turner .shift = 0x1.8p52,
30*5a02ffc3SAndrew Turner
31*5a02ffc3SAndrew Turner .tiny_bound = 0x3e40000000000000, /* asuint64 (0x1p-27). */
32*5a02ffc3SAndrew Turner /* asuint64(0x1.241bf835f9d5fp+4) - asuint64(tiny_bound). */
33*5a02ffc3SAndrew Turner .thresh = 0x01f241bf835f9d5f,
34*5a02ffc3SAndrew Turner };
35*5a02ffc3SAndrew Turner
36*5a02ffc3SAndrew Turner static inline svfloat64_t
expm1_inline(svfloat64_t x,const svbool_t pg,const struct data * d)37*5a02ffc3SAndrew Turner expm1_inline (svfloat64_t x, const svbool_t pg, const struct data *d)
38*5a02ffc3SAndrew Turner {
39*5a02ffc3SAndrew Turner /* Helper routine for calculating exp(x) - 1. Vector port of the helper from
40*5a02ffc3SAndrew Turner the scalar variant of tanh. */
41*5a02ffc3SAndrew Turner
42*5a02ffc3SAndrew Turner /* Reduce argument: f in [-ln2/2, ln2/2], i is exact. */
43*5a02ffc3SAndrew Turner svfloat64_t j
44*5a02ffc3SAndrew Turner = svsub_x (pg, svmla_x (pg, sv_f64 (d->shift), x, d->inv_ln2), d->shift);
45*5a02ffc3SAndrew Turner svint64_t i = svcvt_s64_x (pg, j);
46*5a02ffc3SAndrew Turner svfloat64_t f = svmla_x (pg, x, j, d->ln2_hi);
47*5a02ffc3SAndrew Turner f = svmla_x (pg, f, j, d->ln2_lo);
48*5a02ffc3SAndrew Turner
49*5a02ffc3SAndrew Turner /* Approximate expm1(f) using polynomial. */
50*5a02ffc3SAndrew Turner svfloat64_t f2 = svmul_x (pg, f, f);
51*5a02ffc3SAndrew Turner svfloat64_t f4 = svmul_x (pg, f2, f2);
52*5a02ffc3SAndrew Turner svfloat64_t p = svmla_x (
53*5a02ffc3SAndrew Turner pg, f, f2,
54*5a02ffc3SAndrew Turner sv_estrin_10_f64_x (pg, f, f2, f4, svmul_x (pg, f4, f4), d->poly));
55*5a02ffc3SAndrew Turner
56*5a02ffc3SAndrew Turner /* t = 2 ^ i. */
57*5a02ffc3SAndrew Turner svfloat64_t t = svscale_x (pg, sv_f64 (1), i);
58*5a02ffc3SAndrew Turner /* expm1(x) = p * t + (t - 1). */
59*5a02ffc3SAndrew Turner return svmla_x (pg, svsub_x (pg, t, 1), p, t);
60*5a02ffc3SAndrew Turner }
61*5a02ffc3SAndrew Turner
62*5a02ffc3SAndrew Turner static svfloat64_t NOINLINE
special_case(svfloat64_t x,svfloat64_t y,svbool_t special)63*5a02ffc3SAndrew Turner special_case (svfloat64_t x, svfloat64_t y, svbool_t special)
64*5a02ffc3SAndrew Turner {
65*5a02ffc3SAndrew Turner return sv_call_f64 (tanh, x, y, special);
66*5a02ffc3SAndrew Turner }
67*5a02ffc3SAndrew Turner
68*5a02ffc3SAndrew Turner /* SVE approximation for double-precision tanh(x), using a simplified
69*5a02ffc3SAndrew Turner version of expm1. The greatest observed error is 2.77 ULP:
70*5a02ffc3SAndrew Turner _ZGVsMxv_tanh(-0x1.c4a4ca0f9f3b7p-3) got -0x1.bd6a21a163627p-3
71*5a02ffc3SAndrew Turner want -0x1.bd6a21a163624p-3. */
SV_NAME_D1(tanh)72*5a02ffc3SAndrew Turner svfloat64_t SV_NAME_D1 (tanh) (svfloat64_t x, svbool_t pg)
73*5a02ffc3SAndrew Turner {
74*5a02ffc3SAndrew Turner const struct data *d = ptr_barrier (&data);
75*5a02ffc3SAndrew Turner
76*5a02ffc3SAndrew Turner svuint64_t ia = svreinterpret_u64 (svabs_x (pg, x));
77*5a02ffc3SAndrew Turner
78*5a02ffc3SAndrew Turner /* Trigger special-cases for tiny, boring and infinity/NaN. */
79*5a02ffc3SAndrew Turner svbool_t special = svcmpgt (pg, svsub_x (pg, ia, d->tiny_bound), d->thresh);
80*5a02ffc3SAndrew Turner
81*5a02ffc3SAndrew Turner svfloat64_t u = svadd_x (pg, x, x);
82*5a02ffc3SAndrew Turner
83*5a02ffc3SAndrew Turner /* tanh(x) = (e^2x - 1) / (e^2x + 1). */
84*5a02ffc3SAndrew Turner svfloat64_t q = expm1_inline (u, pg, d);
85*5a02ffc3SAndrew Turner svfloat64_t qp2 = svadd_x (pg, q, 2);
86*5a02ffc3SAndrew Turner
87*5a02ffc3SAndrew Turner if (unlikely (svptest_any (pg, special)))
88*5a02ffc3SAndrew Turner return special_case (x, svdiv_x (pg, q, qp2), special);
89*5a02ffc3SAndrew Turner return svdiv_x (pg, q, qp2);
90*5a02ffc3SAndrew Turner }
91*5a02ffc3SAndrew Turner
92*5a02ffc3SAndrew Turner PL_SIG (SV, D, 1, tanh, -10.0, 10.0)
93*5a02ffc3SAndrew Turner PL_TEST_ULP (SV_NAME_D1 (tanh), 2.27)
94*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_D1 (tanh), 0, 0x1p-27, 5000)
95*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_D1 (tanh), 0x1p-27, 0x1.241bf835f9d5fp+4, 50000)
96*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_D1 (tanh), 0x1.241bf835f9d5fp+4, inf, 1000)
97