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