1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Double-precision SVE log(1+x) function.
3*f3087befSAndrew Turner *
4*f3087befSAndrew Turner * Copyright (c) 2023-2024, Arm Limited.
5*f3087befSAndrew Turner * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*f3087befSAndrew Turner */
7*f3087befSAndrew Turner
8*f3087befSAndrew Turner #include "sv_math.h"
9*f3087befSAndrew Turner #include "sv_poly_f64.h"
10*f3087befSAndrew Turner #include "test_sig.h"
11*f3087befSAndrew Turner #include "test_defs.h"
12*f3087befSAndrew Turner
13*f3087befSAndrew Turner static const struct data
14*f3087befSAndrew Turner {
15*f3087befSAndrew Turner double poly[19];
16*f3087befSAndrew Turner double ln2_hi, ln2_lo;
17*f3087befSAndrew Turner uint64_t hfrt2_top, onemhfrt2_top, inf, mone;
18*f3087befSAndrew Turner } data = {
19*f3087befSAndrew Turner /* Generated using Remez in [ sqrt(2)/2 - 1, sqrt(2) - 1]. Order 20
20*f3087befSAndrew Turner polynomial, however first 2 coefficients are 0 and 1 so are not stored. */
21*f3087befSAndrew Turner .poly = { -0x1.ffffffffffffbp-2, 0x1.55555555551a9p-2, -0x1.00000000008e3p-2,
22*f3087befSAndrew Turner 0x1.9999999a32797p-3, -0x1.555555552fecfp-3, 0x1.249248e071e5ap-3,
23*f3087befSAndrew Turner -0x1.ffffff8bf8482p-4, 0x1.c71c8f07da57ap-4, -0x1.9999ca4ccb617p-4,
24*f3087befSAndrew Turner 0x1.7459ad2e1dfa3p-4, -0x1.554d2680a3ff2p-4, 0x1.3b4c54d487455p-4,
25*f3087befSAndrew Turner -0x1.2548a9ffe80e6p-4, 0x1.0f389a24b2e07p-4, -0x1.eee4db15db335p-5,
26*f3087befSAndrew Turner 0x1.e95b494d4a5ddp-5, -0x1.15fdf07cb7c73p-4, 0x1.0310b70800fcfp-4,
27*f3087befSAndrew Turner -0x1.cfa7385bdb37ep-6, },
28*f3087befSAndrew Turner .ln2_hi = 0x1.62e42fefa3800p-1,
29*f3087befSAndrew Turner .ln2_lo = 0x1.ef35793c76730p-45,
30*f3087befSAndrew Turner /* top32(asuint64(sqrt(2)/2)) << 32. */
31*f3087befSAndrew Turner .hfrt2_top = 0x3fe6a09e00000000,
32*f3087befSAndrew Turner /* (top32(asuint64(1)) - top32(asuint64(sqrt(2)/2))) << 32. */
33*f3087befSAndrew Turner .onemhfrt2_top = 0x00095f6200000000,
34*f3087befSAndrew Turner .inf = 0x7ff0000000000000,
35*f3087befSAndrew Turner .mone = 0xbff0000000000000,
36*f3087befSAndrew Turner };
37*f3087befSAndrew Turner
38*f3087befSAndrew Turner #define AbsMask 0x7fffffffffffffff
39*f3087befSAndrew Turner #define BottomMask 0xffffffff
40*f3087befSAndrew Turner
41*f3087befSAndrew Turner static svfloat64_t NOINLINE
special_case(svbool_t special,svfloat64_t x,svfloat64_t y)42*f3087befSAndrew Turner special_case (svbool_t special, svfloat64_t x, svfloat64_t y)
43*f3087befSAndrew Turner {
44*f3087befSAndrew Turner return sv_call_f64 (log1p, x, y, special);
45*f3087befSAndrew Turner }
46*f3087befSAndrew Turner
47*f3087befSAndrew Turner /* Vector approximation for log1p using polynomial on reduced interval. Maximum
48*f3087befSAndrew Turner observed error is 2.46 ULP:
49*f3087befSAndrew Turner _ZGVsMxv_log1p(0x1.654a1307242a4p+11) got 0x1.fd5565fb590f4p+2
50*f3087befSAndrew Turner want 0x1.fd5565fb590f6p+2. */
SV_NAME_D1(log1p)51*f3087befSAndrew Turner svfloat64_t SV_NAME_D1 (log1p) (svfloat64_t x, svbool_t pg)
52*f3087befSAndrew Turner {
53*f3087befSAndrew Turner const struct data *d = ptr_barrier (&data);
54*f3087befSAndrew Turner svuint64_t ix = svreinterpret_u64 (x);
55*f3087befSAndrew Turner svuint64_t ax = svand_x (pg, ix, AbsMask);
56*f3087befSAndrew Turner svbool_t special
57*f3087befSAndrew Turner = svorr_z (pg, svcmpge (pg, ax, d->inf), svcmpge (pg, ix, d->mone));
58*f3087befSAndrew Turner
59*f3087befSAndrew Turner /* With x + 1 = t * 2^k (where t = f + 1 and k is chosen such that f
60*f3087befSAndrew Turner is in [sqrt(2)/2, sqrt(2)]):
61*f3087befSAndrew Turner log1p(x) = k*log(2) + log1p(f).
62*f3087befSAndrew Turner
63*f3087befSAndrew Turner f may not be representable exactly, so we need a correction term:
64*f3087befSAndrew Turner let m = round(1 + x), c = (1 + x) - m.
65*f3087befSAndrew Turner c << m: at very small x, log1p(x) ~ x, hence:
66*f3087befSAndrew Turner log(1+x) - log(m) ~ c/m.
67*f3087befSAndrew Turner
68*f3087befSAndrew Turner We therefore calculate log1p(x) by k*log2 + log1p(f) + c/m. */
69*f3087befSAndrew Turner
70*f3087befSAndrew Turner /* Obtain correctly scaled k by manipulation in the exponent.
71*f3087befSAndrew Turner The scalar algorithm casts down to 32-bit at this point to calculate k and
72*f3087befSAndrew Turner u_red. We stay in double-width to obtain f and k, using the same constants
73*f3087befSAndrew Turner as the scalar algorithm but shifted left by 32. */
74*f3087befSAndrew Turner svfloat64_t m = svadd_x (pg, x, 1);
75*f3087befSAndrew Turner svuint64_t mi = svreinterpret_u64 (m);
76*f3087befSAndrew Turner svuint64_t u = svadd_x (pg, mi, d->onemhfrt2_top);
77*f3087befSAndrew Turner
78*f3087befSAndrew Turner svint64_t ki = svsub_x (pg, svreinterpret_s64 (svlsr_x (pg, u, 52)), 0x3ff);
79*f3087befSAndrew Turner svfloat64_t k = svcvt_f64_x (pg, ki);
80*f3087befSAndrew Turner
81*f3087befSAndrew Turner /* Reduce x to f in [sqrt(2)/2, sqrt(2)]. */
82*f3087befSAndrew Turner svuint64_t utop
83*f3087befSAndrew Turner = svadd_x (pg, svand_x (pg, u, 0x000fffff00000000), d->hfrt2_top);
84*f3087befSAndrew Turner svuint64_t u_red = svorr_x (pg, utop, svand_x (pg, mi, BottomMask));
85*f3087befSAndrew Turner svfloat64_t f = svsub_x (pg, svreinterpret_f64 (u_red), 1);
86*f3087befSAndrew Turner
87*f3087befSAndrew Turner /* Correction term c/m. */
88*f3087befSAndrew Turner svfloat64_t cm = svdiv_x (pg, svsub_x (pg, x, svsub_x (pg, m, 1)), m);
89*f3087befSAndrew Turner
90*f3087befSAndrew Turner /* Approximate log1p(x) on the reduced input using a polynomial. Because
91*f3087befSAndrew Turner log1p(0)=0 we choose an approximation of the form:
92*f3087befSAndrew Turner x + C0*x^2 + C1*x^3 + C2x^4 + ...
93*f3087befSAndrew Turner Hence approximation has the form f + f^2 * P(f)
94*f3087befSAndrew Turner where P(x) = C0 + C1*x + C2x^2 + ...
95*f3087befSAndrew Turner Assembling this all correctly is dealt with at the final step. */
96*f3087befSAndrew Turner svfloat64_t f2 = svmul_x (pg, f, f), f4 = svmul_x (pg, f2, f2),
97*f3087befSAndrew Turner f8 = svmul_x (pg, f4, f4), f16 = svmul_x (pg, f8, f8);
98*f3087befSAndrew Turner svfloat64_t p = sv_estrin_18_f64_x (pg, f, f2, f4, f8, f16, d->poly);
99*f3087befSAndrew Turner
100*f3087befSAndrew Turner svfloat64_t ylo = svmla_x (pg, cm, k, d->ln2_lo);
101*f3087befSAndrew Turner svfloat64_t yhi = svmla_x (pg, f, k, d->ln2_hi);
102*f3087befSAndrew Turner svfloat64_t y = svmla_x (pg, svadd_x (pg, ylo, yhi), f2, p);
103*f3087befSAndrew Turner
104*f3087befSAndrew Turner if (unlikely (svptest_any (pg, special)))
105*f3087befSAndrew Turner return special_case (special, x, y);
106*f3087befSAndrew Turner
107*f3087befSAndrew Turner return y;
108*f3087befSAndrew Turner }
109*f3087befSAndrew Turner
110*f3087befSAndrew Turner TEST_SIG (SV, D, 1, log1p, -0.9, 10.0)
111*f3087befSAndrew Turner TEST_ULP (SV_NAME_D1 (log1p), 1.97)
112*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_D1 (log1p))
113*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (log1p), 0.0, 0x1p-23, 50000)
114*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (log1p), 0x1p-23, 0.001, 50000)
115*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_D1 (log1p), 0.001, 1.0, 50000)
116*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (log1p), 1, inf, 10000)
117*f3087befSAndrew Turner TEST_INTERVAL (SV_NAME_D1 (log1p), -1, -inf, 10)
118*f3087befSAndrew Turner CLOSE_SVE_ATTR
119