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