]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/aarch64/fpu/sinh_advsimd.c
math: Update mips32/mips64 ulps for log2p1
[thirdparty/glibc.git] / sysdeps / aarch64 / fpu / sinh_advsimd.c
1 /* Double-precision vector (Advanced SIMD) sinh function
2
3 Copyright (C) 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
23 static const struct data
24 {
25 float64x2_t poly[11], inv_ln2;
26 double m_ln2[2];
27 float64x2_t shift;
28 uint64x2_t halff;
29 int64x2_t onef;
30 #if WANT_SIMD_EXCEPT
31 uint64x2_t tiny_bound, thresh;
32 #else
33 uint64x2_t large_bound;
34 #endif
35 } data = {
36 /* Generated using Remez, deg=12 in [-log(2)/2, log(2)/2]. */
37 .poly = { V2 (0x1p-1), V2 (0x1.5555555555559p-3), V2 (0x1.555555555554bp-5),
38 V2 (0x1.111111110f663p-7), V2 (0x1.6c16c16c1b5f3p-10),
39 V2 (0x1.a01a01affa35dp-13), V2 (0x1.a01a018b4ecbbp-16),
40 V2 (0x1.71ddf82db5bb4p-19), V2 (0x1.27e517fc0d54bp-22),
41 V2 (0x1.af5eedae67435p-26), V2 (0x1.1f143d060a28ap-29), },
42
43 .inv_ln2 = V2 (0x1.71547652b82fep0),
44 .m_ln2 = {-0x1.62e42fefa39efp-1, -0x1.abc9e3b39803fp-56},
45 .shift = V2 (0x1.8p52),
46
47 .halff = V2 (0x3fe0000000000000),
48 .onef = V2 (0x3ff0000000000000),
49 #if WANT_SIMD_EXCEPT
50 /* 2^-26, below which sinh(x) rounds to x. */
51 .tiny_bound = V2 (0x3e50000000000000),
52 /* asuint(large_bound) - asuint(tiny_bound). */
53 .thresh = V2 (0x0230000000000000),
54 #else
55 /* 2^9. expm1 helper overflows for large input. */
56 .large_bound = V2 (0x4080000000000000),
57 #endif
58 };
59
60 static inline float64x2_t
61 expm1_inline (float64x2_t x)
62 {
63 const struct data *d = ptr_barrier (&data);
64
65 /* Reduce argument:
66 exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
67 where i = round(x / ln2)
68 and f = x - i * ln2 (f in [-ln2/2, ln2/2]). */
69 float64x2_t j = vsubq_f64 (vfmaq_f64 (d->shift, d->inv_ln2, x), d->shift);
70 int64x2_t i = vcvtq_s64_f64 (j);
71
72 float64x2_t m_ln2 = vld1q_f64 (d->m_ln2);
73 float64x2_t f = vfmaq_laneq_f64 (x, j, m_ln2, 0);
74 f = vfmaq_laneq_f64 (f, j, m_ln2, 1);
75 /* Approximate expm1(f) using polynomial. */
76 float64x2_t f2 = vmulq_f64 (f, f);
77 float64x2_t f4 = vmulq_f64 (f2, f2);
78 float64x2_t f8 = vmulq_f64 (f4, f4);
79 float64x2_t p = vfmaq_f64 (f, f2, v_estrin_10_f64 (f, f2, f4, f8, d->poly));
80 /* t = 2^i. */
81 float64x2_t t = vreinterpretq_f64_u64 (
82 vreinterpretq_u64_s64 (vaddq_s64 (vshlq_n_s64 (i, 52), d->onef)));
83 /* expm1(x) ~= p * t + (t - 1). */
84 return vfmaq_f64 (vsubq_f64 (t, v_f64 (1.0)), p, t);
85 }
86
87 static float64x2_t NOINLINE VPCS_ATTR
88 special_case (float64x2_t x)
89 {
90 return v_call_f64 (sinh, x, x, v_u64 (-1));
91 }
92
93 /* Approximation for vector double-precision sinh(x) using expm1.
94 sinh(x) = (exp(x) - exp(-x)) / 2.
95 The greatest observed error is 2.57 ULP:
96 _ZGVnN2v_sinh (0x1.9fb1d49d1d58bp-2) got 0x1.ab34e59d678dcp-2
97 want 0x1.ab34e59d678d9p-2. */
98 float64x2_t VPCS_ATTR V_NAME_D1 (sinh) (float64x2_t x)
99 {
100 const struct data *d = ptr_barrier (&data);
101
102 float64x2_t ax = vabsq_f64 (x);
103 uint64x2_t sign
104 = veorq_u64 (vreinterpretq_u64_f64 (x), vreinterpretq_u64_f64 (ax));
105 float64x2_t halfsign = vreinterpretq_f64_u64 (vorrq_u64 (sign, d->halff));
106
107 #if WANT_SIMD_EXCEPT
108 uint64x2_t special = vcgeq_u64 (
109 vsubq_u64 (vreinterpretq_u64_f64 (ax), d->tiny_bound), d->thresh);
110 #else
111 uint64x2_t special = vcgeq_u64 (vreinterpretq_u64_f64 (ax), d->large_bound);
112 #endif
113
114 /* Fall back to scalar variant for all lanes if any of them are special. */
115 if (__glibc_unlikely (v_any_u64 (special)))
116 return special_case (x);
117
118 /* Up to the point that expm1 overflows, we can use it to calculate sinh
119 using a slight rearrangement of the definition of sinh. This allows us to
120 retain acceptable accuracy for very small inputs. */
121 float64x2_t t = expm1_inline (ax);
122 t = vaddq_f64 (t, vdivq_f64 (t, vaddq_f64 (t, v_f64 (1.0))));
123 return vmulq_f64 (t, halfsign);
124 }