1*412f47f9SXin Li /*
2*412f47f9SXin Li * Single-precision vector exp(x) - 1 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
12*412f47f9SXin Li /* Largest value of x for which expm1(x) should round to -1. */
13*412f47f9SXin Li #define SpecialBound 0x1.5ebc4p+6f
14*412f47f9SXin Li
15*412f47f9SXin Li static const struct data
16*412f47f9SXin Li {
17*412f47f9SXin Li /* These 4 are grouped together so they can be loaded as one quadword, then
18*412f47f9SXin Li used with _lane forms of svmla/svmls. */
19*412f47f9SXin Li float c2, c4, ln2_hi, ln2_lo;
20*412f47f9SXin Li float c0, c1, c3, inv_ln2, special_bound, shift;
21*412f47f9SXin Li } data = {
22*412f47f9SXin Li /* Generated using fpminimax. */
23*412f47f9SXin Li .c0 = 0x1.fffffep-2, .c1 = 0x1.5554aep-3,
24*412f47f9SXin Li .c2 = 0x1.555736p-5, .c3 = 0x1.12287cp-7,
25*412f47f9SXin Li .c4 = 0x1.6b55a2p-10,
26*412f47f9SXin Li
27*412f47f9SXin Li .special_bound = SpecialBound, .shift = 0x1.8p23f,
28*412f47f9SXin Li .inv_ln2 = 0x1.715476p+0f, .ln2_hi = 0x1.62e4p-1f,
29*412f47f9SXin Li .ln2_lo = 0x1.7f7d1cp-20f,
30*412f47f9SXin Li };
31*412f47f9SXin Li
32*412f47f9SXin Li #define C(i) sv_f32 (d->c##i)
33*412f47f9SXin Li
34*412f47f9SXin Li static svfloat32_t NOINLINE
special_case(svfloat32_t x,svbool_t pg)35*412f47f9SXin Li special_case (svfloat32_t x, svbool_t pg)
36*412f47f9SXin Li {
37*412f47f9SXin Li return sv_call_f32 (expm1f, x, x, pg);
38*412f47f9SXin Li }
39*412f47f9SXin Li
40*412f47f9SXin Li /* Single-precision SVE exp(x) - 1. Maximum error is 1.52 ULP:
41*412f47f9SXin Li _ZGVsMxv_expm1f(0x1.8f4ebcp-2) got 0x1.e859dp-2
42*412f47f9SXin Li want 0x1.e859d4p-2. */
SV_NAME_F1(expm1)43*412f47f9SXin Li svfloat32_t SV_NAME_F1 (expm1) (svfloat32_t x, svbool_t pg)
44*412f47f9SXin Li {
45*412f47f9SXin Li const struct data *d = ptr_barrier (&data);
46*412f47f9SXin Li
47*412f47f9SXin Li /* Large, NaN/Inf. */
48*412f47f9SXin Li svbool_t special = svnot_z (pg, svaclt (pg, x, d->special_bound));
49*412f47f9SXin Li
50*412f47f9SXin Li if (unlikely (svptest_any (pg, special)))
51*412f47f9SXin Li return special_case (x, pg);
52*412f47f9SXin Li
53*412f47f9SXin Li /* This vector is reliant on layout of data - it contains constants
54*412f47f9SXin Li that can be used with _lane forms of svmla/svmls. Values are:
55*412f47f9SXin Li [ coeff_2, coeff_4, ln2_hi, ln2_lo ]. */
56*412f47f9SXin Li svfloat32_t lane_constants = svld1rq (svptrue_b32 (), &d->c2);
57*412f47f9SXin Li
58*412f47f9SXin Li /* Reduce argument to smaller range:
59*412f47f9SXin Li Let i = round(x / ln2)
60*412f47f9SXin Li and f = x - i * ln2, then f is in [-ln2/2, ln2/2].
61*412f47f9SXin Li exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
62*412f47f9SXin Li where 2^i is exact because i is an integer. */
63*412f47f9SXin Li svfloat32_t j = svmla_x (pg, sv_f32 (d->shift), x, d->inv_ln2);
64*412f47f9SXin Li j = svsub_x (pg, j, d->shift);
65*412f47f9SXin Li svint32_t i = svcvt_s32_x (pg, j);
66*412f47f9SXin Li
67*412f47f9SXin Li svfloat32_t f = svmls_lane (x, j, lane_constants, 2);
68*412f47f9SXin Li f = svmls_lane (f, j, lane_constants, 3);
69*412f47f9SXin Li
70*412f47f9SXin Li /* Approximate expm1(f) using polynomial.
71*412f47f9SXin Li Taylor expansion for expm1(x) has the form:
72*412f47f9SXin Li x + ax^2 + bx^3 + cx^4 ....
73*412f47f9SXin Li So we calculate the polynomial P(f) = a + bf + cf^2 + ...
74*412f47f9SXin Li and assemble the approximation expm1(f) ~= f + f^2 * P(f). */
75*412f47f9SXin Li svfloat32_t p12 = svmla_lane (C (1), f, lane_constants, 0);
76*412f47f9SXin Li svfloat32_t p34 = svmla_lane (C (3), f, lane_constants, 1);
77*412f47f9SXin Li svfloat32_t f2 = svmul_x (pg, f, f);
78*412f47f9SXin Li svfloat32_t p = svmla_x (pg, p12, f2, p34);
79*412f47f9SXin Li p = svmla_x (pg, C (0), f, p);
80*412f47f9SXin Li p = svmla_x (pg, f, f2, p);
81*412f47f9SXin Li
82*412f47f9SXin Li /* Assemble the result.
83*412f47f9SXin Li expm1(x) ~= 2^i * (p + 1) - 1
84*412f47f9SXin Li Let t = 2^i. */
85*412f47f9SXin Li svfloat32_t t = svreinterpret_f32 (
86*412f47f9SXin Li svadd_x (pg, svreinterpret_u32 (svlsl_x (pg, i, 23)), 0x3f800000));
87*412f47f9SXin Li return svmla_x (pg, svsub_x (pg, t, 1), p, t);
88*412f47f9SXin Li }
89*412f47f9SXin Li
90*412f47f9SXin Li PL_SIG (SV, F, 1, expm1, -9.9, 9.9)
91*412f47f9SXin Li PL_TEST_ULP (SV_NAME_F1 (expm1), 1.02)
92*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (expm1), 0, SpecialBound, 100000)
93*412f47f9SXin Li PL_TEST_SYM_INTERVAL (SV_NAME_F1 (expm1), SpecialBound, inf, 1000)
94