xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/advsimd/v_log1p_inline.h (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Helper for vector double-precision routines which calculate log(1 + x) and
3*f3087befSAndrew Turner  * do not need special-case handling
4*f3087befSAndrew Turner  *
5*f3087befSAndrew Turner  * Copyright (c) 2022-2024, Arm Limited.
6*f3087befSAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
7*f3087befSAndrew Turner  */
8*f3087befSAndrew Turner #ifndef MATH_V_LOG1P_INLINE_H
9*f3087befSAndrew Turner #define MATH_V_LOG1P_INLINE_H
10*f3087befSAndrew Turner 
11*f3087befSAndrew Turner #include "v_math.h"
12*f3087befSAndrew Turner 
13*f3087befSAndrew Turner struct v_log1p_data
14*f3087befSAndrew Turner {
15*f3087befSAndrew Turner   float64x2_t c0, c2, c4, c6, c8, c10, c12, c14, c16;
16*f3087befSAndrew Turner   uint64x2_t hf_rt2_top, one_m_hf_rt2_top, umask;
17*f3087befSAndrew Turner   int64x2_t one_top;
18*f3087befSAndrew Turner   double c1, c3, c5, c7, c9, c11, c13, c15, c17, c18;
19*f3087befSAndrew Turner   double ln2[2];
20*f3087befSAndrew Turner };
21*f3087befSAndrew Turner 
22*f3087befSAndrew Turner /* Coefficients generated using Remez, deg=20, in [sqrt(2)/2-1, sqrt(2)-1].  */
23*f3087befSAndrew Turner #define V_LOG1P_CONSTANTS_TABLE                                               \
24*f3087befSAndrew Turner   {                                                                           \
25*f3087befSAndrew Turner     .c0 = V2 (-0x1.ffffffffffffbp-2), .c1 = 0x1.55555555551a9p-2,             \
26*f3087befSAndrew Turner     .c2 = V2 (-0x1.00000000008e3p-2), .c3 = 0x1.9999999a32797p-3,             \
27*f3087befSAndrew Turner     .c4 = V2 (-0x1.555555552fecfp-3), .c5 = 0x1.249248e071e5ap-3,             \
28*f3087befSAndrew Turner     .c6 = V2 (-0x1.ffffff8bf8482p-4), .c7 = 0x1.c71c8f07da57ap-4,             \
29*f3087befSAndrew Turner     .c8 = V2 (-0x1.9999ca4ccb617p-4), .c9 = 0x1.7459ad2e1dfa3p-4,             \
30*f3087befSAndrew Turner     .c10 = V2 (-0x1.554d2680a3ff2p-4), .c11 = 0x1.3b4c54d487455p-4,           \
31*f3087befSAndrew Turner     .c12 = V2 (-0x1.2548a9ffe80e6p-4), .c13 = 0x1.0f389a24b2e07p-4,           \
32*f3087befSAndrew Turner     .c14 = V2 (-0x1.eee4db15db335p-5), .c15 = 0x1.e95b494d4a5ddp-5,           \
33*f3087befSAndrew Turner     .c16 = V2 (-0x1.15fdf07cb7c73p-4), .c17 = 0x1.0310b70800fcfp-4,           \
34*f3087befSAndrew Turner     .c18 = -0x1.cfa7385bdb37ep-6,                                             \
35*f3087befSAndrew Turner     .ln2 = { 0x1.62e42fefa3800p-1, 0x1.ef35793c76730p-45 },                   \
36*f3087befSAndrew Turner     .hf_rt2_top = V2 (0x3fe6a09e00000000),                                    \
37*f3087befSAndrew Turner     .one_m_hf_rt2_top = V2 (0x00095f6200000000),                              \
38*f3087befSAndrew Turner     .umask = V2 (0x000fffff00000000), .one_top = V2 (0x3ff)                   \
39*f3087befSAndrew Turner   }
40*f3087befSAndrew Turner 
41*f3087befSAndrew Turner #define BottomMask v_u64 (0xffffffff)
42*f3087befSAndrew Turner 
43*f3087befSAndrew Turner static inline float64x2_t
eval_poly(float64x2_t m,float64x2_t m2,const struct v_log1p_data * d)44*f3087befSAndrew Turner eval_poly (float64x2_t m, float64x2_t m2, const struct v_log1p_data *d)
45*f3087befSAndrew Turner {
46*f3087befSAndrew Turner   /* Approximate log(1+m) on [-0.25, 0.5] using pairwise Horner.  */
47*f3087befSAndrew Turner   float64x2_t c13 = vld1q_f64 (&d->c1);
48*f3087befSAndrew Turner   float64x2_t c57 = vld1q_f64 (&d->c5);
49*f3087befSAndrew Turner   float64x2_t c911 = vld1q_f64 (&d->c9);
50*f3087befSAndrew Turner   float64x2_t c1315 = vld1q_f64 (&d->c13);
51*f3087befSAndrew Turner   float64x2_t c1718 = vld1q_f64 (&d->c17);
52*f3087befSAndrew Turner   float64x2_t p1617 = vfmaq_laneq_f64 (d->c16, m, c1718, 0);
53*f3087befSAndrew Turner   float64x2_t p1415 = vfmaq_laneq_f64 (d->c14, m, c1315, 1);
54*f3087befSAndrew Turner   float64x2_t p1213 = vfmaq_laneq_f64 (d->c12, m, c1315, 0);
55*f3087befSAndrew Turner   float64x2_t p1011 = vfmaq_laneq_f64 (d->c10, m, c911, 1);
56*f3087befSAndrew Turner   float64x2_t p89 = vfmaq_laneq_f64 (d->c8, m, c911, 0);
57*f3087befSAndrew Turner   float64x2_t p67 = vfmaq_laneq_f64 (d->c6, m, c57, 1);
58*f3087befSAndrew Turner   float64x2_t p45 = vfmaq_laneq_f64 (d->c4, m, c57, 0);
59*f3087befSAndrew Turner   float64x2_t p23 = vfmaq_laneq_f64 (d->c2, m, c13, 1);
60*f3087befSAndrew Turner   float64x2_t p01 = vfmaq_laneq_f64 (d->c0, m, c13, 0);
61*f3087befSAndrew Turner   float64x2_t p = vfmaq_laneq_f64 (p1617, m2, c1718, 1);
62*f3087befSAndrew Turner   p = vfmaq_f64 (p1415, m2, p);
63*f3087befSAndrew Turner   p = vfmaq_f64 (p1213, m2, p);
64*f3087befSAndrew Turner   p = vfmaq_f64 (p1011, m2, p);
65*f3087befSAndrew Turner   p = vfmaq_f64 (p89, m2, p);
66*f3087befSAndrew Turner   p = vfmaq_f64 (p67, m2, p);
67*f3087befSAndrew Turner   p = vfmaq_f64 (p45, m2, p);
68*f3087befSAndrew Turner   p = vfmaq_f64 (p23, m2, p);
69*f3087befSAndrew Turner   return vfmaq_f64 (p01, m2, p);
70*f3087befSAndrew Turner }
71*f3087befSAndrew Turner 
72*f3087befSAndrew Turner static inline float64x2_t
log1p_inline(float64x2_t x,const struct v_log1p_data * d)73*f3087befSAndrew Turner log1p_inline (float64x2_t x, const struct v_log1p_data *d)
74*f3087befSAndrew Turner {
75*f3087befSAndrew Turner   /* Helper for calculating log(x + 1):
76*f3087befSAndrew Turner      - No special-case handling - this should be dealt with by the caller.
77*f3087befSAndrew Turner      - Optionally simulate the shortcut for k=0, used in the scalar routine,
78*f3087befSAndrew Turner        using v_sel, for improved accuracy when the argument to log1p is close
79*f3087befSAndrew Turner        to 0. This feature is enabled by defining WANT_V_LOG1P_K0_SHORTCUT as 1
80*f3087befSAndrew Turner        in the source of the caller before including this file.  */
81*f3087befSAndrew Turner   float64x2_t m = vaddq_f64 (x, v_f64 (1.0));
82*f3087befSAndrew Turner   uint64x2_t mi = vreinterpretq_u64_f64 (m);
83*f3087befSAndrew Turner   uint64x2_t u = vaddq_u64 (mi, d->one_m_hf_rt2_top);
84*f3087befSAndrew Turner 
85*f3087befSAndrew Turner   int64x2_t ki
86*f3087befSAndrew Turner       = vsubq_s64 (vreinterpretq_s64_u64 (vshrq_n_u64 (u, 52)), d->one_top);
87*f3087befSAndrew Turner   float64x2_t k = vcvtq_f64_s64 (ki);
88*f3087befSAndrew Turner 
89*f3087befSAndrew Turner   /* Reduce x to f in [sqrt(2)/2, sqrt(2)].  */
90*f3087befSAndrew Turner   uint64x2_t utop = vaddq_u64 (vandq_u64 (u, d->umask), d->hf_rt2_top);
91*f3087befSAndrew Turner   uint64x2_t u_red = vorrq_u64 (utop, vandq_u64 (mi, BottomMask));
92*f3087befSAndrew Turner   float64x2_t f = vsubq_f64 (vreinterpretq_f64_u64 (u_red), v_f64 (1.0));
93*f3087befSAndrew Turner 
94*f3087befSAndrew Turner   /* Correction term c/m.  */
95*f3087befSAndrew Turner   float64x2_t cm = vdivq_f64 (vsubq_f64 (x, vsubq_f64 (m, v_f64 (1.0))), m);
96*f3087befSAndrew Turner 
97*f3087befSAndrew Turner #ifndef WANT_V_LOG1P_K0_SHORTCUT
98*f3087befSAndrew Turner # error                                                                       \
99*f3087befSAndrew Turner       "Cannot use v_log1p_inline.h without specifying whether you need the k0 shortcut for greater accuracy close to 0"
100*f3087befSAndrew Turner #elif WANT_V_LOG1P_K0_SHORTCUT
101*f3087befSAndrew Turner   /* Shortcut if k is 0 - set correction term to 0 and f to x. The result is
102*f3087befSAndrew Turner      that the approximation is solely the polynomial.  */
103*f3087befSAndrew Turner   uint64x2_t k0 = vceqzq_f64 (k);
104*f3087befSAndrew Turner   cm = v_zerofy_f64 (cm, k0);
105*f3087befSAndrew Turner   f = vbslq_f64 (k0, x, f);
106*f3087befSAndrew Turner #endif
107*f3087befSAndrew Turner 
108*f3087befSAndrew Turner   /* Approximate log1p(f) on the reduced input using a polynomial.  */
109*f3087befSAndrew Turner   float64x2_t f2 = vmulq_f64 (f, f);
110*f3087befSAndrew Turner   float64x2_t p = eval_poly (f, f2, d);
111*f3087befSAndrew Turner 
112*f3087befSAndrew Turner   /* Assemble log1p(x) = k * log2 + log1p(f) + c/m.  */
113*f3087befSAndrew Turner   float64x2_t ln2 = vld1q_f64 (&d->ln2[0]);
114*f3087befSAndrew Turner   float64x2_t ylo = vfmaq_laneq_f64 (cm, k, ln2, 1);
115*f3087befSAndrew Turner   float64x2_t yhi = vfmaq_laneq_f64 (f, k, ln2, 0);
116*f3087befSAndrew Turner   return vfmaq_f64 (vaddq_f64 (ylo, yhi), f2, p);
117*f3087befSAndrew Turner }
118*f3087befSAndrew Turner 
119*f3087befSAndrew Turner #endif // MATH_V_LOG1P_INLINE_H
120