1/* Double-precision AdvSIMD log1p
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
23const static struct data
24{
25 float64x2_t poly[19], ln2[2];
26 uint64x2_t hf_rt2_top, one_m_hf_rt2_top, umask, inf, minus_one;
27 int64x2_t one_top;
28} data = {
29 /* Generated using Remez, deg=20, in [sqrt(2)/2-1, sqrt(2)-1]. */
30 .poly = { V2 (-0x1.ffffffffffffbp-2), V2 (0x1.55555555551a9p-2),
31 V2 (-0x1.00000000008e3p-2), V2 (0x1.9999999a32797p-3),
32 V2 (-0x1.555555552fecfp-3), V2 (0x1.249248e071e5ap-3),
33 V2 (-0x1.ffffff8bf8482p-4), V2 (0x1.c71c8f07da57ap-4),
34 V2 (-0x1.9999ca4ccb617p-4), V2 (0x1.7459ad2e1dfa3p-4),
35 V2 (-0x1.554d2680a3ff2p-4), V2 (0x1.3b4c54d487455p-4),
36 V2 (-0x1.2548a9ffe80e6p-4), V2 (0x1.0f389a24b2e07p-4),
37 V2 (-0x1.eee4db15db335p-5), V2 (0x1.e95b494d4a5ddp-5),
38 V2 (-0x1.15fdf07cb7c73p-4), V2 (0x1.0310b70800fcfp-4),
39 V2 (-0x1.cfa7385bdb37ep-6) },
40 .ln2 = { V2 (0x1.62e42fefa3800p-1), V2 (0x1.ef35793c76730p-45) },
41 /* top32(asuint64(sqrt(2)/2)) << 32. */
42 .hf_rt2_top = V2 (0x3fe6a09e00000000),
43 /* (top32(asuint64(1)) - top32(asuint64(sqrt(2)/2))) << 32. */
44 .one_m_hf_rt2_top = V2 (0x00095f6200000000),
45 .umask = V2 (0x000fffff00000000),
46 .one_top = V2 (0x3ff),
47 .inf = V2 (0x7ff0000000000000),
48 .minus_one = V2 (0xbff0000000000000)
49};
50
51#define BottomMask v_u64 (0xffffffff)
52
53static float64x2_t VPCS_ATTR NOINLINE
54special_case (float64x2_t x, float64x2_t y, uint64x2_t special)
55{
56 return v_call_f64 (log1p, x, y, special);
57}
58
59/* Vector log1p approximation using polynomial on reduced interval. Routine is
60 a modification of the algorithm used in scalar log1p, with no shortcut for
61 k=0 and no narrowing for f and k. Maximum observed error is 2.45 ULP:
62 _ZGVnN2v_log1p(0x1.658f7035c4014p+11) got 0x1.fd61d0727429dp+2
63 want 0x1.fd61d0727429fp+2 . */
64VPCS_ATTR float64x2_t V_NAME_D1 (log1p) (float64x2_t x)
65{
66 const struct data *d = ptr_barrier (&data);
67 uint64x2_t ix = vreinterpretq_u64_f64 (x);
68 uint64x2_t ia = vreinterpretq_u64_f64 (vabsq_f64 (x));
69 uint64x2_t special = vcgeq_u64 (ia, d->inf);
70
71#if WANT_SIMD_EXCEPT
72 special = vorrq_u64 (special,
73 vcgeq_u64 (ix, vreinterpretq_u64_f64 (v_f64 (-1))));
74 if (__glibc_unlikely (v_any_u64 (special)))
75 x = v_zerofy_f64 (x, special);
76#else
77 special = vorrq_u64 (special, vcleq_f64 (x, v_f64 (-1)));
78#endif
79
80 /* With x + 1 = t * 2^k (where t = f + 1 and k is chosen such that f
81 is in [sqrt(2)/2, sqrt(2)]):
82 log1p(x) = k*log(2) + log1p(f).
83
84 f may not be representable exactly, so we need a correction term:
85 let m = round(1 + x), c = (1 + x) - m.
86 c << m: at very small x, log1p(x) ~ x, hence:
87 log(1+x) - log(m) ~ c/m.
88
89 We therefore calculate log1p(x) by k*log2 + log1p(f) + c/m. */
90
91 /* Obtain correctly scaled k by manipulation in the exponent.
92 The scalar algorithm casts down to 32-bit at this point to calculate k and
93 u_red. We stay in double-width to obtain f and k, using the same constants
94 as the scalar algorithm but shifted left by 32. */
95 float64x2_t m = vaddq_f64 (x, v_f64 (1));
96 uint64x2_t mi = vreinterpretq_u64_f64 (m);
97 uint64x2_t u = vaddq_u64 (mi, d->one_m_hf_rt2_top);
98
99 int64x2_t ki
100 = vsubq_s64 (vreinterpretq_s64_u64 (vshrq_n_u64 (u, 52)), d->one_top);
101 float64x2_t k = vcvtq_f64_s64 (ki);
102
103 /* Reduce x to f in [sqrt(2)/2, sqrt(2)]. */
104 uint64x2_t utop = vaddq_u64 (vandq_u64 (u, d->umask), d->hf_rt2_top);
105 uint64x2_t u_red = vorrq_u64 (utop, vandq_u64 (mi, BottomMask));
106 float64x2_t f = vsubq_f64 (vreinterpretq_f64_u64 (u_red), v_f64 (1));
107
108 /* Correction term c/m. */
109 float64x2_t cm = vdivq_f64 (vsubq_f64 (x, vsubq_f64 (m, v_f64 (1))), m);
110
111 /* Approximate log1p(x) on the reduced input using a polynomial. Because
112 log1p(0)=0 we choose an approximation of the form:
113 x + C0*x^2 + C1*x^3 + C2x^4 + ...
114 Hence approximation has the form f + f^2 * P(f)
115 where P(x) = C0 + C1*x + C2x^2 + ...
116 Assembling this all correctly is dealt with at the final step. */
117 float64x2_t f2 = vmulq_f64 (f, f);
118 float64x2_t p = v_pw_horner_18_f64 (f, f2, d->poly);
119
120 float64x2_t ylo = vfmaq_f64 (cm, k, d->ln2[1]);
121 float64x2_t yhi = vfmaq_f64 (f, k, d->ln2[0]);
122 float64x2_t y = vaddq_f64 (ylo, yhi);
123
124 if (__glibc_unlikely (v_any_u64 (special)))
125 return special_case (vreinterpretq_f64_u64 (ix), vfmaq_f64 (y, f2, p),
126 special);
127
128 return vfmaq_f64 (y, f2, p);
129}
130

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