Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/erfcf.c
48377 views
/*1* Single-precision vector erfc(x) function.2*3* Copyright (c) 2023-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{13uint32_t off_idx, off_arr;14float max, shift;15float third, two_thirds, two_over_fifteen, two_over_five, tenth;16} data = {17/* Set an offset so the range of the index used for lookup is 644, and it can18be clamped using a saturated add. */19.off_idx = 0xb7fffd7b, /* 0xffffffff - asuint(shift) - 644. */20.off_arr = 0xfffffd7b, /* 0xffffffff - 644. */21.max = 10.0625f, /* 644/64. */22.shift = 0x1p17f,23.third = 0x1.555556p-2f,24.two_thirds = 0x1.555556p-1f,25.two_over_fifteen = 0x1.111112p-3f,26.two_over_five = -0x1.99999ap-2f,27.tenth = -0x1.99999ap-4f,28};2930#define SignMask 0x8000000031#define TableScale 0x28000000 /* 0x1p-47. */3233/* Optimized single-precision vector erfcf(x).34Approximation based on series expansion near x rounded to35nearest multiple of 1/64.36Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,3738erfc(x) ~ erfc(r) - scale * d * poly(r, d), with3940poly(r, d) = 1 - r d + (2/3 r^2 - 1/3) d^2 - r (1/3 r^2 - 1/2) d^341+ (2/15 r^4 - 2/5 r^2 + 1/10) d^44243Values of erfc(r) and scale are read from lookup tables. Stored values44are scaled to avoid hitting the subnormal range.4546Note that for x < 0, erfc(x) = 2.0 - erfc(-x).4748Maximum error: 1.63 ULP (~1.0 ULP for x < 0.0).49_ZGVsMxv_erfcf(0x1.1dbf7ap+3) got 0x1.f51212p-12050want 0x1.f51216p-120. */51svfloat32_t SV_NAME_F1 (erfc) (svfloat32_t x, const svbool_t pg)52{53const struct data *dat = ptr_barrier (&data);5455svfloat32_t a = svabs_x (pg, x);5657/* Clamp input at |x| <= 10.0 + 4/64. */58a = svmin_x (pg, a, dat->max);5960/* Reduce x to the nearest multiple of 1/64. */61svfloat32_t shift = sv_f32 (dat->shift);62svfloat32_t z = svadd_x (pg, a, shift);6364/* Saturate index for the NaN case. */65svuint32_t i = svqadd (svreinterpret_u32 (z), dat->off_idx);6667/* Lookup erfc(r) and 2/sqrt(pi)*exp(-r^2) in tables. */68i = svlsl_x (svptrue_b32 (), i, 1);69const float32_t *p = &__v_erfcf_data.tab[0].erfc - 2 * dat->off_arr;70svfloat32_t erfcr = svld1_gather_index (pg, p, i);71svfloat32_t scale = svld1_gather_index (pg, p + 1, i);7273/* erfc(x) ~ erfc(r) - scale * d * poly(r, d). */74svfloat32_t r = svsub_x (pg, z, shift);75svfloat32_t d = svsub_x (pg, a, r);76svfloat32_t d2 = svmul_x (svptrue_b32 (), d, d);77svfloat32_t r2 = svmul_x (svptrue_b32 (), r, r);7879svfloat32_t coeffs = svld1rq (svptrue_b32 (), &dat->third);8081svfloat32_t p1 = r;82svfloat32_t p2 = svmls_lane (sv_f32 (dat->third), r2, coeffs, 1);83svfloat32_t p384= svmul_x (svptrue_b32 (), r, svmla_lane (sv_f32 (-0.5), r2, coeffs, 0));85svfloat32_t p4 = svmla_lane (sv_f32 (dat->two_over_five), r2, coeffs, 2);86p4 = svmls_x (pg, sv_f32 (dat->tenth), r2, p4);8788svfloat32_t y = svmla_x (pg, p3, d, p4);89y = svmla_x (pg, p2, d, y);90y = svmla_x (pg, p1, d, y);9192/* Solves the |x| = inf/nan case. */93y = svmls_x (pg, erfcr, scale, svmls_x (pg, d, d2, y));9495/* Offset equals 2.0f if sign, else 0.0f. */96svuint32_t sign = svand_x (pg, svreinterpret_u32 (x), SignMask);97svfloat32_t off = svreinterpret_f32 (svlsr_x (pg, sign, 1));98/* Handle sign and scale back in a single fma. */99svfloat32_t fac = svreinterpret_f32 (svorr_x (pg, sign, TableScale));100101return svmla_x (pg, off, fac, y);102}103104TEST_SIG (SV, F, 1, erfc, -4.0, 10.0)105TEST_ULP (SV_NAME_F1 (erfc), 1.14)106TEST_DISABLE_FENV (SV_NAME_F1 (erfc))107TEST_SYM_INTERVAL (SV_NAME_F1 (erfc), 0.0, 0x1p-26, 40000)108TEST_INTERVAL (SV_NAME_F1 (erfc), 0x1p-26, 10.0625, 40000)109TEST_INTERVAL (SV_NAME_F1 (erfc), -0x1p-26, -4.0, 40000)110TEST_INTERVAL (SV_NAME_F1 (erfc), 10.0625, inf, 40000)111TEST_INTERVAL (SV_NAME_F1 (erfc), -4.0, -inf, 40000)112CLOSE_SVE_ATTR113114115