xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/sinh.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-precision SVE sinh(x) function.
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 2023-2024, Arm Limited.
5*f3087befSAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner  */
7*f3087befSAndrew Turner 
8*f3087befSAndrew Turner #include "sv_math.h"
9*f3087befSAndrew Turner #include "sv_poly_f64.h"
10*f3087befSAndrew Turner #include "test_sig.h"
11*f3087befSAndrew Turner #include "test_defs.h"
12*f3087befSAndrew Turner 
13*f3087befSAndrew Turner static const struct data
14*f3087befSAndrew Turner {
15*f3087befSAndrew Turner   float64_t poly[11];
16*f3087befSAndrew Turner   float64_t inv_ln2, m_ln2_hi, m_ln2_lo, shift;
17*f3087befSAndrew Turner   uint64_t halff;
18*f3087befSAndrew Turner   int64_t onef;
19*f3087befSAndrew Turner   uint64_t large_bound;
20*f3087befSAndrew Turner } data = {
21*f3087befSAndrew Turner   /* Generated using Remez, deg=12 in [-log(2)/2, log(2)/2].  */
22*f3087befSAndrew Turner   .poly = { 0x1p-1, 0x1.5555555555559p-3, 0x1.555555555554bp-5,
23*f3087befSAndrew Turner 	    0x1.111111110f663p-7, 0x1.6c16c16c1b5f3p-10,
24*f3087befSAndrew Turner 	    0x1.a01a01affa35dp-13, 0x1.a01a018b4ecbbp-16,
25*f3087befSAndrew Turner 	    0x1.71ddf82db5bb4p-19, 0x1.27e517fc0d54bp-22,
26*f3087befSAndrew Turner 	    0x1.af5eedae67435p-26, 0x1.1f143d060a28ap-29, },
27*f3087befSAndrew Turner 
28*f3087befSAndrew Turner   .inv_ln2 = 0x1.71547652b82fep0,
29*f3087befSAndrew Turner   .m_ln2_hi = -0x1.62e42fefa39efp-1,
30*f3087befSAndrew Turner   .m_ln2_lo = -0x1.abc9e3b39803fp-56,
31*f3087befSAndrew Turner   .shift = 0x1.8p52,
32*f3087befSAndrew Turner 
33*f3087befSAndrew Turner   .halff = 0x3fe0000000000000,
34*f3087befSAndrew Turner   .onef = 0x3ff0000000000000,
35*f3087befSAndrew Turner   /* 2^9. expm1 helper overflows for large input.  */
36*f3087befSAndrew Turner   .large_bound = 0x4080000000000000,
37*f3087befSAndrew Turner };
38*f3087befSAndrew Turner 
39*f3087befSAndrew Turner static inline svfloat64_t
expm1_inline(svfloat64_t x,svbool_t pg)40*f3087befSAndrew Turner expm1_inline (svfloat64_t x, svbool_t pg)
41*f3087befSAndrew Turner {
42*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
43*f3087befSAndrew Turner 
44*f3087befSAndrew Turner   /* Reduce argument:
45*f3087befSAndrew Turner      exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
46*f3087befSAndrew Turner      where i = round(x / ln2)
47*f3087befSAndrew Turner      and   f = x - i * ln2 (f in [-ln2/2, ln2/2]).  */
48*f3087befSAndrew Turner   svfloat64_t j
49*f3087befSAndrew Turner       = svsub_x (pg, svmla_x (pg, sv_f64 (d->shift), x, d->inv_ln2), d->shift);
50*f3087befSAndrew Turner   svint64_t i = svcvt_s64_x (pg, j);
51*f3087befSAndrew Turner   svfloat64_t f = svmla_x (pg, x, j, d->m_ln2_hi);
52*f3087befSAndrew Turner   f = svmla_x (pg, f, j, d->m_ln2_lo);
53*f3087befSAndrew Turner   /* Approximate expm1(f) using polynomial.  */
54*f3087befSAndrew Turner   svfloat64_t f2 = svmul_x (pg, f, f);
55*f3087befSAndrew Turner   svfloat64_t f4 = svmul_x (pg, f2, f2);
56*f3087befSAndrew Turner   svfloat64_t f8 = svmul_x (pg, f4, f4);
57*f3087befSAndrew Turner   svfloat64_t p
58*f3087befSAndrew Turner       = svmla_x (pg, f, f2, sv_estrin_10_f64_x (pg, f, f2, f4, f8, d->poly));
59*f3087befSAndrew Turner   /* t = 2^i.  */
60*f3087befSAndrew Turner   svfloat64_t t = svscale_x (pg, sv_f64 (1), i);
61*f3087befSAndrew Turner   /* expm1(x) ~= p * t + (t - 1).  */
62*f3087befSAndrew Turner   return svmla_x (pg, svsub_x (pg, t, 1.0), p, t);
63*f3087befSAndrew Turner }
64*f3087befSAndrew Turner 
65*f3087befSAndrew Turner static svfloat64_t NOINLINE
special_case(svfloat64_t x,svbool_t pg)66*f3087befSAndrew Turner special_case (svfloat64_t x, svbool_t pg)
67*f3087befSAndrew Turner {
68*f3087befSAndrew Turner   return sv_call_f64 (sinh, x, x, pg);
69*f3087befSAndrew Turner }
70*f3087befSAndrew Turner 
71*f3087befSAndrew Turner /* Approximation for SVE double-precision sinh(x) using expm1.
72*f3087befSAndrew Turner    sinh(x) = (exp(x) - exp(-x)) / 2.
73*f3087befSAndrew Turner    The greatest observed error is 2.57 ULP:
74*f3087befSAndrew Turner    _ZGVsMxv_sinh (0x1.a008538399931p-2) got 0x1.ab929fc64bd66p-2
75*f3087befSAndrew Turner 				       want 0x1.ab929fc64bd63p-2.  */
SV_NAME_D1(sinh)76*f3087befSAndrew Turner svfloat64_t SV_NAME_D1 (sinh) (svfloat64_t x, svbool_t pg)
77*f3087befSAndrew Turner {
78*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
79*f3087befSAndrew Turner 
80*f3087befSAndrew Turner   svfloat64_t ax = svabs_x (pg, x);
81*f3087befSAndrew Turner   svuint64_t sign
82*f3087befSAndrew Turner       = sveor_x (pg, svreinterpret_u64 (x), svreinterpret_u64 (ax));
83*f3087befSAndrew Turner   svfloat64_t halfsign = svreinterpret_f64 (svorr_x (pg, sign, d->halff));
84*f3087befSAndrew Turner 
85*f3087befSAndrew Turner   svbool_t special = svcmpge (pg, svreinterpret_u64 (ax), d->large_bound);
86*f3087befSAndrew Turner 
87*f3087befSAndrew Turner   /* Fall back to scalar variant for all lanes if any are special.  */
88*f3087befSAndrew Turner   if (unlikely (svptest_any (pg, special)))
89*f3087befSAndrew Turner     return special_case (x, pg);
90*f3087befSAndrew Turner 
91*f3087befSAndrew Turner   /* Up to the point that expm1 overflows, we can use it to calculate sinh
92*f3087befSAndrew Turner      using a slight rearrangement of the definition of sinh. This allows us to
93*f3087befSAndrew Turner      retain acceptable accuracy for very small inputs.  */
94*f3087befSAndrew Turner   svfloat64_t t = expm1_inline (ax, pg);
95*f3087befSAndrew Turner   t = svadd_x (pg, t, svdiv_x (pg, t, svadd_x (pg, t, 1.0)));
96*f3087befSAndrew Turner   return svmul_x (pg, t, halfsign);
97*f3087befSAndrew Turner }
98*f3087befSAndrew Turner 
99*f3087befSAndrew Turner TEST_SIG (SV, D, 1, sinh, -10.0, 10.0)
100*f3087befSAndrew Turner TEST_ULP (SV_NAME_D1 (sinh), 2.08)
101*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_D1 (sinh))
102*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (sinh), 0, 0x1p-26, 1000)
103*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (sinh), 0x1p-26, 0x1p9, 500000)
104*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (sinh), 0x1p9, inf, 1000)
105*f3087befSAndrew Turner CLOSE_SVE_ATTR
106