1 /*
2  * Helper for vector 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_V_LOG1P_INLINE_H
9 #define PL_MATH_V_LOG1P_INLINE_H
10 
11 #include "v_math.h"
12 #include "pairwise_horner.h"
13 
14 #define Ln2Hi v_f64 (0x1.62e42fefa3800p-1)
15 #define Ln2Lo v_f64 (0x1.ef35793c76730p-45)
16 #define HfRt2Top 0x3fe6a09e00000000 /* top32(asuint64(sqrt(2)/2)) << 32.  */
17 #define OneMHfRt2Top                                                           \
18   0x00095f6200000000 /* (top32(asuint64(1)) - top32(asuint64(sqrt(2)/2)))      \
19 			<< 32.  */
20 #define OneTop 0x3ff
21 #define BottomMask 0xffffffff
22 #define BigBoundTop 0x5fe /* top12 (asuint64 (0x1p511)).  */
23 
24 #define C(i) v_f64 (__log1p_data.coeffs[i])
25 
26 static inline v_f64_t
27 log1p_inline (v_f64_t x)
28 {
29   /* Helper for calculating log(x + 1). Copied from v_log1p_2u5.c, with several
30      modifications:
31      - No special-case handling - this should be dealt with by the caller.
32      - Pairwise Horner polynomial evaluation for improved accuracy.
33      - Optionally simulate the shortcut for k=0, used in the scalar routine,
34        using v_sel, for improved accuracy when the argument to log1p is close to
35        0. This feature is enabled by defining WANT_V_LOG1P_K0_SHORTCUT as 1 in
36        the source of the caller before including this file.
37      See v_log1pf_2u1.c for details of the algorithm.  */
38   v_f64_t m = x + 1;
39   v_u64_t mi = v_as_u64_f64 (m);
40   v_u64_t u = mi + OneMHfRt2Top;
41 
42   v_s64_t ki = v_as_s64_u64 (u >> 52) - OneTop;
43   v_f64_t k = v_to_f64_s64 (ki);
44 
45   /* Reduce x to f in [sqrt(2)/2, sqrt(2)].  */
46   v_u64_t utop = (u & 0x000fffff00000000) + HfRt2Top;
47   v_u64_t u_red = utop | (mi & BottomMask);
48   v_f64_t f = v_as_f64_u64 (u_red) - 1;
49 
50   /* Correction term c/m.  */
51   v_f64_t cm = (x - (m - 1)) / m;
52 
53 #ifndef WANT_V_LOG1P_K0_SHORTCUT
54 #error                                                                         \
55   "Cannot use v_log1p_inline.h without specifying whether you need the k0 shortcut for greater accuracy close to 0"
56 #elif WANT_V_LOG1P_K0_SHORTCUT
57   /* Shortcut if k is 0 - set correction term to 0 and f to x. The result is
58      that the approximation is solely the polynomial. */
59   v_u64_t k0 = k == 0;
60   if (unlikely (v_any_u64 (k0)))
61     {
62       cm = v_sel_f64 (k0, v_f64 (0), cm);
63       f = v_sel_f64 (k0, x, f);
64     }
65 #endif
66 
67   /* Approximate log1p(f) on the reduced input using a polynomial.  */
68   v_f64_t f2 = f * f;
69   v_f64_t p = PAIRWISE_HORNER_18 (f, f2, C);
70 
71   /* Assemble log1p(x) = k * log2 + log1p(f) + c/m.  */
72   v_f64_t ylo = v_fma_f64 (k, Ln2Lo, cm);
73   v_f64_t yhi = v_fma_f64 (k, Ln2Hi, f);
74   return v_fma_f64 (f2, p, ylo + yhi);
75 }
76 
77 #endif // PL_MATH_V_LOG1P_INLINE_H
78