Path: blob/main/contrib/arm-optimized-routines/math/aarch64/experimental/erff_2u.c
48375 views
/*1* Single-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.06eba8p-3f12#define Shift 0x1p16f13#define OneThird 0x1.555556p-2f1415/* Fast erff approximation based on series expansion near x rounded to16nearest multiple of 1/128.17Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,1819erf(x) ~ erf(r)20+ scale * d * [21+ 122- r d23+ 1/3 (2 r^2 - 1) d^224- 1/6 (r (2 r^2 - 3) ) d^325+ 1/30 (4 r^4 - 12 r^2 + 3) d^426]2728This single precision implementation uses only the following terms:2930erf(x) ~ erf(r) + scale * d * [1 - r * d - 1/3 * d^2]3132Values of erf(r) and scale are read from lookup tables.33For |x| > 3.9375, erf(|x|) rounds to 1.0f.3435Maximum error: 1.93 ULP36erff(0x1.c373e6p-9) got 0x1.fd686cp-937want 0x1.fd6868p-9. */38float39arm_math_erff (float x)40{41/* Get absolute value and sign. */42uint32_t ix = asuint (x);43uint32_t ia = ix & 0x7fffffff;44uint32_t sign = ix & ~0x7fffffff;4546/* |x| < 0x1p-62. Triggers exceptions. */47if (unlikely (ia < 0x20800000))48return fmaf (TwoOverSqrtPiMinusOne, x, x);4950if (ia < 0x407b8000) /* |x| < 4 - 8 / 128 = 3.9375. */51{52/* Lookup erf(r) and scale(r) in tables, e.g. set erf(r) to 0 and scale53to 2/sqrt(pi), when x reduced to r = 0. */54float a = asfloat (ia);55float z = a + Shift;56uint32_t i = asuint (z) - asuint (Shift);57float r = z - Shift;58float erfr = __v_erff_data.tab[i].erf;59float scale = __v_erff_data.tab[i].scale;6061/* erf(x) ~ erf(r) + scale * d * (1 - r * d - 1/3 * d^2). */62float d = a - r;63float d2 = d * d;64float y = -fmaf (OneThird, d, r);65y = fmaf (fmaf (y, d2, d), scale, erfr);66return asfloat (asuint (y) | sign);67}6869/* Special cases : erff(nan)=nan, erff(+inf)=+1 and erff(-inf)=-1. */70if (unlikely (ia >= 0x7f800000))71return (1.0f - (float) (sign >> 30)) + 1.0f / x;7273/* Boring domain (|x| >= 4.0). */74return asfloat (sign | asuint (1.0f));75}7677TEST_ULP (arm_math_erff, 1.43)78TEST_SYM_INTERVAL (arm_math_erff, 0, 3.9375, 40000)79TEST_SYM_INTERVAL (arm_math_erff, 3.9375, inf, 40000)80TEST_SYM_INTERVAL (arm_math_erff, 0, inf, 40000)818283