Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/erf_2u5.c
48375 views
/*1* Double-precision 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 "math_config.h"8#include "test_sig.h"9#include "test_defs.h"1011#define TwoOverSqrtPiMinusOne 0x1.06eba8214db69p-312#define Shift 0x1p451314/* Polynomial coefficients. */15#define OneThird 0x1.5555555555555p-216#define TwoThird 0x1.5555555555555p-11718#define TwoOverFifteen 0x1.1111111111111p-319#define TwoOverFive 0x1.999999999999ap-220#define Tenth 0x1.999999999999ap-42122#define TwoOverNine 0x1.c71c71c71c71cp-323#define TwoOverFortyFive 0x1.6c16c16c16c17p-524#define Sixth 0x1.555555555555p-32526/* Fast erf approximation based on series expansion near x rounded to27nearest multiple of 1/128.28Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,2930erf(x) ~ erf(r)31+ scale * d * [32+ 133- r d34+ 1/3 (2 r^2 - 1) d^235- 1/6 (r (2 r^2 - 3)) d^336+ 1/30 (4 r^4 - 12 r^2 + 3) d^437- 1/90 (4 r^4 - 20 r^2 + 15) d^538]3940Maximum measure error: 2.29 ULP41erf(-0x1.00003c924e5d1p-8) got -0x1.20dd59132ebadp-842want -0x1.20dd59132ebafp-8. */43double44arm_math_erf (double x)45{46/* Get absolute value and sign. */47uint64_t ix = asuint64 (x);48uint64_t ia = ix & 0x7fffffffffffffff;49uint64_t sign = ix & ~0x7fffffffffffffff;5051/* |x| < 0x1p-508. Triggers exceptions. */52if (unlikely (ia < 0x2030000000000000))53return fma (TwoOverSqrtPiMinusOne, x, x);5455if (ia < 0x4017f80000000000) /* |x| < 6 - 1 / 128 = 5.9921875. */56{57/* Set r to multiple of 1/128 nearest to |x|. */58double a = asdouble (ia);59double z = a + Shift;60uint64_t i = asuint64 (z) - asuint64 (Shift);61double r = z - Shift;62/* Lookup erf(r) and scale(r) in table.63Set erf(r) to 0 and scale to 2/sqrt(pi) for |x| <= 0x1.cp-9. */64double erfr = __v_erf_data.tab[i].erf;65double scale = __v_erf_data.tab[i].scale;6667/* erf(x) ~ erf(r) + scale * d * poly (d, r). */68double d = a - r;69double r2 = r * r;70double d2 = d * d;7172/* poly (d, r) = 1 + p1(r) * d + p2(r) * d^2 + ... + p5(r) * d^5. */73double p1 = -r;74double p2 = fma (TwoThird, r2, -OneThird);75double p3 = -r * fma (OneThird, r2, -0.5);76double p4 = fma (fma (TwoOverFifteen, r2, -TwoOverFive), r2, Tenth);77double p578= -r * fma (fma (TwoOverFortyFive, r2, -TwoOverNine), r2, Sixth);7980double p34 = fma (p4, d, p3);81double p12 = fma (p2, d, p1);82double y = fma (p5, d2, p34);83y = fma (y, d2, p12);8485y = fma (fma (y, d2, d), scale, erfr);86return asdouble (asuint64 (y) | sign);87}8889/* Special cases : erf(nan)=nan, erf(+inf)=+1 and erf(-inf)=-1. */90if (unlikely (ia >= 0x7ff0000000000000))91return (1.0 - (double) (sign >> 62)) + 1.0 / x;9293/* Boring domain (|x| >= 6.0). */94return asdouble (sign | asuint64 (1.0));95}9697TEST_ULP (arm_math_erf, 1.79)98TEST_SYM_INTERVAL (arm_math_erf, 0, 5.9921875, 40000)99TEST_SYM_INTERVAL (arm_math_erf, 5.9921875, inf, 40000)100TEST_SYM_INTERVAL (arm_math_erf, 0, inf, 40000)101102103