1 /* 2 * Single-precision vector erf(x) function. 3 * 4 * Copyright (c) 2023-2024, Arm Limited. 5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception 6 */ 7 8 #include "sv_math.h" 9 #include "test_sig.h" 10 #include "test_defs.h" 11 12 static const struct data 13 { 14 float min, max, scale, shift, third; 15 } data = { 16 .min = 0x1.cp-7f, /* 1/64 - 1/512. */ 17 .max = 3.9375, /* 4 - 8/128. */ 18 .scale = 0x1.20dd76p+0f, /* 2/sqrt(pi). */ 19 .shift = 0x1p16f, 20 .third = 0x1.555556p-2f, /* 1/3. */ 21 }; 22 23 #define SignMask (0x80000000) 24 25 /* Single-precision implementation of vector erf(x). 26 Approximation based on series expansion near x rounded to 27 nearest multiple of 1/128. 28 Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r, 29 30 erf(x) ~ erf(r) + scale * d * [1 - r * d - 1/3 * d^2] 31 32 Values of erf(r) and scale are read from lookup tables. 33 For |x| < 0x1.cp-7, the algorithm sets r = 0, erf(r) = 0, and scale = 2 / 34 sqrt(pi), so it simply boils down to a Taylor series expansion near 0. For 35 |x| > 3.9375, erf(|x|) rounds to 1.0f. 36 37 Maximum error on each interval: 38 - [0, 0x1.cp-7]: 1.93 ULP 39 _ZGVsMxv_erff(0x1.c373e6p-9) got 0x1.fd686cp-9 want 0x1.fd6868p-9 40 - [0x1.cp-7, 4.0]: 1.26 ULP 41 _ZGVsMxv_erff(0x1.1d002ep+0) got 0x1.c4eb9ap-1 want 0x1.c4eb98p-1. */ 42 svfloat32_t SV_NAME_F1 (erf) (svfloat32_t x, const svbool_t pg) 43 { 44 const struct data *dat = ptr_barrier (&data); 45 46 /* |x| > 1/64 - 1/512. */ 47 svbool_t a_gt_min = svacgt (pg, x, dat->min); 48 49 /* |x| >= 4.0 - 8/128. */ 50 svbool_t a_ge_max = svacge (pg, x, dat->max); 51 svfloat32_t a = svabs_x (pg, x); 52 53 svfloat32_t shift = sv_f32 (dat->shift); 54 svfloat32_t z = svadd_x (pg, a, shift); 55 svuint32_t i = svand_x (pg, svreinterpret_u32 (z), 0xfff); 56 i = svadd_x (pg, i, i); 57 58 /* r and erf(r) set to 0 for |x| below min. */ 59 svfloat32_t r = svsub_z (a_gt_min, z, shift); 60 svfloat32_t erfr 61 = svld1_gather_index (a_gt_min, &__v_erff_data.tab[0].erf, i); 62 63 /* scale set to 2/sqrt(pi) for |x| below min. */ 64 svfloat32_t scale 65 = svld1_gather_index (a_gt_min, &__v_erff_data.tab[0].scale, i); 66 scale = svsel (a_gt_min, scale, sv_f32 (dat->scale)); 67 68 /* erf(x) ~ erf(r) + scale * d * (1 - r * d + 1/3 * d^2). */ 69 svfloat32_t d = svsub_x (pg, a, r); 70 svfloat32_t d2 = svmul_x (pg, d, d); 71 svfloat32_t y = svmla_x (pg, r, d, dat->third); 72 y = svmla_x (pg, erfr, scale, svmls_x (pg, d, d2, y)); 73 74 /* Solves the |x| = inf case. */ 75 y = svsel (a_ge_max, sv_f32 (1.0f), y); 76 77 /* Copy sign. */ 78 svuint32_t ix = svreinterpret_u32 (x); 79 svuint32_t iy = svreinterpret_u32 (y); 80 svuint32_t sign = svand_x (pg, ix, SignMask); 81 return svreinterpret_f32 (svorr_x (pg, sign, iy)); 82 } 83 84 TEST_SIG (SV, F, 1, erf, -4.0, 4.0) 85 TEST_ULP (SV_NAME_F1 (erf), 1.43) 86 TEST_DISABLE_FENV (SV_NAME_F1 (erf)) 87 TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 0, 0x1.cp-7, 40000) 88 TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 0x1.cp-7, 3.9375, 40000) 89 TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 3.9375, inf, 40000) 90 TEST_SYM_INTERVAL (SV_NAME_F1 (erf), 0, inf, 4000) 91 CLOSE_SVE_ATTR 92