Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/log10f.c
48378 views
/*1* Single-precision SVE log10 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"1011static const struct data12{13float poly_0246[4];14float poly_1357[4];15float ln2, inv_ln10;16uint32_t off, lower;17} data = {18.poly_1357 = {19/* Coefficients copied from the AdvSIMD routine, then rearranged so that coeffs201, 3, 5 and 7 can be loaded as a single quad-word, hence used with _lane21variant of MLA intrinsic. */220x1.2879c8p-3f, 0x1.6408f8p-4f, 0x1.f0e514p-5f, 0x1.f5f76ap-5f23},24.poly_0246 = { -0x1.bcb79cp-3f, -0x1.bcd472p-4f, -0x1.246f8p-4f,25-0x1.0fc92cp-4f },26.ln2 = 0x1.62e43p-1f,27.inv_ln10 = 0x1.bcb7b2p-2f,28.off = 0x3f2aaaab,29/* Lower bound is the smallest positive normal float 0x00800000. For30optimised register use subnormals are detected after offset has been31subtracted, so lower bound is 0x0080000 - offset (which wraps around). */32.lower = 0x00800000 - 0x3f2aaaab33};3435#define Thres 0x7f000000 /* asuint32(inf) - 0x00800000. */36#define MantissaMask 0x007fffff3738static svfloat32_t NOINLINE39special_case (svuint32_t u_off, svfloat32_t p, svfloat32_t r2, svfloat32_t y,40svbool_t cmp)41{42return sv_call_f32 (43log10f, svreinterpret_f32 (svadd_x (svptrue_b32 (), u_off, data.off)),44svmla_x (svptrue_b32 (), p, r2, y), cmp);45}4647/* Optimised implementation of SVE log10f using the same algorithm and48polynomial as AdvSIMD log10f.49Maximum error is 3.31ulps:50SV_NAME_F1 (log10)(0x1.555c16p+0) got 0x1.ffe2fap-451want 0x1.ffe2f4p-4. */52svfloat32_t SV_NAME_F1 (log10) (svfloat32_t x, const svbool_t pg)53{54const struct data *d = ptr_barrier (&data);5556svuint32_t u_off = svreinterpret_u32 (x);5758u_off = svsub_x (pg, u_off, d->off);59svbool_t special = svcmpge (pg, svsub_x (pg, u_off, d->lower), Thres);6061/* x = 2^n * (1+r), where 2/3 < 1+r < 4/3. */62svfloat32_t n = svcvt_f32_x (63pg, svasr_x (pg, svreinterpret_s32 (u_off), 23)); /* signextend. */64svuint32_t ix = svand_x (pg, u_off, MantissaMask);65ix = svadd_x (pg, ix, d->off);66svfloat32_t r = svsub_x (pg, svreinterpret_f32 (ix), 1.0f);6768/* y = log10(1+r) + n*log10(2)69log10(1+r) ~ r * InvLn(10) + P(r)70where P(r) is a polynomial. Use order 9 for log10(1+x), i.e. order 8 for71log10(1+x)/x, with x in [-1/3, 1/3] (offset=2/3). */72svfloat32_t r2 = svmul_x (svptrue_b32 (), r, r);73svfloat32_t r4 = svmul_x (svptrue_b32 (), r2, r2);74svfloat32_t p_1357 = svld1rq (svptrue_b32 (), &d->poly_1357[0]);75svfloat32_t q_01 = svmla_lane (sv_f32 (d->poly_0246[0]), r, p_1357, 0);76svfloat32_t q_23 = svmla_lane (sv_f32 (d->poly_0246[1]), r, p_1357, 1);77svfloat32_t q_45 = svmla_lane (sv_f32 (d->poly_0246[2]), r, p_1357, 2);78svfloat32_t q_67 = svmla_lane (sv_f32 (d->poly_0246[3]), r, p_1357, 3);79svfloat32_t q_47 = svmla_x (pg, q_45, r2, q_67);80svfloat32_t q_03 = svmla_x (pg, q_01, r2, q_23);81svfloat32_t y = svmla_x (pg, q_03, r4, q_47);8283/* Using hi = Log10(2)*n + r*InvLn(10) is faster but less accurate. */84svfloat32_t hi = svmla_x (pg, r, n, d->ln2);85hi = svmul_x (pg, hi, d->inv_ln10);8687if (unlikely (svptest_any (pg, special)))88return special_case (u_off, hi, r2, y, special);89return svmla_x (svptrue_b32 (), hi, r2, y);90}9192TEST_SIG (SV, F, 1, log10, 0.01, 11.1)93TEST_ULP (SV_NAME_F1 (log10), 2.82)94TEST_DISABLE_FENV (SV_NAME_F1 (log10))95TEST_INTERVAL (SV_NAME_F1 (log10), -0.0, -0x1p126, 100)96TEST_INTERVAL (SV_NAME_F1 (log10), 0x1p-149, 0x1p-126, 4000)97TEST_INTERVAL (SV_NAME_F1 (log10), 0x1p-126, 0x1p-23, 50000)98TEST_INTERVAL (SV_NAME_F1 (log10), 0x1p-23, 1.0, 50000)99TEST_INTERVAL (SV_NAME_F1 (log10), 1.0, 100, 50000)100TEST_INTERVAL (SV_NAME_F1 (log10), 100, inf, 50000)101CLOSE_SVE_ATTR102103104