Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/expm1f_1u6.c
48375 views
/*1* Single-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_f32.h"8#include "math_config.h"9#include "test_sig.h"10#include "test_defs.h"1112#define Shift (0x1.8p23f)13#define InvLn2 (0x1.715476p+0f)14#define Ln2hi (0x1.62e4p-1f)15#define Ln2lo (0x1.7f7d1cp-20f)16#define AbsMask (0x7fffffff)17#define InfLimit \18(0x1.644716p6) /* Smallest value of x for which expm1(x) overflows. */19#define NegLimit \20(-0x1.9bbabcp+6) /* Largest value of x for which expm1(x) rounds to 1. */2122/* Approximation for exp(x) - 1 using polynomial on a reduced interval.23The maximum error is 1.51 ULP:24expm1f(0x1.8baa96p-2) got 0x1.e2fb9p-225want 0x1.e2fb94p-2. */26float27expm1f (float x)28{29uint32_t ix = asuint (x);30uint32_t ax = ix & AbsMask;3132/* Tiny: |x| < 0x1p-23. expm1(x) is closely approximated by x.33Inf: x == +Inf => expm1(x) = x. */34if (ax <= 0x34000000 || (ix == 0x7f800000))35return x;3637/* +/-NaN. */38if (ax > 0x7f800000)39return __math_invalidf (x);4041if (x >= InfLimit)42return __math_oflowf (0);4344if (x <= NegLimit || ix == 0xff800000)45return -1;4647/* Reduce argument to smaller range:48Let i = round(x / ln2)49and f = x - i * ln2, then f is in [-ln2/2, ln2/2].50exp(x) - 1 = 2^i * (expm1(f) + 1) - 151where 2^i is exact because i is an integer. */52float j = fmaf (InvLn2, x, Shift) - Shift;53int32_t i = j;54float f = fmaf (j, -Ln2hi, x);55f = fmaf (j, -Ln2lo, f);5657/* Approximate expm1(f) using polynomial.58Taylor expansion for expm1(x) has the form:59x + ax^2 + bx^3 + cx^4 ....60So we calculate the polynomial P(f) = a + bf + cf^2 + ...61and assemble the approximation expm1(f) ~= f + f^2 * P(f). */62float p = fmaf (f * f, horner_4_f32 (f, __expm1f_poly), f);63/* Assemble the result, using a slight rearrangement to achieve acceptable64accuracy.65expm1(x) ~= 2^i * (p + 1) - 166Let t = 2^(i - 1). */67float t = ldexpf (0.5f, i);68/* expm1(x) ~= 2 * (p * t + (t - 1/2)). */69return 2 * fmaf (p, t, t - 0.5f);70}7172TEST_SIG (S, F, 1, expm1, -9.9, 9.9)73TEST_ULP (expm1f, 1.02)74TEST_SYM_INTERVAL (expm1f, 0, 0x1p-23, 1000)75TEST_INTERVAL (expm1f, 0x1p-23, 0x1.644716p6, 100000)76TEST_INTERVAL (expm1f, 0x1.644716p6, inf, 1000)77TEST_INTERVAL (expm1f, -0x1p-23, -0x1.9bbabcp+6, 100000)78TEST_INTERVAL (expm1f, -0x1.9bbabcp+6, -inf, 1000)798081