xref: /freebsd/contrib/arm-optimized-routines/pl/math/sv_expf_2u.c (revision 5a02ffc32e777041dd2dad4e651ed2a0865a0a5d)
1 /*
2  * Single-precision vector e^x function.
3  *
4  * Copyright (c) 2019-2023, Arm Limited.
5  * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6  */
7 
8 #include "sv_math.h"
9 #include "pl_sig.h"
10 #include "pl_test.h"
11 
12 static const struct data
13 {
14   float poly[5];
15   float inv_ln2, ln2_hi, ln2_lo, shift, thres;
16 } data = {
17   /* Coefficients copied from the polynomial in AdvSIMD variant, reversed for
18      compatibility with polynomial helpers.  */
19   .poly = { 0x1.ffffecp-1f, 0x1.fffdb6p-2f, 0x1.555e66p-3f, 0x1.573e2ep-5f,
20 	    0x1.0e4020p-7f },
21   .inv_ln2 = 0x1.715476p+0f,
22   .ln2_hi = 0x1.62e4p-1f,
23   .ln2_lo = 0x1.7f7d1cp-20f,
24   /* 1.5*2^17 + 127.  */
25   .shift = 0x1.903f8p17f,
26   /* Roughly 87.3. For x < -Thres, the result is subnormal and not handled
27      correctly by FEXPA.  */
28   .thres = 0x1.5d5e2ap+6f,
29 };
30 
31 #define C(i) sv_f32 (d->poly[i])
32 #define ExponentBias 0x3f800000
33 
34 static svfloat32_t NOINLINE
special_case(svfloat32_t x,svfloat32_t y,svbool_t special)35 special_case (svfloat32_t x, svfloat32_t y, svbool_t special)
36 {
37   return sv_call_f32 (expf, x, y, special);
38 }
39 
40 /* Optimised single-precision SVE exp function.
41    Worst-case error is 1.04 ulp:
42    SV_NAME_F1 (exp)(0x1.a8eda4p+1) got 0x1.ba74bcp+4
43 				  want 0x1.ba74bap+4.  */
SV_NAME_F1(exp)44 svfloat32_t SV_NAME_F1 (exp) (svfloat32_t x, const svbool_t pg)
45 {
46   const struct data *d = ptr_barrier (&data);
47 
48   /* exp(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]
49      x = ln2*n + r, with r in [-ln2/2, ln2/2].  */
50 
51   /* Load some constants in quad-word chunks to minimise memory access (last
52      lane is wasted).  */
53   svfloat32_t invln2_and_ln2 = svld1rq (svptrue_b32 (), &d->inv_ln2);
54 
55   /* n = round(x/(ln2/N)).  */
56   svfloat32_t z = svmla_lane (sv_f32 (d->shift), x, invln2_and_ln2, 0);
57   svfloat32_t n = svsub_x (pg, z, d->shift);
58 
59   /* r = x - n*ln2/N.  */
60   svfloat32_t r = svmls_lane (x, n, invln2_and_ln2, 1);
61   r = svmls_lane (r, n, invln2_and_ln2, 2);
62 
63   /* scale = 2^(n/N).  */
64   svbool_t is_special_case = svacgt (pg, x, d->thres);
65   svfloat32_t scale = svexpa (svreinterpret_u32 (z));
66 
67   /* y = exp(r) - 1 ~= r + C0 r^2 + C1 r^3 + C2 r^4 + C3 r^5 + C4 r^6.  */
68   svfloat32_t p12 = svmla_x (pg, C (1), C (2), r);
69   svfloat32_t p34 = svmla_x (pg, C (3), C (4), r);
70   svfloat32_t r2 = svmul_x (pg, r, r);
71   svfloat32_t p14 = svmla_x (pg, p12, p34, r2);
72   svfloat32_t p0 = svmul_x (pg, r, C (0));
73   svfloat32_t poly = svmla_x (pg, p0, r2, p14);
74 
75   if (unlikely (svptest_any (pg, is_special_case)))
76     return special_case (x, svmla_x (pg, scale, scale, poly), is_special_case);
77 
78   return svmla_x (pg, scale, scale, poly);
79 }
80 
81 PL_SIG (SV, F, 1, exp, -9.9, 9.9)
82 PL_TEST_ULP (SV_NAME_F1 (exp), 0.55)
83 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (exp), 0, 0x1p-23, 40000)
84 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (exp), 0x1p-23, 1, 50000)
85 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (exp), 1, 0x1p23, 50000)
86 PL_TEST_SYM_INTERVAL (SV_NAME_F1 (exp), 0x1p23, inf, 50000)
87