1/*
2 * Single-precision scalar tan(x) function.
3 *
4 * Copyright (c) 2021-2023, Arm Limited.
5 * SPDX-License-Identifier: MIT OR Apache-2.0 WITH LLVM-exception
6 */
7#include "math_config.h"
8#include "pl_sig.h"
9#include "pl_test.h"
10#include "poly_scalar_f32.h"
11
12/* Useful constants.  */
13#define NegPio2_1 (-0x1.921fb6p+0f)
14#define NegPio2_2 (0x1.777a5cp-25f)
15#define NegPio2_3 (0x1.ee59dap-50f)
16/* Reduced from 0x1p20 to 0x1p17 to ensure 3.5ulps.  */
17#define RangeVal (0x1p17f)
18#define InvPio2 ((0x1.45f306p-1f))
19#define Shift (0x1.8p+23f)
20#define AbsMask (0x7fffffff)
21#define Pio4 (0x1.921fb6p-1)
22/* 2PI * 2^-64.  */
23#define Pio2p63 (0x1.921FB54442D18p-62)
24
25static inline float
26eval_P (float z)
27{
28  return pw_horner_5_f32 (z, z * z, __tanf_poly_data.poly_tan);
29}
30
31static inline float
32eval_Q (float z)
33{
34  return pairwise_poly_3_f32 (z, z * z, __tanf_poly_data.poly_cotan);
35}
36
37/* Reduction of the input argument x using Cody-Waite approach, such that x = r
38   + n * pi/2 with r lives in [-pi/4, pi/4] and n is a signed integer.  */
39static inline float
40reduce (float x, int32_t *in)
41{
42  /* n = rint(x/(pi/2)).  */
43  float r = x;
44  float q = fmaf (InvPio2, r, Shift);
45  float n = q - Shift;
46  /* There is no rounding here, n is representable by a signed integer.  */
47  *in = (int32_t) n;
48  /* r = x - n * (pi/2)  (range reduction into -pi/4 .. pi/4).  */
49  r = fmaf (NegPio2_1, n, r);
50  r = fmaf (NegPio2_2, n, r);
51  r = fmaf (NegPio2_3, n, r);
52  return r;
53}
54
55/* Table with 4/PI to 192 bit precision.  To avoid unaligned accesses
56   only 8 new bits are added per entry, making the table 4 times larger.  */
57static const uint32_t __inv_pio4[24]
58  = {0x000000a2, 0x0000a2f9, 0x00a2f983, 0xa2f9836e, 0xf9836e4e, 0x836e4e44,
59     0x6e4e4415, 0x4e441529, 0x441529fc, 0x1529fc27, 0x29fc2757, 0xfc2757d1,
60     0x2757d1f5, 0x57d1f534, 0xd1f534dd, 0xf534ddc0, 0x34ddc0db, 0xddc0db62,
61     0xc0db6295, 0xdb629599, 0x6295993c, 0x95993c43, 0x993c4390, 0x3c439041};
62
63/* Reduce the range of XI to a multiple of PI/2 using fast integer arithmetic.
64   XI is a reinterpreted float and must be >= 2.0f (the sign bit is ignored).
65   Return the modulo between -PI/4 and PI/4 and store the quadrant in NP.
66   Reduction uses a table of 4/PI with 192 bits of precision.  A 32x96->128 bit
67   multiply computes the exact 2.62-bit fixed-point modulo.  Since the result
68   can have at most 29 leading zeros after the binary point, the double
69   precision result is accurate to 33 bits.  */
70static inline double
71reduce_large (uint32_t xi, int *np)
72{
73  const uint32_t *arr = &__inv_pio4[(xi >> 26) & 15];
74  int shift = (xi >> 23) & 7;
75  uint64_t n, res0, res1, res2;
76
77  xi = (xi & 0xffffff) | 0x800000;
78  xi <<= shift;
79
80  res0 = xi * arr[0];
81  res1 = (uint64_t) xi * arr[4];
82  res2 = (uint64_t) xi * arr[8];
83  res0 = (res2 >> 32) | (res0 << 32);
84  res0 += res1;
85
86  n = (res0 + (1ULL << 61)) >> 62;
87  res0 -= n << 62;
88  double x = (int64_t) res0;
89  *np = n;
90  return x * Pio2p63;
91}
92
93/* Top 12 bits of the float representation with the sign bit cleared.  */
94static inline uint32_t
95top12 (float x)
96{
97  return (asuint (x) >> 20);
98}
99
100/* Fast single-precision tan implementation.
101   Maximum ULP error: 3.293ulps.
102   tanf(0x1.c849eap+16) got -0x1.fe8d98p-1 want -0x1.fe8d9ep-1.  */
103float
104tanf (float x)
105{
106  /* Get top words.  */
107  uint32_t ix = asuint (x);
108  uint32_t ia = ix & AbsMask;
109  uint32_t ia12 = ia >> 20;
110
111  /* Dispatch between no reduction (small numbers), fast reduction and
112     slow large numbers reduction. The reduction step determines r float
113     (|r| < pi/4) and n signed integer such that x = r + n * pi/2.  */
114  int32_t n;
115  float r;
116  if (ia12 < top12 (Pio4))
117    {
118      /* Optimize small values.  */
119      if (unlikely (ia12 < top12 (0x1p-12f)))
120	{
121	  if (unlikely (ia12 < top12 (0x1p-126f)))
122	    /* Force underflow for tiny x.  */
123	    force_eval_float (x * x);
124	  return x;
125	}
126
127      /* tan (x) ~= x + x^3 * P(x^2).  */
128      float x2 = x * x;
129      float y = eval_P (x2);
130      return fmaf (x2, x * y, x);
131    }
132  /* Similar to other trigonometric routines, fast inaccurate reduction is
133     performed for values of x from pi/4 up to RangeVal. In order to keep errors
134     below 3.5ulps, we set the value of RangeVal to 2^17. This might differ for
135     other trigonometric routines. Above this value more advanced but slower
136     reduction techniques need to be implemented to reach a similar accuracy.
137  */
138  else if (ia12 < top12 (RangeVal))
139    {
140      /* Fast inaccurate reduction.  */
141      r = reduce (x, &n);
142    }
143  else if (ia12 < 0x7f8)
144    {
145      /* Slow accurate reduction.  */
146      uint32_t sign = ix & ~AbsMask;
147      double dar = reduce_large (ia, &n);
148      float ar = (float) dar;
149      r = asfloat (asuint (ar) ^ sign);
150    }
151  else
152    {
153      /* tan(Inf or NaN) is NaN.  */
154      return __math_invalidf (x);
155    }
156
157  /* If x lives in an interval where |tan(x)|
158     - is finite then use an approximation of tangent in the form
159       tan(r) ~ r + r^3 * P(r^2) = r + r * r^2 * P(r^2).
160     - grows to infinity then use an approximation of cotangent in the form
161       cotan(z) ~ 1/z + z * Q(z^2), where the reciprocal can be computed early.
162       Using symmetries of tangent and the identity tan(r) = cotan(pi/2 - r),
163       we only need to change the sign of r to obtain tan(x) from cotan(r).
164     This 2-interval approach requires 2 different sets of coefficients P and
165     Q, where Q is a lower order polynomial than P.  */
166
167  /* Determine if x lives in an interval where |tan(x)| grows to infinity.  */
168  uint32_t alt = (uint32_t) n & 1;
169
170  /* Perform additional reduction if required.  */
171  float z = alt ? -r : r;
172
173  /* Prepare backward transformation.  */
174  float z2 = r * r;
175  float offset = alt ? 1.0f / z : z;
176  float scale = alt ? z : z * z2;
177
178  /* Evaluate polynomial approximation of tan or cotan.  */
179  float p = alt ? eval_Q (z2) : eval_P (z2);
180
181  /* A unified way of assembling the result on both interval types.  */
182  return fmaf (scale, p, offset);
183}
184
185PL_SIG (S, F, 1, tan, -3.1, 3.1)
186PL_TEST_ULP (tanf, 2.80)
187PL_TEST_INTERVAL (tanf, 0, 0xffff0000, 10000)
188PL_TEST_SYM_INTERVAL (tanf, 0x1p-127, 0x1p-14, 50000)
189PL_TEST_SYM_INTERVAL (tanf, 0x1p-14, 0.7, 50000)
190PL_TEST_SYM_INTERVAL (tanf, 0.7, 1.5, 50000)
191PL_TEST_SYM_INTERVAL (tanf, 1.5, 0x1p17, 50000)
192PL_TEST_SYM_INTERVAL (tanf, 0x1p17, 0x1p54, 50000)
193PL_TEST_SYM_INTERVAL (tanf, 0x1p54, inf, 50000)
194