1*412f47f9SXin Li /*
2*412f47f9SXin Li * Double-precision SVE 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 "sv_math.h"
9*412f47f9SXin Li #include "pl_sig.h"
10*412f47f9SXin Li #include "pl_test.h"
11*412f47f9SXin Li #include "poly_sve_f64.h"
12*412f47f9SXin Li
13*412f47f9SXin Li #define SpecialBound 307.0 /* floor (log10 (2^1023)). */
14*412f47f9SXin Li
15*412f47f9SXin Li static const struct data
16*412f47f9SXin Li {
17*412f47f9SXin Li double poly[5];
18*412f47f9SXin Li double shift, log10_2, log2_10_hi, log2_10_lo, scale_thres, special_bound;
19*412f47f9SXin Li } data = {
20*412f47f9SXin Li /* Coefficients generated using Remez algorithm.
21*412f47f9SXin Li rel error: 0x1.9fcb9b3p-60
22*412f47f9SXin Li abs error: 0x1.a20d9598p-60 in [ -log10(2)/128, log10(2)/128 ]
23*412f47f9SXin Li max ulp err 0.52 +0.5. */
24*412f47f9SXin Li .poly = { 0x1.26bb1bbb55516p1, 0x1.53524c73cd32ap1, 0x1.0470591daeafbp1,
25*412f47f9SXin Li 0x1.2bd77b1361ef6p0, 0x1.142b5d54e9621p-1 },
26*412f47f9SXin Li /* 1.5*2^46+1023. This value is further explained below. */
27*412f47f9SXin Li .shift = 0x1.800000000ffc0p+46,
28*412f47f9SXin Li .log10_2 = 0x1.a934f0979a371p1, /* 1/log2(10). */
29*412f47f9SXin Li .log2_10_hi = 0x1.34413509f79ffp-2, /* log2(10). */
30*412f47f9SXin Li .log2_10_lo = -0x1.9dc1da994fd21p-59,
31*412f47f9SXin Li .scale_thres = 1280.0,
32*412f47f9SXin Li .special_bound = SpecialBound,
33*412f47f9SXin Li };
34*412f47f9SXin Li
35*412f47f9SXin Li #define SpecialOffset 0x6000000000000000 /* 0x1p513. */
36*412f47f9SXin Li /* SpecialBias1 + SpecialBias1 = asuint(1.0). */
37*412f47f9SXin Li #define SpecialBias1 0x7000000000000000 /* 0x1p769. */
38*412f47f9SXin Li #define SpecialBias2 0x3010000000000000 /* 0x1p-254. */
39*412f47f9SXin Li
40*412f47f9SXin Li /* Update of both special and non-special cases, if any special case is
41*412f47f9SXin Li detected. */
42*412f47f9SXin Li static inline svfloat64_t
special_case(svbool_t pg,svfloat64_t s,svfloat64_t y,svfloat64_t n,const struct data * d)43*412f47f9SXin Li special_case (svbool_t pg, svfloat64_t s, svfloat64_t y, svfloat64_t n,
44*412f47f9SXin Li const struct data *d)
45*412f47f9SXin Li {
46*412f47f9SXin Li /* s=2^n may overflow, break it up into s=s1*s2,
47*412f47f9SXin Li such that exp = s + s*y can be computed as s1*(s2+s2*y)
48*412f47f9SXin Li and s1*s1 overflows only if n>0. */
49*412f47f9SXin Li
50*412f47f9SXin Li /* If n<=0 then set b to 0x6, 0 otherwise. */
51*412f47f9SXin Li svbool_t p_sign = svcmple (pg, n, 0.0); /* n <= 0. */
52*412f47f9SXin Li svuint64_t b = svdup_u64_z (p_sign, SpecialOffset);
53*412f47f9SXin Li
54*412f47f9SXin Li /* Set s1 to generate overflow depending on sign of exponent n. */
55*412f47f9SXin Li svfloat64_t s1 = svreinterpret_f64 (svsubr_x (pg, b, SpecialBias1));
56*412f47f9SXin Li /* Offset s to avoid overflow in final result if n is below threshold. */
57*412f47f9SXin Li svfloat64_t s2 = svreinterpret_f64 (
58*412f47f9SXin Li svadd_x (pg, svsub_x (pg, svreinterpret_u64 (s), SpecialBias2), b));
59*412f47f9SXin Li
60*412f47f9SXin Li /* |n| > 1280 => 2^(n) overflows. */
61*412f47f9SXin Li svbool_t p_cmp = svacgt (pg, n, d->scale_thres);
62*412f47f9SXin Li
63*412f47f9SXin Li svfloat64_t r1 = svmul_x (pg, s1, s1);
64*412f47f9SXin Li svfloat64_t r2 = svmla_x (pg, s2, s2, y);
65*412f47f9SXin Li svfloat64_t r0 = svmul_x (pg, r2, s1);
66*412f47f9SXin Li
67*412f47f9SXin Li return svsel (p_cmp, r1, r0);
68*412f47f9SXin Li }
69*412f47f9SXin Li
70*412f47f9SXin Li /* Fast vector implementation of exp10 using FEXPA instruction.
71*412f47f9SXin Li Maximum measured error is 1.02 ulp.
72*412f47f9SXin Li SV_NAME_D1 (exp10)(-0x1.2862fec805e58p+2) got 0x1.885a89551d782p-16
73*412f47f9SXin Li want 0x1.885a89551d781p-16. */
SV_NAME_D1(exp10)74*412f47f9SXin Li svfloat64_t SV_NAME_D1 (exp10) (svfloat64_t x, svbool_t pg)
75*412f47f9SXin Li {
76*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
77*412f47f9SXin Li svbool_t no_big_scale = svacle (pg, x, d->special_bound);
78*412f47f9SXin Li svbool_t special = svnot_z (pg, no_big_scale);
79*412f47f9SXin Li
80*412f47f9SXin Li /* n = round(x/(log10(2)/N)). */
81*412f47f9SXin Li svfloat64_t shift = sv_f64 (d->shift);
82*412f47f9SXin Li svfloat64_t z = svmla_x (pg, shift, x, d->log10_2);
83*412f47f9SXin Li svfloat64_t n = svsub_x (pg, z, shift);
84*412f47f9SXin Li
85*412f47f9SXin Li /* r = x - n*log10(2)/N. */
86*412f47f9SXin Li svfloat64_t log2_10 = svld1rq (svptrue_b64 (), &d->log2_10_hi);
87*412f47f9SXin Li svfloat64_t r = x;
88*412f47f9SXin Li r = svmls_lane (r, n, log2_10, 0);
89*412f47f9SXin Li r = svmls_lane (r, n, log2_10, 1);
90*412f47f9SXin Li
91*412f47f9SXin Li /* scale = 2^(n/N), computed using FEXPA. FEXPA does not propagate NaNs, so
92*412f47f9SXin Li for consistent NaN handling we have to manually propagate them. This
93*412f47f9SXin Li comes at significant performance cost. */
94*412f47f9SXin Li svuint64_t u = svreinterpret_u64 (z);
95*412f47f9SXin Li svfloat64_t scale = svexpa (u);
96*412f47f9SXin Li
97*412f47f9SXin Li /* Approximate exp10(r) using polynomial. */
98*412f47f9SXin Li svfloat64_t r2 = svmul_x (pg, r, r);
99*412f47f9SXin Li svfloat64_t y = svmla_x (pg, svmul_x (pg, r, d->poly[0]), r2,
100*412f47f9SXin Li sv_pairwise_poly_3_f64_x (pg, r, r2, d->poly + 1));
101*412f47f9SXin Li
102*412f47f9SXin Li /* Assemble result as exp10(x) = 2^n * exp10(r). If |x| > SpecialBound
103*412f47f9SXin Li multiplication may overflow, so use special case routine. */
104*412f47f9SXin Li if (unlikely (svptest_any (pg, special)))
105*412f47f9SXin Li {
106*412f47f9SXin Li /* FEXPA zeroes the sign bit, however the sign is meaningful to the
107*412f47f9SXin Li special case function so needs to be copied.
108*412f47f9SXin Li e = sign bit of u << 46. */
109*412f47f9SXin Li svuint64_t e = svand_x (pg, svlsl_x (pg, u, 46), 0x8000000000000000);
110*412f47f9SXin Li /* Copy sign to scale. */
111*412f47f9SXin Li scale = svreinterpret_f64 (svadd_x (pg, e, svreinterpret_u64 (scale)));
112*412f47f9SXin Li return special_case (pg, scale, y, n, d);
113*412f47f9SXin Li }
114*412f47f9SXin Li
115*412f47f9SXin Li /* No special case. */
116*412f47f9SXin Li return svmla_x (pg, scale, scale, y);
117*412f47f9SXin Li }
118*412f47f9SXin Li
119*412f47f9SXin Li PL_SIG (SV, D, 1, exp10, -9.9, 9.9)
120*412f47f9SXin Li PL_TEST_ULP (SV_NAME_D1 (exp10), 0.52)
121*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_D1 (exp10), 0, 307, 10000)
122*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_D1 (exp10), 307, inf, 1000)
123