aboutsummaryrefslogtreecommitdiff
path: root/contrib/arm-optimized-routines/math/aarch64/sve/atanf.c
blob: a2cd37b127444907750d221f931fff130088fc36 (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
/*
 * Single-precision vector atan(x) function.
 *
 * Copyright (c) 2021-2024, Arm Limited.
 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
 */

#include "sv_math.h"
#include "test_sig.h"
#include "test_defs.h"
#include "sv_poly_f32.h"

static const struct data
{
  float32_t poly[8];
  float32_t pi_over_2;
} data = {
  /* Coefficients of polynomial P such that atan(x)~x+x*P(x^2) on
    [2**-128, 1.0].  */
  .poly = { -0x1.55555p-2f, 0x1.99935ep-3f, -0x1.24051ep-3f, 0x1.bd7368p-4f,
	    -0x1.491f0ep-4f, 0x1.93a2c0p-5f, -0x1.4c3c60p-6f, 0x1.01fd88p-8f },
  .pi_over_2 = 0x1.921fb6p+0f,
};

#define SignMask (0x80000000)

/* Fast implementation of SVE atanf based on
   atan(x) ~ shift + z + z^3 * P(z^2) with reduction to [0,1] using
   z=-1/x and shift = pi/2.
   Largest observed error is 2.9 ULP, close to +/-1.0:
   _ZGVsMxv_atanf (0x1.0468f6p+0) got -0x1.967f06p-1
				 want -0x1.967fp-1.  */
svfloat32_t SV_NAME_F1 (atan) (svfloat32_t x, const svbool_t pg)
{
  const struct data *d = ptr_barrier (&data);

  /* No need to trigger special case. Small cases, infs and nans
     are supported by our approximation technique.  */
  svuint32_t ix = svreinterpret_u32 (x);
  svuint32_t sign = svand_x (pg, ix, SignMask);

  /* Argument reduction:
     y := arctan(x) for x < 1
     y := pi/2 + arctan(-1/x) for x > 1
     Hence, use z=-1/a if x>=1, otherwise z=a.  */
  svbool_t red = svacgt (pg, x, 1.0f);
  /* Avoid dependency in abs(x) in division (and comparison).  */
  svfloat32_t z = svsel (red, svdiv_x (pg, sv_f32 (1.0f), x), x);
  /* Use absolute value only when needed (odd powers of z).  */
  svfloat32_t az = svabs_x (pg, z);
  az = svneg_m (az, red, az);

  /* Use split Estrin scheme for P(z^2) with deg(P)=7.  */
  svfloat32_t z2 = svmul_x (pg, z, z);
  svfloat32_t z4 = svmul_x (pg, z2, z2);
  svfloat32_t z8 = svmul_x (pg, z4, z4);

  svfloat32_t y = sv_estrin_7_f32_x (pg, z2, z4, z8, d->poly);

  /* y = shift + z + z^3 * P(z^2).  */
  svfloat32_t z3 = svmul_x (pg, z2, az);
  y = svmla_x (pg, az, z3, y);

  /* Apply shift as indicated by 'red' predicate.  */
  y = svadd_m (red, y, sv_f32 (d->pi_over_2));

  /* y = atan(x) if x>0, -atan(-x) otherwise.  */
  return svreinterpret_f32 (sveor_x (pg, svreinterpret_u32 (y), sign));
}

TEST_SIG (SV, F, 1, atan, -3.1, 3.1)
TEST_ULP (SV_NAME_F1 (atan), 2.9)
TEST_DISABLE_FENV (SV_NAME_F1 (atan))
TEST_INTERVAL (SV_NAME_F1 (atan), 0.0, 1.0, 40000)
TEST_INTERVAL (SV_NAME_F1 (atan), 1.0, 100.0, 40000)
TEST_INTERVAL (SV_NAME_F1 (atan), 100, inf, 40000)
TEST_INTERVAL (SV_NAME_F1 (atan), -0, -inf, 40000)
CLOSE_SVE_ATTR