Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/expf.c
48375 views
/*1* Single-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*/6#include "v_math.h"7#include "test_defs.h"8#include "test_sig.h"910static const struct data11{12float32x4_t c1, c3, c4, inv_ln2;13float ln2_hi, ln2_lo, c0, c2;14uint32x4_t exponent_bias, special_offset, special_bias;15#if !WANT_SIMD_EXCEPT16float32x4_t special_bound, scale_thresh;17#endif18} data = {19/* maxerr: 1.45358 +0.5 ulp. */20.c0 = 0x1.0e4020p-7f,21.c1 = V4 (0x1.573e2ep-5f),22.c2 = 0x1.555e66p-3f,23.c3 = V4 (0x1.fffdb6p-2f),24.c4 = V4 (0x1.ffffecp-1f),25.inv_ln2 = V4 (0x1.715476p+0f),26.ln2_hi = 0x1.62e4p-1f,27.ln2_lo = 0x1.7f7d1cp-20f,28.exponent_bias = V4 (0x3f800000),29.special_offset = V4 (0x82000000),30.special_bias = V4 (0x7f000000),31#if !WANT_SIMD_EXCEPT32.special_bound = V4 (126.0f),33.scale_thresh = V4 (192.0f),34#endif35};3637#define C(i) d->poly[i]3839#if WANT_SIMD_EXCEPT4041# define TinyBound v_u32 (0x20000000) /* asuint (0x1p-63). */42# define BigBound v_u32 (0x42800000) /* asuint (0x1p6). */43# define SpecialBound v_u32 (0x22800000) /* BigBound - TinyBound. */4445static float32x4_t VPCS_ATTR NOINLINE46special_case (float32x4_t x, float32x4_t y, uint32x4_t cmp)47{48/* If fenv exceptions are to be triggered correctly, fall back to the scalar49routine to special lanes. */50return v_call_f32 (expf, x, y, cmp);51}5253#else5455static float32x4_t VPCS_ATTR NOINLINE56special_case (float32x4_t poly, float32x4_t n, uint32x4_t e, uint32x4_t cmp1,57float32x4_t scale, const struct data *d)58{59/* 2^n may overflow, break it up into s1*s2. */60uint32x4_t b = vandq_u32 (vclezq_f32 (n), d->special_offset);61float32x4_t s1 = vreinterpretq_f32_u32 (vaddq_u32 (b, d->special_bias));62float32x4_t s2 = vreinterpretq_f32_u32 (vsubq_u32 (e, b));63uint32x4_t cmp2 = vcagtq_f32 (n, d->scale_thresh);64float32x4_t r2 = vmulq_f32 (s1, s1);65// (s2 + p*s2)*s1 = s2(p+1)s166float32x4_t r1 = vmulq_f32 (vfmaq_f32 (s2, poly, s2), s1);67/* Similar to r1 but avoids double rounding in the subnormal range. */68float32x4_t r0 = vfmaq_f32 (scale, poly, scale);69float32x4_t r = vbslq_f32 (cmp1, r1, r0);70return vbslq_f32 (cmp2, r2, r);71}7273#endif7475float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (exp) (float32x4_t x)76{77const struct data *d = ptr_barrier (&data);78float32x4_t ln2_c02 = vld1q_f32 (&d->ln2_hi);7980#if WANT_SIMD_EXCEPT81/* asuint(x) - TinyBound >= BigBound - TinyBound. */82uint32x4_t cmp = vcgeq_u32 (83vsubq_u32 (vandq_u32 (vreinterpretq_u32_f32 (x), v_u32 (0x7fffffff)),84TinyBound),85SpecialBound);86float32x4_t xm = x;87/* If any lanes are special, mask them with 1 and retain a copy of x to allow88special case handler to fix special lanes later. This is only necessary if89fenv exceptions are to be triggered correctly. */90if (unlikely (v_any_u32 (cmp)))91x = vbslq_f32 (cmp, v_f32 (1), x);92#endif9394/* exp(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]95x = ln2*n + r, with r in [-ln2/2, ln2/2]. */96float32x4_t n = vrndaq_f32 (vmulq_f32 (x, d->inv_ln2));97float32x4_t r = vfmsq_laneq_f32 (x, n, ln2_c02, 0);98r = vfmsq_laneq_f32 (r, n, ln2_c02, 1);99uint32x4_t e = vshlq_n_u32 (vreinterpretq_u32_s32 (vcvtq_s32_f32 (n)), 23);100float32x4_t scale = vreinterpretq_f32_u32 (vaddq_u32 (e, d->exponent_bias));101102#if !WANT_SIMD_EXCEPT103uint32x4_t cmp = vcagtq_f32 (n, d->special_bound);104#endif105106float32x4_t r2 = vmulq_f32 (r, r);107float32x4_t p = vfmaq_laneq_f32 (d->c1, r, ln2_c02, 2);108float32x4_t q = vfmaq_laneq_f32 (d->c3, r, ln2_c02, 3);109q = vfmaq_f32 (q, p, r2);110p = vmulq_f32 (d->c4, r);111float32x4_t poly = vfmaq_f32 (p, q, r2);112113if (unlikely (v_any_u32 (cmp)))114#if WANT_SIMD_EXCEPT115return special_case (xm, vfmaq_f32 (scale, poly, scale), cmp);116#else117return special_case (poly, n, e, cmp, scale, d);118#endif119120return vfmaq_f32 (scale, poly, scale);121}122123HALF_WIDTH_ALIAS_F1 (exp)124125TEST_SIG (V, F, 1, exp, -9.9, 9.9)126TEST_ULP (V_NAME_F1 (exp), 1.49)127TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (exp), WANT_SIMD_EXCEPT)128TEST_INTERVAL (V_NAME_F1 (exp), 0, 0xffff0000, 10000)129TEST_SYM_INTERVAL (V_NAME_F1 (exp), 0x1p-14, 0x1p8, 500000)130131132