xref: /freebsd/contrib/arm-optimized-routines/pl/math/v_exp10_2u.c (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
1*5a02ffc3SAndrew Turner /*
2*5a02ffc3SAndrew Turner  * Double-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 
13*5a02ffc3SAndrew Turner /* Value of |x| above which scale overflows without special treatment.  */
14*5a02ffc3SAndrew Turner #define SpecialBound 306.0 /* floor (log10 (2^1023)) - 1.  */
15*5a02ffc3SAndrew Turner /* Value of n above which scale overflows even with special treatment.  */
16*5a02ffc3SAndrew Turner #define ScaleBound 163840.0 /* 1280.0 * N.  */
17*5a02ffc3SAndrew Turner 
18*5a02ffc3SAndrew Turner const static struct data
19*5a02ffc3SAndrew Turner {
20*5a02ffc3SAndrew Turner   float64x2_t poly[4];
21*5a02ffc3SAndrew Turner   float64x2_t log10_2, log2_10_hi, log2_10_lo, shift;
22*5a02ffc3SAndrew Turner #if !WANT_SIMD_EXCEPT
23*5a02ffc3SAndrew Turner   float64x2_t special_bound, scale_thresh;
24*5a02ffc3SAndrew Turner #endif
25*5a02ffc3SAndrew Turner } data = {
26*5a02ffc3SAndrew Turner   /* Coefficients generated using Remez algorithm.
27*5a02ffc3SAndrew Turner      rel error: 0x1.5ddf8f28p-54
28*5a02ffc3SAndrew Turner      abs error: 0x1.5ed266c8p-54 in [ -log10(2)/256, log10(2)/256 ]
29*5a02ffc3SAndrew Turner      maxerr: 1.14432 +0.5 ulp.  */
30*5a02ffc3SAndrew Turner   .poly = { V2 (0x1.26bb1bbb5524p1), V2 (0x1.53524c73cecdap1),
31*5a02ffc3SAndrew Turner 	    V2 (0x1.047060efb781cp1), V2 (0x1.2bd76040f0d16p0) },
32*5a02ffc3SAndrew Turner   .log10_2 = V2 (0x1.a934f0979a371p8),	   /* N/log2(10).  */
33*5a02ffc3SAndrew Turner   .log2_10_hi = V2 (0x1.34413509f79ffp-9), /* log2(10)/N.  */
34*5a02ffc3SAndrew Turner   .log2_10_lo = V2 (-0x1.9dc1da994fd21p-66),
35*5a02ffc3SAndrew Turner   .shift = V2 (0x1.8p+52),
36*5a02ffc3SAndrew Turner #if !WANT_SIMD_EXCEPT
37*5a02ffc3SAndrew Turner   .scale_thresh = V2 (ScaleBound),
38*5a02ffc3SAndrew Turner   .special_bound = V2 (SpecialBound),
39*5a02ffc3SAndrew Turner #endif
40*5a02ffc3SAndrew Turner };
41*5a02ffc3SAndrew Turner 
42*5a02ffc3SAndrew Turner #define N (1 << V_EXP_TABLE_BITS)
43*5a02ffc3SAndrew Turner #define IndexMask v_u64 (N - 1)
44*5a02ffc3SAndrew Turner 
45*5a02ffc3SAndrew Turner #if WANT_SIMD_EXCEPT
46*5a02ffc3SAndrew Turner 
47*5a02ffc3SAndrew Turner # define TinyBound v_u64 (0x2000000000000000) /* asuint64 (0x1p-511).  */
48*5a02ffc3SAndrew Turner # define BigBound v_u64 (0x4070000000000000)  /* asuint64 (0x1p8).  */
49*5a02ffc3SAndrew Turner # define Thres v_u64 (0x2070000000000000)     /* BigBound - TinyBound.  */
50*5a02ffc3SAndrew Turner 
51*5a02ffc3SAndrew Turner static float64x2_t VPCS_ATTR NOINLINE
special_case(float64x2_t x,float64x2_t y,uint64x2_t cmp)52*5a02ffc3SAndrew Turner special_case (float64x2_t x, float64x2_t y, uint64x2_t cmp)
53*5a02ffc3SAndrew Turner {
54*5a02ffc3SAndrew Turner   /* If fenv exceptions are to be triggered correctly, fall back to the scalar
55*5a02ffc3SAndrew Turner      routine for special lanes.  */
56*5a02ffc3SAndrew Turner   return v_call_f64 (exp10, x, y, cmp);
57*5a02ffc3SAndrew Turner }
58*5a02ffc3SAndrew Turner 
59*5a02ffc3SAndrew Turner #else
60*5a02ffc3SAndrew Turner 
61*5a02ffc3SAndrew Turner # define SpecialOffset v_u64 (0x6000000000000000) /* 0x1p513.  */
62*5a02ffc3SAndrew Turner /* SpecialBias1 + SpecialBias1 = asuint(1.0).  */
63*5a02ffc3SAndrew Turner # define SpecialBias1 v_u64 (0x7000000000000000)  /* 0x1p769.  */
64*5a02ffc3SAndrew Turner # define SpecialBias2 v_u64 (0x3010000000000000)  /* 0x1p-254.  */
65*5a02ffc3SAndrew Turner 
66*5a02ffc3SAndrew Turner static inline float64x2_t VPCS_ATTR
special_case(float64x2_t s,float64x2_t y,float64x2_t n,const struct data * d)67*5a02ffc3SAndrew Turner special_case (float64x2_t s, float64x2_t y, float64x2_t n,
68*5a02ffc3SAndrew Turner 	      const struct data *d)
69*5a02ffc3SAndrew Turner {
70*5a02ffc3SAndrew Turner   /* 2^(n/N) may overflow, break it up into s1*s2.  */
71*5a02ffc3SAndrew Turner   uint64x2_t b = vandq_u64 (vcltzq_f64 (n), SpecialOffset);
72*5a02ffc3SAndrew Turner   float64x2_t s1 = vreinterpretq_f64_u64 (vsubq_u64 (SpecialBias1, b));
73*5a02ffc3SAndrew Turner   float64x2_t s2 = vreinterpretq_f64_u64 (
74*5a02ffc3SAndrew Turner       vaddq_u64 (vsubq_u64 (vreinterpretq_u64_f64 (s), SpecialBias2), b));
75*5a02ffc3SAndrew Turner   uint64x2_t cmp = vcagtq_f64 (n, d->scale_thresh);
76*5a02ffc3SAndrew Turner   float64x2_t r1 = vmulq_f64 (s1, s1);
77*5a02ffc3SAndrew Turner   float64x2_t r0 = vmulq_f64 (vfmaq_f64 (s2, y, s2), s1);
78*5a02ffc3SAndrew Turner   return vbslq_f64 (cmp, r1, r0);
79*5a02ffc3SAndrew Turner }
80*5a02ffc3SAndrew Turner 
81*5a02ffc3SAndrew Turner #endif
82*5a02ffc3SAndrew Turner 
83*5a02ffc3SAndrew Turner /* Fast vector implementation of exp10.
84*5a02ffc3SAndrew Turner    Maximum measured error is 1.64 ulp.
85*5a02ffc3SAndrew Turner    _ZGVnN2v_exp10(0x1.ccd1c9d82cc8cp+0) got 0x1.f8dab6d7fed0cp+5
86*5a02ffc3SAndrew Turner 				       want 0x1.f8dab6d7fed0ap+5.  */
V_NAME_D1(exp10)87*5a02ffc3SAndrew Turner float64x2_t VPCS_ATTR V_NAME_D1 (exp10) (float64x2_t x)
88*5a02ffc3SAndrew Turner {
89*5a02ffc3SAndrew Turner   const struct data *d = ptr_barrier (&data);
90*5a02ffc3SAndrew Turner   uint64x2_t cmp;
91*5a02ffc3SAndrew Turner #if WANT_SIMD_EXCEPT
92*5a02ffc3SAndrew Turner   /* If any lanes are special, mask them with 1 and retain a copy of x to allow
93*5a02ffc3SAndrew Turner      special_case to fix special lanes later. This is only necessary if fenv
94*5a02ffc3SAndrew Turner      exceptions are to be triggered correctly.  */
95*5a02ffc3SAndrew Turner   float64x2_t xm = x;
96*5a02ffc3SAndrew Turner   uint64x2_t iax = vreinterpretq_u64_f64 (vabsq_f64 (x));
97*5a02ffc3SAndrew Turner   cmp = vcgeq_u64 (vsubq_u64 (iax, TinyBound), Thres);
98*5a02ffc3SAndrew Turner   if (unlikely (v_any_u64 (cmp)))
99*5a02ffc3SAndrew Turner     x = vbslq_f64 (cmp, v_f64 (1), x);
100*5a02ffc3SAndrew Turner #else
101*5a02ffc3SAndrew Turner   cmp = vcageq_f64 (x, d->special_bound);
102*5a02ffc3SAndrew Turner #endif
103*5a02ffc3SAndrew Turner 
104*5a02ffc3SAndrew Turner   /* n = round(x/(log10(2)/N)).  */
105*5a02ffc3SAndrew Turner   float64x2_t z = vfmaq_f64 (d->shift, x, d->log10_2);
106*5a02ffc3SAndrew Turner   uint64x2_t u = vreinterpretq_u64_f64 (z);
107*5a02ffc3SAndrew Turner   float64x2_t n = vsubq_f64 (z, d->shift);
108*5a02ffc3SAndrew Turner 
109*5a02ffc3SAndrew Turner   /* r = x - n*log10(2)/N.  */
110*5a02ffc3SAndrew Turner   float64x2_t r = x;
111*5a02ffc3SAndrew Turner   r = vfmsq_f64 (r, d->log2_10_hi, n);
112*5a02ffc3SAndrew Turner   r = vfmsq_f64 (r, d->log2_10_lo, n);
113*5a02ffc3SAndrew Turner 
114*5a02ffc3SAndrew Turner   uint64x2_t e = vshlq_n_u64 (u, 52 - V_EXP_TABLE_BITS);
115*5a02ffc3SAndrew Turner   uint64x2_t i = vandq_u64 (u, IndexMask);
116*5a02ffc3SAndrew Turner 
117*5a02ffc3SAndrew Turner   /* y = exp10(r) - 1 ~= C0 r + C1 r^2 + C2 r^3 + C3 r^4.  */
118*5a02ffc3SAndrew Turner   float64x2_t r2 = vmulq_f64 (r, r);
119*5a02ffc3SAndrew Turner   float64x2_t p = vfmaq_f64 (d->poly[0], r, d->poly[1]);
120*5a02ffc3SAndrew Turner   float64x2_t y = vfmaq_f64 (d->poly[2], r, d->poly[3]);
121*5a02ffc3SAndrew Turner   p = vfmaq_f64 (p, y, r2);
122*5a02ffc3SAndrew Turner   y = vmulq_f64 (r, p);
123*5a02ffc3SAndrew Turner 
124*5a02ffc3SAndrew Turner   /* s = 2^(n/N).  */
125*5a02ffc3SAndrew Turner   u = v_lookup_u64 (__v_exp_data, i);
126*5a02ffc3SAndrew Turner   float64x2_t s = vreinterpretq_f64_u64 (vaddq_u64 (u, e));
127*5a02ffc3SAndrew Turner 
128*5a02ffc3SAndrew Turner   if (unlikely (v_any_u64 (cmp)))
129*5a02ffc3SAndrew Turner #if WANT_SIMD_EXCEPT
130*5a02ffc3SAndrew Turner     return special_case (xm, vfmaq_f64 (s, y, s), cmp);
131*5a02ffc3SAndrew Turner #else
132*5a02ffc3SAndrew Turner     return special_case (s, y, n, d);
133*5a02ffc3SAndrew Turner #endif
134*5a02ffc3SAndrew Turner 
135*5a02ffc3SAndrew Turner   return vfmaq_f64 (s, y, s);
136*5a02ffc3SAndrew Turner }
137*5a02ffc3SAndrew Turner 
138*5a02ffc3SAndrew Turner PL_SIG (S, D, 1, exp10, -9.9, 9.9)
139*5a02ffc3SAndrew Turner PL_SIG (V, D, 1, exp10, -9.9, 9.9)
140*5a02ffc3SAndrew Turner PL_TEST_ULP (V_NAME_D1 (exp10), 1.15)
141*5a02ffc3SAndrew Turner PL_TEST_EXPECT_FENV (V_NAME_D1 (exp10), WANT_SIMD_EXCEPT)
142*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_D1 (exp10), 0, SpecialBound, 5000)
143*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_D1 (exp10), SpecialBound, ScaleBound, 5000)
144*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (V_NAME_D1 (exp10), ScaleBound, inf, 10000)
145