Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/logf.c
48378 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*/67#include "sv_math.h"8#include "test_sig.h"9#include "test_defs.h"1011static const struct data12{13float poly_0135[4];14float poly_246[3];15float ln2;16uint32_t off, lower;17} data = {18.poly_0135 = {19/* Coefficients copied from the AdvSIMD routine in math/, then rearranged so20that coeffs 0, 1, 3 and 5 can be loaded as a single quad-word, hence used21with _lane variant of MLA intrinsic. */22-0x1.3e737cp-3f, 0x1.5a9aa2p-3f, 0x1.961348p-3f, 0x1.555d7cp-2f23},24.poly_246 = { -0x1.4f9934p-3f, -0x1.00187cp-2f, -0x1.ffffc8p-2f },25.ln2 = 0x1.62e43p-1f,26.off = 0x3f2aaaab,27/* Lower bound is the smallest positive normal float 0x00800000. For28optimised register use subnormals are detected after offset has been29subtracted, so lower bound is 0x0080000 - offset (which wraps around). */30.lower = 0x00800000 - 0x3f2aaaab31};3233#define Thresh (0x7f000000) /* asuint32(inf) - 0x00800000. */34#define Mask (0x007fffff)3536static svfloat32_t NOINLINE37special_case (svuint32_t u_off, svfloat32_t p, svfloat32_t r2, svfloat32_t y,38svbool_t cmp)39{40return sv_call_f32 (41logf, svreinterpret_f32 (svadd_x (svptrue_b32 (), u_off, data.off)),42svmla_x (svptrue_b32 (), p, r2, y), cmp);43}4445/* Optimised implementation of SVE logf, using the same algorithm and46polynomial as the AdvSIMD routine. Maximum error is 3.34 ULPs:47SV_NAME_F1 (log)(0x1.557298p+0) got 0x1.26edecp-248want 0x1.26ede6p-2. */49svfloat32_t SV_NAME_F1 (log) (svfloat32_t x, const svbool_t pg)50{51const struct data *d = ptr_barrier (&data);5253svuint32_t u_off = svreinterpret_u32 (x);5455u_off = svsub_x (pg, u_off, d->off);56svbool_t cmp = svcmpge (pg, svsub_x (pg, u_off, d->lower), Thresh);5758/* x = 2^n * (1+r), where 2/3 < 1+r < 4/3. */59svfloat32_t n = svcvt_f32_x (60pg, svasr_x (pg, svreinterpret_s32 (u_off), 23)); /* Sign-extend. */6162svuint32_t u = svand_x (pg, u_off, Mask);63u = svadd_x (pg, u, d->off);64svfloat32_t r = svsub_x (pg, svreinterpret_f32 (u), 1.0f);6566/* y = log(1+r) + n*ln2. */67svfloat32_t r2 = svmul_x (svptrue_b32 (), r, r);68/* n*ln2 + r + r2*(P6 + r*P5 + r2*(P4 + r*P3 + r2*(P2 + r*P1 + r2*P0))). */69svfloat32_t p_0135 = svld1rq (svptrue_b32 (), &d->poly_0135[0]);70svfloat32_t p = svmla_lane (sv_f32 (d->poly_246[0]), r, p_0135, 1);71svfloat32_t q = svmla_lane (sv_f32 (d->poly_246[1]), r, p_0135, 2);72svfloat32_t y = svmla_lane (sv_f32 (d->poly_246[2]), r, p_0135, 3);73p = svmla_lane (p, r2, p_0135, 0);7475q = svmla_x (pg, q, r2, p);76y = svmla_x (pg, y, r2, q);77p = svmla_x (pg, r, n, d->ln2);7879if (unlikely (svptest_any (pg, cmp)))80return special_case (u_off, p, r2, y, cmp);81return svmla_x (pg, p, r2, y);82}8384TEST_SIG (SV, F, 1, log, 0.01, 11.1)85TEST_ULP (SV_NAME_F1 (log), 2.85)86TEST_DISABLE_FENV (SV_NAME_F1 (log))87TEST_INTERVAL (SV_NAME_F1 (log), -0.0, -inf, 100)88TEST_INTERVAL (SV_NAME_F1 (log), 0, 0x1p-126, 100)89TEST_INTERVAL (SV_NAME_F1 (log), 0x1p-126, 0x1p-23, 50000)90TEST_INTERVAL (SV_NAME_F1 (log), 0x1p-23, 1.0, 50000)91TEST_INTERVAL (SV_NAME_F1 (log), 1.0, 100, 50000)92TEST_INTERVAL (SV_NAME_F1 (log), 100, inf, 50000)93CLOSE_SVE_ATTR949596