Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/exp.c
48375 views
/*1* Double-precision vector e^x function.2*3* Copyright (c) 2019-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "mathlib.h"8#include "v_math.h"9#include "test_defs.h"10#include "test_sig.h"1112#define N (1 << V_EXP_TABLE_BITS)13#define IndexMask (N - 1)1415const static volatile struct16{17float64x2_t poly[3];18float64x2_t inv_ln2, ln2_hi, ln2_lo, shift;19#if !WANT_SIMD_EXCEPT20float64x2_t special_bound, scale_thresh;21#endif22} data = {23/* maxerr: 1.88 +0.5 ulp24rel error: 1.4337*2^-5325abs error: 1.4299*2^-53 in [ -ln2/256, ln2/256 ]. */26.poly = { V2 (0x1.ffffffffffd43p-2), V2 (0x1.55555c75adbb2p-3),27V2 (0x1.55555da646206p-5) },28#if !WANT_SIMD_EXCEPT29.scale_thresh = V2 (163840.0), /* 1280.0 * N. */30.special_bound = V2 (704.0),31#endif32.inv_ln2 = V2 (0x1.71547652b82fep7), /* N/ln2. */33.ln2_hi = V2 (0x1.62e42fefa39efp-8), /* ln2/N. */34.ln2_lo = V2 (0x1.abc9e3b39803f3p-63),35.shift = V2 (0x1.8p+52)36};3738#define C(i) data.poly[i]39#define Tab __v_exp_data4041#if WANT_SIMD_EXCEPT4243# define TinyBound v_u64 (0x2000000000000000) /* asuint64 (0x1p-511). */44# define BigBound v_u64 (0x4080000000000000) /* asuint64 (0x1p9). */45# define SpecialBound v_u64 (0x2080000000000000) /* BigBound - TinyBound. */4647static float64x2_t VPCS_ATTR NOINLINE48special_case (float64x2_t x, float64x2_t y, uint64x2_t cmp)49{50/* If fenv exceptions are to be triggered correctly, fall back to the scalar51routine to special lanes. */52return v_call_f64 (exp, x, y, cmp);53}5455#else5657# define SpecialOffset v_u64 (0x6000000000000000) /* 0x1p513. */58/* SpecialBias1 + SpecialBias1 = asuint(1.0). */59# define SpecialBias1 v_u64 (0x7000000000000000) /* 0x1p769. */60# define SpecialBias2 v_u64 (0x3010000000000000) /* 0x1p-254. */6162static inline float64x2_t VPCS_ATTR63special_case (float64x2_t s, float64x2_t y, float64x2_t n)64{65/* 2^(n/N) may overflow, break it up into s1*s2. */66uint64x2_t b = vandq_u64 (vcltzq_f64 (n), SpecialOffset);67float64x2_t s1 = vreinterpretq_f64_u64 (vsubq_u64 (SpecialBias1, b));68float64x2_t s2 = vreinterpretq_f64_u64 (69vaddq_u64 (vsubq_u64 (vreinterpretq_u64_f64 (s), SpecialBias2), b));70uint64x2_t cmp = vcagtq_f64 (n, data.scale_thresh);71float64x2_t r1 = vmulq_f64 (s1, s1);72float64x2_t r0 = vmulq_f64 (vfmaq_f64 (s2, y, s2), s1);73return vbslq_f64 (cmp, r1, r0);74}7576#endif7778float64x2_t VPCS_ATTR V_NAME_D1 (exp) (float64x2_t x)79{80float64x2_t n, r, r2, s, y, z;81uint64x2_t cmp, u, e;8283#if WANT_SIMD_EXCEPT84/* If any lanes are special, mask them with 1 and retain a copy of x to allow85special_case to fix special lanes later. This is only necessary if fenv86exceptions are to be triggered correctly. */87float64x2_t xm = x;88uint64x2_t iax = vreinterpretq_u64_f64 (vabsq_f64 (x));89cmp = vcgeq_u64 (vsubq_u64 (iax, TinyBound), SpecialBound);90if (unlikely (v_any_u64 (cmp)))91x = vbslq_f64 (cmp, v_f64 (1), x);92#else93cmp = vcagtq_f64 (x, data.special_bound);94#endif9596/* n = round(x/(ln2/N)). */97z = vfmaq_f64 (data.shift, x, data.inv_ln2);98u = vreinterpretq_u64_f64 (z);99n = vsubq_f64 (z, data.shift);100101/* r = x - n*ln2/N. */102r = x;103r = vfmsq_f64 (r, data.ln2_hi, n);104r = vfmsq_f64 (r, data.ln2_lo, n);105106e = vshlq_n_u64 (u, 52 - V_EXP_TABLE_BITS);107108/* y = exp(r) - 1 ~= r + C0 r^2 + C1 r^3 + C2 r^4. */109r2 = vmulq_f64 (r, r);110y = vfmaq_f64 (C (0), C (1), r);111y = vfmaq_f64 (y, C (2), r2);112y = vfmaq_f64 (r, y, r2);113114/* s = 2^(n/N). */115u = (uint64x2_t){ Tab[u[0] & IndexMask], Tab[u[1] & IndexMask] };116s = vreinterpretq_f64_u64 (vaddq_u64 (u, e));117118if (unlikely (v_any_u64 (cmp)))119#if WANT_SIMD_EXCEPT120return special_case (xm, vfmaq_f64 (s, y, s), cmp);121#else122return special_case (s, y, n);123#endif124125return vfmaq_f64 (s, y, s);126}127128TEST_SIG (V, D, 1, exp, -9.9, 9.9)129TEST_ULP (V_NAME_D1 (exp), 1.9)130TEST_DISABLE_FENV_IF_NOT (V_NAME_D1 (exp), WANT_SIMD_EXCEPT)131TEST_INTERVAL (V_NAME_D1 (exp), 0, 0xffff000000000000, 10000)132TEST_SYM_INTERVAL (V_NAME_D1 (exp), 0x1p-6, 0x1p6, 400000)133TEST_SYM_INTERVAL (V_NAME_D1 (exp), 633.3, 733.3, 10000)134135136