Path: blob/main/contrib/arm-optimized-routines/math/aarch64/advsimd/logf.c
48375 views
/*1* Single-precision vector log function.2*3* Copyright (c) 2019-2024, Arm Limited.4* SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception5*/6#include "v_math.h"7#include "test_defs.h"8#include "test_sig.h"910static const struct data11{12float32x4_t c2, c4, c6, ln2;13uint32x4_t off, offset_lower_bound, mantissa_mask;14uint16x8_t special_bound;15float c1, c3, c5, c0;16} data = {17/* 3.34 ulp error. */18.c0 = -0x1.3e737cp-3f,19.c1 = 0x1.5a9aa2p-3f,20.c2 = V4 (-0x1.4f9934p-3f),21.c3 = 0x1.961348p-3f,22.c4 = V4 (-0x1.00187cp-2f),23.c5 = 0x1.555d7cp-2f,24.c6 = V4 (-0x1.ffffc8p-2f),25.ln2 = V4 (0x1.62e43p-1f),26/* Lower bound is the smallest positive normal float 0x00800000. For27optimised register use subnormals are detected after offset has been28subtracted, so lower bound is 0x0080000 - offset (which wraps around). */29.offset_lower_bound = V4 (0x00800000 - 0x3f2aaaab),30.special_bound = V8 (0x7f00), /* top16(asuint32(inf) - 0x00800000). */31.off = V4 (0x3f2aaaab), /* 0.666667. */32.mantissa_mask = V4 (0x007fffff)33};3435static float32x4_t VPCS_ATTR NOINLINE36special_case (float32x4_t p, uint32x4_t u_off, float32x4_t y, float32x4_t r2,37uint16x4_t cmp, const struct data *d)38{39/* Fall back to scalar code. */40return v_call_f32 (logf, vreinterpretq_f32_u32 (vaddq_u32 (u_off, d->off)),41vfmaq_f32 (p, y, r2), vmovl_u16 (cmp));42}4344float32x4_t VPCS_ATTR NOINLINE V_NAME_F1 (log) (float32x4_t x)45{46const struct data *d = ptr_barrier (&data);47float32x4_t c1350 = vld1q_f32 (&d->c1);4849/* To avoid having to mov x out of the way, keep u after offset has been50applied, and recover x by adding the offset back in the special-case51handler. */52uint32x4_t u_off = vsubq_u32 (vreinterpretq_u32_f32 (x), d->off);5354/* x = 2^n * (1+r), where 2/3 < 1+r < 4/3. */55float32x4_t n = vcvtq_f32_s32 (56vshrq_n_s32 (vreinterpretq_s32_u32 (u_off), 23)); /* signextend. */57uint16x4_t cmp = vcge_u16 (vsubhn_u32 (u_off, d->offset_lower_bound),58vget_low_u16 (d->special_bound));5960uint32x4_t u = vaddq_u32 (vandq_u32 (u_off, d->mantissa_mask), d->off);61float32x4_t r = vsubq_f32 (vreinterpretq_f32_u32 (u), v_f32 (1.0f));6263/* y = log(1+r) + n*ln2. */64float32x4_t r2 = vmulq_f32 (r, r);65/* n*ln2 + r + r2*(P1 + r*P2 + r2*(P3 + r*P4 + r2*(P5 + r*P6 + r2*P7))). */66float32x4_t p = vfmaq_laneq_f32 (d->c2, r, c1350, 0);67float32x4_t q = vfmaq_laneq_f32 (d->c4, r, c1350, 1);68float32x4_t y = vfmaq_laneq_f32 (d->c6, r, c1350, 2);69p = vfmaq_laneq_f32 (p, r2, c1350, 3);7071q = vfmaq_f32 (q, p, r2);72y = vfmaq_f32 (y, q, r2);73p = vfmaq_f32 (r, d->ln2, n);7475if (unlikely (v_any_u16h (cmp)))76return special_case (p, u_off, y, r2, cmp, d);77return vfmaq_f32 (p, y, r2);78}7980HALF_WIDTH_ALIAS_F1 (log)8182TEST_SIG (V, F, 1, log, 0.01, 11.1)83TEST_ULP (V_NAME_F1 (log), 2.9)84TEST_DISABLE_FENV_IF_NOT (V_NAME_F1 (log), WANT_SIMD_EXCEPT)85TEST_INTERVAL (V_NAME_F1 (log), 0, 0xffff0000, 10000)86TEST_INTERVAL (V_NAME_F1 (log), 0x1p-4, 0x1p4, 500000)87TEST_INTERVAL (V_NAME_F1 (log), 0, inf, 50000)888990