Path: blob/main/contrib/arm-optimized-routines/math/aarch64/sve/erf.c
48375 views
/*1* Double-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{13double third;14double tenth, two_over_five, two_over_fifteen;15double two_over_nine, two_over_fortyfive;16double max, shift;17} data = {18.third = 0x1.5555555555556p-2, /* used to compute 2/3 and 1/6 too. */19.two_over_fifteen = 0x1.1111111111111p-3,20.tenth = -0x1.999999999999ap-4,21.two_over_five = -0x1.999999999999ap-2,22.two_over_nine = -0x1.c71c71c71c71cp-3,23.two_over_fortyfive = 0x1.6c16c16c16c17p-5,24.max = 5.9921875, /* 6 - 1/128. */25.shift = 0x1p45,26};2728#define SignMask (0x8000000000000000)2930/* Double-precision implementation of vector erf(x).31Approximation based on series expansion near x rounded to32nearest multiple of 1/128.33Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,34erf(x) ~ erf(r) + scale * d * [35+ 136- r d37+ 1/3 (2 r^2 - 1) d^238- 1/6 (r (2 r^2 - 3)) d^339+ 1/30 (4 r^4 - 12 r^2 + 3) d^440- 1/90 (4 r^4 - 20 r^2 + 15) d^541]4243Maximum measure error: 2.29 ULP44_ZGVsMxv_erf(-0x1.00003c924e5d1p-8) got -0x1.20dd59132ebadp-845want -0x1.20dd59132ebafp-8. */46svfloat64_t SV_NAME_D1 (erf) (svfloat64_t x, const svbool_t pg)47{48const struct data *dat = ptr_barrier (&data);4950/* |x| >= 6.0 - 1/128. Opposite conditions except none of them catch NaNs so51they can be used in lookup and BSLs to yield the expected results. */52svbool_t a_ge_max = svacge (pg, x, dat->max);53svbool_t a_lt_max = svaclt (pg, x, dat->max);5455/* Set r to multiple of 1/128 nearest to |x|. */56svfloat64_t a = svabs_x (pg, x);57svfloat64_t shift = sv_f64 (dat->shift);58svfloat64_t z = svadd_x (pg, a, shift);59svuint64_t i = svand_x (pg, svreinterpret_u64 (z), 0xfff);60i = svadd_x (pg, i, i);6162/* Lookup without shortcut for small values but with predicate to avoid63segfault for large values and NaNs. */64svfloat64_t r = svsub_x (pg, z, shift);65svfloat64_t erfr66= svld1_gather_index (a_lt_max, &__v_erf_data.tab[0].erf, i);67svfloat64_t scale68= svld1_gather_index (a_lt_max, &__v_erf_data.tab[0].scale, i);6970/* erf(x) ~ erf(r) + scale * d * poly (r, d). */71svfloat64_t d = svsub_x (pg, a, r);72svfloat64_t d2 = svmul_x (pg, d, d);73svfloat64_t r2 = svmul_x (pg, r, r);7475/* poly (d, r) = 1 + p1(r) * d + p2(r) * d^2 + ... + p5(r) * d^5. */76svfloat64_t p1 = r;77svfloat64_t third = sv_f64 (dat->third);78svfloat64_t twothird = svmul_x (pg, third, 2.0);79svfloat64_t sixth = svmul_x (pg, third, 0.5);80svfloat64_t p2 = svmls_x (pg, third, r2, twothird);81svfloat64_t p3 = svmad_x (pg, r2, third, -0.5);82p3 = svmul_x (pg, r, p3);83svfloat64_t p484= svmla_x (pg, sv_f64 (dat->two_over_five), r2, dat->two_over_fifteen);85p4 = svmls_x (pg, sv_f64 (dat->tenth), r2, p4);86svfloat64_t p587= svmla_x (pg, sv_f64 (dat->two_over_nine), r2, dat->two_over_fortyfive);88p5 = svmla_x (pg, sixth, r2, p5);89p5 = svmul_x (pg, r, p5);9091svfloat64_t p34 = svmla_x (pg, p3, d, p4);92svfloat64_t p12 = svmla_x (pg, p1, d, p2);93svfloat64_t y = svmla_x (pg, p34, d2, p5);94y = svmla_x (pg, p12, d2, y);9596y = svmla_x (pg, erfr, scale, svmls_x (pg, d, d2, y));9798/* Solves the |x| = inf and NaN cases. */99y = svsel (a_ge_max, sv_f64 (1.0), y);100101/* Copy sign. */102svuint64_t ix = svreinterpret_u64 (x);103svuint64_t iy = svreinterpret_u64 (y);104svuint64_t sign = svand_x (pg, ix, SignMask);105return svreinterpret_f64 (svorr_x (pg, sign, iy));106}107108TEST_SIG (SV, D, 1, erf, -6.0, 6.0)109TEST_ULP (SV_NAME_D1 (erf), 1.79)110TEST_DISABLE_FENV (SV_NAME_D1 (erf))111TEST_SYM_INTERVAL (SV_NAME_D1 (erf), 0, 5.9921875, 40000)112TEST_SYM_INTERVAL (SV_NAME_D1 (erf), 5.9921875, inf, 40000)113TEST_SYM_INTERVAL (SV_NAME_D1 (erf), 0, inf, 4000)114CLOSE_SVE_ATTR115116117