1*412f47f9SXin Li /*
2*412f47f9SXin Li * Double-precision vector sinh(x) function.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2022-2024, Arm Limited.
5*412f47f9SXin Li * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li */
7*412f47f9SXin Li
8*412f47f9SXin Li #include "v_math.h"
9*412f47f9SXin Li #include "poly_advsimd_f64.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], inv_ln2;
16*412f47f9SXin Li double m_ln2[2];
17*412f47f9SXin Li float64x2_t shift;
18*412f47f9SXin Li uint64x2_t halff;
19*412f47f9SXin Li int64x2_t onef;
20*412f47f9SXin Li #if WANT_SIMD_EXCEPT
21*412f47f9SXin Li uint64x2_t tiny_bound, thresh;
22*412f47f9SXin Li #else
23*412f47f9SXin Li uint64x2_t large_bound;
24*412f47f9SXin Li #endif
25*412f47f9SXin Li } data = {
26*412f47f9SXin Li /* Generated using Remez, deg=12 in [-log(2)/2, log(2)/2]. */
27*412f47f9SXin Li .poly = { V2 (0x1p-1), V2 (0x1.5555555555559p-3), V2 (0x1.555555555554bp-5),
28*412f47f9SXin Li V2 (0x1.111111110f663p-7), V2 (0x1.6c16c16c1b5f3p-10),
29*412f47f9SXin Li V2 (0x1.a01a01affa35dp-13), V2 (0x1.a01a018b4ecbbp-16),
30*412f47f9SXin Li V2 (0x1.71ddf82db5bb4p-19), V2 (0x1.27e517fc0d54bp-22),
31*412f47f9SXin Li V2 (0x1.af5eedae67435p-26), V2 (0x1.1f143d060a28ap-29), },
32*412f47f9SXin Li
33*412f47f9SXin Li .inv_ln2 = V2 (0x1.71547652b82fep0),
34*412f47f9SXin Li .m_ln2 = {-0x1.62e42fefa39efp-1, -0x1.abc9e3b39803fp-56},
35*412f47f9SXin Li .shift = V2 (0x1.8p52),
36*412f47f9SXin Li
37*412f47f9SXin Li .halff = V2 (0x3fe0000000000000),
38*412f47f9SXin Li .onef = V2 (0x3ff0000000000000),
39*412f47f9SXin Li #if WANT_SIMD_EXCEPT
40*412f47f9SXin Li /* 2^-26, below which sinh(x) rounds to x. */
41*412f47f9SXin Li .tiny_bound = V2 (0x3e50000000000000),
42*412f47f9SXin Li /* asuint(large_bound) - asuint(tiny_bound). */
43*412f47f9SXin Li .thresh = V2 (0x0230000000000000),
44*412f47f9SXin Li #else
45*412f47f9SXin Li /* 2^9. expm1 helper overflows for large input. */
46*412f47f9SXin Li .large_bound = V2 (0x4080000000000000),
47*412f47f9SXin Li #endif
48*412f47f9SXin Li };
49*412f47f9SXin Li
50*412f47f9SXin Li static inline float64x2_t
expm1_inline(float64x2_t x)51*412f47f9SXin Li expm1_inline (float64x2_t x)
52*412f47f9SXin Li {
53*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
54*412f47f9SXin Li
55*412f47f9SXin Li /* Reduce argument:
56*412f47f9SXin Li exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
57*412f47f9SXin Li where i = round(x / ln2)
58*412f47f9SXin Li and f = x - i * ln2 (f in [-ln2/2, ln2/2]). */
59*412f47f9SXin Li float64x2_t j = vsubq_f64 (vfmaq_f64 (d->shift, d->inv_ln2, x), d->shift);
60*412f47f9SXin Li int64x2_t i = vcvtq_s64_f64 (j);
61*412f47f9SXin Li
62*412f47f9SXin Li float64x2_t m_ln2 = vld1q_f64 (d->m_ln2);
63*412f47f9SXin Li float64x2_t f = vfmaq_laneq_f64 (x, j, m_ln2, 0);
64*412f47f9SXin Li f = vfmaq_laneq_f64 (f, j, m_ln2, 1);
65*412f47f9SXin Li /* Approximate expm1(f) using polynomial. */
66*412f47f9SXin Li float64x2_t f2 = vmulq_f64 (f, f);
67*412f47f9SXin Li float64x2_t f4 = vmulq_f64 (f2, f2);
68*412f47f9SXin Li float64x2_t f8 = vmulq_f64 (f4, f4);
69*412f47f9SXin Li float64x2_t p = vfmaq_f64 (f, f2, v_estrin_10_f64 (f, f2, f4, f8, d->poly));
70*412f47f9SXin Li /* t = 2^i. */
71*412f47f9SXin Li float64x2_t t = vreinterpretq_f64_u64 (
72*412f47f9SXin Li vreinterpretq_u64_s64 (vaddq_s64 (vshlq_n_s64 (i, 52), d->onef)));
73*412f47f9SXin Li /* expm1(x) ~= p * t + (t - 1). */
74*412f47f9SXin Li return vfmaq_f64 (vsubq_f64 (t, v_f64 (1.0)), p, t);
75*412f47f9SXin Li }
76*412f47f9SXin Li
77*412f47f9SXin Li static float64x2_t NOINLINE VPCS_ATTR
special_case(float64x2_t x)78*412f47f9SXin Li special_case (float64x2_t x)
79*412f47f9SXin Li {
80*412f47f9SXin Li return v_call_f64 (sinh, x, x, v_u64 (-1));
81*412f47f9SXin Li }
82*412f47f9SXin Li
83*412f47f9SXin Li /* Approximation for vector double-precision sinh(x) using expm1.
84*412f47f9SXin Li sinh(x) = (exp(x) - exp(-x)) / 2.
85*412f47f9SXin Li The greatest observed error is 2.57 ULP:
86*412f47f9SXin Li _ZGVnN2v_sinh (0x1.9fb1d49d1d58bp-2) got 0x1.ab34e59d678dcp-2
87*412f47f9SXin Li want 0x1.ab34e59d678d9p-2. */
V_NAME_D1(sinh)88*412f47f9SXin Li float64x2_t VPCS_ATTR V_NAME_D1 (sinh) (float64x2_t x)
89*412f47f9SXin Li {
90*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
91*412f47f9SXin Li
92*412f47f9SXin Li float64x2_t ax = vabsq_f64 (x);
93*412f47f9SXin Li uint64x2_t sign
94*412f47f9SXin Li = veorq_u64 (vreinterpretq_u64_f64 (x), vreinterpretq_u64_f64 (ax));
95*412f47f9SXin Li float64x2_t halfsign = vreinterpretq_f64_u64 (vorrq_u64 (sign, d->halff));
96*412f47f9SXin Li
97*412f47f9SXin Li #if WANT_SIMD_EXCEPT
98*412f47f9SXin Li uint64x2_t special = vcgeq_u64 (
99*412f47f9SXin Li vsubq_u64 (vreinterpretq_u64_f64 (ax), d->tiny_bound), d->thresh);
100*412f47f9SXin Li #else
101*412f47f9SXin Li uint64x2_t special = vcgeq_u64 (vreinterpretq_u64_f64 (ax), d->large_bound);
102*412f47f9SXin Li #endif
103*412f47f9SXin Li
104*412f47f9SXin Li /* Fall back to scalar variant for all lanes if any of them are special. */
105*412f47f9SXin Li if (unlikely (v_any_u64 (special)))
106*412f47f9SXin Li return special_case (x);
107*412f47f9SXin Li
108*412f47f9SXin Li /* Up to the point that expm1 overflows, we can use it to calculate sinh
109*412f47f9SXin Li using a slight rearrangement of the definition of sinh. This allows us to
110*412f47f9SXin Li retain acceptable accuracy for very small inputs. */
111*412f47f9SXin Li float64x2_t t = expm1_inline (ax);
112*412f47f9SXin Li t = vaddq_f64 (t, vdivq_f64 (t, vaddq_f64 (t, v_f64 (1.0))));
113*412f47f9SXin Li return vmulq_f64 (t, halfsign);
114*412f47f9SXin Li }
115*412f47f9SXin Li
116*412f47f9SXin Li PL_SIG (V, D, 1, sinh, -10.0, 10.0)
117*412f47f9SXin Li PL_TEST_ULP (V_NAME_D1 (sinh), 2.08)
118*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_D1 (sinh), WANT_SIMD_EXCEPT)
119*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (sinh), 0, 0x1p-26, 1000)
120*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (sinh), 0x1p-26, 0x1p9, 500000)
121*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (sinh), 0x1p9, inf, 1000)
122