Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/expm1.c
48375 views
/*1* Double-precision vector exp(x) - 1 function.2*3* Copyright (c) 2023-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "sv_math.h"8#include "sv_poly_f64.h"9#include "test_sig.h"10#include "test_defs.h"1112#define SpecialBound 0x1.62b7d369a5aa9p+913#define ExponentBias 0x3ff00000000000001415static const struct data16{17double poly[11];18double shift, inv_ln2, special_bound;19/* To be loaded in one quad-word. */20double ln2_hi, ln2_lo;21} data = {22/* Generated using fpminimax. */23.poly = { 0x1p-1, 0x1.5555555555559p-3, 0x1.555555555554bp-5,240x1.111111110f663p-7, 0x1.6c16c16c1b5f3p-10, 0x1.a01a01affa35dp-13,250x1.a01a018b4ecbbp-16, 0x1.71ddf82db5bb4p-19, 0x1.27e517fc0d54bp-22,260x1.af5eedae67435p-26, 0x1.1f143d060a28ap-29, },2728.special_bound = SpecialBound,29.inv_ln2 = 0x1.71547652b82fep0,30.ln2_hi = 0x1.62e42fefa39efp-1,31.ln2_lo = 0x1.abc9e3b39803fp-56,32.shift = 0x1.8p52,33};3435static svfloat64_t NOINLINE36special_case (svfloat64_t x, svfloat64_t y, svbool_t pg)37{38return sv_call_f64 (expm1, x, y, pg);39}4041/* Double-precision vector exp(x) - 1 function.42The maximum error observed error is 2.18 ULP:43_ZGVsMxv_expm1(0x1.634ba0c237d7bp-2) got 0x1.a8b9ea8d66e22p-244want 0x1.a8b9ea8d66e2p-2. */45svfloat64_t SV_NAME_D1 (expm1) (svfloat64_t x, svbool_t pg)46{47const struct data *d = ptr_barrier (&data);4849/* Large, Nan/Inf. */50svbool_t special = svnot_z (pg, svaclt (pg, x, d->special_bound));5152/* Reduce argument to smaller range:53Let i = round(x / ln2)54and f = x - i * ln2, then f is in [-ln2/2, ln2/2].55exp(x) - 1 = 2^i * (expm1(f) + 1) - 156where 2^i is exact because i is an integer. */57svfloat64_t shift = sv_f64 (d->shift);58svfloat64_t n = svsub_x (pg, svmla_x (pg, shift, x, d->inv_ln2), shift);59svint64_t i = svcvt_s64_x (pg, n);60svfloat64_t ln2 = svld1rq (svptrue_b64 (), &d->ln2_hi);61svfloat64_t f = svmls_lane (x, n, ln2, 0);62f = svmls_lane (f, n, ln2, 1);6364/* Approximate expm1(f) using polynomial.65Taylor expansion for expm1(x) has the form:66x + ax^2 + bx^3 + cx^4 ....67So we calculate the polynomial P(f) = a + bf + cf^2 + ...68and assemble the approximation expm1(f) ~= f + f^2 * P(f). */69svfloat64_t f2 = svmul_x (pg, f, f);70svfloat64_t f4 = svmul_x (pg, f2, f2);71svfloat64_t f8 = svmul_x (pg, f4, f4);72svfloat64_t p73= svmla_x (pg, f, f2, sv_estrin_10_f64_x (pg, f, f2, f4, f8, d->poly));7475/* Assemble the result.76expm1(x) ~= 2^i * (p + 1) - 177Let t = 2^i. */78svint64_t u = svadd_x (pg, svlsl_x (pg, i, 52), ExponentBias);79svfloat64_t t = svreinterpret_f64 (u);8081/* expm1(x) ~= p * t + (t - 1). */82svfloat64_t y = svmla_x (pg, svsub_x (pg, t, 1), p, t);8384if (unlikely (svptest_any (pg, special)))85return special_case (x, y, special);8687return y;88}8990TEST_SIG (SV, D, 1, expm1, -9.9, 9.9)91TEST_ULP (SV_NAME_D1 (expm1), 1.68)92TEST_DISABLE_FENV (SV_NAME_D1 (expm1))93TEST_SYM_INTERVAL (SV_NAME_D1 (expm1), 0, 0x1p-23, 1000)94TEST_SYM_INTERVAL (SV_NAME_D1 (expm1), 0x1p-23, SpecialBound, 200000)95TEST_SYM_INTERVAL (SV_NAME_D1 (expm1), SpecialBound, inf, 1000)96CLOSE_SVE_ATTR979899