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