xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_log10_2u5.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Double-precision vector log10(x) function.
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 2022-2024, Arm Limited.
5*412f47f9SXin Li  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*412f47f9SXin Li  */
7*412f47f9SXin Li 
8*412f47f9SXin Li #include "v_math.h"
9*412f47f9SXin Li #include "pl_sig.h"
10*412f47f9SXin Li #include "pl_test.h"
11*412f47f9SXin Li #include "poly_advsimd_f64.h"
12*412f47f9SXin Li 
13*412f47f9SXin Li #define N (1 << V_LOG10_TABLE_BITS)
14*412f47f9SXin Li 
15*412f47f9SXin Li static const struct data
16*412f47f9SXin Li {
17*412f47f9SXin Li   uint64x2_t min_norm;
18*412f47f9SXin Li   uint32x4_t special_bound;
19*412f47f9SXin Li   float64x2_t poly[5];
20*412f47f9SXin Li   float64x2_t invln10, log10_2, ln2;
21*412f47f9SXin Li   uint64x2_t sign_exp_mask;
22*412f47f9SXin Li } data = {
23*412f47f9SXin Li   /* Computed from log coefficients divided by log(10) then rounded to double
24*412f47f9SXin Li      precision.  */
25*412f47f9SXin Li   .poly = { V2 (-0x1.bcb7b1526e506p-3), V2 (0x1.287a7636be1d1p-3),
26*412f47f9SXin Li 	    V2 (-0x1.bcb7b158af938p-4), V2 (0x1.63c78734e6d07p-4),
27*412f47f9SXin Li 	    V2 (-0x1.287461742fee4p-4) },
28*412f47f9SXin Li   .ln2 = V2 (0x1.62e42fefa39efp-1),
29*412f47f9SXin Li   .invln10 = V2 (0x1.bcb7b1526e50ep-2),
30*412f47f9SXin Li   .log10_2 = V2 (0x1.34413509f79ffp-2),
31*412f47f9SXin Li   .min_norm = V2 (0x0010000000000000), /* asuint64(0x1p-1022).  */
32*412f47f9SXin Li   .special_bound = V4 (0x7fe00000),    /* asuint64(inf) - min_norm.  */
33*412f47f9SXin Li   .sign_exp_mask = V2 (0xfff0000000000000),
34*412f47f9SXin Li };
35*412f47f9SXin Li 
36*412f47f9SXin Li #define Off v_u64 (0x3fe6900900000000)
37*412f47f9SXin Li #define IndexMask (N - 1)
38*412f47f9SXin Li 
39*412f47f9SXin Li #define T(s, i) __v_log10_data.s[i]
40*412f47f9SXin Li 
41*412f47f9SXin Li struct entry
42*412f47f9SXin Li {
43*412f47f9SXin Li   float64x2_t invc;
44*412f47f9SXin Li   float64x2_t log10c;
45*412f47f9SXin Li };
46*412f47f9SXin Li 
47*412f47f9SXin Li static inline struct entry
lookup(uint64x2_t i)48*412f47f9SXin Li lookup (uint64x2_t i)
49*412f47f9SXin Li {
50*412f47f9SXin Li   struct entry e;
51*412f47f9SXin Li   uint64_t i0
52*412f47f9SXin Li       = (vgetq_lane_u64 (i, 0) >> (52 - V_LOG10_TABLE_BITS)) & IndexMask;
53*412f47f9SXin Li   uint64_t i1
54*412f47f9SXin Li       = (vgetq_lane_u64 (i, 1) >> (52 - V_LOG10_TABLE_BITS)) & IndexMask;
55*412f47f9SXin Li   float64x2_t e0 = vld1q_f64 (&__v_log10_data.table[i0].invc);
56*412f47f9SXin Li   float64x2_t e1 = vld1q_f64 (&__v_log10_data.table[i1].invc);
57*412f47f9SXin Li   e.invc = vuzp1q_f64 (e0, e1);
58*412f47f9SXin Li   e.log10c = vuzp2q_f64 (e0, e1);
59*412f47f9SXin Li   return e;
60*412f47f9SXin Li }
61*412f47f9SXin Li 
62*412f47f9SXin Li static float64x2_t VPCS_ATTR NOINLINE
special_case(float64x2_t x,float64x2_t y,float64x2_t hi,float64x2_t r2,uint32x2_t special)63*412f47f9SXin Li special_case (float64x2_t x, float64x2_t y, float64x2_t hi, float64x2_t r2,
64*412f47f9SXin Li 	      uint32x2_t special)
65*412f47f9SXin Li {
66*412f47f9SXin Li   return v_call_f64 (log10, x, vfmaq_f64 (hi, r2, y), vmovl_u32 (special));
67*412f47f9SXin Li }
68*412f47f9SXin Li 
69*412f47f9SXin Li /* Fast implementation of double-precision vector log10
70*412f47f9SXin Li    is a slight modification of double-precision vector log.
71*412f47f9SXin Li    Max ULP error: < 2.5 ulp (nearest rounding.)
72*412f47f9SXin Li    Maximum measured at 2.46 ulp for x in [0.96, 0.97]
73*412f47f9SXin Li    _ZGVnN2v_log10(0x1.13192407fcb46p+0) got 0x1.fff6be3cae4bbp-6
74*412f47f9SXin Li 				       want 0x1.fff6be3cae4b9p-6.  */
V_NAME_D1(log10)75*412f47f9SXin Li float64x2_t VPCS_ATTR V_NAME_D1 (log10) (float64x2_t x)
76*412f47f9SXin Li {
77*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
78*412f47f9SXin Li   uint64x2_t ix = vreinterpretq_u64_f64 (x);
79*412f47f9SXin Li   uint32x2_t special = vcge_u32 (vsubhn_u64 (ix, d->min_norm),
80*412f47f9SXin Li 				 vget_low_u32 (d->special_bound));
81*412f47f9SXin Li 
82*412f47f9SXin Li   /* x = 2^k z; where z is in range [OFF,2*OFF) and exact.
83*412f47f9SXin Li      The range is split into N subintervals.
84*412f47f9SXin Li      The ith subinterval contains z and c is near its center.  */
85*412f47f9SXin Li   uint64x2_t tmp = vsubq_u64 (ix, Off);
86*412f47f9SXin Li   int64x2_t k = vshrq_n_s64 (vreinterpretq_s64_u64 (tmp), 52);
87*412f47f9SXin Li   uint64x2_t iz = vsubq_u64 (ix, vandq_u64 (tmp, d->sign_exp_mask));
88*412f47f9SXin Li   float64x2_t z = vreinterpretq_f64_u64 (iz);
89*412f47f9SXin Li 
90*412f47f9SXin Li   struct entry e = lookup (tmp);
91*412f47f9SXin Li 
92*412f47f9SXin Li   /* log10(x) = log1p(z/c-1)/log(10) + log10(c) + k*log10(2).  */
93*412f47f9SXin Li   float64x2_t r = vfmaq_f64 (v_f64 (-1.0), z, e.invc);
94*412f47f9SXin Li   float64x2_t kd = vcvtq_f64_s64 (k);
95*412f47f9SXin Li 
96*412f47f9SXin Li   /* hi = r / log(10) + log10(c) + k*log10(2).
97*412f47f9SXin Li      Constants in v_log10_data.c are computed (in extended precision) as
98*412f47f9SXin Li      e.log10c := e.logc * ivln10.  */
99*412f47f9SXin Li   float64x2_t w = vfmaq_f64 (e.log10c, r, d->invln10);
100*412f47f9SXin Li 
101*412f47f9SXin Li   /* y = log10(1+r) + n * log10(2).  */
102*412f47f9SXin Li   float64x2_t hi = vfmaq_f64 (w, kd, d->log10_2);
103*412f47f9SXin Li 
104*412f47f9SXin Li   /* y = r2*(A0 + r*A1 + r2*(A2 + r*A3 + r2*A4)) + hi.  */
105*412f47f9SXin Li   float64x2_t r2 = vmulq_f64 (r, r);
106*412f47f9SXin Li   float64x2_t y = v_pw_horner_4_f64 (r, r2, d->poly);
107*412f47f9SXin Li 
108*412f47f9SXin Li   if (unlikely (v_any_u32h (special)))
109*412f47f9SXin Li     return special_case (x, y, hi, r2, special);
110*412f47f9SXin Li   return vfmaq_f64 (hi, r2, y);
111*412f47f9SXin Li }
112*412f47f9SXin Li 
113*412f47f9SXin Li PL_SIG (V, D, 1, log10, 0.01, 11.1)
114*412f47f9SXin Li PL_TEST_ULP (V_NAME_D1 (log10), 1.97)
115*412f47f9SXin Li PL_TEST_EXPECT_FENV_ALWAYS (V_NAME_D1 (log10))
116*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log10), -0.0, -inf, 1000)
117*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log10), 0, 0x1p-149, 1000)
118*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log10), 0x1p-149, 0x1p-126, 4000)
119*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log10), 0x1p-126, 0x1p-23, 50000)
120*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log10), 0x1p-23, 1.0, 50000)
121*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log10), 1.0, 100, 50000)
122*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log10), 100, inf, 50000)
123