xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/sv_log_inline.h (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Double-precision vector log(x) function - inline version
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 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 "math_config.h"
10*f3087befSAndrew Turner 
11*f3087befSAndrew Turner #ifndef SV_LOG_INLINE_POLY_ORDER
12*f3087befSAndrew Turner #  error Cannot use inline log helper without specifying poly order (options are 4 or 5)
13*f3087befSAndrew Turner #endif
14*f3087befSAndrew Turner 
15*f3087befSAndrew Turner #if SV_LOG_INLINE_POLY_ORDER == 4
16*f3087befSAndrew Turner #  define POLY                                                                \
17*f3087befSAndrew Turner     {                                                                         \
18*f3087befSAndrew Turner       -0x1.ffffffffcbad3p-2, 0x1.555555578ed68p-2, -0x1.0000d3a1e7055p-2,     \
19*f3087befSAndrew Turner 	  0x1.999392d02a63ep-3                                                \
20*f3087befSAndrew Turner     }
21*f3087befSAndrew Turner #elif SV_LOG_INLINE_POLY_ORDER == 5
22*f3087befSAndrew Turner #  define POLY                                                                \
23*f3087befSAndrew Turner     {                                                                         \
24*f3087befSAndrew Turner       -0x1.ffffffffffff7p-2, 0x1.55555555170d4p-2, -0x1.0000000399c27p-2,     \
25*f3087befSAndrew Turner 	  0x1.999b2e90e94cap-3, -0x1.554e550bd501ep-3                         \
26*f3087befSAndrew Turner     }
27*f3087befSAndrew Turner #else
28*f3087befSAndrew Turner #  error Can only choose order 4 or 5 for log poly
29*f3087befSAndrew Turner #endif
30*f3087befSAndrew Turner 
31*f3087befSAndrew Turner struct sv_log_inline_data
32*f3087befSAndrew Turner {
33*f3087befSAndrew Turner   double poly[SV_LOG_INLINE_POLY_ORDER];
34*f3087befSAndrew Turner   double ln2;
35*f3087befSAndrew Turner   uint64_t off, sign_exp_mask;
36*f3087befSAndrew Turner };
37*f3087befSAndrew Turner 
38*f3087befSAndrew Turner #define SV_LOG_CONSTANTS                                                      \
39*f3087befSAndrew Turner   {                                                                           \
40*f3087befSAndrew Turner     .poly = POLY, .ln2 = 0x1.62e42fefa39efp-1,                                \
41*f3087befSAndrew Turner     .sign_exp_mask = 0xfff0000000000000, .off = 0x3fe6900900000000            \
42*f3087befSAndrew Turner   }
43*f3087befSAndrew Turner 
44*f3087befSAndrew Turner #define P(i) sv_f64 (d->poly[i])
45*f3087befSAndrew Turner #define N (1 << V_LOG_TABLE_BITS)
46*f3087befSAndrew Turner 
47*f3087befSAndrew Turner static inline svfloat64_t
sv_log_inline(svbool_t pg,svfloat64_t x,const struct sv_log_inline_data * d)48*f3087befSAndrew Turner sv_log_inline (svbool_t pg, svfloat64_t x, const struct sv_log_inline_data *d)
49*f3087befSAndrew Turner {
50*f3087befSAndrew Turner   svuint64_t ix = svreinterpret_u64 (x);
51*f3087befSAndrew Turner 
52*f3087befSAndrew Turner   /* x = 2^k z; where z is in range [Off,2*Off) and exact.
53*f3087befSAndrew Turner      The range is split into N subintervals.
54*f3087befSAndrew Turner      The ith subinterval contains z and c is near its center.  */
55*f3087befSAndrew Turner   svuint64_t tmp = svsub_x (pg, ix, d->off);
56*f3087befSAndrew Turner   /* Calculate table index = (tmp >> (52 - V_LOG_TABLE_BITS)) % N.
57*f3087befSAndrew Turner      The actual value of i is double this due to table layout.  */
58*f3087befSAndrew Turner   svuint64_t i
59*f3087befSAndrew Turner       = svand_x (pg, svlsr_x (pg, tmp, (51 - V_LOG_TABLE_BITS)), (N - 1) << 1);
60*f3087befSAndrew Turner   svint64_t k
61*f3087befSAndrew Turner       = svasr_x (pg, svreinterpret_s64 (tmp), 52); /* Arithmetic shift.  */
62*f3087befSAndrew Turner   svuint64_t iz = svsub_x (pg, ix, svand_x (pg, tmp, 0xfffULL << 52));
63*f3087befSAndrew Turner   svfloat64_t z = svreinterpret_f64 (iz);
64*f3087befSAndrew Turner 
65*f3087befSAndrew Turner   /* Lookup in 2 global lists (length N).  */
66*f3087befSAndrew Turner   svfloat64_t invc = svld1_gather_index (pg, &__v_log_data.table[0].invc, i);
67*f3087befSAndrew Turner   svfloat64_t logc = svld1_gather_index (pg, &__v_log_data.table[0].logc, i);
68*f3087befSAndrew Turner 
69*f3087befSAndrew Turner   /* log(x) = log1p(z/c-1) + log(c) + k*Ln2.  */
70*f3087befSAndrew Turner   svfloat64_t r = svmad_x (pg, invc, z, -1);
71*f3087befSAndrew Turner   svfloat64_t kd = svcvt_f64_x (pg, k);
72*f3087befSAndrew Turner   /* hi = r + log(c) + k*Ln2.  */
73*f3087befSAndrew Turner   svfloat64_t hi = svmla_x (pg, svadd_x (pg, logc, r), kd, __v_log_data.ln2);
74*f3087befSAndrew Turner   /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi.  */
75*f3087befSAndrew Turner   svfloat64_t r2 = svmul_x (pg, r, r);
76*f3087befSAndrew Turner   svfloat64_t y = svmla_x (pg, P (2), r, P (3));
77*f3087befSAndrew Turner   svfloat64_t p = svmla_x (pg, P (0), r, P (1));
78*f3087befSAndrew Turner #if SV_LOG_INLINE_POLY_ORDER == 5
79*f3087befSAndrew Turner   y = svmla_x (pg, P (4), r2);
80*f3087befSAndrew Turner #endif
81*f3087befSAndrew Turner   y = svmla_x (pg, p, r2, y);
82*f3087befSAndrew Turner   return svmla_x (pg, hi, r2, y);
83*f3087befSAndrew Turner }
84