1*412f47f9SXin Li /*
2*412f47f9SXin Li * Double-precision vector 2^x function.
3*412f47f9SXin Li *
4*412f47f9SXin Li * Copyright (c) 2019-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 "v_math.h"
9*412f47f9SXin Li #include "poly_advsimd_f64.h"
10*412f47f9SXin Li #include "pl_sig.h"
11*412f47f9SXin Li #include "pl_test.h"
12*412f47f9SXin Li
13*412f47f9SXin Li #define N (1 << V_EXP_TABLE_BITS)
14*412f47f9SXin Li #define IndexMask (N - 1)
15*412f47f9SXin Li #define BigBound 1022.0
16*412f47f9SXin Li #define UOFlowBound 1280.0
17*412f47f9SXin Li #define TinyBound 0x2000000000000000 /* asuint64(0x1p-511). */
18*412f47f9SXin Li
19*412f47f9SXin Li static const struct data
20*412f47f9SXin Li {
21*412f47f9SXin Li float64x2_t poly[4];
22*412f47f9SXin Li float64x2_t shift, scale_big_bound, scale_uoflow_bound;
23*412f47f9SXin Li } data = {
24*412f47f9SXin Li /* Coefficients are computed using Remez algorithm with
25*412f47f9SXin Li minimisation of the absolute error. */
26*412f47f9SXin Li .poly = { V2 (0x1.62e42fefa3686p-1), V2 (0x1.ebfbdff82c241p-3),
27*412f47f9SXin Li V2 (0x1.c6b09b16de99ap-5), V2 (0x1.3b2abf5571ad8p-7) },
28*412f47f9SXin Li .shift = V2 (0x1.8p52 / N),
29*412f47f9SXin Li .scale_big_bound = V2 (BigBound),
30*412f47f9SXin Li .scale_uoflow_bound = V2 (UOFlowBound),
31*412f47f9SXin Li };
32*412f47f9SXin Li
33*412f47f9SXin Li static inline uint64x2_t
lookup_sbits(uint64x2_t i)34*412f47f9SXin Li lookup_sbits (uint64x2_t i)
35*412f47f9SXin Li {
36*412f47f9SXin Li return (uint64x2_t){ __v_exp_data[i[0] & IndexMask],
37*412f47f9SXin Li __v_exp_data[i[1] & IndexMask] };
38*412f47f9SXin Li }
39*412f47f9SXin Li
40*412f47f9SXin Li #if WANT_SIMD_EXCEPT
41*412f47f9SXin Li
42*412f47f9SXin Li # define Thres 0x2080000000000000 /* asuint64(512.0) - TinyBound. */
43*412f47f9SXin Li
44*412f47f9SXin Li /* Call scalar exp2 as a fallback. */
45*412f47f9SXin Li static float64x2_t VPCS_ATTR NOINLINE
special_case(float64x2_t x,float64x2_t y,uint64x2_t is_special)46*412f47f9SXin Li special_case (float64x2_t x, float64x2_t y, uint64x2_t is_special)
47*412f47f9SXin Li {
48*412f47f9SXin Li return v_call_f64 (exp2, x, y, is_special);
49*412f47f9SXin Li }
50*412f47f9SXin Li
51*412f47f9SXin Li #else
52*412f47f9SXin Li
53*412f47f9SXin Li # define SpecialOffset 0x6000000000000000 /* 0x1p513. */
54*412f47f9SXin Li /* SpecialBias1 + SpecialBias1 = asuint(1.0). */
55*412f47f9SXin Li # define SpecialBias1 0x7000000000000000 /* 0x1p769. */
56*412f47f9SXin Li # define SpecialBias2 0x3010000000000000 /* 0x1p-254. */
57*412f47f9SXin Li
58*412f47f9SXin Li static inline float64x2_t VPCS_ATTR
special_case(float64x2_t s,float64x2_t y,float64x2_t n,const struct data * d)59*412f47f9SXin Li special_case (float64x2_t s, float64x2_t y, float64x2_t n,
60*412f47f9SXin Li const struct data *d)
61*412f47f9SXin Li {
62*412f47f9SXin Li /* 2^(n/N) may overflow, break it up into s1*s2. */
63*412f47f9SXin Li uint64x2_t b = vandq_u64 (vclezq_f64 (n), v_u64 (SpecialOffset));
64*412f47f9SXin Li float64x2_t s1 = vreinterpretq_f64_u64 (vsubq_u64 (v_u64 (SpecialBias1), b));
65*412f47f9SXin Li float64x2_t s2 = vreinterpretq_f64_u64 (
66*412f47f9SXin Li vaddq_u64 (vsubq_u64 (vreinterpretq_u64_f64 (s), v_u64 (SpecialBias2)), b));
67*412f47f9SXin Li uint64x2_t cmp = vcagtq_f64 (n, d->scale_uoflow_bound);
68*412f47f9SXin Li float64x2_t r1 = vmulq_f64 (s1, s1);
69*412f47f9SXin Li float64x2_t r0 = vmulq_f64 (vfmaq_f64 (s2, s2, y), s1);
70*412f47f9SXin Li return vbslq_f64 (cmp, r1, r0);
71*412f47f9SXin Li }
72*412f47f9SXin Li
73*412f47f9SXin Li #endif
74*412f47f9SXin Li
75*412f47f9SXin Li /* Fast vector implementation of exp2.
76*412f47f9SXin Li Maximum measured error is 1.65 ulp.
77*412f47f9SXin Li _ZGVnN2v_exp2(-0x1.4c264ab5b559bp-6) got 0x1.f8db0d4df721fp-1
78*412f47f9SXin Li want 0x1.f8db0d4df721dp-1. */
79*412f47f9SXin Li VPCS_ATTR
V_NAME_D1(exp2)80*412f47f9SXin Li float64x2_t V_NAME_D1 (exp2) (float64x2_t x)
81*412f47f9SXin Li {
82*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
83*412f47f9SXin Li uint64x2_t cmp;
84*412f47f9SXin Li #if WANT_SIMD_EXCEPT
85*412f47f9SXin Li uint64x2_t ia = vreinterpretq_u64_f64 (vabsq_f64 (x));
86*412f47f9SXin Li cmp = vcgeq_u64 (vsubq_u64 (ia, v_u64 (TinyBound)), v_u64 (Thres));
87*412f47f9SXin Li /* Mask special lanes and retain a copy of x for passing to special-case
88*412f47f9SXin Li handler. */
89*412f47f9SXin Li float64x2_t xc = x;
90*412f47f9SXin Li x = v_zerofy_f64 (x, cmp);
91*412f47f9SXin Li #else
92*412f47f9SXin Li cmp = vcagtq_f64 (x, d->scale_big_bound);
93*412f47f9SXin Li #endif
94*412f47f9SXin Li
95*412f47f9SXin Li /* n = round(x/N). */
96*412f47f9SXin Li float64x2_t z = vaddq_f64 (d->shift, x);
97*412f47f9SXin Li uint64x2_t u = vreinterpretq_u64_f64 (z);
98*412f47f9SXin Li float64x2_t n = vsubq_f64 (z, d->shift);
99*412f47f9SXin Li
100*412f47f9SXin Li /* r = x - n/N. */
101*412f47f9SXin Li float64x2_t r = vsubq_f64 (x, n);
102*412f47f9SXin Li
103*412f47f9SXin Li /* s = 2^(n/N). */
104*412f47f9SXin Li uint64x2_t e = vshlq_n_u64 (u, 52 - V_EXP_TABLE_BITS);
105*412f47f9SXin Li u = lookup_sbits (u);
106*412f47f9SXin Li float64x2_t s = vreinterpretq_f64_u64 (vaddq_u64 (u, e));
107*412f47f9SXin Li
108*412f47f9SXin Li /* y ~ exp2(r) - 1. */
109*412f47f9SXin Li float64x2_t r2 = vmulq_f64 (r, r);
110*412f47f9SXin Li float64x2_t y = v_pairwise_poly_3_f64 (r, r2, d->poly);
111*412f47f9SXin Li y = vmulq_f64 (r, y);
112*412f47f9SXin Li
113*412f47f9SXin Li if (unlikely (v_any_u64 (cmp)))
114*412f47f9SXin Li #if !WANT_SIMD_EXCEPT
115*412f47f9SXin Li return special_case (s, y, n, d);
116*412f47f9SXin Li #else
117*412f47f9SXin Li return special_case (xc, vfmaq_f64 (s, s, y), cmp);
118*412f47f9SXin Li #endif
119*412f47f9SXin Li return vfmaq_f64 (s, s, y);
120*412f47f9SXin Li }
121*412f47f9SXin Li
122*412f47f9SXin Li PL_SIG (V, D, 1, exp2, -9.9, 9.9)
123*412f47f9SXin Li PL_TEST_ULP (V_NAME_D1 (exp2), 1.15)
124*412f47f9SXin Li PL_TEST_EXPECT_FENV (V_NAME_D1 (exp2), WANT_SIMD_EXCEPT)
125*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (exp2), 0, TinyBound, 5000)
126*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (exp2), TinyBound, BigBound, 10000)
127*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (exp2), BigBound, UOFlowBound, 5000)
128*412f47f9SXin Li PL_TEST_SYM_INTERVAL (V_NAME_D1 (exp2), UOFlowBound, inf, 10000)
129