xref: /freebsd/contrib/arm-optimized-routines/pl/math/v_log1p_2u5.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*
2  * Double-precision vector log(1+x) function.
3  *
4  * Copyright (c) 2022-2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "v_math.h"
9 #include "poly_advsimd_f64.h"
10 #include "pl_sig.h"
11 #include "pl_test.h"
12 
13 const static struct data
14 {
15   float64x2_t poly[19], ln2[2];
16   uint64x2_t hf_rt2_top, one_m_hf_rt2_top, umask, inf, minus_one;
17   int64x2_t one_top;
18 } data = {
19   /* Generated using Remez, deg=20, in [sqrt(2)/2-1, sqrt(2)-1].  */
20   .poly = { V2 (-0x1.ffffffffffffbp-2), V2 (0x1.55555555551a9p-2),
21 	    V2 (-0x1.00000000008e3p-2), V2 (0x1.9999999a32797p-3),
22 	    V2 (-0x1.555555552fecfp-3), V2 (0x1.249248e071e5ap-3),
23 	    V2 (-0x1.ffffff8bf8482p-4), V2 (0x1.c71c8f07da57ap-4),
24 	    V2 (-0x1.9999ca4ccb617p-4), V2 (0x1.7459ad2e1dfa3p-4),
25 	    V2 (-0x1.554d2680a3ff2p-4), V2 (0x1.3b4c54d487455p-4),
26 	    V2 (-0x1.2548a9ffe80e6p-4), V2 (0x1.0f389a24b2e07p-4),
27 	    V2 (-0x1.eee4db15db335p-5), V2 (0x1.e95b494d4a5ddp-5),
28 	    V2 (-0x1.15fdf07cb7c73p-4), V2 (0x1.0310b70800fcfp-4),
29 	    V2 (-0x1.cfa7385bdb37ep-6) },
30   .ln2 = { V2 (0x1.62e42fefa3800p-1), V2 (0x1.ef35793c76730p-45) },
31   /* top32(asuint64(sqrt(2)/2)) << 32.  */
32   .hf_rt2_top = V2 (0x3fe6a09e00000000),
33   /* (top32(asuint64(1)) - top32(asuint64(sqrt(2)/2))) << 32.  */
34   .one_m_hf_rt2_top = V2 (0x00095f6200000000),
35   .umask = V2 (0x000fffff00000000),
36   .one_top = V2 (0x3ff),
37   .inf = V2 (0x7ff0000000000000),
38   .minus_one = V2 (0xbff0000000000000)
39 };
40 
41 #define BottomMask v_u64 (0xffffffff)
42 
43 static float64x2_t VPCS_ATTR NOINLINE
44 special_case (float64x2_t x, float64x2_t y, uint64x2_t special)
45 {
46   return v_call_f64 (log1p, x, y, special);
47 }
48 
49 /* Vector log1p approximation using polynomial on reduced interval. Routine is
50    a modification of the algorithm used in scalar log1p, with no shortcut for
51    k=0 and no narrowing for f and k. Maximum observed error is 2.45 ULP:
52    _ZGVnN2v_log1p(0x1.658f7035c4014p+11) got 0x1.fd61d0727429dp+2
53 					want 0x1.fd61d0727429fp+2 .  */
54 VPCS_ATTR float64x2_t V_NAME_D1 (log1p) (float64x2_t x)
55 {
56   const struct data *d = ptr_barrier (&data);
57   uint64x2_t ix = vreinterpretq_u64_f64 (x);
58   uint64x2_t ia = vreinterpretq_u64_f64 (vabsq_f64 (x));
59   uint64x2_t special = vcgeq_u64 (ia, d->inf);
60 
61 #if WANT_SIMD_EXCEPT
62   special = vorrq_u64 (special,
63 		       vcgeq_u64 (ix, vreinterpretq_u64_f64 (v_f64 (-1))));
64   if (unlikely (v_any_u64 (special)))
65     x = v_zerofy_f64 (x, special);
66 #else
67   special = vorrq_u64 (special, vcleq_f64 (x, v_f64 (-1)));
68 #endif
69 
70   /* With x + 1 = t * 2^k (where t = f + 1 and k is chosen such that f
71 			   is in [sqrt(2)/2, sqrt(2)]):
72      log1p(x) = k*log(2) + log1p(f).
73 
74      f may not be representable exactly, so we need a correction term:
75      let m = round(1 + x), c = (1 + x) - m.
76      c << m: at very small x, log1p(x) ~ x, hence:
77      log(1+x) - log(m) ~ c/m.
78 
79      We therefore calculate log1p(x) by k*log2 + log1p(f) + c/m.  */
80 
81   /* Obtain correctly scaled k by manipulation in the exponent.
82      The scalar algorithm casts down to 32-bit at this point to calculate k and
83      u_red. We stay in double-width to obtain f and k, using the same constants
84      as the scalar algorithm but shifted left by 32.  */
85   float64x2_t m = vaddq_f64 (x, v_f64 (1));
86   uint64x2_t mi = vreinterpretq_u64_f64 (m);
87   uint64x2_t u = vaddq_u64 (mi, d->one_m_hf_rt2_top);
88 
89   int64x2_t ki
90       = vsubq_s64 (vreinterpretq_s64_u64 (vshrq_n_u64 (u, 52)), d->one_top);
91   float64x2_t k = vcvtq_f64_s64 (ki);
92 
93   /* Reduce x to f in [sqrt(2)/2, sqrt(2)].  */
94   uint64x2_t utop = vaddq_u64 (vandq_u64 (u, d->umask), d->hf_rt2_top);
95   uint64x2_t u_red = vorrq_u64 (utop, vandq_u64 (mi, BottomMask));
96   float64x2_t f = vsubq_f64 (vreinterpretq_f64_u64 (u_red), v_f64 (1));
97 
98   /* Correction term c/m.  */
99   float64x2_t cm = vdivq_f64 (vsubq_f64 (x, vsubq_f64 (m, v_f64 (1))), m);
100 
101   /* Approximate log1p(x) on the reduced input using a polynomial. Because
102      log1p(0)=0 we choose an approximation of the form:
103        x + C0*x^2 + C1*x^3 + C2x^4 + ...
104      Hence approximation has the form f + f^2 * P(f)
105       where P(x) = C0 + C1*x + C2x^2 + ...
106      Assembling this all correctly is dealt with at the final step.  */
107   float64x2_t f2 = vmulq_f64 (f, f);
108   float64x2_t p = v_pw_horner_18_f64 (f, f2, d->poly);
109 
110   float64x2_t ylo = vfmaq_f64 (cm, k, d->ln2[1]);
111   float64x2_t yhi = vfmaq_f64 (f, k, d->ln2[0]);
112   float64x2_t y = vaddq_f64 (ylo, yhi);
113 
114   if (unlikely (v_any_u64 (special)))
115     return special_case (vreinterpretq_f64_u64 (ix), vfmaq_f64 (y, f2, p),
116 			 special);
117 
118   return vfmaq_f64 (y, f2, p);
119 }
120 
121 PL_SIG (V, D, 1, log1p, -0.9, 10.0)
122 PL_TEST_ULP (V_NAME_D1 (log1p), 1.97)
123 PL_TEST_EXPECT_FENV (V_NAME_D1 (log1p), WANT_SIMD_EXCEPT)
124 PL_TEST_SYM_INTERVAL (V_NAME_D1 (log1p), 0.0, 0x1p-23, 50000)
125 PL_TEST_SYM_INTERVAL (V_NAME_D1 (log1p), 0x1p-23, 0.001, 50000)
126 PL_TEST_SYM_INTERVAL (V_NAME_D1 (log1p), 0.001, 1.0, 50000)
127 PL_TEST_INTERVAL (V_NAME_D1 (log1p), 1, inf, 40000)
128 PL_TEST_INTERVAL (V_NAME_D1 (log1p), -1.0, -inf, 500)
129