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