xref: /freebsd/contrib/arm-optimized-routines/math/aarch64/sve/exp2f.c (revision f3087bef11543b42e0d69b708f367097a4118d24)
1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner  * Single-precision SVE 2^x function.
3*f3087befSAndrew Turner  *
4*f3087befSAndrew Turner  * Copyright (c) 2023-2024, 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 Thres 0x1.5d5e2ap+6f
13*f3087befSAndrew Turner 
14*f3087befSAndrew Turner static const struct data
15*f3087befSAndrew Turner {
16*f3087befSAndrew Turner   float c0, c2, c4, c1, c3;
17*f3087befSAndrew Turner   float shift, thres;
18*f3087befSAndrew Turner } data = {
19*f3087befSAndrew Turner   /* Coefficients copied from the polynomial in AdvSIMD variant.  */
20*f3087befSAndrew Turner   .c0 = 0x1.62e422p-1f,
21*f3087befSAndrew Turner   .c1 = 0x1.ebf9bcp-3f,
22*f3087befSAndrew Turner   .c2 = 0x1.c6bd32p-5f,
23*f3087befSAndrew Turner   .c3 = 0x1.3ce9e4p-7f,
24*f3087befSAndrew Turner   .c4 = 0x1.59977ap-10f,
25*f3087befSAndrew Turner   /* 1.5*2^17 + 127.  */
26*f3087befSAndrew Turner   .shift = 0x1.803f8p17f,
27*f3087befSAndrew Turner   /* Roughly 87.3. For x < -Thres, the result is subnormal and not handled
28*f3087befSAndrew Turner      correctly by FEXPA.  */
29*f3087befSAndrew Turner   .thres = Thres,
30*f3087befSAndrew Turner };
31*f3087befSAndrew Turner 
32*f3087befSAndrew Turner static inline svfloat32_t
sv_exp2f_inline(svfloat32_t x,const svbool_t pg,const struct data * d)33*f3087befSAndrew Turner sv_exp2f_inline (svfloat32_t x, const svbool_t pg, const struct data *d)
34*f3087befSAndrew Turner {
35*f3087befSAndrew Turner   /* exp2(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]
36*f3087befSAndrew Turner     x = n + r, with r in [-1/2, 1/2].  */
37*f3087befSAndrew Turner   svfloat32_t z = svadd_x (svptrue_b32 (), x, d->shift);
38*f3087befSAndrew Turner   svfloat32_t n = svsub_x (svptrue_b32 (), z, d->shift);
39*f3087befSAndrew Turner   svfloat32_t r = svsub_x (svptrue_b32 (), x, n);
40*f3087befSAndrew Turner 
41*f3087befSAndrew Turner   svfloat32_t scale = svexpa (svreinterpret_u32 (z));
42*f3087befSAndrew Turner 
43*f3087befSAndrew Turner   /* Polynomial evaluation: poly(r) ~ exp2(r)-1.
44*f3087befSAndrew Turner      Evaluate polynomial use hybrid scheme - offset ESTRIN by 1 for
45*f3087befSAndrew Turner      coefficients 1 to 4, and apply most significant coefficient directly.  */
46*f3087befSAndrew Turner   svfloat32_t even_coeffs = svld1rq (svptrue_b32 (), &d->c0);
47*f3087befSAndrew Turner   svfloat32_t r2 = svmul_x (svptrue_b32 (), r, r);
48*f3087befSAndrew Turner   svfloat32_t p12 = svmla_lane (sv_f32 (d->c1), r, even_coeffs, 1);
49*f3087befSAndrew Turner   svfloat32_t p34 = svmla_lane (sv_f32 (d->c3), r, even_coeffs, 2);
50*f3087befSAndrew Turner   svfloat32_t p14 = svmla_x (pg, p12, r2, p34);
51*f3087befSAndrew Turner   svfloat32_t p0 = svmul_lane (r, even_coeffs, 0);
52*f3087befSAndrew Turner   svfloat32_t poly = svmla_x (pg, p0, r2, p14);
53*f3087befSAndrew Turner 
54*f3087befSAndrew Turner   return svmla_x (pg, scale, scale, poly);
55*f3087befSAndrew Turner }
56*f3087befSAndrew Turner 
57*f3087befSAndrew Turner static svfloat32_t NOINLINE
special_case(svfloat32_t x,svbool_t special,const struct data * d)58*f3087befSAndrew Turner special_case (svfloat32_t x, svbool_t special, const struct data *d)
59*f3087befSAndrew Turner {
60*f3087befSAndrew Turner   return sv_call_f32 (exp2f, x, sv_exp2f_inline (x, svptrue_b32 (), d),
61*f3087befSAndrew Turner 		      special);
62*f3087befSAndrew Turner }
63*f3087befSAndrew Turner 
64*f3087befSAndrew Turner /* Single-precision SVE exp2f routine. Implements the same algorithm
65*f3087befSAndrew Turner    as AdvSIMD exp2f.
66*f3087befSAndrew Turner    Worst case error is 1.04 ULPs.
67*f3087befSAndrew Turner    _ZGVsMxv_exp2f(-0x1.af994ap-3) got 0x1.ba6a66p-1
68*f3087befSAndrew Turner 				 want 0x1.ba6a64p-1.  */
SV_NAME_F1(exp2)69*f3087befSAndrew Turner svfloat32_t SV_NAME_F1 (exp2) (svfloat32_t x, const svbool_t pg)
70*f3087befSAndrew Turner {
71*f3087befSAndrew Turner   const struct data *d = ptr_barrier (&data);
72*f3087befSAndrew Turner   svbool_t special = svacgt (pg, x, d->thres);
73*f3087befSAndrew Turner   if (unlikely (svptest_any (special, special)))
74*f3087befSAndrew Turner     return special_case (x, special, d);
75*f3087befSAndrew Turner   return sv_exp2f_inline (x, pg, d);
76*f3087befSAndrew Turner }
77*f3087befSAndrew Turner 
78*f3087befSAndrew Turner TEST_SIG (SV, F, 1, exp2, -9.9, 9.9)
79*f3087befSAndrew Turner TEST_ULP (SV_NAME_F1 (exp2), 0.54)
80*f3087befSAndrew Turner TEST_DISABLE_FENV (SV_NAME_F1 (exp2))
81*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (exp2), 0, Thres, 50000)
82*f3087befSAndrew Turner TEST_SYM_INTERVAL (SV_NAME_F1 (exp2), Thres, inf, 50000)
83*f3087befSAndrew Turner CLOSE_SVE_ATTR
84