Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/log2.c
48375 views
/*1* Double-precision SVE log2 function.2*3* Copyright (c) 2022-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "sv_math.h"8#include "test_sig.h"9#include "test_defs.h"1011#define N (1 << V_LOG2_TABLE_BITS)12#define Max (0x7ff0000000000000)13#define Min (0x0010000000000000)14#define Thresh (0x7fe0000000000000) /* Max - Min. */1516static const struct data17{18double c0, c2;19double c1, c3;20double invln2, c4;21uint64_t off;22} data = {23.c0 = -0x1.71547652b83p-1,24.c1 = 0x1.ec709dc340953p-2,25.c2 = -0x1.71547651c8f35p-2,26.c3 = 0x1.2777ebe12dda5p-2,27.c4 = -0x1.ec738d616fe26p-3,28.invln2 = 0x1.71547652b82fep0,29.off = 0x3fe6900900000000,30};3132static svfloat64_t NOINLINE33special_case (svfloat64_t w, svuint64_t tmp, svfloat64_t y, svfloat64_t r2,34svbool_t special, const struct data *d)35{36svfloat64_t x = svreinterpret_f64 (svadd_x (svptrue_b64 (), tmp, d->off));37return sv_call_f64 (log2, x, svmla_x (svptrue_b64 (), w, r2, y), special);38}3940/* Double-precision SVE log2 routine.41Implements the same algorithm as AdvSIMD log10, with coefficients and table42entries scaled in extended precision.43The maximum observed error is 2.58 ULP:44SV_NAME_D1 (log2)(0x1.0b556b093869bp+0) got 0x1.fffb34198d9dap-545want 0x1.fffb34198d9ddp-5. */46svfloat64_t SV_NAME_D1 (log2) (svfloat64_t x, const svbool_t pg)47{48const struct data *d = ptr_barrier (&data);4950svuint64_t ix = svreinterpret_u64 (x);51svbool_t special = svcmpge (pg, svsub_x (pg, ix, Min), Thresh);5253/* x = 2^k z; where z is in range [Off,2*Off) and exact.54The range is split into N subintervals.55The ith subinterval contains z and c is near its center. */56svuint64_t tmp = svsub_x (pg, ix, d->off);57svuint64_t i = svlsr_x (pg, tmp, 51 - V_LOG2_TABLE_BITS);58i = svand_x (pg, i, (N - 1) << 1);59svfloat64_t k = svcvt_f64_x (pg, svasr_x (pg, svreinterpret_s64 (tmp), 52));60svfloat64_t z = svreinterpret_f64 (61svsub_x (pg, ix, svand_x (pg, tmp, 0xfffULL << 52)));6263svfloat64_t invc = svld1_gather_index (pg, &__v_log2_data.table[0].invc, i);64svfloat64_t log2c65= svld1_gather_index (pg, &__v_log2_data.table[0].log2c, i);6667/* log2(x) = log1p(z/c-1)/log(2) + log2(c) + k. */6869svfloat64_t invln2_and_c4 = svld1rq_f64 (svptrue_b64 (), &d->invln2);70svfloat64_t r = svmad_x (pg, invc, z, -1.0);71svfloat64_t w = svmla_lane_f64 (log2c, r, invln2_and_c4, 0);72w = svadd_x (pg, k, w);7374svfloat64_t odd_coeffs = svld1rq_f64 (svptrue_b64 (), &d->c1);75svfloat64_t r2 = svmul_x (svptrue_b64 (), r, r);76svfloat64_t y = svmla_lane_f64 (sv_f64 (d->c2), r, odd_coeffs, 1);77svfloat64_t p = svmla_lane_f64 (sv_f64 (d->c0), r, odd_coeffs, 0);78y = svmla_lane_f64 (y, r2, invln2_and_c4, 1);79y = svmla_x (pg, p, r2, y);8081if (unlikely (svptest_any (pg, special)))82return special_case (w, tmp, y, r2, special, d);83return svmla_x (pg, w, r2, y);84}8586TEST_SIG (SV, D, 1, log2, 0.01, 11.1)87TEST_ULP (SV_NAME_D1 (log2), 2.09)88TEST_DISABLE_FENV (SV_NAME_D1 (log2))89TEST_INTERVAL (SV_NAME_D1 (log2), -0.0, -0x1p126, 1000)90TEST_INTERVAL (SV_NAME_D1 (log2), 0.0, 0x1p-126, 4000)91TEST_INTERVAL (SV_NAME_D1 (log2), 0x1p-126, 0x1p-23, 50000)92TEST_INTERVAL (SV_NAME_D1 (log2), 0x1p-23, 1.0, 50000)93TEST_INTERVAL (SV_NAME_D1 (log2), 1.0, 100, 50000)94TEST_INTERVAL (SV_NAME_D1 (log2), 100, inf, 50000)95CLOSE_SVE_ATTR969798