Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/exp2f.c
48375 views
/*1* Single-precision vector 2^x function.2*3* Copyright (c) 2019-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "v_math.h"8#include "test_defs.h"9#include "test_sig.h"1011static const struct data12{13float32x4_t c1, c3;14uint32x4_t exponent_bias, special_offset, special_bias;15#if !WANT_SIMD_EXCEPT16float32x4_t scale_thresh, special_bound;17#endif18float c0, c2, c4, zero;19} data = {20/* maxerr: 1.962 ulp. */21.c0 = 0x1.59977ap-10f,22.c1 = V4 (0x1.3ce9e4p-7f),23.c2 = 0x1.c6bd32p-5f,24.c3 = V4 (0x1.ebf9bcp-3f),25.c4 = 0x1.62e422p-1f,26.exponent_bias = V4 (0x3f800000),27.special_offset = V4 (0x82000000),28.special_bias = V4 (0x7f000000),29#if !WANT_SIMD_EXCEPT30.special_bound = V4 (126.0f),31.scale_thresh = V4 (192.0f),32#endif33};3435#if WANT_SIMD_EXCEPT3637# define TinyBound v_u32 (0x20000000) /* asuint (0x1p-63). */38# define BigBound v_u32 (0x42800000) /* asuint (0x1p6). */39# define SpecialBound v_u32 (0x22800000) /* BigBound - TinyBound. */4041static float32x4_t VPCS_ATTR NOINLINE42special_case (float32x4_t x, float32x4_t y, uint32x4_t cmp)43{44/* If fenv exceptions are to be triggered correctly, fall back to the scalar45routine for special lanes. */46return v_call_f32 (exp2f, x, y, cmp);47}4849#else5051static float32x4_t VPCS_ATTR NOINLINE52special_case (float32x4_t poly, float32x4_t n, uint32x4_t e, uint32x4_t cmp1,53float32x4_t scale, const struct data *d)54{55/* 2^n may overflow, break it up into s1*s2. */56uint32x4_t b = vandq_u32 (vclezq_f32 (n), d->special_offset);57float32x4_t s1 = vreinterpretq_f32_u32 (vaddq_u32 (b, d->special_bias));58float32x4_t s2 = vreinterpretq_f32_u32 (vsubq_u32 (e, b));59uint32x4_t cmp2 = vcagtq_f32 (n, d->scale_thresh);60float32x4_t r2 = vmulq_f32 (s1, s1);61float32x4_t r1 = vmulq_f32 (vfmaq_f32 (s2, poly, s2), s1);62/* Similar to r1 but avoids double rounding in the subnormal range. */63float32x4_t r0 = vfmaq_f32 (scale, poly, scale);64float32x4_t r = vbslq_f32 (cmp1, r1, r0);65return vbslq_f32 (cmp2, r2, r);66}6768#endif6970float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (exp2) (float32x4_t x)71{72const struct data *d = ptr_barrier (&data);7374#if WANT_SIMD_EXCEPT75/* asuint(|x|) - TinyBound >= BigBound - TinyBound. */76uint32x4_t ia = vreinterpretq_u32_f32 (vabsq_f32 (x));77uint32x4_t cmp = vcgeq_u32 (vsubq_u32 (ia, TinyBound), SpecialBound);78float32x4_t xm = x;79/* If any lanes are special, mask them with 1 and retain a copy of x to allow80special_case to fix special lanes later. This is only necessary if fenv81exceptions are to be triggered correctly. */82if (unlikely (v_any_u32 (cmp)))83x = vbslq_f32 (cmp, v_f32 (1), x);84#endif8586/* exp2(x) = 2^n (1 + poly(r)), with 1 + poly(r) in [1/sqrt(2),sqrt(2)]87x = n + r, with r in [-1/2, 1/2]. */88float32x4_t n = vrndaq_f32 (x);89float32x4_t r = vsubq_f32 (x, n);90uint32x4_t e = vshlq_n_u32 (vreinterpretq_u32_s32 (vcvtaq_s32_f32 (x)), 23);91float32x4_t scale = vreinterpretq_f32_u32 (vaddq_u32 (e, d->exponent_bias));9293#if !WANT_SIMD_EXCEPT94uint32x4_t cmp = vcagtq_f32 (n, d->special_bound);95#endif9697float32x4_t c024 = vld1q_f32 (&d->c0);98float32x4_t r2 = vmulq_f32 (r, r);99float32x4_t p = vfmaq_laneq_f32 (d->c1, r, c024, 0);100float32x4_t q = vfmaq_laneq_f32 (d->c3, r, c024, 1);101q = vfmaq_f32 (q, p, r2);102p = vmulq_laneq_f32 (r, c024, 2);103float32x4_t poly = vfmaq_f32 (p, q, r2);104105if (unlikely (v_any_u32 (cmp)))106#if WANT_SIMD_EXCEPT107return special_case (xm, vfmaq_f32 (scale, poly, scale), cmp);108#else109return special_case (poly, n, e, cmp, scale, d);110#endif111112return vfmaq_f32 (scale, poly, scale);113}114115HALF_WIDTH_ALIAS_F1 (exp2)116117TEST_SIG (V, F, 1, exp2, -9.9, 9.9)118TEST_ULP (V_NAME_F1 (exp2), 1.49)119TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (exp2), WANT_SIMD_EXCEPT)120TEST_INTERVAL (V_NAME_F1 (exp2), 0, 0xffff0000, 10000)121TEST_SYM_INTERVAL (V_NAME_F1 (exp2), 0x1p-14, 0x1p8, 500000)122123124