1*390c8400Smartynas /* $OpenBSD: s_tanl.c,v 1.1 2008/12/09 20:00:35 martynas Exp $ */
2*390c8400Smartynas /*-
3*390c8400Smartynas * Copyright (c) 2007 Steven G. Kargl
4*390c8400Smartynas * All rights reserved.
5*390c8400Smartynas *
6*390c8400Smartynas * Redistribution and use in source and binary forms, with or without
7*390c8400Smartynas * modification, are permitted provided that the following conditions
8*390c8400Smartynas * are met:
9*390c8400Smartynas * 1. Redistributions of source code must retain the above copyright
10*390c8400Smartynas * notice unmodified, this list of conditions, and the following
11*390c8400Smartynas * disclaimer.
12*390c8400Smartynas * 2. Redistributions in binary form must reproduce the above copyright
13*390c8400Smartynas * notice, this list of conditions and the following disclaimer in the
14*390c8400Smartynas * documentation and/or other materials provided with the distribution.
15*390c8400Smartynas *
16*390c8400Smartynas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*390c8400Smartynas * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*390c8400Smartynas * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*390c8400Smartynas * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*390c8400Smartynas * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*390c8400Smartynas * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*390c8400Smartynas * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*390c8400Smartynas * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*390c8400Smartynas * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*390c8400Smartynas * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*390c8400Smartynas */
27*390c8400Smartynas
28*390c8400Smartynas /*
29*390c8400Smartynas * Compute tan(x) for x where x is reduced to y = x - k * pi / 2.
30*390c8400Smartynas * Limited testing on pseudorandom numbers drawn within [0:4e8] shows
31*390c8400Smartynas * an accuracy of <= 1.5 ULP where 247024 values of x out of 40 million
32*390c8400Smartynas * possibles resulted in tan(x) that exceeded 0.5 ULP (ie., 0.6%).
33*390c8400Smartynas */
34*390c8400Smartynas
35*390c8400Smartynas #include <sys/types.h>
36*390c8400Smartynas #include <machine/ieee.h>
37*390c8400Smartynas #include <float.h>
38*390c8400Smartynas #include <math.h>
39*390c8400Smartynas
40*390c8400Smartynas #include "math_private.h"
41*390c8400Smartynas
42*390c8400Smartynas #if LDBL_MANT_DIG == 64
43*390c8400Smartynas #define NX 3
44*390c8400Smartynas #define PREC 2
45*390c8400Smartynas #elif LDBL_MANT_DIG == 113
46*390c8400Smartynas #define NX 5
47*390c8400Smartynas #define PREC 3
48*390c8400Smartynas #else
49*390c8400Smartynas #error "Unsupported long double format"
50*390c8400Smartynas #endif
51*390c8400Smartynas
52*390c8400Smartynas static const long double two24 = 1.67772160000000000000e+07L;
53*390c8400Smartynas
54*390c8400Smartynas long double
tanl(long double x)55*390c8400Smartynas tanl(long double x)
56*390c8400Smartynas {
57*390c8400Smartynas union {
58*390c8400Smartynas long double e;
59*390c8400Smartynas struct ieee_ext bits;
60*390c8400Smartynas } z;
61*390c8400Smartynas int i, e0, s;
62*390c8400Smartynas double xd[NX], yd[PREC];
63*390c8400Smartynas long double hi, lo;
64*390c8400Smartynas
65*390c8400Smartynas z.e = x;
66*390c8400Smartynas s = z.bits.ext_sign;
67*390c8400Smartynas z.bits.ext_sign = 0;
68*390c8400Smartynas
69*390c8400Smartynas /* If x = +-0 or x is subnormal, then tan(x) = x. */
70*390c8400Smartynas if (z.bits.ext_exp == 0)
71*390c8400Smartynas return (x);
72*390c8400Smartynas
73*390c8400Smartynas /* If x = NaN or Inf, then tan(x) = NaN. */
74*390c8400Smartynas if (z.bits.ext_exp == 32767)
75*390c8400Smartynas return ((x - x) / (x - x));
76*390c8400Smartynas
77*390c8400Smartynas /* Optimize the case where x is already within range. */
78*390c8400Smartynas if (z.e < M_PI_4) {
79*390c8400Smartynas hi = __kernel_tanl(z.e, 0, 0);
80*390c8400Smartynas return (s ? -hi : hi);
81*390c8400Smartynas }
82*390c8400Smartynas
83*390c8400Smartynas /* Split z.e into a 24-bit representation. */
84*390c8400Smartynas e0 = ilogbl(z.e) - 23;
85*390c8400Smartynas z.e = scalbnl(z.e, -e0);
86*390c8400Smartynas for (i = 0; i < NX; i++) {
87*390c8400Smartynas xd[i] = (double)((int32_t)z.e);
88*390c8400Smartynas z.e = (z.e - xd[i]) * two24;
89*390c8400Smartynas }
90*390c8400Smartynas
91*390c8400Smartynas /* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
92*390c8400Smartynas e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC);
93*390c8400Smartynas
94*390c8400Smartynas #if PREC == 2
95*390c8400Smartynas hi = (long double)yd[0] + yd[1];
96*390c8400Smartynas lo = yd[1] - (hi - yd[0]);
97*390c8400Smartynas #else /* PREC == 3 */
98*390c8400Smartynas long double t;
99*390c8400Smartynas t = (long double)yd[2] + yd[1];
100*390c8400Smartynas hi = t + yd[0];
101*390c8400Smartynas lo = yd[0] - (hi - t);
102*390c8400Smartynas #endif
103*390c8400Smartynas
104*390c8400Smartynas switch (e0 & 3) {
105*390c8400Smartynas case 0:
106*390c8400Smartynas case 2:
107*390c8400Smartynas hi = __kernel_tanl(hi, lo, 0);
108*390c8400Smartynas break;
109*390c8400Smartynas case 1:
110*390c8400Smartynas case 3:
111*390c8400Smartynas hi = __kernel_tanl(hi, lo, 1);
112*390c8400Smartynas break;
113*390c8400Smartynas }
114*390c8400Smartynas
115*390c8400Smartynas return (s ? -hi : hi);
116*390c8400Smartynas }
117