Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/log2f.c
48375 views
/*1* Single-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{13float32x4_t c0, c2, c4, c6, c8;14uint32x4_t off, offset_lower_bound;15uint16x8_t special_bound;16uint32x4_t mantissa_mask;17float c1, c3, c5, c7;18} data = {19/* Coefficients generated using Remez algorithm approximate20log2(1+r)/r for r in [ -1/3, 1/3 ].21rel error: 0x1.c4c4b0cp-26. */22.c0 = V4 (0x1.715476p0f), /* (float)(1 / ln(2)). */23.c1 = -0x1.715458p-1f,24.c2 = V4 (0x1.ec701cp-2f),25.c3 = -0x1.7171a4p-2f,26.c4 = V4 (0x1.27a0b8p-2f),27.c5 = -0x1.e5143ep-3f,28.c6 = V4 (0x1.9d8ecap-3f),29.c7 = -0x1.c675bp-3f,30.c8 = V4 (0x1.9e495p-3f),31/* Lower bound is the smallest positive normal float 0x00800000. For32optimised register use subnormals are detected after offset has been33subtracted, so lower bound is 0x0080000 - offset (which wraps around). */34.offset_lower_bound = V4 (0x00800000 - 0x3f2aaaab),35.special_bound = V8 (0x7f00), /* top16(asuint32(inf) - 0x00800000). */36.off = V4 (0x3f2aaaab), /* 0.666667. */37.mantissa_mask = V4 (0x007fffff),38};3940static float32x4_t VPCS_ATTR NOINLINE41special_case (float32x4_t n, uint32x4_t u_off, float32x4_t p, float32x4_t r,42uint16x4_t cmp, const struct data *d)43{44/* Fall back to scalar code. */45return v_call_f32 (log2f, vreinterpretq_f32_u32 (vaddq_u32 (u_off, d->off)),46vfmaq_f32 (n, p, r), vmovl_u16 (cmp));47}4849/* Fast implementation for single precision AdvSIMD log2,50relies on same argument reduction as AdvSIMD logf.51Maximum error: 2.48 ULPs52_ZGVnN4v_log2f(0x1.558174p+0) got 0x1.a9be84p-253want 0x1.a9be8p-2. */54float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (log2) (float32x4_t x)55{56const struct data *d = ptr_barrier (&data);5758/* To avoid having to mov x out of the way, keep u after offset has been59applied, and recover x by adding the offset back in the special-case60handler. */61uint32x4_t u_off = vreinterpretq_u32_f32 (x);6263/* x = 2^n * (1+r), where 2/3 < 1+r < 4/3. */64u_off = vsubq_u32 (u_off, d->off);65float32x4_t n = vcvtq_f32_s32 (66vshrq_n_s32 (vreinterpretq_s32_u32 (u_off), 23)); /* signextend. */6768uint16x4_t special = vcge_u16 (vsubhn_u32 (u_off, d->offset_lower_bound),69vget_low_u16 (d->special_bound));7071uint32x4_t u = vaddq_u32 (vandq_u32 (u_off, d->mantissa_mask), d->off);72float32x4_t r = vsubq_f32 (vreinterpretq_f32_u32 (u), v_f32 (1.0f));7374/* y = log2(1+r) + n. */75float32x4_t r2 = vmulq_f32 (r, r);7677float32x4_t c1357 = vld1q_f32 (&d->c1);78float32x4_t c01 = vfmaq_laneq_f32 (d->c0, r, c1357, 0);79float32x4_t c23 = vfmaq_laneq_f32 (d->c2, r, c1357, 1);80float32x4_t c45 = vfmaq_laneq_f32 (d->c4, r, c1357, 2);81float32x4_t c67 = vfmaq_laneq_f32 (d->c6, r, c1357, 3);82float32x4_t p68 = vfmaq_f32 (c67, r2, d->c8);83float32x4_t p48 = vfmaq_f32 (c45, r2, p68);84float32x4_t p28 = vfmaq_f32 (c23, r2, p48);85float32x4_t p = vfmaq_f32 (c01, r2, p28);8687if (unlikely (v_any_u16h (special)))88return special_case (n, u_off, p, r, special, d);89return vfmaq_f32 (n, p, r);90}9192HALF_WIDTH_ALIAS_F1 (log2)9394TEST_SIG (V, F, 1, log2, 0.01, 11.1)95TEST_ULP (V_NAME_F1 (log2), 1.99)96TEST_INTERVAL (V_NAME_F1 (log2), -0.0, -0x1p126, 100)97TEST_INTERVAL (V_NAME_F1 (log2), 0x1p-149, 0x1p-126, 4000)98TEST_INTERVAL (V_NAME_F1 (log2), 0x1p-126, 0x1p-23, 50000)99TEST_INTERVAL (V_NAME_F1 (log2), 0x1p-23, 1.0, 50000)100TEST_INTERVAL (V_NAME_F1 (log2), 1.0, 100, 50000)101TEST_INTERVAL (V_NAME_F1 (log2), 100, inf, 50000)102103104