xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_log2_3u.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Double-precision vector log2 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_LOG2_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 invln2;
21*412f47f9SXin Li   uint64x2_t sign_exp_mask;
22*412f47f9SXin Li } data = {
23*412f47f9SXin Li   /* Each coefficient was generated to approximate log(r) for |r| < 0x1.fp-9
24*412f47f9SXin Li      and N = 128, then scaled by log2(e) in extended precision and rounded back
25*412f47f9SXin Li      to double precision.  */
26*412f47f9SXin Li   .poly = { V2 (-0x1.71547652b83p-1), V2 (0x1.ec709dc340953p-2),
27*412f47f9SXin Li 	    V2 (-0x1.71547651c8f35p-2), V2 (0x1.2777ebe12dda5p-2),
28*412f47f9SXin Li 	    V2 (-0x1.ec738d616fe26p-3) },
29*412f47f9SXin Li   .invln2 = V2 (0x1.71547652b82fep0),
30*412f47f9SXin Li   .min_norm = V2 (0x0010000000000000), /* asuint64(0x1p-1022).  */
31*412f47f9SXin Li   .special_bound = V4 (0x7fe00000),    /* asuint64(inf) - min_norm.  */
32*412f47f9SXin Li   .sign_exp_mask = V2 (0xfff0000000000000),
33*412f47f9SXin Li };
34*412f47f9SXin Li 
35*412f47f9SXin Li #define Off v_u64 (0x3fe6900900000000)
36*412f47f9SXin Li #define IndexMask (N - 1)
37*412f47f9SXin Li 
38*412f47f9SXin Li struct entry
39*412f47f9SXin Li {
40*412f47f9SXin Li   float64x2_t invc;
41*412f47f9SXin Li   float64x2_t log2c;
42*412f47f9SXin Li };
43*412f47f9SXin Li 
44*412f47f9SXin Li static inline struct entry
lookup(uint64x2_t i)45*412f47f9SXin Li lookup (uint64x2_t i)
46*412f47f9SXin Li {
47*412f47f9SXin Li   struct entry e;
48*412f47f9SXin Li   uint64_t i0
49*412f47f9SXin Li       = (vgetq_lane_u64 (i, 0) >> (52 - V_LOG2_TABLE_BITS)) & IndexMask;
50*412f47f9SXin Li   uint64_t i1
51*412f47f9SXin Li       = (vgetq_lane_u64 (i, 1) >> (52 - V_LOG2_TABLE_BITS)) & IndexMask;
52*412f47f9SXin Li   float64x2_t e0 = vld1q_f64 (&__v_log2_data.table[i0].invc);
53*412f47f9SXin Li   float64x2_t e1 = vld1q_f64 (&__v_log2_data.table[i1].invc);
54*412f47f9SXin Li   e.invc = vuzp1q_f64 (e0, e1);
55*412f47f9SXin Li   e.log2c = vuzp2q_f64 (e0, e1);
56*412f47f9SXin Li   return e;
57*412f47f9SXin Li }
58*412f47f9SXin Li 
59*412f47f9SXin Li static float64x2_t VPCS_ATTR NOINLINE
special_case(float64x2_t x,float64x2_t y,float64x2_t w,float64x2_t r2,uint32x2_t special)60*412f47f9SXin Li special_case (float64x2_t x, float64x2_t y, float64x2_t w, float64x2_t r2,
61*412f47f9SXin Li 	      uint32x2_t special)
62*412f47f9SXin Li {
63*412f47f9SXin Li   return v_call_f64 (log2, x, vfmaq_f64 (w, r2, y), vmovl_u32 (special));
64*412f47f9SXin Li }
65*412f47f9SXin Li 
66*412f47f9SXin Li /* Double-precision vector log2 routine. Implements the same algorithm as
67*412f47f9SXin Li    vector log10, with coefficients and table entries scaled in extended
68*412f47f9SXin Li    precision. The maximum observed error is 2.58 ULP:
69*412f47f9SXin Li    _ZGVnN2v_log2(0x1.0b556b093869bp+0) got 0x1.fffb34198d9dap-5
70*412f47f9SXin Li 				      want 0x1.fffb34198d9ddp-5.  */
V_NAME_D1(log2)71*412f47f9SXin Li float64x2_t VPCS_ATTR V_NAME_D1 (log2) (float64x2_t x)
72*412f47f9SXin Li {
73*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
74*412f47f9SXin Li   uint64x2_t ix = vreinterpretq_u64_f64 (x);
75*412f47f9SXin Li   uint32x2_t special = vcge_u32 (vsubhn_u64 (ix, d->min_norm),
76*412f47f9SXin Li 				 vget_low_u32 (d->special_bound));
77*412f47f9SXin Li 
78*412f47f9SXin Li   /* x = 2^k z; where z is in range [Off,2*Off) and exact.
79*412f47f9SXin Li      The range is split into N subintervals.
80*412f47f9SXin Li      The ith subinterval contains z and c is near its center.  */
81*412f47f9SXin Li   uint64x2_t tmp = vsubq_u64 (ix, Off);
82*412f47f9SXin Li   int64x2_t k = vshrq_n_s64 (vreinterpretq_s64_u64 (tmp), 52);
83*412f47f9SXin Li   uint64x2_t iz = vsubq_u64 (ix, vandq_u64 (tmp, d->sign_exp_mask));
84*412f47f9SXin Li   float64x2_t z = vreinterpretq_f64_u64 (iz);
85*412f47f9SXin Li 
86*412f47f9SXin Li   struct entry e = lookup (tmp);
87*412f47f9SXin Li 
88*412f47f9SXin Li   /* log2(x) = log1p(z/c-1)/log(2) + log2(c) + k.  */
89*412f47f9SXin Li 
90*412f47f9SXin Li   float64x2_t r = vfmaq_f64 (v_f64 (-1.0), z, e.invc);
91*412f47f9SXin Li   float64x2_t kd = vcvtq_f64_s64 (k);
92*412f47f9SXin Li   float64x2_t w = vfmaq_f64 (e.log2c, r, d->invln2);
93*412f47f9SXin Li 
94*412f47f9SXin Li   float64x2_t r2 = vmulq_f64 (r, r);
95*412f47f9SXin Li   float64x2_t y = v_pw_horner_4_f64 (r, r2, d->poly);
96*412f47f9SXin Li   w = vaddq_f64 (kd, w);
97*412f47f9SXin Li 
98*412f47f9SXin Li   if (unlikely (v_any_u32h (special)))
99*412f47f9SXin Li     return special_case (x, y, w, r2, special);
100*412f47f9SXin Li   return vfmaq_f64 (w, r2, y);
101*412f47f9SXin Li }
102*412f47f9SXin Li 
103*412f47f9SXin Li PL_SIG (V, D, 1, log2, 0.01, 11.1)
104*412f47f9SXin Li PL_TEST_ULP (V_NAME_D1 (log2), 2.09)
105*412f47f9SXin Li PL_TEST_EXPECT_FENV_ALWAYS (V_NAME_D1 (log2))
106*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log2), -0.0, -0x1p126, 100)
107*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log2), 0x1p-149, 0x1p-126, 4000)
108*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log2), 0x1p-126, 0x1p-23, 50000)
109*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log2), 0x1p-23, 1.0, 50000)
110*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log2), 1.0, 100, 50000)
111*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_D1 (log2), 100, inf, 50000)
112