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