1*f3087befSAndrew Turner /*
2*f3087befSAndrew Turner * Double-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_f64.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 InvLn2 0x1.71547652b82fep0
14*f3087befSAndrew Turner #define Ln2hi 0x1.62e42fefa39efp-1
15*f3087befSAndrew Turner #define Ln2lo 0x1.abc9e3b39803fp-56
16*f3087befSAndrew Turner #define Shift 0x1.8p52
17*f3087befSAndrew Turner /* 0x1p-51, below which expm1(x) is within 2 ULP of x. */
18*f3087befSAndrew Turner #define TinyBound 0x3cc0000000000000
19*f3087befSAndrew Turner /* Above which expm1(x) overflows. */
20*f3087befSAndrew Turner #define BigBound 0x1.63108c75a1937p+9
21*f3087befSAndrew Turner /* Below which expm1(x) rounds to 1. */
22*f3087befSAndrew Turner #define NegBound -0x1.740bf7c0d927dp+9
23*f3087befSAndrew Turner #define AbsMask 0x7fffffffffffffff
24*f3087befSAndrew Turner
25*f3087befSAndrew Turner /* Approximation for exp(x) - 1 using polynomial on a reduced interval.
26*f3087befSAndrew Turner The maximum error observed error is 2.17 ULP:
27*f3087befSAndrew Turner expm1(0x1.63f90a866748dp-2) got 0x1.a9af56603878ap-2
28*f3087befSAndrew Turner want 0x1.a9af566038788p-2. */
29*f3087befSAndrew Turner double
expm1(double x)30*f3087befSAndrew Turner expm1 (double x)
31*f3087befSAndrew Turner {
32*f3087befSAndrew Turner uint64_t ix = asuint64 (x);
33*f3087befSAndrew Turner uint64_t ax = ix & AbsMask;
34*f3087befSAndrew Turner
35*f3087befSAndrew Turner /* Tiny, +Infinity. */
36*f3087befSAndrew Turner if (ax <= TinyBound || ix == 0x7ff0000000000000)
37*f3087befSAndrew Turner return x;
38*f3087befSAndrew Turner
39*f3087befSAndrew Turner /* +/-NaN. */
40*f3087befSAndrew Turner if (ax > 0x7ff0000000000000)
41*f3087befSAndrew Turner return __math_invalid (x);
42*f3087befSAndrew Turner
43*f3087befSAndrew Turner /* Result is too large to be represented as a double. */
44*f3087befSAndrew Turner if (x >= 0x1.63108c75a1937p+9)
45*f3087befSAndrew Turner return __math_oflow (0);
46*f3087befSAndrew Turner
47*f3087befSAndrew Turner /* Result rounds to -1 in double precision. */
48*f3087befSAndrew Turner if (x <= NegBound)
49*f3087befSAndrew Turner return -1;
50*f3087befSAndrew Turner
51*f3087befSAndrew Turner /* Reduce argument to smaller range:
52*f3087befSAndrew Turner Let i = round(x / ln2)
53*f3087befSAndrew Turner and f = x - i * ln2, then f is in [-ln2/2, ln2/2].
54*f3087befSAndrew Turner exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
55*f3087befSAndrew Turner where 2^i is exact because i is an integer. */
56*f3087befSAndrew Turner double j = fma (InvLn2, x, Shift) - Shift;
57*f3087befSAndrew Turner int64_t i = j;
58*f3087befSAndrew Turner double f = fma (j, -Ln2hi, x);
59*f3087befSAndrew Turner f = fma (j, -Ln2lo, f);
60*f3087befSAndrew Turner
61*f3087befSAndrew Turner /* Approximate expm1(f) using polynomial.
62*f3087befSAndrew Turner Taylor expansion for expm1(x) has the form:
63*f3087befSAndrew Turner x + ax^2 + bx^3 + cx^4 ....
64*f3087befSAndrew Turner So we calculate the polynomial P(f) = a + bf + cf^2 + ...
65*f3087befSAndrew Turner and assemble the approximation expm1(f) ~= f + f^2 * P(f). */
66*f3087befSAndrew Turner double f2 = f * f;
67*f3087befSAndrew Turner double f4 = f2 * f2;
68*f3087befSAndrew Turner double p = fma (f2, estrin_10_f64 (f, f2, f4, f4 * f4, __expm1_poly), f);
69*f3087befSAndrew Turner
70*f3087befSAndrew Turner /* Assemble the result, using a slight rearrangement to achieve acceptable
71*f3087befSAndrew Turner accuracy.
72*f3087befSAndrew Turner expm1(x) ~= 2^i * (p + 1) - 1
73*f3087befSAndrew Turner Let t = 2^(i - 1). */
74*f3087befSAndrew Turner double t = ldexp (0.5, i);
75*f3087befSAndrew Turner /* expm1(x) ~= 2 * (p * t + (t - 1/2)). */
76*f3087befSAndrew Turner return 2 * fma (p, t, t - 0.5);
77*f3087befSAndrew Turner }
78*f3087befSAndrew Turner
79*f3087befSAndrew Turner TEST_SIG (S, D, 1, expm1, -9.9, 9.9)
80*f3087befSAndrew Turner TEST_ULP (expm1, 1.68)
81*f3087befSAndrew Turner TEST_SYM_INTERVAL (expm1, 0, 0x1p-51, 1000)
82*f3087befSAndrew Turner TEST_INTERVAL (expm1, 0x1p-51, 0x1.63108c75a1937p+9, 100000)
83*f3087befSAndrew Turner TEST_INTERVAL (expm1, -0x1p-51, -0x1.740bf7c0d927dp+9, 100000)
84*f3087befSAndrew Turner TEST_INTERVAL (expm1, 0x1.63108c75a1937p+9, inf, 100)
85*f3087befSAndrew Turner TEST_INTERVAL (expm1, -0x1.740bf7c0d927dp+9, -inf, 100)
86