xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_exp2f_1u6.c (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
1*5a02ffc3SAndrew Turner /*
2*5a02ffc3SAndrew Turner  * Single-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_f32.h"
10*5a02ffc3SAndrew Turner #include "pl_sig.h"
11*5a02ffc3SAndrew Turner #include "pl_test.h"
12*5a02ffc3SAndrew Turner 
13*5a02ffc3SAndrew Turner static const struct data
14*5a02ffc3SAndrew Turner {
15*5a02ffc3SAndrew Turner   float poly[5];
16*5a02ffc3SAndrew Turner   float shift, thres;
17*5a02ffc3SAndrew Turner } data = {
18*5a02ffc3SAndrew Turner   /* Coefficients copied from the polynomial in AdvSIMD variant, reversed for
19*5a02ffc3SAndrew Turner      compatibility with polynomial helpers.  */
20*5a02ffc3SAndrew Turner   .poly = { 0x1.62e422p-1f, 0x1.ebf9bcp-3f, 0x1.c6bd32p-5f, 0x1.3ce9e4p-7f,
21*5a02ffc3SAndrew Turner 	    0x1.59977ap-10f },
22*5a02ffc3SAndrew Turner   /* 1.5*2^17 + 127.  */
23*5a02ffc3SAndrew Turner   .shift = 0x1.903f8p17f,
24*5a02ffc3SAndrew Turner   /* Roughly 87.3. For x < -Thres, the result is subnormal and not handled
25*5a02ffc3SAndrew Turner      correctly by FEXPA.  */
26*5a02ffc3SAndrew Turner   .thres = 0x1.5d5e2ap+6f,
27*5a02ffc3SAndrew Turner };
28*5a02ffc3SAndrew Turner 
29*5a02ffc3SAndrew Turner static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t y,svbool_t special)30*5a02ffc3SAndrew Turner special_case (svfloat32_t x, svfloat32_t y, svbool_t special)
31*5a02ffc3SAndrew Turner {
32*5a02ffc3SAndrew Turner   return sv_call_f32 (exp2f, x, y, special);
33*5a02ffc3SAndrew Turner }
34*5a02ffc3SAndrew Turner 
35*5a02ffc3SAndrew Turner /* Single-precision SVE exp2f routine. Implements the same algorithm
36*5a02ffc3SAndrew Turner    as AdvSIMD exp2f.
37*5a02ffc3SAndrew Turner    Worst case error is 1.04 ULPs.
38*5a02ffc3SAndrew Turner    SV_NAME_F1 (exp2)(0x1.943b9p-1) got 0x1.ba7eb2p+0
39*5a02ffc3SAndrew Turner 				  want 0x1.ba7ebp+0.  */
SV_NAME_F1(exp2)40*5a02ffc3SAndrew Turner svfloat32_t SV_NAME_F1 (exp2) (svfloat32_t x, const svbool_t pg)
41*5a02ffc3SAndrew Turner {
42*5a02ffc3SAndrew Turner   const struct data *d = ptr_barrier (&data);
43*5a02ffc3SAndrew Turner   /* exp2(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]
44*5a02ffc3SAndrew Turner     x = n + r, with r in [-1/2, 1/2].  */
45*5a02ffc3SAndrew Turner   svfloat32_t shift = sv_f32 (d->shift);
46*5a02ffc3SAndrew Turner   svfloat32_t z = svadd_x (pg, x, shift);
47*5a02ffc3SAndrew Turner   svfloat32_t n = svsub_x (pg, z, shift);
48*5a02ffc3SAndrew Turner   svfloat32_t r = svsub_x (pg, x, n);
49*5a02ffc3SAndrew Turner 
50*5a02ffc3SAndrew Turner   svbool_t special = svacgt (pg, x, d->thres);
51*5a02ffc3SAndrew Turner   svfloat32_t scale = svexpa (svreinterpret_u32 (z));
52*5a02ffc3SAndrew Turner 
53*5a02ffc3SAndrew Turner   /* Polynomial evaluation: poly(r) ~ exp2(r)-1.
54*5a02ffc3SAndrew Turner      Evaluate polynomial use hybrid scheme - offset ESTRIN by 1 for
55*5a02ffc3SAndrew Turner      coefficients 1 to 4, and apply most significant coefficient directly.  */
56*5a02ffc3SAndrew Turner   svfloat32_t r2 = svmul_x (pg, r, r);
57*5a02ffc3SAndrew Turner   svfloat32_t p14 = sv_pairwise_poly_3_f32_x (pg, r, r2, d->poly + 1);
58*5a02ffc3SAndrew Turner   svfloat32_t p0 = svmul_x (pg, r, d->poly[0]);
59*5a02ffc3SAndrew Turner   svfloat32_t poly = svmla_x (pg, p0, r2, p14);
60*5a02ffc3SAndrew Turner 
61*5a02ffc3SAndrew Turner   if (unlikely (svptest_any (pg, special)))
62*5a02ffc3SAndrew Turner     return special_case (x, svmla_x (pg, scale, scale, poly), special);
63*5a02ffc3SAndrew Turner 
64*5a02ffc3SAndrew Turner   return svmla_x (pg, scale, scale, poly);
65*5a02ffc3SAndrew Turner }
66*5a02ffc3SAndrew Turner 
67*5a02ffc3SAndrew Turner PL_SIG (SV, F, 1, exp2, -9.9, 9.9)
68*5a02ffc3SAndrew Turner PL_TEST_ULP (SV_NAME_F1 (exp2), 0.55)
69*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), 0, Thres, 40000)
70*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), Thres, 1, 50000)
71*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), 1, Thres, 50000)
72*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), Thres, inf, 50000)
73*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), -0, -0x1p-23, 40000)
74*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), -0x1p-23, -1, 50000)
75*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), -1, -0x1p23, 50000)
76*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), -0x1p23, -inf, 50000)
77*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), -0, ScaleThres, 40000)
78*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), ScaleThres, -1, 50000)
79*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), -1, ScaleThres, 50000)
80*5a02ffc3SAndrew Turner PL_TEST_INTERVAL (SV_NAME_F1 (exp2), ScaleThres, -inf, 50000)
81