1*5a02ffc3SAndrew Turner /*
2*5a02ffc3SAndrew Turner * Double-precision SVE 2^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 "sv_math.h"
9*5a02ffc3SAndrew Turner #include "poly_sve_f64.h"
10*5a02ffc3SAndrew Turner #include "pl_sig.h"
11*5a02ffc3SAndrew Turner #include "pl_test.h"
12*5a02ffc3SAndrew Turner
13*5a02ffc3SAndrew Turner #define N (1 << V_EXP_TABLE_BITS)
14*5a02ffc3SAndrew Turner
15*5a02ffc3SAndrew Turner #define BigBound 1022
16*5a02ffc3SAndrew Turner #define UOFlowBound 1280
17*5a02ffc3SAndrew Turner
18*5a02ffc3SAndrew Turner static const struct data
19*5a02ffc3SAndrew Turner {
20*5a02ffc3SAndrew Turner double poly[4];
21*5a02ffc3SAndrew Turner double shift, big_bound, uoflow_bound;
22*5a02ffc3SAndrew Turner } data = {
23*5a02ffc3SAndrew Turner /* Coefficients are computed using Remez algorithm with
24*5a02ffc3SAndrew Turner minimisation of the absolute error. */
25*5a02ffc3SAndrew Turner .poly = { 0x1.62e42fefa3686p-1, 0x1.ebfbdff82c241p-3, 0x1.c6b09b16de99ap-5,
26*5a02ffc3SAndrew Turner 0x1.3b2abf5571ad8p-7 },
27*5a02ffc3SAndrew Turner .shift = 0x1.8p52 / N,
28*5a02ffc3SAndrew Turner .uoflow_bound = UOFlowBound,
29*5a02ffc3SAndrew Turner .big_bound = BigBound,
30*5a02ffc3SAndrew Turner };
31*5a02ffc3SAndrew Turner
32*5a02ffc3SAndrew Turner #define SpecialOffset 0x6000000000000000 /* 0x1p513. */
33*5a02ffc3SAndrew Turner /* SpecialBias1 + SpecialBias1 = asuint(1.0). */
34*5a02ffc3SAndrew Turner #define SpecialBias1 0x7000000000000000 /* 0x1p769. */
35*5a02ffc3SAndrew Turner #define SpecialBias2 0x3010000000000000 /* 0x1p-254. */
36*5a02ffc3SAndrew Turner
37*5a02ffc3SAndrew Turner /* Update of both special and non-special cases, if any special case is
38*5a02ffc3SAndrew Turner detected. */
39*5a02ffc3SAndrew Turner static inline svfloat64_t
special_case(svbool_t pg,svfloat64_t s,svfloat64_t y,svfloat64_t n,const struct data * d)40*5a02ffc3SAndrew Turner special_case (svbool_t pg, svfloat64_t s, svfloat64_t y, svfloat64_t n,
41*5a02ffc3SAndrew Turner const struct data *d)
42*5a02ffc3SAndrew Turner {
43*5a02ffc3SAndrew Turner /* s=2^n may overflow, break it up into s=s1*s2,
44*5a02ffc3SAndrew Turner such that exp = s + s*y can be computed as s1*(s2+s2*y)
45*5a02ffc3SAndrew Turner and s1*s1 overflows only if n>0. */
46*5a02ffc3SAndrew Turner
47*5a02ffc3SAndrew Turner /* If n<=0 then set b to 0x6, 0 otherwise. */
48*5a02ffc3SAndrew Turner svbool_t p_sign = svcmple (pg, n, 0.0); /* n <= 0. */
49*5a02ffc3SAndrew Turner svuint64_t b = svdup_u64_z (p_sign, SpecialOffset);
50*5a02ffc3SAndrew Turner
51*5a02ffc3SAndrew Turner /* Set s1 to generate overflow depending on sign of exponent n. */
52*5a02ffc3SAndrew Turner svfloat64_t s1 = svreinterpret_f64 (svsubr_x (pg, b, SpecialBias1));
53*5a02ffc3SAndrew Turner /* Offset s to avoid overflow in final result if n is below threshold. */
54*5a02ffc3SAndrew Turner svfloat64_t s2 = svreinterpret_f64 (
55*5a02ffc3SAndrew Turner svadd_x (pg, svsub_x (pg, svreinterpret_u64 (s), SpecialBias2), b));
56*5a02ffc3SAndrew Turner
57*5a02ffc3SAndrew Turner /* |n| > 1280 => 2^(n) overflows. */
58*5a02ffc3SAndrew Turner svbool_t p_cmp = svacgt (pg, n, d->uoflow_bound);
59*5a02ffc3SAndrew Turner
60*5a02ffc3SAndrew Turner svfloat64_t r1 = svmul_x (pg, s1, s1);
61*5a02ffc3SAndrew Turner svfloat64_t r2 = svmla_x (pg, s2, s2, y);
62*5a02ffc3SAndrew Turner svfloat64_t r0 = svmul_x (pg, r2, s1);
63*5a02ffc3SAndrew Turner
64*5a02ffc3SAndrew Turner return svsel (p_cmp, r1, r0);
65*5a02ffc3SAndrew Turner }
66*5a02ffc3SAndrew Turner
67*5a02ffc3SAndrew Turner /* Fast vector implementation of exp2.
68*5a02ffc3SAndrew Turner Maximum measured error is 1.65 ulp.
69*5a02ffc3SAndrew Turner _ZGVsMxv_exp2(-0x1.4c264ab5b559bp-6) got 0x1.f8db0d4df721fp-1
70*5a02ffc3SAndrew Turner want 0x1.f8db0d4df721dp-1. */
SV_NAME_D1(exp2)71*5a02ffc3SAndrew Turner svfloat64_t SV_NAME_D1 (exp2) (svfloat64_t x, svbool_t pg)
72*5a02ffc3SAndrew Turner {
73*5a02ffc3SAndrew Turner const struct data *d = ptr_barrier (&data);
74*5a02ffc3SAndrew Turner svbool_t no_big_scale = svacle (pg, x, d->big_bound);
75*5a02ffc3SAndrew Turner svbool_t special = svnot_z (pg, no_big_scale);
76*5a02ffc3SAndrew Turner
77*5a02ffc3SAndrew Turner /* Reduce x to k/N + r, where k is integer and r in [-1/2N, 1/2N]. */
78*5a02ffc3SAndrew Turner svfloat64_t shift = sv_f64 (d->shift);
79*5a02ffc3SAndrew Turner svfloat64_t kd = svadd_x (pg, x, shift);
80*5a02ffc3SAndrew Turner svuint64_t ki = svreinterpret_u64 (kd);
81*5a02ffc3SAndrew Turner /* kd = k/N. */
82*5a02ffc3SAndrew Turner kd = svsub_x (pg, kd, shift);
83*5a02ffc3SAndrew Turner svfloat64_t r = svsub_x (pg, x, kd);
84*5a02ffc3SAndrew Turner
85*5a02ffc3SAndrew Turner /* scale ~= 2^(k/N). */
86*5a02ffc3SAndrew Turner svuint64_t idx = svand_x (pg, ki, N - 1);
87*5a02ffc3SAndrew Turner svuint64_t sbits = svld1_gather_index (pg, __v_exp_data, idx);
88*5a02ffc3SAndrew Turner /* This is only a valid scale when -1023*N < k < 1024*N. */
89*5a02ffc3SAndrew Turner svuint64_t top = svlsl_x (pg, ki, 52 - V_EXP_TABLE_BITS);
90*5a02ffc3SAndrew Turner svfloat64_t scale = svreinterpret_f64 (svadd_x (pg, sbits, top));
91*5a02ffc3SAndrew Turner
92*5a02ffc3SAndrew Turner /* Approximate exp2(r) using polynomial. */
93*5a02ffc3SAndrew Turner svfloat64_t r2 = svmul_x (pg, r, r);
94*5a02ffc3SAndrew Turner svfloat64_t p = sv_pairwise_poly_3_f64_x (pg, r, r2, d->poly);
95*5a02ffc3SAndrew Turner svfloat64_t y = svmul_x (pg, r, p);
96*5a02ffc3SAndrew Turner
97*5a02ffc3SAndrew Turner /* Assemble exp2(x) = exp2(r) * scale. */
98*5a02ffc3SAndrew Turner if (unlikely (svptest_any (pg, special)))
99*5a02ffc3SAndrew Turner return special_case (pg, scale, y, kd, d);
100*5a02ffc3SAndrew Turner return svmla_x (pg, scale, scale, y);
101*5a02ffc3SAndrew Turner }
102*5a02ffc3SAndrew Turner
103*5a02ffc3SAndrew Turner PL_SIG (SV, D, 1, exp2, -9.9, 9.9)
104*5a02ffc3SAndrew Turner PL_TEST_ULP (SV_NAME_D1 (exp2), 1.15)
105*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_D1 (exp2), 0, BigBound, 1000)
106*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_D1 (exp2), BigBound, UOFlowBound, 100000)
107*5a02ffc3SAndrew Turner PL_TEST_SYM_INTERVAL (SV_NAME_D1 (exp2), UOFlowBound, inf, 1000)
108