1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Single-precision e^x - 1 function.
3*f3087befSAndrew Turner *
4*f3087befSAndrew Turner * Copyright (c) 2022-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 "poly_scalar_f32.h"
9*f3087befSAndrew Turner #include "math_config.h"
10*f3087befSAndrew Turner #include "test_sig.h"
11*f3087befSAndrew Turner #include "test_defs.h"
12*f3087befSAndrew Turner
13*f3087befSAndrew Turner #define Shift (0x1.8p23f)
14*f3087befSAndrew Turner #define InvLn2 (0x1.715476p+0f)
15*f3087befSAndrew Turner #define Ln2hi (0x1.62e4p-1f)
16*f3087befSAndrew Turner #define Ln2lo (0x1.7f7d1cp-20f)
17*f3087befSAndrew Turner #define AbsMask (0x7fffffff)
18*f3087befSAndrew Turner #define InfLimit \
19*f3087befSAndrew Turner (0x1.644716p6) /* Smallest value of x for which expm1(x) overflows. */
20*f3087befSAndrew Turner #define NegLimit \
21*f3087befSAndrew Turner (-0x1.9bbabcp+6) /* Largest value of x for which expm1(x) rounds to 1. */
22*f3087befSAndrew Turner
23*f3087befSAndrew Turner /* Approximation for exp(x) - 1 using polynomial on a reduced interval.
24*f3087befSAndrew Turner The maximum error is 1.51 ULP:
25*f3087befSAndrew Turner expm1f(0x1.8baa96p-2) got 0x1.e2fb9p-2
26*f3087befSAndrew Turner want 0x1.e2fb94p-2. */
27*f3087befSAndrew Turner float
expm1f(float x)28*f3087befSAndrew Turner expm1f (float x)
29*f3087befSAndrew Turner {
30*f3087befSAndrew Turner uint32_t ix = asuint (x);
31*f3087befSAndrew Turner uint32_t ax = ix & AbsMask;
32*f3087befSAndrew Turner
33*f3087befSAndrew Turner /* Tiny: |x| < 0x1p-23. expm1(x) is closely approximated by x.
34*f3087befSAndrew Turner Inf: x == +Inf => expm1(x) = x. */
35*f3087befSAndrew Turner if (ax <= 0x34000000 || (ix == 0x7f800000))
36*f3087befSAndrew Turner return x;
37*f3087befSAndrew Turner
38*f3087befSAndrew Turner /* +/-NaN. */
39*f3087befSAndrew Turner if (ax > 0x7f800000)
40*f3087befSAndrew Turner return __math_invalidf (x);
41*f3087befSAndrew Turner
42*f3087befSAndrew Turner if (x >= InfLimit)
43*f3087befSAndrew Turner return __math_oflowf (0);
44*f3087befSAndrew Turner
45*f3087befSAndrew Turner if (x <= NegLimit || ix == 0xff800000)
46*f3087befSAndrew Turner return -1;
47*f3087befSAndrew Turner
48*f3087befSAndrew Turner /* Reduce argument to smaller range:
49*f3087befSAndrew Turner Let i = round(x / ln2)
50*f3087befSAndrew Turner and f = x - i * ln2, then f is in [-ln2/2, ln2/2].
51*f3087befSAndrew Turner exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
52*f3087befSAndrew Turner where 2^i is exact because i is an integer. */
53*f3087befSAndrew Turner float j = fmaf (InvLn2, x, Shift) - Shift;
54*f3087befSAndrew Turner int32_t i = j;
55*f3087befSAndrew Turner float f = fmaf (j, -Ln2hi, x);
56*f3087befSAndrew Turner f = fmaf (j, -Ln2lo, f);
57*f3087befSAndrew Turner
58*f3087befSAndrew Turner /* Approximate expm1(f) using polynomial.
59*f3087befSAndrew Turner Taylor expansion for expm1(x) has the form:
60*f3087befSAndrew Turner x + ax^2 + bx^3 + cx^4 ....
61*f3087befSAndrew Turner So we calculate the polynomial P(f) = a + bf + cf^2 + ...
62*f3087befSAndrew Turner and assemble the approximation expm1(f) ~= f + f^2 * P(f). */
63*f3087befSAndrew Turner float p = fmaf (f * f, horner_4_f32 (f, __expm1f_poly), f);
64*f3087befSAndrew Turner /* Assemble the result, using a slight rearrangement to achieve acceptable
65*f3087befSAndrew Turner accuracy.
66*f3087befSAndrew Turner expm1(x) ~= 2^i * (p + 1) - 1
67*f3087befSAndrew Turner Let t = 2^(i - 1). */
68*f3087befSAndrew Turner float t = ldexpf (0.5f, i);
69*f3087befSAndrew Turner /* expm1(x) ~= 2 * (p * t + (t - 1/2)). */
70*f3087befSAndrew Turner return 2 * fmaf (p, t, t - 0.5f);
71*f3087befSAndrew Turner }
72*f3087befSAndrew Turner
73*f3087befSAndrew Turner TEST_SIG (S, F, 1, expm1, -9.9, 9.9)
74*f3087befSAndrew Turner TEST_ULP (expm1f, 1.02)
75*f3087befSAndrew Turner TEST_SYM_INTERVAL (expm1f, 0, 0x1p-23, 1000)
76*f3087befSAndrew Turner TEST_INTERVAL (expm1f, 0x1p-23, 0x1.644716p6, 100000)
77*f3087befSAndrew Turner TEST_INTERVAL (expm1f, 0x1.644716p6, inf, 1000)
78*f3087befSAndrew Turner TEST_INTERVAL (expm1f, -0x1p-23, -0x1.9bbabcp+6, 100000)
79*f3087befSAndrew Turner TEST_INTERVAL (expm1f, -0x1.9bbabcp+6, -inf, 1000)
80