1 | /* Double-precision SVE expm1 |
2 | |
3 | Copyright (C) 2023-2024 Free Software Foundation, Inc. |
4 | This file is part of the GNU C Library. |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either |
9 | version 2.1 of the License, or (at your option) any later version. |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | Lesser General Public License for more details. |
15 | |
16 | You should have received a copy of the GNU Lesser General Public |
17 | License along with the GNU C Library; if not, see |
18 | <https://www.gnu.org/licenses/>. */ |
19 | |
20 | #include "sv_math.h" |
21 | #include "poly_sve_f64.h" |
22 | |
23 | #define SpecialBound 0x1.62b7d369a5aa9p+9 |
24 | #define ExponentBias 0x3ff0000000000000 |
25 | |
26 | static const struct data |
27 | { |
28 | double poly[11]; |
29 | double shift, inv_ln2, special_bound; |
30 | /* To be loaded in one quad-word. */ |
31 | double ln2_hi, ln2_lo; |
32 | } data = { |
33 | /* Generated using fpminimax. */ |
34 | .poly = { 0x1p-1, 0x1.5555555555559p-3, 0x1.555555555554bp-5, |
35 | 0x1.111111110f663p-7, 0x1.6c16c16c1b5f3p-10, 0x1.a01a01affa35dp-13, |
36 | 0x1.a01a018b4ecbbp-16, 0x1.71ddf82db5bb4p-19, 0x1.27e517fc0d54bp-22, |
37 | 0x1.af5eedae67435p-26, 0x1.1f143d060a28ap-29, }, |
38 | |
39 | .special_bound = SpecialBound, |
40 | .inv_ln2 = 0x1.71547652b82fep0, |
41 | .ln2_hi = 0x1.62e42fefa39efp-1, |
42 | .ln2_lo = 0x1.abc9e3b39803fp-56, |
43 | .shift = 0x1.8p52, |
44 | }; |
45 | |
46 | static svfloat64_t NOINLINE |
47 | special_case (svfloat64_t x, svfloat64_t y, svbool_t pg) |
48 | { |
49 | return sv_call_f64 (f: expm1, x, y, cmp: pg); |
50 | } |
51 | |
52 | /* Double-precision vector exp(x) - 1 function. |
53 | The maximum error observed error is 2.18 ULP: |
54 | _ZGVsMxv_expm1(0x1.634ba0c237d7bp-2) got 0x1.a8b9ea8d66e22p-2 |
55 | want 0x1.a8b9ea8d66e2p-2. */ |
56 | svfloat64_t SV_NAME_D1 (expm1) (svfloat64_t x, svbool_t pg) |
57 | { |
58 | const struct data *d = ptr_barrier (&data); |
59 | |
60 | /* Large, Nan/Inf. */ |
61 | svbool_t special = svnot_z (pg, svaclt (pg, x, d->special_bound)); |
62 | |
63 | /* Reduce argument to smaller range: |
64 | Let i = round(x / ln2) |
65 | and f = x - i * ln2, then f is in [-ln2/2, ln2/2]. |
66 | exp(x) - 1 = 2^i * (expm1(f) + 1) - 1 |
67 | where 2^i is exact because i is an integer. */ |
68 | svfloat64_t shift = sv_f64 (x: d->shift); |
69 | svfloat64_t n = svsub_x (pg, svmla_x (pg, shift, x, d->inv_ln2), shift); |
70 | svint64_t i = svcvt_s64_x (pg, n); |
71 | svfloat64_t ln2 = svld1rq (svptrue_b64 (), &d->ln2_hi); |
72 | svfloat64_t f = svmls_lane (x, n, ln2, 0); |
73 | f = svmls_lane (f, n, ln2, 1); |
74 | |
75 | /* Approximate expm1(f) using polynomial. |
76 | Taylor expansion for expm1(x) has the form: |
77 | x + ax^2 + bx^3 + cx^4 .... |
78 | So we calculate the polynomial P(f) = a + bf + cf^2 + ... |
79 | and assemble the approximation expm1(f) ~= f + f^2 * P(f). */ |
80 | svfloat64_t f2 = svmul_x (pg, f, f); |
81 | svfloat64_t f4 = svmul_x (pg, f2, f2); |
82 | svfloat64_t f8 = svmul_x (pg, f4, f4); |
83 | svfloat64_t p |
84 | = svmla_x (pg, f, f2, sv_estrin_10_f64_x (pg, x: f, x2: f2, x4: f4, x8: f8, poly: d->poly)); |
85 | |
86 | /* Assemble the result. |
87 | expm1(x) ~= 2^i * (p + 1) - 1 |
88 | Let t = 2^i. */ |
89 | svint64_t u = svadd_x (pg, svlsl_x (pg, i, 52), ExponentBias); |
90 | svfloat64_t t = svreinterpret_f64 (u); |
91 | |
92 | /* expm1(x) ~= p * t + (t - 1). */ |
93 | svfloat64_t y = svmla_x (pg, svsub_x (pg, t, 1), p, t); |
94 | |
95 | if (__glibc_unlikely (svptest_any (pg, special))) |
96 | return special_case (x, y, pg: special); |
97 | |
98 | return y; |
99 | } |
100 | |