about summary refs log tree commit diff
path: root/sysdeps/aarch64/fpu/expm1f_sve.c
blob: 7c852125cdbd0a2b59671e668f22616bb0069df7 (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
94
95
96
97
98
99
/* Single-precision SVE expm1

   Copyright (C) 2023-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"
#include "poly_sve_f32.h"

/* Largest value of x for which expm1(x) should round to -1.  */
#define SpecialBound 0x1.5ebc4p+6f

static const struct data
{
  /* These 4 are grouped together so they can be loaded as one quadword, then
     used with _lane forms of svmla/svmls.  */
  float c2, c4, ln2_hi, ln2_lo;
  float c0, c1, c3, inv_ln2, special_bound, shift;
} data = {
  /* Generated using fpminimax.  */
  .c0 = 0x1.fffffep-2,		 .c1 = 0x1.5554aep-3,
  .c2 = 0x1.555736p-5,		 .c3 = 0x1.12287cp-7,
  .c4 = 0x1.6b55a2p-10,

  .special_bound = SpecialBound, .shift = 0x1.8p23f,
  .inv_ln2 = 0x1.715476p+0f,	 .ln2_hi = 0x1.62e4p-1f,
  .ln2_lo = 0x1.7f7d1cp-20f,
};

#define C(i) sv_f32 (d->c##i)

static svfloat32_t NOINLINE
special_case (svfloat32_t x, svbool_t pg)
{
  return sv_call_f32 (expm1f, x, x, pg);
}

/* Single-precision SVE exp(x) - 1. Maximum error is 1.52 ULP:
   _ZGVsMxv_expm1f(0x1.8f4ebcp-2) got 0x1.e859dp-2
				 want 0x1.e859d4p-2.  */
svfloat32_t SV_NAME_F1 (expm1) (svfloat32_t x, svbool_t pg)
{
  const struct data *d = ptr_barrier (&data);

  /* Large, NaN/Inf.  */
  svbool_t special = svnot_z (pg, svaclt (pg, x, d->special_bound));

  if (__glibc_unlikely (svptest_any (pg, special)))
    return special_case (x, pg);

  /* This vector is reliant on layout of data - it contains constants
     that can be used with _lane forms of svmla/svmls. Values are:
     [ coeff_2, coeff_4, ln2_hi, ln2_lo ].  */
  svfloat32_t lane_constants = svld1rq (svptrue_b32 (), &d->c2);

  /* Reduce argument to smaller range:
     Let i = round(x / ln2)
     and f = x - i * ln2, then f is in [-ln2/2, ln2/2].
     exp(x) - 1 = 2^i * (expm1(f) + 1) - 1
     where 2^i is exact because i is an integer.  */
  svfloat32_t j = svmla_x (pg, sv_f32 (d->shift), x, d->inv_ln2);
  j = svsub_x (pg, j, d->shift);
  svint32_t i = svcvt_s32_x (pg, j);

  svfloat32_t f = svmls_lane (x, j, lane_constants, 2);
  f = svmls_lane (f, j, lane_constants, 3);

  /* Approximate expm1(f) using polynomial.
     Taylor expansion for expm1(x) has the form:
	 x + ax^2 + bx^3 + cx^4 ....
     So we calculate the polynomial P(f) = a + bf + cf^2 + ...
     and assemble the approximation expm1(f) ~= f + f^2 * P(f).  */
  svfloat32_t p12 = svmla_lane (C (1), f, lane_constants, 0);
  svfloat32_t p34 = svmla_lane (C (3), f, lane_constants, 1);
  svfloat32_t f2 = svmul_x (pg, f, f);
  svfloat32_t p = svmla_x (pg, p12, f2, p34);
  p = svmla_x (pg, C (0), f, p);
  p = svmla_x (pg, f, f2, p);

  /* Assemble the result.
     expm1(x) ~= 2^i * (p + 1) - 1
     Let t = 2^i.  */
  svfloat32_t t = svreinterpret_f32 (
      svadd_x (pg, svreinterpret_u32 (svlsl_x (pg, i, 23)), 0x3f800000));
  return svmla_x (pg, svsub_x (pg, t, 1), p, t);
}