aboutsummaryrefslogtreecommitdiff
path: root/pl/math/v_tanf_3u5.c
blob: e1f0a30ed5e3556bf5cadcf69193a9fca966c330 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Single-precision vector tan(x) function.
 *
 * Copyright (c) 2021-2023, Arm Limited.
 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
 */

#include "v_math.h"
#include "poly_advsimd_f32.h"
#include "pl_sig.h"
#include "pl_test.h"

static const struct data
{
  float32x4_t poly[6];
  float32x4_t neg_half_pi_1, neg_half_pi_2, neg_half_pi_3, two_over_pi, shift;
#if !WANT_SIMD_EXCEPT
  float32x4_t range_val;
#endif
} data = {
  /* Coefficients generated using FPMinimax.  */
  .poly = { V4 (0x1.55555p-2f), V4 (0x1.11166p-3f), V4 (0x1.b88a78p-5f),
	    V4 (0x1.7b5756p-6f), V4 (0x1.4ef4cep-8f), V4 (0x1.0e1e74p-7f) },
  .neg_half_pi_1 = V4 (-0x1.921fb6p+0f),
  .neg_half_pi_2 = V4 (0x1.777a5cp-25f),
  .neg_half_pi_3 = V4 (0x1.ee59dap-50f),
  .two_over_pi = V4 (0x1.45f306p-1f),
  .shift = V4 (0x1.8p+23f),
#if !WANT_SIMD_EXCEPT
  .range_val = V4 (0x1p15f),
#endif
};

#define RangeVal v_u32 (0x47000000)  /* asuint32(0x1p15f).  */
#define TinyBound v_u32 (0x30000000) /* asuint32 (0x1p-31f).  */
#define Thresh v_u32 (0x16000000)    /* asuint32(RangeVal) - TinyBound.  */

/* Special cases (fall back to scalar calls).  */
static float32x4_t VPCS_ATTR NOINLINE
special_case (float32x4_t x, float32x4_t y, uint32x4_t cmp)
{
  return v_call_f32 (tanf, x, y, cmp);
}

/* Use a full Estrin scheme to evaluate polynomial.  */
static inline float32x4_t
eval_poly (float32x4_t z, const struct data *d)
{
  float32x4_t z2 = vmulq_f32 (z, z);
#if WANT_SIMD_EXCEPT
  /* Tiny z (<= 0x1p-31) will underflow when calculating z^4. If fp exceptions
     are to be triggered correctly, sidestep this by fixing such lanes to 0.  */
  uint32x4_t will_uflow
    = vcleq_u32 (vreinterpretq_u32_f32 (vabsq_f32 (z)), TinyBound);
  if (unlikely (v_any_u32 (will_uflow)))
    z2 = vbslq_f32 (will_uflow, v_f32 (0), z2);
#endif
  float32x4_t z4 = vmulq_f32 (z2, z2);
  return v_estrin_5_f32 (z, z2, z4, d->poly);
}

/* Fast implementation of AdvSIMD tanf.
   Maximum error is 3.45 ULP:
   __v_tanf(-0x1.e5f0cap+13) got 0x1.ff9856p-1
			    want 0x1.ff9850p-1.  */
float32x4_t VPCS_ATTR V_NAME_F1 (tan) (float32x4_t x)
{
  const struct data *d = ptr_barrier (&data);
  float32x4_t special_arg = x;

  /* iax >= RangeVal means x, if not inf or NaN, is too large to perform fast
     regression.  */
#if WANT_SIMD_EXCEPT
  uint32x4_t iax = vreinterpretq_u32_f32 (vabsq_f32 (x));
  /* If fp exceptions are to be triggered correctly, also special-case tiny
     input, as this will load to overflow later. Fix any special lanes to 1 to
     prevent any exceptions being triggered.  */
  uint32x4_t special = vcgeq_u32 (vsubq_u32 (iax, TinyBound), Thresh);
  if (unlikely (v_any_u32 (special)))
    x = vbslq_f32 (special, v_f32 (1.0f), x);
#else
  /* Otherwise, special-case large and special values.  */
  uint32x4_t special = vcageq_f32 (x, d->range_val);
#endif

  /* n = rint(x/(pi/2)).  */
  float32x4_t q = vfmaq_f32 (d->shift, d->two_over_pi, x);
  float32x4_t n = vsubq_f32 (q, d->shift);
  /* Determine if x lives in an interval, where |tan(x)| grows to infinity.  */
  uint32x4_t pred_alt = vtstq_u32 (vreinterpretq_u32_f32 (q), v_u32 (1));

  /* r = x - n * (pi/2)  (range reduction into -pi./4 .. pi/4).  */
  float32x4_t r;
  r = vfmaq_f32 (x, d->neg_half_pi_1, n);
  r = vfmaq_f32 (r, d->neg_half_pi_2, n);
  r = vfmaq_f32 (r, d->neg_half_pi_3, n);

  /* If x lives in an interval, where |tan(x)|
     - is finite, then use a polynomial approximation of the form
       tan(r) ~ r + r^3 * P(r^2) = r + r * r^2 * P(r^2).
     - grows to infinity then use symmetries of tangent and the identity
       tan(r) = cotan(pi/2 - r) to express tan(x) as 1/tan(-r). Finally, use
       the same polynomial approximation of tan as above.  */

  /* Invert sign of r if odd quadrant.  */
  float32x4_t z = vmulq_f32 (r, vbslq_f32 (pred_alt, v_f32 (-1), v_f32 (1)));

  /* Evaluate polynomial approximation of tangent on [-pi/4, pi/4].  */
  float32x4_t z2 = vmulq_f32 (r, r);
  float32x4_t p = eval_poly (z2, d);
  float32x4_t y = vfmaq_f32 (z, vmulq_f32 (z, z2), p);

  /* Compute reciprocal and apply if required.  */
  float32x4_t inv_y = vdivq_f32 (v_f32 (1.0f), y);

  if (unlikely (v_any_u32 (special)))
    return special_case (special_arg, vbslq_f32 (pred_alt, inv_y, y), special);
  return vbslq_f32 (pred_alt, inv_y, y);
}

PL_SIG (V, F, 1, tan, -3.1, 3.1)
PL_TEST_ULP (V_NAME_F1 (tan), 2.96)
PL_TEST_EXPECT_FENV (V_NAME_F1 (tan), WANT_SIMD_EXCEPT)
PL_TEST_INTERVAL (V_NAME_F1 (tan), 0, 0x1p-31, 5000)
PL_TEST_INTERVAL (V_NAME_F1 (tan), 0x1p-31, 0x1p15, 500000)
PL_TEST_INTERVAL (V_NAME_F1 (tan), 0x1p15, inf, 5000)
PL_TEST_INTERVAL (V_NAME_F1 (tan), -0, -0x1p-31, 5000)
PL_TEST_INTERVAL (V_NAME_F1 (tan), -0x1p-31, -0x1p15, 500000)
PL_TEST_INTERVAL (V_NAME_F1 (tan), -0x1p15, -inf, 5000)