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