about summary refs log tree commit diff
path: root/sysdeps/aarch64/fpu/erff_sve.c
blob: 38f00db9be7424d39d879c06c6c5f3c99a671bc2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* Single-precision vector (SVE) erf function

   Copyright (C) 2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include "sv_math.h"

static const struct data
{
  float min, max, scale, shift, third;
} data = {
  .min = 0x1.cp-7f,	   /* 1/64 - 1/512.  */
  .max = 3.9375,	   /* 4 - 8/128.  */
  .scale = 0x1.20dd76p+0f, /* 2/sqrt(pi).  */
  .shift = 0x1p16f,
  .third = 0x1.555556p-2f, /* 1/3.  */
};

#define SignMask (0x80000000)

/* Single-precision implementation of vector erf(x).
   Approximation based on series expansion near x rounded to
   nearest multiple of 1/128.
   Let d = x - r, and scale = 2 / sqrt(pi) * exp(-r^2). For x near r,

   erf(x) ~ erf(r) + scale * d * [1 - r * d - 1/3 * d^2]

   Values of erf(r) and scale are read from lookup tables.
   For |x| < 0x1.cp-7, the algorithm sets r = 0, erf(r) = 0, and scale = 2 /
   sqrt(pi), so it simply boils down to a Taylor series expansion near 0. For
   |x| > 3.9375, erf(|x|) rounds to 1.0f.

   Maximum error on each interval:
   - [0, 0x1.cp-7]: 1.93 ULP
     _ZGVsMxv_erff(0x1.c373e6p-9) got 0x1.fd686cp-9 want 0x1.fd6868p-9
   - [0x1.cp-7, 4.0]: 1.26 ULP
     _ZGVsMxv_erff(0x1.1d002ep+0) got 0x1.c4eb9ap-1 want 0x1.c4eb98p-1.  */
svfloat32_t SV_NAME_F1 (erf) (svfloat32_t x, const svbool_t pg)
{
  const struct data *dat = ptr_barrier (&data);

  /* |x| > 1/64 - 1/512.  */
  svbool_t a_gt_min = svacgt (pg, x, dat->min);

  /* |x| >= 4.0 - 8/128.  */
  svbool_t a_ge_max = svacge (pg, x, dat->max);
  svfloat32_t a = svabs_x (pg, x);

  svfloat32_t shift = sv_f32 (dat->shift);
  svfloat32_t z = svadd_x (pg, a, shift);
  svuint32_t i
      = svsub_x (pg, svreinterpret_u32 (z), svreinterpret_u32 (shift));

  /* Saturate lookup index.  */
  i = svsel (a_ge_max, sv_u32 (512), i);

  /* r and erf(r) set to 0 for |x| below min.  */
  svfloat32_t r = svsub_z (a_gt_min, z, shift);
  svfloat32_t erfr = svld1_gather_index (a_gt_min, __sv_erff_data.erf, i);

  /* scale set to 2/sqrt(pi) for |x| below min.  */
  svfloat32_t scale = svld1_gather_index (a_gt_min, __sv_erff_data.scale, i);
  scale = svsel (a_gt_min, scale, sv_f32 (dat->scale));

  /* erf(x) ~ erf(r) + scale * d * (1 - r * d + 1/3 * d^2).  */
  svfloat32_t d = svsub_x (pg, a, r);
  svfloat32_t d2 = svmul_x (pg, d, d);
  svfloat32_t y = svmla_x (pg, r, d, dat->third);
  y = svmla_x (pg, erfr, scale, svmls_x (pg, d, d2, y));

  /* Solves the |x| = inf case.  */
  y = svsel (a_ge_max, sv_f32 (1.0f), y);

  /* Copy sign.  */
  svuint32_t ix = svreinterpret_u32 (x);
  svuint32_t iy = svreinterpret_u32 (y);
  svuint32_t sign = svand_x (pg, ix, SignMask);
  return svreinterpret_f32 (svorr_x (pg, sign, iy));
}