1*412f47f9SXin Li /*
2*412f47f9SXin Li * Double-precision vector log(x) function - inline version
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2019-2024, 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 "math_config.h"
10*412f47f9SXin Li
11*412f47f9SXin Li #ifndef V_LOG_INLINE_POLY_ORDER
12*412f47f9SXin Li # error Cannot use inline log helper without specifying poly order (options are 4 or 5)
13*412f47f9SXin Li #endif
14*412f47f9SXin Li
15*412f47f9SXin Li #if V_LOG_INLINE_POLY_ORDER == 4
16*412f47f9SXin Li # define POLY \
17*412f47f9SXin Li { \
18*412f47f9SXin Li V2 (-0x1.ffffffffcbad3p-2), V2 (0x1.555555578ed68p-2), \
19*412f47f9SXin Li V2 (-0x1.0000d3a1e7055p-2), V2 (0x1.999392d02a63ep-3) \
20*412f47f9SXin Li }
21*412f47f9SXin Li #elif V_LOG_INLINE_POLY_ORDER == 5
22*412f47f9SXin Li # define POLY \
23*412f47f9SXin Li { \
24*412f47f9SXin Li V2 (-0x1.ffffffffffff7p-2), V2 (0x1.55555555170d4p-2), \
25*412f47f9SXin Li V2 (-0x1.0000000399c27p-2), V2 (0x1.999b2e90e94cap-3), \
26*412f47f9SXin Li V2 (-0x1.554e550bd501ep-3) \
27*412f47f9SXin Li }
28*412f47f9SXin Li #else
29*412f47f9SXin Li # error Can only choose order 4 or 5 for log poly
30*412f47f9SXin Li #endif
31*412f47f9SXin Li
32*412f47f9SXin Li struct v_log_inline_data
33*412f47f9SXin Li {
34*412f47f9SXin Li float64x2_t poly[V_LOG_INLINE_POLY_ORDER];
35*412f47f9SXin Li float64x2_t ln2;
36*412f47f9SXin Li uint64x2_t off, sign_exp_mask;
37*412f47f9SXin Li };
38*412f47f9SXin Li
39*412f47f9SXin Li #define V_LOG_CONSTANTS \
40*412f47f9SXin Li { \
41*412f47f9SXin Li .poly = POLY, .ln2 = V2 (0x1.62e42fefa39efp-1), \
42*412f47f9SXin Li .sign_exp_mask = V2 (0xfff0000000000000), .off = V2 (0x3fe6900900000000) \
43*412f47f9SXin Li }
44*412f47f9SXin Li
45*412f47f9SXin Li #define A(i) d->poly[i]
46*412f47f9SXin Li #define N (1 << V_LOG_TABLE_BITS)
47*412f47f9SXin Li #define IndexMask (N - 1)
48*412f47f9SXin Li
49*412f47f9SXin Li struct entry
50*412f47f9SXin Li {
51*412f47f9SXin Li float64x2_t invc;
52*412f47f9SXin Li float64x2_t logc;
53*412f47f9SXin Li };
54*412f47f9SXin Li
55*412f47f9SXin Li static inline struct entry
log_lookup(uint64x2_t i)56*412f47f9SXin Li log_lookup (uint64x2_t i)
57*412f47f9SXin Li {
58*412f47f9SXin Li /* Since N is a power of 2, n % N = n & (N - 1). */
59*412f47f9SXin Li struct entry e;
60*412f47f9SXin Li uint64_t i0 = (vgetq_lane_u64 (i, 0) >> (52 - V_LOG_TABLE_BITS)) & IndexMask;
61*412f47f9SXin Li uint64_t i1 = (vgetq_lane_u64 (i, 1) >> (52 - V_LOG_TABLE_BITS)) & IndexMask;
62*412f47f9SXin Li float64x2_t e0 = vld1q_f64 (&__v_log_data.table[i0].invc);
63*412f47f9SXin Li float64x2_t e1 = vld1q_f64 (&__v_log_data.table[i1].invc);
64*412f47f9SXin Li e.invc = vuzp1q_f64 (e0, e1);
65*412f47f9SXin Li e.logc = vuzp2q_f64 (e0, e1);
66*412f47f9SXin Li return e;
67*412f47f9SXin Li }
68*412f47f9SXin Li
69*412f47f9SXin Li static inline float64x2_t
v_log_inline(float64x2_t x,const struct v_log_inline_data * d)70*412f47f9SXin Li v_log_inline (float64x2_t x, const struct v_log_inline_data *d)
71*412f47f9SXin Li {
72*412f47f9SXin Li float64x2_t z, r, r2, p, y, kd, hi;
73*412f47f9SXin Li uint64x2_t ix, iz, tmp;
74*412f47f9SXin Li int64x2_t k;
75*412f47f9SXin Li struct entry e;
76*412f47f9SXin Li
77*412f47f9SXin Li ix = vreinterpretq_u64_f64 (x);
78*412f47f9SXin Li
79*412f47f9SXin Li /* x = 2^k z; where z is in range [Off,2*Off) and exact.
80*412f47f9SXin Li The range is split into N subintervals.
81*412f47f9SXin Li The ith subinterval contains z and c is near its center. */
82*412f47f9SXin Li tmp = vsubq_u64 (ix, d->off);
83*412f47f9SXin Li k = vshrq_n_s64 (vreinterpretq_s64_u64 (tmp), 52); /* arithmetic shift. */
84*412f47f9SXin Li iz = vsubq_u64 (ix, vandq_u64 (tmp, d->sign_exp_mask));
85*412f47f9SXin Li z = vreinterpretq_f64_u64 (iz);
86*412f47f9SXin Li e = log_lookup (tmp);
87*412f47f9SXin Li
88*412f47f9SXin Li /* log(x) = log1p(z/c-1) + log(c) + k*Ln2. */
89*412f47f9SXin Li r = vfmaq_f64 (v_f64 (-1.0), z, e.invc);
90*412f47f9SXin Li kd = vcvtq_f64_s64 (k);
91*412f47f9SXin Li
92*412f47f9SXin Li /* hi = r + log(c) + k*Ln2. */
93*412f47f9SXin Li hi = vfmaq_f64 (vaddq_f64 (e.logc, r), kd, d->ln2);
94*412f47f9SXin Li /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi. */
95*412f47f9SXin Li r2 = vmulq_f64 (r, r);
96*412f47f9SXin Li y = vfmaq_f64 (A (2), A (3), r);
97*412f47f9SXin Li p = vfmaq_f64 (A (0), A (1), r);
98*412f47f9SXin Li #if V_LOG_POLY_ORDER == 5
99*412f47f9SXin Li y = vfmaq_f64 (y, A (4), r2);
100*412f47f9SXin Li #endif
101*412f47f9SXin Li y = vfmaq_f64 (p, y, r2);
102*412f47f9SXin Li
103*412f47f9SXin Li return vfmaq_f64 (hi, y, r2);
104*412f47f9SXin Li }
105