1 /*
2 * Double-precision SVE log(x) function.
3 *
4 * Copyright (c) 2020-2023, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7
8 #include "sv_math.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11
12 #if SV_SUPPORTED
13
14 #define A(i) __sv_log_data.poly[i]
15 #define Ln2 (0x1.62e42fefa39efp-1)
16 #define N (1 << SV_LOG_TABLE_BITS)
17 #define OFF (0x3fe6900900000000)
18
19 double
20 optr_aor_log_f64 (double);
21
22 static NOINLINE sv_f64_t
__sv_log_specialcase(sv_f64_t x,sv_f64_t y,svbool_t cmp)23 __sv_log_specialcase (sv_f64_t x, sv_f64_t y, svbool_t cmp)
24 {
25 return sv_call_f64 (optr_aor_log_f64, x, y, cmp);
26 }
27
28 /* SVE port of Neon log algorithm from math/.
29 Maximum measured error is 2.17 ulp:
30 __sv_log(0x1.a6129884398a3p+0) got 0x1.ffffff1cca043p-2
31 want 0x1.ffffff1cca045p-2. */
32 sv_f64_t
__sv_log_x(sv_f64_t x,const svbool_t pg)33 __sv_log_x (sv_f64_t x, const svbool_t pg)
34 {
35 sv_u64_t ix = sv_as_u64_f64 (x);
36 sv_u64_t top = svlsr_n_u64_x (pg, ix, 48);
37 svbool_t cmp = svcmpge_u64 (pg, svsub_n_u64_x (pg, top, 0x0010),
38 sv_u64 (0x7ff0 - 0x0010));
39
40 /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
41 The range is split into N subintervals.
42 The ith subinterval contains z and c is near its center. */
43 sv_u64_t tmp = svsub_n_u64_x (pg, ix, OFF);
44 /* Equivalent to (tmp >> (52 - SV_LOG_TABLE_BITS)) % N, since N is a power
45 of 2. */
46 sv_u64_t i
47 = svand_n_u64_x (pg, svlsr_n_u64_x (pg, tmp, (52 - SV_LOG_TABLE_BITS)),
48 N - 1);
49 sv_s64_t k
50 = svasr_n_s64_x (pg, sv_as_s64_u64 (tmp), 52); /* Arithmetic shift. */
51 sv_u64_t iz = svsub_u64_x (pg, ix, svand_n_u64_x (pg, tmp, 0xfffULL << 52));
52 sv_f64_t z = sv_as_f64_u64 (iz);
53 /* Lookup in 2 global lists (length N). */
54 sv_f64_t invc = sv_lookup_f64_x (pg, __sv_log_data.invc, i);
55 sv_f64_t logc = sv_lookup_f64_x (pg, __sv_log_data.logc, i);
56
57 /* log(x) = log1p(z/c-1) + log(c) + k*Ln2. */
58 sv_f64_t r = sv_fma_f64_x (pg, z, invc, sv_f64 (-1.0));
59 sv_f64_t kd = sv_to_f64_s64_x (pg, k);
60 /* hi = r + log(c) + k*Ln2. */
61 sv_f64_t hi = sv_fma_n_f64_x (pg, Ln2, kd, svadd_f64_x (pg, logc, r));
62 /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi. */
63 sv_f64_t r2 = svmul_f64_x (pg, r, r);
64 sv_f64_t y = sv_fma_n_f64_x (pg, A (3), r, sv_f64 (A (2)));
65 sv_f64_t p = sv_fma_n_f64_x (pg, A (1), r, sv_f64 (A (0)));
66 y = sv_fma_n_f64_x (pg, A (4), r2, y);
67 y = sv_fma_f64_x (pg, y, r2, p);
68 y = sv_fma_f64_x (pg, y, r2, hi);
69
70 if (unlikely (svptest_any (pg, cmp)))
71 return __sv_log_specialcase (x, y, cmp);
72 return y;
73 }
74
75 PL_ALIAS (__sv_log_x, _ZGVsMxv_log)
76
77 PL_SIG (SV, D, 1, log, 0.01, 11.1)
78 PL_TEST_ULP (__sv_log, 1.68)
79 PL_TEST_INTERVAL (__sv_log, -0.0, -0x1p126, 100)
80 PL_TEST_INTERVAL (__sv_log, 0x1p-149, 0x1p-126, 4000)
81 PL_TEST_INTERVAL (__sv_log, 0x1p-126, 0x1p-23, 50000)
82 PL_TEST_INTERVAL (__sv_log, 0x1p-23, 1.0, 50000)
83 PL_TEST_INTERVAL (__sv_log, 1.0, 100, 50000)
84 PL_TEST_INTERVAL (__sv_log, 100, inf, 50000)
85 #endif // SV_SUPPORTED
86