Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/erff.c
48375 views
/*1* Single-precision vector erf(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{13float min, max, scale, shift, third;14} data = {15.min = 0x1.cp-7f, /* 1/64 - 1/512. */16.max = 3.9375, /* 4 - 8/128. */17.scale = 0x1.20dd76p+0f, /* 2/sqrt(pi). */18.shift = 0x1p16f,19.third = 0x1.555556p-2f, /* 1/3. */20};2122#define SignMask (0x80000000)2324/* Single-precision implementation of vector erf(x).25Approximation based on series expansion near x rounded to26nearest multiple of 1/128.27Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,2829erf(x) ~ erf(r) + scale * d * [1 - r * d - 1/3 * d^2]3031Values of erf(r) and scale are read from lookup tables.32For |x| < 0x1.cp-7, the algorithm sets r = 0, erf(r) = 0, and scale = 2 /33sqrt(pi), so it simply boils down to a Taylor series expansion near 0. For34|x| > 3.9375, erf(|x|) rounds to 1.0f.3536Maximum error on each interval:37- [0, 0x1.cp-7]: 1.93 ULP38_ZGVsMxv_erff(0x1.c373e6p-9) got 0x1.fd686cp-9 want 0x1.fd6868p-939- [0x1.cp-7, 4.0]: 1.26 ULP40_ZGVsMxv_erff(0x1.1d002ep+0) got 0x1.c4eb9ap-1 want 0x1.c4eb98p-1. */41svfloat32_t SV_NAME_F1 (erf) (svfloat32_t x, const svbool_t pg)42{43const struct data *dat = ptr_barrier (&data);4445/* |x| > 1/64 - 1/512. */46svbool_t a_gt_min = svacgt (pg, x, dat->min);4748/* |x| >= 4.0 - 8/128. */49svbool_t a_ge_max = svacge (pg, x, dat->max);50svfloat32_t a = svabs_x (pg, x);5152svfloat32_t shift = sv_f32 (dat->shift);53svfloat32_t z = svadd_x (pg, a, shift);54svuint32_t i = svand_x (pg, svreinterpret_u32 (z), 0xfff);55i = svadd_x (pg, i, i);5657/* r and erf(r) set to 0 for |x| below min. */58svfloat32_t r = svsub_z (a_gt_min, z, shift);59svfloat32_t erfr60= svld1_gather_index (a_gt_min, &__v_erff_data.tab[0].erf, i);6162/* scale set to 2/sqrt(pi) for |x| below min. */63svfloat32_t scale64= svld1_gather_index (a_gt_min, &__v_erff_data.tab[0].scale, i);65scale = svsel (a_gt_min, scale, sv_f32 (dat->scale));6667/* erf(x) ~ erf(r) + scale * d * (1 - r * d + 1/3 * d^2). */68svfloat32_t d = svsub_x (pg, a, r);69svfloat32_t d2 = svmul_x (pg, d, d);70svfloat32_t y = svmla_x (pg, r, d, dat->third);71y = svmla_x (pg, erfr, scale, svmls_x (pg, d, d2, y));7273/* Solves the |x| = inf case. */74y = svsel (a_ge_max, sv_f32 (1.0f), y);7576/* Copy sign. */77svuint32_t ix = svreinterpret_u32 (x);78svuint32_t iy = svreinterpret_u32 (y);79svuint32_t sign = svand_x (pg, ix, SignMask);80return svreinterpret_f32 (svorr_x (pg, sign, iy));81}8283TEST_SIG (SV, F, 1, erf, -4.0, 4.0)84TEST_ULP (SV_NAME_F1 (erf), 1.43)85TEST_DISABLE_FENV (SV_NAME_F1 (erf))86TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 0, 0x1.cp-7, 40000)87TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 0x1.cp-7, 3.9375, 40000)88TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 3.9375, inf, 40000)89TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 0, inf, 4000)90CLOSE_SVE_ATTR919293