xref: /freebsd/contrib/arm-optimized-routines/pl/math/v_exp10f_2u4.c (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
1*5a02ffc3SAndrew Turner /*
2*5a02ffc3SAndrew Turner  * Single-precision vector 10^x function.
3*5a02ffc3SAndrew Turner  *
4*5a02ffc3SAndrew Turner  * Copyright (c) 2023, Arm Limited.
5*5a02ffc3SAndrew Turner  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6*5a02ffc3SAndrew Turner  */
7*5a02ffc3SAndrew Turner 
8*5a02ffc3SAndrew Turner #include "mathlib.h"
9*5a02ffc3SAndrew Turner #include "v_math.h"
10*5a02ffc3SAndrew Turner #include "pl_sig.h"
11*5a02ffc3SAndrew Turner #include "pl_test.h"
12*5a02ffc3SAndrew Turner #include "poly_advsimd_f32.h"
13*5a02ffc3SAndrew Turner 
14*5a02ffc3SAndrew Turner #define ScaleBound 192.0f
15*5a02ffc3SAndrew Turner 
16*5a02ffc3SAndrew Turner static const struct data
17*5a02ffc3SAndrew Turner {
18*5a02ffc3SAndrew Turner   float32x4_t poly[5];
19*5a02ffc3SAndrew Turner   float32x4_t log10_2_and_inv, shift;
20*5a02ffc3SAndrew Turner 
21*5a02ffc3SAndrew Turner #if !WANT_SIMD_EXCEPT
22*5a02ffc3SAndrew Turner   float32x4_t scale_thresh;
23*5a02ffc3SAndrew Turner #endif
24*5a02ffc3SAndrew Turner } data = {
25*5a02ffc3SAndrew Turner   /* Coefficients generated using Remez algorithm with minimisation of relative
26*5a02ffc3SAndrew Turner      error.
27*5a02ffc3SAndrew Turner      rel error: 0x1.89dafa3p-24
28*5a02ffc3SAndrew Turner      abs error: 0x1.167d55p-23 in [-log10(2)/2, log10(2)/2]
29*5a02ffc3SAndrew Turner      maxerr: 1.85943 +0.5 ulp.  */
30*5a02ffc3SAndrew Turner   .poly = { V4 (0x1.26bb16p+1f), V4 (0x1.5350d2p+1f), V4 (0x1.04744ap+1f),
31*5a02ffc3SAndrew Turner 	    V4 (0x1.2d8176p+0f), V4 (0x1.12b41ap-1f) },
32*5a02ffc3SAndrew Turner   .shift = V4 (0x1.8p23f),
33*5a02ffc3SAndrew Turner 
34*5a02ffc3SAndrew Turner   /* Stores constants 1/log10(2), log10(2)_high, log10(2)_low, 0.  */
35*5a02ffc3SAndrew Turner   .log10_2_and_inv = { 0x1.a934fp+1, 0x1.344136p-2, -0x1.ec10cp-27, 0 },
36*5a02ffc3SAndrew Turner #if !WANT_SIMD_EXCEPT
37*5a02ffc3SAndrew Turner   .scale_thresh = V4 (ScaleBound)
38*5a02ffc3SAndrew Turner #endif
39*5a02ffc3SAndrew Turner };
40*5a02ffc3SAndrew Turner 
41*5a02ffc3SAndrew Turner #define ExponentBias v_u32 (0x3f800000)
42*5a02ffc3SAndrew Turner 
43*5a02ffc3SAndrew Turner #if WANT_SIMD_EXCEPT
44*5a02ffc3SAndrew Turner 
45*5a02ffc3SAndrew Turner # define SpecialBound 38.0f	       /* rint(log10(2^127)).  */
46*5a02ffc3SAndrew Turner # define TinyBound v_u32 (0x20000000) /* asuint (0x1p-63).  */
47*5a02ffc3SAndrew Turner # define BigBound v_u32 (0x42180000)  /* asuint (SpecialBound).  */
48*5a02ffc3SAndrew Turner # define Thres v_u32 (0x22180000)     /* BigBound - TinyBound.  */
49*5a02ffc3SAndrew Turner 
50*5a02ffc3SAndrew Turner static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t x,float32x4_t y,uint32x4_t cmp)51*5a02ffc3SAndrew Turner special_case (float32x4_t x, float32x4_t y, uint32x4_t cmp)
52*5a02ffc3SAndrew Turner {
53*5a02ffc3SAndrew Turner   /* If fenv exceptions are to be triggered correctly, fall back to the scalar
54*5a02ffc3SAndrew Turner      routine to special lanes.  */
55*5a02ffc3SAndrew Turner   return v_call_f32 (exp10f, x, y, cmp);
56*5a02ffc3SAndrew Turner }
57*5a02ffc3SAndrew Turner 
58*5a02ffc3SAndrew Turner #else
59*5a02ffc3SAndrew Turner 
60*5a02ffc3SAndrew Turner # define SpecialBound 126.0f /* rint (log2 (2^127 / (1 + sqrt (2)))).  */
61*5a02ffc3SAndrew Turner # define SpecialOffset v_u32 (0x82000000)
62*5a02ffc3SAndrew Turner # define SpecialBias v_u32 (0x7f000000)
63*5a02ffc3SAndrew Turner 
64*5a02ffc3SAndrew Turner static float32x4_t VPCS_ATTR NOINLINE
special_case(float32x4_t poly,float32x4_t n,uint32x4_t e,uint32x4_t cmp1,float32x4_t scale,const struct data * d)65*5a02ffc3SAndrew Turner special_case (float32x4_t poly, float32x4_t n, uint32x4_t e, uint32x4_t cmp1,
66*5a02ffc3SAndrew Turner 	      float32x4_t scale, const struct data *d)
67*5a02ffc3SAndrew Turner {
68*5a02ffc3SAndrew Turner   /* 2^n may overflow, break it up into s1*s2.  */
69*5a02ffc3SAndrew Turner   uint32x4_t b = vandq_u32 (vclezq_f32 (n), SpecialOffset);
70*5a02ffc3SAndrew Turner   float32x4_t s1 = vreinterpretq_f32_u32 (vaddq_u32 (b, SpecialBias));
71*5a02ffc3SAndrew Turner   float32x4_t s2 = vreinterpretq_f32_u32 (vsubq_u32 (e, b));
72*5a02ffc3SAndrew Turner   uint32x4_t cmp2 = vcagtq_f32 (n, d->scale_thresh);
73*5a02ffc3SAndrew Turner   float32x4_t r2 = vmulq_f32 (s1, s1);
74*5a02ffc3SAndrew Turner   float32x4_t r1 = vmulq_f32 (vfmaq_f32 (s2, poly, s2), s1);
75*5a02ffc3SAndrew Turner   /* Similar to r1 but avoids double rounding in the subnormal range.  */
76*5a02ffc3SAndrew Turner   float32x4_t r0 = vfmaq_f32 (scale, poly, scale);
77*5a02ffc3SAndrew Turner   float32x4_t r = vbslq_f32 (cmp1, r1, r0);
78*5a02ffc3SAndrew Turner   return vbslq_f32 (cmp2, r2, r);
79*5a02ffc3SAndrew Turner }
80*5a02ffc3SAndrew Turner 
81*5a02ffc3SAndrew Turner #endif
82*5a02ffc3SAndrew Turner 
83*5a02ffc3SAndrew Turner /* Fast vector implementation of single-precision exp10.
84*5a02ffc3SAndrew Turner    Algorithm is accurate to 2.36 ULP.
85*5a02ffc3SAndrew Turner    _ZGVnN4v_exp10f(0x1.be2b36p+1) got 0x1.7e79c4p+11
86*5a02ffc3SAndrew Turner 				 want 0x1.7e79cp+11.  */
V_NAME_F1(exp10)87*5a02ffc3SAndrew Turner float32x4_t VPCS_ATTR V_NAME_F1 (exp10) (float32x4_t x)
88*5a02ffc3SAndrew Turner {
89*5a02ffc3SAndrew Turner   const struct data *d = ptr_barrier (&data);
90*5a02ffc3SAndrew Turner #if WANT_SIMD_EXCEPT
91*5a02ffc3SAndrew Turner   /* asuint(x) - TinyBound >= BigBound - TinyBound.  */
92*5a02ffc3SAndrew Turner   uint32x4_t cmp = vcgeq_u32 (
93*5a02ffc3SAndrew Turner       vsubq_u32 (vreinterpretq_u32_f32 (vabsq_f32 (x)), TinyBound), Thres);
94*5a02ffc3SAndrew Turner   float32x4_t xm = x;
95*5a02ffc3SAndrew Turner   /* If any lanes are special, mask them with 1 and retain a copy of x to allow
96*5a02ffc3SAndrew Turner      special case handler to fix special lanes later. This is only necessary if
97*5a02ffc3SAndrew Turner      fenv exceptions are to be triggered correctly.  */
98*5a02ffc3SAndrew Turner   if (unlikely (v_any_u32 (cmp)))
99*5a02ffc3SAndrew Turner     x = v_zerofy_f32 (x, cmp);
100*5a02ffc3SAndrew Turner #endif
101*5a02ffc3SAndrew Turner 
102*5a02ffc3SAndrew Turner   /* exp10(x) = 2^n * 10^r = 2^n * (1 + poly (r)),
103*5a02ffc3SAndrew Turner      with poly(r) in [1/sqrt(2), sqrt(2)] and
104*5a02ffc3SAndrew Turner      x = r + n * log10 (2), with r in [-log10(2)/2, log10(2)/2].  */
105*5a02ffc3SAndrew Turner   float32x4_t z = vfmaq_laneq_f32 (d->shift, x, d->log10_2_and_inv, 0);
106*5a02ffc3SAndrew Turner   float32x4_t n = vsubq_f32 (z, d->shift);
107*5a02ffc3SAndrew Turner   float32x4_t r = vfmsq_laneq_f32 (x, n, d->log10_2_and_inv, 1);
108*5a02ffc3SAndrew Turner   r = vfmsq_laneq_f32 (r, n, d->log10_2_and_inv, 2);
109*5a02ffc3SAndrew Turner   uint32x4_t e = vshlq_n_u32 (vreinterpretq_u32_f32 (z), 23);
110*5a02ffc3SAndrew Turner 
111*5a02ffc3SAndrew Turner   float32x4_t scale = vreinterpretq_f32_u32 (vaddq_u32 (e, ExponentBias));
112*5a02ffc3SAndrew Turner 
113*5a02ffc3SAndrew Turner #if !WANT_SIMD_EXCEPT
114*5a02ffc3SAndrew Turner   uint32x4_t cmp = vcagtq_f32 (n, v_f32 (SpecialBound));
115*5a02ffc3SAndrew Turner #endif
116*5a02ffc3SAndrew Turner 
117*5a02ffc3SAndrew Turner   float32x4_t r2 = vmulq_f32 (r, r);
118*5a02ffc3SAndrew Turner   float32x4_t poly
119*5a02ffc3SAndrew Turner       = vfmaq_f32 (vmulq_f32 (r, d->poly[0]),
120*5a02ffc3SAndrew Turner 		   v_pairwise_poly_3_f32 (r, r2, d->poly + 1), r2);
121*5a02ffc3SAndrew Turner 
122*5a02ffc3SAndrew Turner   if (unlikely (v_any_u32 (cmp)))
123*5a02ffc3SAndrew Turner #if WANT_SIMD_EXCEPT
124*5a02ffc3SAndrew Turner     return special_case (xm, vfmaq_f32 (scale, poly, scale), cmp);
125*5a02ffc3SAndrew Turner #else
126*5a02ffc3SAndrew Turner     return special_case (poly, n, e, cmp, scale, d);
127*5a02ffc3SAndrew Turner #endif
128*5a02ffc3SAndrew Turner 
129*5a02ffc3SAndrew Turner   return vfmaq_f32 (scale, poly, scale);
130*5a02ffc3SAndrew Turner }
131*5a02ffc3SAndrew Turner 
132*5a02ffc3SAndrew Turner PL_SIG (S, F, 1, exp10, -9.9, 9.9)
133*5a02ffc3SAndrew Turner PL_SIG (V, F, 1, exp10, -9.9, 9.9)
134*5a02ffc3SAndrew Turner PL_TEST_ULP (V_NAME_F1 (exp10), 1.86)
135*5a02ffc3SAndrew Turner PL_TEST_EXPECT_FENV (V_NAME_F1 (exp10), WANT_SIMD_EXCEPT)
136*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_F1 (exp10), 0, SpecialBound, 5000)
137*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_F1 (exp10), SpecialBound, ScaleBound, 5000)
138*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_F1 (exp10), ScaleBound, inf, 10000)
139