1/* Double-precision AdvSIMD 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 "v_math.h"
21#include "poly_advsimd_f64.h"
22
23static const struct data
24{
25 float64x2_t poly[11];
26 float64x2_t invln2, ln2, shift;
27 int64x2_t exponent_bias;
28#if WANT_SIMD_EXCEPT
29 uint64x2_t thresh, tiny_bound;
30#else
31 float64x2_t oflow_bound;
32#endif
33} data = {
34 /* Generated using fpminimax, with degree=12 in [log(2)/2, log(2)/2]. */
35 .poly = { V2 (0x1p-1), V2 (0x1.5555555555559p-3), V2 (0x1.555555555554bp-5),
36 V2 (0x1.111111110f663p-7), V2 (0x1.6c16c16c1b5f3p-10),
37 V2 (0x1.a01a01affa35dp-13), V2 (0x1.a01a018b4ecbbp-16),
38 V2 (0x1.71ddf82db5bb4p-19), V2 (0x1.27e517fc0d54bp-22),
39 V2 (0x1.af5eedae67435p-26), V2 (0x1.1f143d060a28ap-29) },
40 .invln2 = V2 (0x1.71547652b82fep0),
41 .ln2 = { 0x1.62e42fefa39efp-1, 0x1.abc9e3b39803fp-56 },
42 .shift = V2 (0x1.8p52),
43 .exponent_bias = V2 (0x3ff0000000000000),
44#if WANT_SIMD_EXCEPT
45 /* asuint64(oflow_bound) - asuint64(0x1p-51), shifted left by 1 for abs
46 compare. */
47 .thresh = V2 (0x78c56fa6d34b552),
48 /* asuint64(0x1p-51) << 1. */
49 .tiny_bound = V2 (0x3cc0000000000000 << 1),
50#else
51 /* Value above which expm1(x) should overflow. Absolute value of the
52 underflow bound is greater than this, so it catches both cases - there is
53 a small window where fallbacks are triggered unnecessarily. */
54 .oflow_bound = V2 (0x1.62b7d369a5aa9p+9),
55#endif
56};
57
58static float64x2_t VPCS_ATTR NOINLINE
59special_case (float64x2_t x, float64x2_t y, uint64x2_t special)
60{
61 return v_call_f64 (expm1, x, y, special);
62}
63
64/* Double-precision vector exp(x) - 1 function.
65 The maximum error observed error is 2.18 ULP:
66 _ZGVnN2v_expm1 (0x1.634ba0c237d7bp-2) got 0x1.a8b9ea8d66e22p-2
67 want 0x1.a8b9ea8d66e2p-2. */
68float64x2_t VPCS_ATTR V_NAME_D1 (expm1) (float64x2_t x)
69{
70 const struct data *d = ptr_barrier (&data);
71
72 uint64x2_t ix = vreinterpretq_u64_f64 (x);
73
74#if WANT_SIMD_EXCEPT
75 /* If fp exceptions are to be triggered correctly, fall back to scalar for
76 |x| < 2^-51, |x| > oflow_bound, Inf & NaN. Add ix to itself for
77 shift-left by 1, and compare with thresh which was left-shifted offline -
78 this is effectively an absolute compare. */
79 uint64x2_t special
80 = vcgeq_u64 (vsubq_u64 (vaddq_u64 (ix, ix), d->tiny_bound), d->thresh);
81 if (__glibc_unlikely (v_any_u64 (special)))
82 x = v_zerofy_f64 (x, special);
83#else
84 /* Large input, NaNs and Infs. */
85 uint64x2_t special = vcageq_f64 (x, d->oflow_bound);
86#endif
87
88 /* Reduce argument to smaller range:
89 Let i = round(x / ln2)
90 and f = x - i * ln2, then f is in [-ln2/2, ln2/2].
91 exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
92 where 2^i is exact because i is an integer. */
93 float64x2_t n = vsubq_f64 (vfmaq_f64 (d->shift, d->invln2, x), d->shift);
94 int64x2_t i = vcvtq_s64_f64 (n);
95 float64x2_t f = vfmsq_laneq_f64 (x, n, d->ln2, 0);
96 f = vfmsq_laneq_f64 (f, n, d->ln2, 1);
97
98 /* Approximate expm1(f) using polynomial.
99 Taylor expansion for expm1(x) has the form:
100 x + ax^2 + bx^3 + cx^4 ....
101 So we calculate the polynomial P(f) = a + bf + cf^2 + ...
102 and assemble the approximation expm1(f) ~= f + f^2 * P(f). */
103 float64x2_t f2 = vmulq_f64 (f, f);
104 float64x2_t f4 = vmulq_f64 (f2, f2);
105 float64x2_t f8 = vmulq_f64 (f4, f4);
106 float64x2_t p = vfmaq_f64 (f, f2, v_estrin_10_f64 (f, f2, f4, f8, d->poly));
107
108 /* Assemble the result.
109 expm1(x) ~= 2^i * (p + 1) - 1
110 Let t = 2^i. */
111 int64x2_t u = vaddq_s64 (vshlq_n_s64 (i, 52), d->exponent_bias);
112 float64x2_t t = vreinterpretq_f64_s64 (u);
113
114 if (__glibc_unlikely (v_any_u64 (special)))
115 return special_case (vreinterpretq_f64_u64 (ix),
116 vfmaq_f64 (vsubq_f64 (t, v_f64 (1.0)), p, t),
117 special);
118
119 /* expm1(x) ~= p * t + (t - 1). */
120 return vfmaq_f64 (vsubq_f64 (t, v_f64 (1.0)), p, t);
121}
122

source code of glibc/sysdeps/aarch64/fpu/expm1_advsimd.c