Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/exp2.c
48375 views
/*1* Double-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 "v_poly_f64.h"9#include "test_sig.h"10#include "test_defs.h"1112#define N (1 << V_EXP_TABLE_BITS)13#define IndexMask (N - 1)14#define BigBound 1022.015#define UOFlowBound 1280.016#define TinyBound 0x2000000000000000 /* asuint64(0x1p-511). */1718static const struct data19{20float64x2_t poly[4];21float64x2_t shift, scale_big_bound, scale_uoflow_bound;22} data = {23/* Coefficients are computed using Remez algorithm with24minimisation of the absolute error. */25.poly = { V2 (0x1.62e42fefa3686p-1), V2 (0x1.ebfbdff82c241p-3),26V2 (0x1.c6b09b16de99ap-5), V2 (0x1.3b2abf5571ad8p-7) },27.shift = V2 (0x1.8p52 / N),28.scale_big_bound = V2 (BigBound),29.scale_uoflow_bound = V2 (UOFlowBound),30};3132static inline uint64x2_t33lookup_sbits (uint64x2_t i)34{35return (uint64x2_t){ __v_exp_data[i[0] & IndexMask],36__v_exp_data[i[1] & IndexMask] };37}3839#if WANT_SIMD_EXCEPT4041# define Thres 0x2080000000000000 /* asuint64(512.0) - TinyBound. */4243/* Call scalar exp2 as a fallback. */44static float64x2_t VPCS_ATTR NOINLINE45special_case (float64x2_t x, float64x2_t y, uint64x2_t is_special)46{47return v_call_f64 (exp2, x, y, is_special);48}4950#else5152# define SpecialOffset 0x6000000000000000 /* 0x1p513. */53/* SpecialBias1 + SpecialBias1 = asuint(1.0). */54# define SpecialBias1 0x7000000000000000 /* 0x1p769. */55# define SpecialBias2 0x3010000000000000 /* 0x1p-254. */5657static inline float64x2_t VPCS_ATTR58special_case (float64x2_t s, float64x2_t y, float64x2_t n,59const struct data *d)60{61/* 2^(n/N) may overflow, break it up into s1*s2. */62uint64x2_t b = vandq_u64 (vclezq_f64 (n), v_u64 (SpecialOffset));63float64x2_t s1 = vreinterpretq_f64_u64 (vsubq_u64 (v_u64 (SpecialBias1), b));64float64x2_t s2 = vreinterpretq_f64_u64 (vaddq_u64 (65vsubq_u64 (vreinterpretq_u64_f64 (s), v_u64 (SpecialBias2)), b));66uint64x2_t cmp = vcagtq_f64 (n, d->scale_uoflow_bound);67float64x2_t r1 = vmulq_f64 (s1, s1);68float64x2_t r0 = vmulq_f64 (vfmaq_f64 (s2, s2, y), s1);69return vbslq_f64 (cmp, r1, r0);70}7172#endif7374/* Fast vector implementation of exp2.75Maximum measured error is 1.65 ulp.76_ZGVnN2v_exp2(-0x1.4c264ab5b559bp-6) got 0x1.f8db0d4df721fp-177want 0x1.f8db0d4df721dp-1. */78VPCS_ATTR79float64x2_t V_NAME_D1 (exp2) (float64x2_t x)80{81const struct data *d = ptr_barrier (&data);82uint64x2_t cmp;83#if WANT_SIMD_EXCEPT84uint64x2_t ia = vreinterpretq_u64_f64 (vabsq_f64 (x));85cmp = vcgeq_u64 (vsubq_u64 (ia, v_u64 (TinyBound)), v_u64 (Thres));86/* Mask special lanes and retain a copy of x for passing to special-case87handler. */88float64x2_t xc = x;89x = v_zerofy_f64 (x, cmp);90#else91cmp = vcagtq_f64 (x, d->scale_big_bound);92#endif9394/* n = round(x/N). */95float64x2_t z = vaddq_f64 (d->shift, x);96uint64x2_t u = vreinterpretq_u64_f64 (z);97float64x2_t n = vsubq_f64 (z, d->shift);9899/* r = x - n/N. */100float64x2_t r = vsubq_f64 (x, n);101102/* s = 2^(n/N). */103uint64x2_t e = vshlq_n_u64 (u, 52 - V_EXP_TABLE_BITS);104u = lookup_sbits (u);105float64x2_t s = vreinterpretq_f64_u64 (vaddq_u64 (u, e));106107/* y ~ exp2(r) - 1. */108float64x2_t r2 = vmulq_f64 (r, r);109float64x2_t y = v_pairwise_poly_3_f64 (r, r2, d->poly);110y = vmulq_f64 (r, y);111112if (unlikely (v_any_u64 (cmp)))113#if !WANT_SIMD_EXCEPT114return special_case (s, y, n, d);115#else116return special_case (xc, vfmaq_f64 (s, s, y), cmp);117#endif118return vfmaq_f64 (s, s, y);119}120121TEST_SIG (V, D, 1, exp2, -9.9, 9.9)122TEST_ULP (V_NAME_D1 (exp2), 1.15)123TEST_DISABLE_FENV_IF_NOT (V_NAME_D1 (exp2), WANT_SIMD_EXCEPT)124TEST_SYM_INTERVAL (V_NAME_D1 (exp2), 0, TinyBound, 5000)125TEST_SYM_INTERVAL (V_NAME_D1 (exp2), TinyBound, BigBound, 10000)126TEST_SYM_INTERVAL (V_NAME_D1 (exp2), BigBound, UOFlowBound, 5000)127TEST_SYM_INTERVAL (V_NAME_D1 (exp2), UOFlowBound, inf, 10000)128129130