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