Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/expf_1u.c
48378 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"89static const struct data10{11float32x4_t shift, inv_ln2;12uint32x4_t exponent_bias;13float32x4_t c1, c2, c3, c4;14float32x4_t special_bound, scale_thresh;15uint32x4_t special_offset, special_bias;16float ln2_hi, ln2_lo, c0, nothing;17} data = {18.ln2_hi = 0x1.62e4p-1f,19.ln2_lo = 0x1.7f7d1cp-20f,20.shift = V4 (0x1.8p23f),21.inv_ln2 = V4 (0x1.715476p+0f),22.exponent_bias = V4 (0x3f800000),23.special_bound = V4 (126.0f),24.scale_thresh = V4 (192.0f),25.special_offset = V4 (0x83000000),26.special_bias = V4 (0x7f000000),27/* maxerr: 0.36565 +0.5 ulp. */28.c0 = 0x1.6a6000p-10f,29.c1 = V4 (0x1.12718ep-7f),30.c2 = V4 (0x1.555af0p-5f),31.c3 = V4 (0x1.555430p-3f),32.c4 = V4 (0x1.fffff4p-2f),33};3435static float32x4_t VPCS_ATTR NOINLINE36specialcase (float32x4_t p, float32x4_t n, uint32x4_t e, const struct data *d)37{38/* 2^n may overflow, break it up into s1*s2. */39uint32x4_t b = vandq_u32 (vclezq_f32 (n), d->special_offset);40float32x4_t s1 = vreinterpretq_f32_u32 (vaddq_u32 (b, d->special_bias));41float32x4_t s2 = vreinterpretq_f32_u32 (vsubq_u32 (e, b));42uint32x4_t cmp = vcagtq_f32 (n, d->scale_thresh);43float32x4_t r1 = vmulq_f32 (s1, s1);44float32x4_t r0 = vmulq_f32 (vmulq_f32 (p, s1), s2);45return vreinterpretq_f32_u32 ((cmp & vreinterpretq_u32_f32 (r1))46| (~cmp & vreinterpretq_u32_f32 (r0)));47}4849float32x4_t VPCS_ATTR50_ZGVnN4v_expf_1u (float32x4_t x)51{52const struct data *d = ptr_barrier (&data);53float32x4_t ln2_c0 = vld1q_f32 (&d->ln2_hi);5455/* exp(x) = 2^n * poly(r), with poly(r) in [1/sqrt(2),sqrt(2)]56x = ln2*n + r, with r in [-ln2/2, ln2/2]. */57float32x4_t z = vmulq_f32 (x, d->inv_ln2);58float32x4_t n = vrndaq_f32 (z);59float32x4_t r = vfmsq_laneq_f32 (x, n, ln2_c0, 0);60r = vfmsq_laneq_f32 (r, n, ln2_c0, 1);61uint32x4_t e = vshlq_n_u32 (vreinterpretq_u32_s32 (vcvtaq_s32_f32 (z)), 23);62float32x4_t scale = vreinterpretq_f32_u32 (e + d->exponent_bias);63uint32x4_t cmp = vcagtq_f32 (n, d->special_bound);64float32x4_t p = vfmaq_laneq_f32 (d->c1, r, ln2_c0, 2);65p = vfmaq_f32 (d->c2, p, r);66p = vfmaq_f32 (d->c3, p, r);67p = vfmaq_f32 (d->c4, p, r);68p = vfmaq_f32 (v_f32 (1.0f), p, r);69p = vfmaq_f32 (v_f32 (1.0f), p, r);70if (unlikely (v_any_u32 (cmp)))71return specialcase (p, n, e, d);72return scale * p;73}7475TEST_ULP (_ZGVnN4v_expf_1u, 0.4)76TEST_DISABLE_FENV (_ZGVnN4v_expf_1u)77TEST_INTERVAL (_ZGVnN4v_expf_1u, 0, 0xffff0000, 10000)78TEST_SYM_INTERVAL (_ZGVnN4v_expf_1u, 0x1p-14, 0x1p8, 500000)798081