Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/expm1f.c
48375 views
/*1* Single-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 "test_sig.h"9#include "test_defs.h"1011/* Largest value of x for which expm1(x) should round to -1. */12#define SpecialBound 0x1.5ebc4p+6f1314static const struct data15{16/* These 4 are grouped together so they can be loaded as one quadword, then17used with _lane forms of svmla/svmls. */18float c2, c4, ln2_hi, ln2_lo;19float c0, inv_ln2, c1, c3, special_bound;20} data = {21/* Generated using fpminimax. */22.c0 = 0x1.fffffep-2, .c1 = 0x1.5554aep-3,23.c2 = 0x1.555736p-5, .c3 = 0x1.12287cp-7,24.c4 = 0x1.6b55a2p-10, .inv_ln2 = 0x1.715476p+0f,25.special_bound = SpecialBound, .ln2_lo = 0x1.7f7d1cp-20f,26.ln2_hi = 0x1.62e4p-1f,2728};2930static svfloat32_t NOINLINE31special_case (svfloat32_t x, svbool_t pg)32{33return sv_call_f32 (expm1f, x, x, pg);34}3536/* Single-precision SVE exp(x) - 1. Maximum error is 1.52 ULP:37_ZGVsMxv_expm1f(0x1.8f4ebcp-2) got 0x1.e859dp-238want 0x1.e859d4p-2. */39svfloat32_t SV_NAME_F1 (expm1) (svfloat32_t x, svbool_t pg)40{41const struct data *d = ptr_barrier (&data);4243/* Large, NaN/Inf. */44svbool_t special = svnot_z (pg, svaclt (pg, x, d->special_bound));4546if (unlikely (svptest_any (pg, special)))47return special_case (x, pg);4849/* This vector is reliant on layout of data - it contains constants50that can be used with _lane forms of svmla/svmls. Values are:51[ coeff_2, coeff_4, ln2_hi, ln2_lo ]. */52svfloat32_t lane_constants = svld1rq (svptrue_b32 (), &d->c2);5354/* Reduce argument to smaller range:55Let i = round(x / ln2)56and f = x - i * ln2, then f is in [-ln2/2, ln2/2].57exp(x) - 1 = 2^i * (expm1(f) + 1) - 158where 2^i is exact because i is an integer. */59svfloat32_t j = svmul_x (svptrue_b32 (), x, d->inv_ln2);60j = svrinta_x (pg, j);6162svfloat32_t f = svmls_lane (x, j, lane_constants, 2);63f = svmls_lane (f, j, lane_constants, 3);6465/* Approximate expm1(f) using polynomial.66Taylor expansion for expm1(x) has the form:67x + ax^2 + bx^3 + cx^4 ....68So we calculate the polynomial P(f) = a + bf + cf^2 + ...69and assemble the approximation expm1(f) ~= f + f^2 * P(f). */70svfloat32_t p12 = svmla_lane (sv_f32 (d->c1), f, lane_constants, 0);71svfloat32_t p34 = svmla_lane (sv_f32 (d->c3), f, lane_constants, 1);72svfloat32_t f2 = svmul_x (svptrue_b32 (), f, f);73svfloat32_t p = svmla_x (pg, p12, f2, p34);7475p = svmla_x (pg, sv_f32 (d->c0), f, p);76p = svmla_x (pg, f, f2, p);7778/* Assemble the result.79expm1(x) ~= 2^i * (p + 1) - 180Let t = 2^i. */81svfloat32_t t = svscale_x (pg, sv_f32 (1.0f), svcvt_s32_x (pg, j));82return svmla_x (pg, svsub_x (pg, t, 1.0f), p, t);83}8485TEST_SIG (SV, F, 1, expm1, -9.9, 9.9)86TEST_ULP (SV_NAME_F1 (expm1), 1.02)87TEST_DISABLE_FENV (SV_NAME_F1 (expm1))88TEST_SYM_INTERVAL (SV_NAME_F1 (expm1), 0, SpecialBound, 100000)89TEST_SYM_INTERVAL (SV_NAME_F1 (expm1), SpecialBound, inf, 1000)90CLOSE_SVE_ATTR919293