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