Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/log2.c
48375 views
/*1* Double-precision vector log2 function.2*3* Copyright (c) 2022-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/67#include "v_math.h"8#include "test_sig.h"9#include "test_defs.h"1011static const struct data12{13uint64x2_t off, sign_exp_mask, offset_lower_bound;14uint32x4_t special_bound;15float64x2_t c0, c2;16double c1, c3, invln2, c4;17} data = {18/* Each coefficient was generated to approximate log(r) for |r| < 0x1.fp-919and N = 128, then scaled by log2(e) in extended precision and rounded back20to double precision. */21.c0 = V2 (-0x1.71547652b8300p-1),22.c1 = 0x1.ec709dc340953p-2,23.c2 = V2 (-0x1.71547651c8f35p-2),24.c3 = 0x1.2777ebe12dda5p-2,25.c4 = -0x1.ec738d616fe26p-3,26.invln2 = 0x1.71547652b82fep0,27.off = V2 (0x3fe6900900000000),28.sign_exp_mask = V2 (0xfff0000000000000),29/* Lower bound is 0x0010000000000000. For30optimised register use subnormals are detected after offset has been31subtracted, so lower bound - offset (which wraps around). */32.offset_lower_bound = V2 (0x0010000000000000 - 0x3fe6900900000000),33.special_bound = V4 (0x7fe00000), /* asuint64(inf) - asuint64(0x1p-1022). */34};3536#define N (1 << V_LOG2_TABLE_BITS)37#define IndexMask (N - 1)3839struct entry40{41float64x2_t invc;42float64x2_t log2c;43};4445static inline struct entry46lookup (uint64x2_t i)47{48struct entry e;49uint64_t i050= (vgetq_lane_u64 (i, 0) >> (52 - V_LOG2_TABLE_BITS)) & IndexMask;51uint64_t i152= (vgetq_lane_u64 (i, 1) >> (52 - V_LOG2_TABLE_BITS)) & IndexMask;53float64x2_t e0 = vld1q_f64 (&__v_log2_data.table[i0].invc);54float64x2_t e1 = vld1q_f64 (&__v_log2_data.table[i1].invc);55e.invc = vuzp1q_f64 (e0, e1);56e.log2c = vuzp2q_f64 (e0, e1);57return e;58}5960static float64x2_t VPCS_ATTR NOINLINE61special_case (float64x2_t hi, uint64x2_t u_off, float64x2_t y, float64x2_t r2,62uint32x2_t special, const struct data *d)63{64float64x2_t x = vreinterpretq_f64_u64 (vaddq_u64 (u_off, d->off));65return v_call_f64 (log2, x, vfmaq_f64 (hi, y, r2), vmovl_u32 (special));66}6768/* Double-precision vector log2 routine. Implements the same algorithm as69vector log10, with coefficients and table entries scaled in extended70precision. The maximum observed error is 2.58 ULP:71_ZGVnN2v_log2(0x1.0b556b093869bp+0) got 0x1.fffb34198d9dap-572want 0x1.fffb34198d9ddp-5. */73float64x2_t VPCS_ATTR V_NAME_D1 (log2) (float64x2_t x)74{75const struct data *d = ptr_barrier (&data);7677/* To avoid having to mov x out of the way, keep u after offset has been78applied, and recover x by adding the offset back in the special-case79handler. */80uint64x2_t u = vreinterpretq_u64_f64 (x);81uint64x2_t u_off = vsubq_u64 (u, d->off);8283/* x = 2^k z; where z is in range [Off,2*Off) and exact.84The range is split into N subintervals.85The ith subinterval contains z and c is near its center. */86int64x2_t k = vshrq_n_s64 (vreinterpretq_s64_u64 (u_off), 52);87uint64x2_t iz = vsubq_u64 (u, vandq_u64 (u_off, d->sign_exp_mask));88float64x2_t z = vreinterpretq_f64_u64 (iz);8990struct entry e = lookup (u_off);9192uint32x2_t special = vcge_u32 (vsubhn_u64 (u_off, d->offset_lower_bound),93vget_low_u32 (d->special_bound));9495/* log2(x) = log1p(z/c-1)/log(2) + log2(c) + k. */96float64x2_t r = vfmaq_f64 (v_f64 (-1.0), z, e.invc);97float64x2_t kd = vcvtq_f64_s64 (k);9899float64x2_t invln2_and_c4 = vld1q_f64 (&d->invln2);100float64x2_t hi101= vfmaq_laneq_f64 (vaddq_f64 (e.log2c, kd), r, invln2_and_c4, 0);102103float64x2_t r2 = vmulq_f64 (r, r);104float64x2_t odd_coeffs = vld1q_f64 (&d->c1);105float64x2_t y = vfmaq_laneq_f64 (d->c2, r, odd_coeffs, 1);106float64x2_t p = vfmaq_laneq_f64 (d->c0, r, odd_coeffs, 0);107y = vfmaq_laneq_f64 (y, r2, invln2_and_c4, 1);108y = vfmaq_f64 (p, r2, y);109110if (unlikely (v_any_u32h (special)))111return special_case (hi, u_off, y, r2, special, d);112return vfmaq_f64 (hi, y, r2);113}114115TEST_SIG (V, D, 1, log2, 0.01, 11.1)116TEST_ULP (V_NAME_D1 (log2), 2.09)117TEST_INTERVAL (V_NAME_D1 (log2), -0.0, -0x1p126, 100)118TEST_INTERVAL (V_NAME_D1 (log2), 0x1p-149, 0x1p-126, 4000)119TEST_INTERVAL (V_NAME_D1 (log2), 0x1p-126, 0x1p-23, 50000)120TEST_INTERVAL (V_NAME_D1 (log2), 0x1p-23, 1.0, 50000)121TEST_INTERVAL (V_NAME_D1 (log2), 1.0, 100, 50000)122TEST_INTERVAL (V_NAME_D1 (log2), 100, inf, 50000)123124125