Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/expm1_2u5.c
48375 views
/*1* Double-precision e^x - 1 function.2*3* Copyright (c) 2022-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "poly_scalar_f64.h"8#include "math_config.h"9#include "test_sig.h"10#include "test_defs.h"1112#define InvLn2 0x1.71547652b82fep013#define Ln2hi 0x1.62e42fefa39efp-114#define Ln2lo 0x1.abc9e3b39803fp-5615#define Shift 0x1.8p5216/* 0x1p-51, below which expm1(x) is within 2 ULP of x. */17#define TinyBound 0x3cc000000000000018/* Above which expm1(x) overflows. */19#define BigBound 0x1.63108c75a1937p+920/* Below which expm1(x) rounds to 1. */21#define NegBound -0x1.740bf7c0d927dp+922#define AbsMask 0x7fffffffffffffff2324/* Approximation for exp(x) - 1 using polynomial on a reduced interval.25The maximum error observed error is 2.17 ULP:26expm1(0x1.63f90a866748dp-2) got 0x1.a9af56603878ap-227want 0x1.a9af566038788p-2. */28double29expm1 (double x)30{31uint64_t ix = asuint64 (x);32uint64_t ax = ix & AbsMask;3334/* Tiny, +Infinity. */35if (ax <= TinyBound || ix == 0x7ff0000000000000)36return x;3738/* +/-NaN. */39if (ax > 0x7ff0000000000000)40return __math_invalid (x);4142/* Result is too large to be represented as a double. */43if (x >= 0x1.63108c75a1937p+9)44return __math_oflow (0);4546/* Result rounds to -1 in double precision. */47if (x <= NegBound)48return -1;4950/* Reduce argument to smaller range:51Let i = round(x / ln2)52and f = x - i * ln2, then f is in [-ln2/2, ln2/2].53exp(x) - 1 = 2^i * (expm1(f) + 1) - 154where 2^i is exact because i is an integer. */55double j = fma (InvLn2, x, Shift) - Shift;56int64_t i = j;57double f = fma (j, -Ln2hi, x);58f = fma (j, -Ln2lo, f);5960/* Approximate expm1(f) using polynomial.61Taylor expansion for expm1(x) has the form:62x + ax^2 + bx^3 + cx^4 ....63So we calculate the polynomial P(f) = a + bf + cf^2 + ...64and assemble the approximation expm1(f) ~= f + f^2 * P(f). */65double f2 = f * f;66double f4 = f2 * f2;67double p = fma (f2, estrin_10_f64 (f, f2, f4, f4 * f4, __expm1_poly), f);6869/* Assemble the result, using a slight rearrangement to achieve acceptable70accuracy.71expm1(x) ~= 2^i * (p + 1) - 172Let t = 2^(i - 1). */73double t = ldexp (0.5, i);74/* expm1(x) ~= 2 * (p * t + (t - 1/2)). */75return 2 * fma (p, t, t - 0.5);76}7778TEST_SIG (S, D, 1, expm1, -9.9, 9.9)79TEST_ULP (expm1, 1.68)80TEST_SYM_INTERVAL (expm1, 0, 0x1p-51, 1000)81TEST_INTERVAL (expm1, 0x1p-51, 0x1.63108c75a1937p+9, 100000)82TEST_INTERVAL (expm1, -0x1p-51, -0x1.740bf7c0d927dp+9, 100000)83TEST_INTERVAL (expm1, 0x1.63108c75a1937p+9, inf, 100)84TEST_INTERVAL (expm1, -0x1.740bf7c0d927dp+9, -inf, 100)858687