xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/expm1.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-precision vector exp(x) - 1 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 #define SpecialBound 0x1.62b7d369a5aa9p+9
14*f3087befSAndrew Turner #define ExponentBias 0x3ff0000000000000
15*f3087befSAndrew Turner 
16*f3087befSAndrew Turner static const struct data
17*f3087befSAndrew Turner {
18*f3087befSAndrew Turner   double poly[11];
19*f3087befSAndrew Turner   double shift, inv_ln2, special_bound;
20*f3087befSAndrew Turner   /* To be loaded in one quad-word.  */
21*f3087befSAndrew Turner   double ln2_hi, ln2_lo;
22*f3087befSAndrew Turner } data = {
23*f3087befSAndrew Turner   /* Generated using fpminimax.  */
24*f3087befSAndrew Turner   .poly = { 0x1p-1, 0x1.5555555555559p-3, 0x1.555555555554bp-5,
25*f3087befSAndrew Turner             0x1.111111110f663p-7, 0x1.6c16c16c1b5f3p-10, 0x1.a01a01affa35dp-13,
26*f3087befSAndrew Turner             0x1.a01a018b4ecbbp-16, 0x1.71ddf82db5bb4p-19, 0x1.27e517fc0d54bp-22,
27*f3087befSAndrew Turner             0x1.af5eedae67435p-26, 0x1.1f143d060a28ap-29, },
28*f3087befSAndrew Turner 
29*f3087befSAndrew Turner   .special_bound = SpecialBound,
30*f3087befSAndrew Turner   .inv_ln2 = 0x1.71547652b82fep0,
31*f3087befSAndrew Turner   .ln2_hi = 0x1.62e42fefa39efp-1,
32*f3087befSAndrew Turner   .ln2_lo = 0x1.abc9e3b39803fp-56,
33*f3087befSAndrew Turner   .shift = 0x1.8p52,
34*f3087befSAndrew Turner };
35*f3087befSAndrew Turner 
36*f3087befSAndrew Turner static svfloat64_t NOINLINE
special_case(svfloat64_t x,svfloat64_t y,svbool_t pg)37*f3087befSAndrew Turner special_case (svfloat64_t x, svfloat64_t y, svbool_t pg)
38*f3087befSAndrew Turner {
39*f3087befSAndrew Turner   return sv_call_f64 (expm1, x, y, pg);
40*f3087befSAndrew Turner }
41*f3087befSAndrew Turner 
42*f3087befSAndrew Turner /* Double-precision vector exp(x) - 1 function.
43*f3087befSAndrew Turner    The maximum error observed error is 2.18 ULP:
44*f3087befSAndrew Turner    _ZGVsMxv_expm1(0x1.634ba0c237d7bp-2) got 0x1.a8b9ea8d66e22p-2
45*f3087befSAndrew Turner 				       want 0x1.a8b9ea8d66e2p-2.  */
SV_NAME_D1(expm1)46*f3087befSAndrew Turner svfloat64_t SV_NAME_D1 (expm1) (svfloat64_t x, svbool_t pg)
47*f3087befSAndrew Turner {
48*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
49*f3087befSAndrew Turner 
50*f3087befSAndrew Turner   /* Large, Nan/Inf.  */
51*f3087befSAndrew Turner   svbool_t special = svnot_z (pg, svaclt (pg, x, d->special_bound));
52*f3087befSAndrew Turner 
53*f3087befSAndrew Turner   /* Reduce argument to smaller range:
54*f3087befSAndrew Turner      Let i = round(x / ln2)
55*f3087befSAndrew Turner      and f = x - i * ln2, then f is in [-ln2/2, ln2/2].
56*f3087befSAndrew Turner      exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
57*f3087befSAndrew Turner      where 2^i is exact because i is an integer.  */
58*f3087befSAndrew Turner   svfloat64_t shift = sv_f64 (d->shift);
59*f3087befSAndrew Turner   svfloat64_t n = svsub_x (pg, svmla_x (pg, shift, x, d->inv_ln2), shift);
60*f3087befSAndrew Turner   svint64_t i = svcvt_s64_x (pg, n);
61*f3087befSAndrew Turner   svfloat64_t ln2 = svld1rq (svptrue_b64 (), &d->ln2_hi);
62*f3087befSAndrew Turner   svfloat64_t f = svmls_lane (x, n, ln2, 0);
63*f3087befSAndrew Turner   f = svmls_lane (f, n, ln2, 1);
64*f3087befSAndrew Turner 
65*f3087befSAndrew Turner   /* Approximate expm1(f) using polynomial.
66*f3087befSAndrew Turner      Taylor expansion for expm1(x) has the form:
67*f3087befSAndrew Turner 	 x + ax^2 + bx^3 + cx^4 ....
68*f3087befSAndrew Turner      So we calculate the polynomial P(f) = a + bf + cf^2 + ...
69*f3087befSAndrew Turner      and assemble the approximation expm1(f) ~= f + f^2 * P(f).  */
70*f3087befSAndrew Turner   svfloat64_t f2 = svmul_x (pg, f, f);
71*f3087befSAndrew Turner   svfloat64_t f4 = svmul_x (pg, f2, f2);
72*f3087befSAndrew Turner   svfloat64_t f8 = svmul_x (pg, f4, f4);
73*f3087befSAndrew Turner   svfloat64_t p
74*f3087befSAndrew Turner       = svmla_x (pg, f, f2, sv_estrin_10_f64_x (pg, f, f2, f4, f8, d->poly));
75*f3087befSAndrew Turner 
76*f3087befSAndrew Turner   /* Assemble the result.
77*f3087befSAndrew Turner    expm1(x) ~= 2^i * (p + 1) - 1
78*f3087befSAndrew Turner    Let t = 2^i.  */
79*f3087befSAndrew Turner   svint64_t u = svadd_x (pg, svlsl_x (pg, i, 52), ExponentBias);
80*f3087befSAndrew Turner   svfloat64_t t = svreinterpret_f64 (u);
81*f3087befSAndrew Turner 
82*f3087befSAndrew Turner   /* expm1(x) ~= p * t + (t - 1).  */
83*f3087befSAndrew Turner   svfloat64_t y = svmla_x (pg, svsub_x (pg, t, 1), p, t);
84*f3087befSAndrew Turner 
85*f3087befSAndrew Turner   if (unlikely (svptest_any (pg, special)))
86*f3087befSAndrew Turner     return special_case (x, y, special);
87*f3087befSAndrew Turner 
88*f3087befSAndrew Turner   return y;
89*f3087befSAndrew Turner }
90*f3087befSAndrew Turner 
91*f3087befSAndrew Turner TEST_SIG (SV, D, 1, expm1, -9.9, 9.9)
92*f3087befSAndrew Turner TEST_ULP (SV_NAME_D1 (expm1), 1.68)
93*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_D1 (expm1))
94*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (expm1), 0, 0x1p-23, 1000)
95*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (expm1), 0x1p-23, SpecialBound, 200000)
96*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (expm1), SpecialBound, inf, 1000)
97*f3087befSAndrew Turner CLOSE_SVE_ATTR
98