aboutsummaryrefslogtreecommitdiff
path: root/pl/math/sv_atan2_2u5.c
blob: c0475956fdefdf7a7491b0cec0a48e3cbe715fda (plain)
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
/*
 * Double-precision vector atan2(x) function.
 *
 * Copyright (c) 2021-2022, Arm Limited.
 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
 */

#include "sv_math.h"
#include "pl_sig.h"

#if SV_SUPPORTED

#include "sv_atan_common.h"

/* Useful constants.  */
#define PiOver2 sv_f64 (0x1.921fb54442d18p+0)
#define SignMask sv_u64 (0x8000000000000000)

/* Special cases i.e. 0, infinity, nan (fall back to scalar calls).  */
__attribute__ ((noinline)) static sv_f64_t
specialcase (sv_f64_t y, sv_f64_t x, sv_f64_t ret, const svbool_t cmp)
{
  return sv_call2_f64 (atan2, y, x, ret, cmp);
}

/* Returns a predicate indicating true if the input is the bit representation of
   0, infinity or nan.  */
static inline svbool_t
zeroinfnan (sv_u64_t i, const svbool_t pg)
{
  return svcmpge_u64 (pg, svsub_n_u64_x (pg, svlsl_n_u64_x (pg, i, 1), 1),
		      sv_u64 (2 * asuint64 (INFINITY) - 1));
}

/* Fast implementation of SVE atan2. Errors are greatest when y and
   x are reasonably close together. The greatest observed error is 2.28 ULP:
   sv_atan2(-0x1.5915b1498e82fp+732, 0x1.54d11ef838826p+732)
   got -0x1.954f42f1fa841p-1 want -0x1.954f42f1fa843p-1.  */
sv_f64_t
__sv_atan2_x (sv_f64_t y, sv_f64_t x, const svbool_t pg)
{
  sv_u64_t ix = sv_as_u64_f64 (x);
  sv_u64_t iy = sv_as_u64_f64 (y);

  svbool_t cmp_x = zeroinfnan (ix, pg);
  svbool_t cmp_y = zeroinfnan (iy, pg);
  svbool_t cmp_xy = svorr_b_z (pg, cmp_x, cmp_y);

  sv_u64_t sign_x = svand_u64_x (pg, ix, SignMask);
  sv_u64_t sign_y = svand_u64_x (pg, iy, SignMask);
  sv_u64_t sign_xy = sveor_u64_x (pg, sign_x, sign_y);

  sv_f64_t ax = svabs_f64_x (pg, x);
  sv_f64_t ay = svabs_f64_x (pg, y);

  svbool_t pred_xlt0 = svcmplt_f64 (pg, x, sv_f64 (0.0));
  svbool_t pred_aygtax = svcmpgt_f64 (pg, ay, ax);

  /* Set up z for call to atan.  */
  sv_f64_t n = svsel_f64 (pred_aygtax, svneg_f64_x (pg, ax), ay);
  sv_f64_t d = svsel_f64 (pred_aygtax, ay, ax);
  sv_f64_t z = svdiv_f64_x (pg, n, d);

  /* Work out the correct shift.  */
  sv_f64_t shift = svsel_f64 (pred_xlt0, sv_f64 (-2.0), sv_f64 (0.0));
  shift = svsel_f64 (pred_aygtax, svadd_n_f64_x (pg, shift, 1.0), shift);
  shift = svmul_f64_x (pg, shift, PiOver2);

  sv_f64_t ret = __sv_atan_common (pg, pg, z, z, shift);

  /* Account for the sign of x and y.  */
  ret = sv_as_f64_u64 (sveor_u64_x (pg, sv_as_u64_f64 (ret), sign_xy));

  if (unlikely (svptest_any (pg, cmp_xy)))
    {
      return specialcase (y, x, ret, cmp_xy);
    }

  return ret;
}

strong_alias (__sv_atan2_x, _ZGVsMxvv_atan2)

  /* Arity of 2 means no mathbench entry emitted. See test/mathbench_funcs.h.
   */
  PL_SIG (SV, D, 2, atan2)
#endif