xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_log1p_inline.h (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Helper for vector double-precision routines which calculate log(1 + x) and do
3*412f47f9SXin Li  * not need special-case handling
4*412f47f9SXin Li  *
5*412f47f9SXin Li  * Copyright (c) 2022-2023, Arm Limited.
6*412f47f9SXin Li  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
7*412f47f9SXin Li  */
8*412f47f9SXin Li #ifndef PL_MATH_V_LOG1P_INLINE_H
9*412f47f9SXin Li #define PL_MATH_V_LOG1P_INLINE_H
10*412f47f9SXin Li 
11*412f47f9SXin Li #include "v_math.h"
12*412f47f9SXin Li #include "poly_advsimd_f64.h"
13*412f47f9SXin Li 
14*412f47f9SXin Li struct v_log1p_data
15*412f47f9SXin Li {
16*412f47f9SXin Li   float64x2_t poly[19], ln2[2];
17*412f47f9SXin Li   uint64x2_t hf_rt2_top, one_m_hf_rt2_top, umask;
18*412f47f9SXin Li   int64x2_t one_top;
19*412f47f9SXin Li };
20*412f47f9SXin Li 
21*412f47f9SXin Li /* Coefficients generated using Remez, deg=20, in [sqrt(2)/2-1, sqrt(2)-1].  */
22*412f47f9SXin Li #define V_LOG1P_CONSTANTS_TABLE                                               \
23*412f47f9SXin Li   {                                                                           \
24*412f47f9SXin Li     .poly = { V2 (-0x1.ffffffffffffbp-2), V2 (0x1.55555555551a9p-2),          \
25*412f47f9SXin Li 	      V2 (-0x1.00000000008e3p-2), V2 (0x1.9999999a32797p-3),          \
26*412f47f9SXin Li 	      V2 (-0x1.555555552fecfp-3), V2 (0x1.249248e071e5ap-3),          \
27*412f47f9SXin Li 	      V2 (-0x1.ffffff8bf8482p-4), V2 (0x1.c71c8f07da57ap-4),          \
28*412f47f9SXin Li 	      V2 (-0x1.9999ca4ccb617p-4), V2 (0x1.7459ad2e1dfa3p-4),          \
29*412f47f9SXin Li 	      V2 (-0x1.554d2680a3ff2p-4), V2 (0x1.3b4c54d487455p-4),          \
30*412f47f9SXin Li 	      V2 (-0x1.2548a9ffe80e6p-4), V2 (0x1.0f389a24b2e07p-4),          \
31*412f47f9SXin Li 	      V2 (-0x1.eee4db15db335p-5), V2 (0x1.e95b494d4a5ddp-5),          \
32*412f47f9SXin Li 	      V2 (-0x1.15fdf07cb7c73p-4), V2 (0x1.0310b70800fcfp-4),          \
33*412f47f9SXin Li 	      V2 (-0x1.cfa7385bdb37ep-6) },                                   \
34*412f47f9SXin Li     .ln2 = { V2 (0x1.62e42fefa3800p-1), V2 (0x1.ef35793c76730p-45) },         \
35*412f47f9SXin Li     .hf_rt2_top = V2 (0x3fe6a09e00000000),                                    \
36*412f47f9SXin Li     .one_m_hf_rt2_top = V2 (0x00095f6200000000),                              \
37*412f47f9SXin Li     .umask = V2 (0x000fffff00000000), .one_top = V2 (0x3ff)                   \
38*412f47f9SXin Li   }
39*412f47f9SXin Li 
40*412f47f9SXin Li #define BottomMask v_u64 (0xffffffff)
41*412f47f9SXin Li 
42*412f47f9SXin Li static inline float64x2_t
log1p_inline(float64x2_t x,const struct v_log1p_data * d)43*412f47f9SXin Li log1p_inline (float64x2_t x, const struct v_log1p_data *d)
44*412f47f9SXin Li {
45*412f47f9SXin Li   /* Helper for calculating log(x + 1). Copied from v_log1p_2u5.c, with several
46*412f47f9SXin Li      modifications:
47*412f47f9SXin Li      - No special-case handling - this should be dealt with by the caller.
48*412f47f9SXin Li      - Pairwise Horner polynomial evaluation for improved accuracy.
49*412f47f9SXin Li      - Optionally simulate the shortcut for k=0, used in the scalar routine,
50*412f47f9SXin Li        using v_sel, for improved accuracy when the argument to log1p is close to
51*412f47f9SXin Li        0. This feature is enabled by defining WANT_V_LOG1P_K0_SHORTCUT as 1 in
52*412f47f9SXin Li        the source of the caller before including this file.
53*412f47f9SXin Li      See v_log1pf_2u1.c for details of the algorithm.  */
54*412f47f9SXin Li   float64x2_t m = vaddq_f64 (x, v_f64 (1));
55*412f47f9SXin Li   uint64x2_t mi = vreinterpretq_u64_f64 (m);
56*412f47f9SXin Li   uint64x2_t u = vaddq_u64 (mi, d->one_m_hf_rt2_top);
57*412f47f9SXin Li 
58*412f47f9SXin Li   int64x2_t ki
59*412f47f9SXin Li       = vsubq_s64 (vreinterpretq_s64_u64 (vshrq_n_u64 (u, 52)), d->one_top);
60*412f47f9SXin Li   float64x2_t k = vcvtq_f64_s64 (ki);
61*412f47f9SXin Li 
62*412f47f9SXin Li   /* Reduce x to f in [sqrt(2)/2, sqrt(2)].  */
63*412f47f9SXin Li   uint64x2_t utop = vaddq_u64 (vandq_u64 (u, d->umask), d->hf_rt2_top);
64*412f47f9SXin Li   uint64x2_t u_red = vorrq_u64 (utop, vandq_u64 (mi, BottomMask));
65*412f47f9SXin Li   float64x2_t f = vsubq_f64 (vreinterpretq_f64_u64 (u_red), v_f64 (1));
66*412f47f9SXin Li 
67*412f47f9SXin Li   /* Correction term c/m.  */
68*412f47f9SXin Li   float64x2_t cm = vdivq_f64 (vsubq_f64 (x, vsubq_f64 (m, v_f64 (1))), m);
69*412f47f9SXin Li 
70*412f47f9SXin Li #ifndef WANT_V_LOG1P_K0_SHORTCUT
71*412f47f9SXin Li #error                                                                         \
72*412f47f9SXin Li   "Cannot use v_log1p_inline.h without specifying whether you need the k0 shortcut for greater accuracy close to 0"
73*412f47f9SXin Li #elif WANT_V_LOG1P_K0_SHORTCUT
74*412f47f9SXin Li   /* Shortcut if k is 0 - set correction term to 0 and f to x. The result is
75*412f47f9SXin Li      that the approximation is solely the polynomial.  */
76*412f47f9SXin Li   uint64x2_t k0 = vceqzq_f64 (k);
77*412f47f9SXin Li   cm = v_zerofy_f64 (cm, k0);
78*412f47f9SXin Li   f = vbslq_f64 (k0, x, f);
79*412f47f9SXin Li #endif
80*412f47f9SXin Li 
81*412f47f9SXin Li   /* Approximate log1p(f) on the reduced input using a polynomial.  */
82*412f47f9SXin Li   float64x2_t f2 = vmulq_f64 (f, f);
83*412f47f9SXin Li   float64x2_t p = v_pw_horner_18_f64 (f, f2, d->poly);
84*412f47f9SXin Li 
85*412f47f9SXin Li   /* Assemble log1p(x) = k * log2 + log1p(f) + c/m.  */
86*412f47f9SXin Li   float64x2_t ylo = vfmaq_f64 (cm, k, d->ln2[1]);
87*412f47f9SXin Li   float64x2_t yhi = vfmaq_f64 (f, k, d->ln2[0]);
88*412f47f9SXin Li   return vfmaq_f64 (vaddq_f64 (ylo, yhi), f2, p);
89*412f47f9SXin Li }
90*412f47f9SXin Li 
91*412f47f9SXin Li #endif // PL_MATH_V_LOG1P_INLINE_H
92