1 /*
2  * Helper for SVE double-precision routines which calculate log(1 + x) and do
3  * not need special-case handling
4  *
5  * Copyright (c) 2022-2023, Arm Limited.
6  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
7  */
8 #ifndef PL_MATH_SV_LOG1P_INLINE_H
9 #define PL_MATH_SV_LOG1P_INLINE_H
10 
11 #include "sv_math.h"
12 #include "poly_sve_f64.h"
13 
14 static const struct sv_log1p_data
15 {
16   double poly[19], ln2[2];
17   uint64_t hf_rt2_top;
18   uint64_t one_m_hf_rt2_top;
19   uint32_t bottom_mask;
20   int64_t one_top;
21 } sv_log1p_data = {
22   /* Coefficients generated using Remez, deg=20, in [sqrt(2)/2-1, sqrt(2)-1].
23    */
24   .poly = { -0x1.ffffffffffffbp-2, 0x1.55555555551a9p-2, -0x1.00000000008e3p-2,
25 	    0x1.9999999a32797p-3, -0x1.555555552fecfp-3, 0x1.249248e071e5ap-3,
26 	    -0x1.ffffff8bf8482p-4, 0x1.c71c8f07da57ap-4, -0x1.9999ca4ccb617p-4,
27 	    0x1.7459ad2e1dfa3p-4, -0x1.554d2680a3ff2p-4, 0x1.3b4c54d487455p-4,
28 	    -0x1.2548a9ffe80e6p-4, 0x1.0f389a24b2e07p-4, -0x1.eee4db15db335p-5,
29 	    0x1.e95b494d4a5ddp-5, -0x1.15fdf07cb7c73p-4, 0x1.0310b70800fcfp-4,
30 	    -0x1.cfa7385bdb37ep-6 },
31   .ln2 = { 0x1.62e42fefa3800p-1, 0x1.ef35793c76730p-45 },
32   .hf_rt2_top = 0x3fe6a09e00000000,
33   .one_m_hf_rt2_top = 0x00095f6200000000,
34   .bottom_mask = 0xffffffff,
35   .one_top = 0x3ff
36 };
37 
38 static inline svfloat64_t
39 sv_log1p_inline (svfloat64_t x, const svbool_t pg)
40 {
41   /* Helper for calculating log(x + 1). Adapted from v_log1p_inline.h, which
42      differs from v_log1p_2u5.c by:
43      - No special-case handling - this should be dealt with by the caller.
44      - Pairwise Horner polynomial evaluation for improved accuracy.
45      - Optionally simulate the shortcut for k=0, used in the scalar routine,
46        using svsel, for improved accuracy when the argument to log1p is close
47      to 0. This feature is enabled by defining WANT_SV_LOG1P_K0_SHORTCUT as 1
48      in the source of the caller before including this file.
49      See sv_log1p_2u1.c for details of the algorithm.  */
50   const struct sv_log1p_data *d = ptr_barrier (&sv_log1p_data);
51   svfloat64_t m = svadd_x (pg, x, 1);
52   svuint64_t mi = svreinterpret_u64 (m);
53   svuint64_t u = svadd_x (pg, mi, d->one_m_hf_rt2_top);
54 
55   svint64_t ki
56       = svsub_x (pg, svreinterpret_s64 (svlsr_x (pg, u, 52)), d->one_top);
57   svfloat64_t k = svcvt_f64_x (pg, ki);
58 
59   /* Reduce x to f in [sqrt(2)/2, sqrt(2)].  */
60   svuint64_t utop
61       = svadd_x (pg, svand_x (pg, u, 0x000fffff00000000), d->hf_rt2_top);
62   svuint64_t u_red = svorr_x (pg, utop, svand_x (pg, mi, d->bottom_mask));
63   svfloat64_t f = svsub_x (pg, svreinterpret_f64 (u_red), 1);
64 
65   /* Correction term c/m.  */
66   svfloat64_t c = svsub_x (pg, x, svsub_x (pg, m, 1));
67   svfloat64_t cm;
68 
69 #ifndef WANT_SV_LOG1P_K0_SHORTCUT
70 #error                                                                         \
71   "Cannot use sv_log1p_inline.h without specifying whether you need the k0 shortcut for greater accuracy close to 0"
72 #elif WANT_SV_LOG1P_K0_SHORTCUT
73   /* Shortcut if k is 0 - set correction term to 0 and f to x. The result is
74      that the approximation is solely the polynomial.  */
75   svbool_t knot0 = svcmpne (pg, k, 0);
76   cm = svdiv_z (knot0, c, m);
77   if (likely (!svptest_any (pg, knot0)))
78     {
79       f = svsel (knot0, f, x);
80     }
81 #else
82   /* No shortcut.  */
83   cm = svdiv_x (pg, c, m);
84 #endif
85 
86   /* Approximate log1p(f) on the reduced input using a polynomial.  */
87   svfloat64_t f2 = svmul_x (pg, f, f);
88   svfloat64_t p = sv_pw_horner_18_f64_x (pg, f, f2, d->poly);
89 
90   /* Assemble log1p(x) = k * log2 + log1p(f) + c/m.  */
91   svfloat64_t ylo = svmla_x (pg, cm, k, d->ln2[0]);
92   svfloat64_t yhi = svmla_x (pg, f, k, d->ln2[1]);
93 
94   return svmla_x (pg, svadd_x (pg, ylo, yhi), f2, p);
95 }
96 #endif // PL_MATH_SV_LOG1P_INLINE_H
97