xref: /aosp_15_r20/external/arm-optimized-routines/pl/math/v_log1pf_2u1.c (revision 412f47f9e737e10ed5cc46ec6a8d7fa2264f8a14)
1*412f47f9SXin Li /*
2*412f47f9SXin Li  * Single-precision vector log(1+x) function.
3*412f47f9SXin Li  *
4*412f47f9SXin Li  * Copyright (c) 2022-2023, 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_f32.h"
12*412f47f9SXin Li 
13*412f47f9SXin Li const static struct data
14*412f47f9SXin Li {
15*412f47f9SXin Li   float32x4_t poly[8], ln2;
16*412f47f9SXin Li   uint32x4_t tiny_bound, minus_one, four, thresh;
17*412f47f9SXin Li   int32x4_t three_quarters;
18*412f47f9SXin Li } data = {
19*412f47f9SXin Li   .poly = { /* Generated using FPMinimax in [-0.25, 0.5]. First two coefficients
20*412f47f9SXin Li 	       (1, -0.5) are not stored as they can be generated more
21*412f47f9SXin Li 	       efficiently.  */
22*412f47f9SXin Li 	    V4 (0x1.5555aap-2f), V4 (-0x1.000038p-2f), V4 (0x1.99675cp-3f),
23*412f47f9SXin Li 	    V4 (-0x1.54ef78p-3f), V4 (0x1.28a1f4p-3f), V4 (-0x1.0da91p-3f),
24*412f47f9SXin Li 	    V4 (0x1.abcb6p-4f), V4 (-0x1.6f0d5ep-5f) },
25*412f47f9SXin Li   .ln2 = V4 (0x1.62e43p-1f),
26*412f47f9SXin Li   .tiny_bound = V4 (0x34000000), /* asuint32(0x1p-23). ulp=0.5 at 0x1p-23.  */
27*412f47f9SXin Li   .thresh = V4 (0x4b800000), /* asuint32(INFINITY) - tiny_bound.  */
28*412f47f9SXin Li   .minus_one = V4 (0xbf800000),
29*412f47f9SXin Li   .four = V4 (0x40800000),
30*412f47f9SXin Li   .three_quarters = V4 (0x3f400000)
31*412f47f9SXin Li };
32*412f47f9SXin Li 
33*412f47f9SXin Li static inline float32x4_t
eval_poly(float32x4_t m,const float32x4_t * p)34*412f47f9SXin Li eval_poly (float32x4_t m, const float32x4_t *p)
35*412f47f9SXin Li {
36*412f47f9SXin Li   /* Approximate log(1+m) on [-0.25, 0.5] using split Estrin scheme.  */
37*412f47f9SXin Li   float32x4_t p_12 = vfmaq_f32 (v_f32 (-0.5), m, p[0]);
38*412f47f9SXin Li   float32x4_t p_34 = vfmaq_f32 (p[1], m, p[2]);
39*412f47f9SXin Li   float32x4_t p_56 = vfmaq_f32 (p[3], m, p[4]);
40*412f47f9SXin Li   float32x4_t p_78 = vfmaq_f32 (p[5], m, p[6]);
41*412f47f9SXin Li 
42*412f47f9SXin Li   float32x4_t m2 = vmulq_f32 (m, m);
43*412f47f9SXin Li   float32x4_t p_02 = vfmaq_f32 (m, m2, p_12);
44*412f47f9SXin Li   float32x4_t p_36 = vfmaq_f32 (p_34, m2, p_56);
45*412f47f9SXin Li   float32x4_t p_79 = vfmaq_f32 (p_78, m2, p[7]);
46*412f47f9SXin Li 
47*412f47f9SXin Li   float32x4_t m4 = vmulq_f32 (m2, m2);
48*412f47f9SXin Li   float32x4_t p_06 = vfmaq_f32 (p_02, m4, p_36);
49*412f47f9SXin Li   return vfmaq_f32 (p_06, m4, vmulq_f32 (m4, p_79));
50*412f47f9SXin Li }
51*412f47f9SXin Li 
52*412f47f9SXin Li static float32x4_t NOINLINE VPCS_ATTR
special_case(float32x4_t x,float32x4_t y,uint32x4_t special)53*412f47f9SXin Li special_case (float32x4_t x, float32x4_t y, uint32x4_t special)
54*412f47f9SXin Li {
55*412f47f9SXin Li   return v_call_f32 (log1pf, x, y, special);
56*412f47f9SXin Li }
57*412f47f9SXin Li 
58*412f47f9SXin Li /* Vector log1pf approximation using polynomial on reduced interval. Accuracy
59*412f47f9SXin Li    is roughly 2.02 ULP:
60*412f47f9SXin Li    log1pf(0x1.21e13ap-2) got 0x1.fe8028p-3 want 0x1.fe802cp-3.  */
V_NAME_F1(log1p)61*412f47f9SXin Li VPCS_ATTR float32x4_t V_NAME_F1 (log1p) (float32x4_t x)
62*412f47f9SXin Li {
63*412f47f9SXin Li   const struct data *d = ptr_barrier (&data);
64*412f47f9SXin Li 
65*412f47f9SXin Li   uint32x4_t ix = vreinterpretq_u32_f32 (x);
66*412f47f9SXin Li   uint32x4_t ia = vreinterpretq_u32_f32 (vabsq_f32 (x));
67*412f47f9SXin Li   uint32x4_t special_cases
68*412f47f9SXin Li       = vorrq_u32 (vcgeq_u32 (vsubq_u32 (ia, d->tiny_bound), d->thresh),
69*412f47f9SXin Li 		   vcgeq_u32 (ix, d->minus_one));
70*412f47f9SXin Li   float32x4_t special_arg = x;
71*412f47f9SXin Li 
72*412f47f9SXin Li #if WANT_SIMD_EXCEPT
73*412f47f9SXin Li   if (unlikely (v_any_u32 (special_cases)))
74*412f47f9SXin Li     /* Side-step special lanes so fenv exceptions are not triggered
75*412f47f9SXin Li        inadvertently.  */
76*412f47f9SXin Li     x = v_zerofy_f32 (x, special_cases);
77*412f47f9SXin Li #endif
78*412f47f9SXin Li 
79*412f47f9SXin Li   /* With x + 1 = t * 2^k (where t = m + 1 and k is chosen such that m
80*412f47f9SXin Li 			   is in [-0.25, 0.5]):
81*412f47f9SXin Li      log1p(x) = log(t) + log(2^k) = log1p(m) + k*log(2).
82*412f47f9SXin Li 
83*412f47f9SXin Li      We approximate log1p(m) with a polynomial, then scale by
84*412f47f9SXin Li      k*log(2). Instead of doing this directly, we use an intermediate
85*412f47f9SXin Li      scale factor s = 4*k*log(2) to ensure the scale is representable
86*412f47f9SXin Li      as a normalised fp32 number.  */
87*412f47f9SXin Li 
88*412f47f9SXin Li   float32x4_t m = vaddq_f32 (x, v_f32 (1.0f));
89*412f47f9SXin Li 
90*412f47f9SXin Li   /* Choose k to scale x to the range [-1/4, 1/2].  */
91*412f47f9SXin Li   int32x4_t k
92*412f47f9SXin Li       = vandq_s32 (vsubq_s32 (vreinterpretq_s32_f32 (m), d->three_quarters),
93*412f47f9SXin Li 		   v_s32 (0xff800000));
94*412f47f9SXin Li   uint32x4_t ku = vreinterpretq_u32_s32 (k);
95*412f47f9SXin Li 
96*412f47f9SXin Li   /* Scale x by exponent manipulation.  */
97*412f47f9SXin Li   float32x4_t m_scale
98*412f47f9SXin Li       = vreinterpretq_f32_u32 (vsubq_u32 (vreinterpretq_u32_f32 (x), ku));
99*412f47f9SXin Li 
100*412f47f9SXin Li   /* Scale up to ensure that the scale factor is representable as normalised
101*412f47f9SXin Li      fp32 number, and scale m down accordingly.  */
102*412f47f9SXin Li   float32x4_t s = vreinterpretq_f32_u32 (vsubq_u32 (d->four, ku));
103*412f47f9SXin Li   m_scale = vaddq_f32 (m_scale, vfmaq_f32 (v_f32 (-1.0f), v_f32 (0.25f), s));
104*412f47f9SXin Li 
105*412f47f9SXin Li   /* Evaluate polynomial on the reduced interval.  */
106*412f47f9SXin Li   float32x4_t p = eval_poly (m_scale, d->poly);
107*412f47f9SXin Li 
108*412f47f9SXin Li   /* The scale factor to be applied back at the end - by multiplying float(k)
109*412f47f9SXin Li      by 2^-23 we get the unbiased exponent of k.  */
110*412f47f9SXin Li   float32x4_t scale_back = vcvtq_f32_s32 (vshrq_n_s32 (k, 23));
111*412f47f9SXin Li 
112*412f47f9SXin Li   /* Apply the scaling back.  */
113*412f47f9SXin Li   float32x4_t y = vfmaq_f32 (p, scale_back, d->ln2);
114*412f47f9SXin Li 
115*412f47f9SXin Li   if (unlikely (v_any_u32 (special_cases)))
116*412f47f9SXin Li     return special_case (special_arg, y, special_cases);
117*412f47f9SXin Li   return y;
118*412f47f9SXin Li }
119*412f47f9SXin Li 
120*412f47f9SXin Li PL_SIG (V, F, 1, log1p, -0.9, 10.0)
121*412f47f9SXin Li PL_TEST_ULP (V_NAME_F1 (log1p), 1.53)
122*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_F1 (log1p), WANT_SIMD_EXCEPT)
123*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (log1p), 0.0, 0x1p-23, 30000)
124*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_F1 (log1p), 0x1p-23, 1, 50000)
125*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (log1p), 1, inf, 50000)
126*412f47f9SXin Li PL_TEST_INTERVAL (V_NAME_F1 (log1p), -1.0, -inf, 1000)
127