1 /*-
2  * Copyright (c) 2008 David Schultz <das@FreeBSD.ORG>
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, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <float.h>
30 
31 #include "fpmath.h"
32 
33 #define	BIAS		(LDBL_MAX_EXP - 1)
34 #define	MANH_SIZE	(LDBL_MANH_SIZE + 1)
35 
36 /* Approximation thresholds. */
37 #define	ASIN_LINEAR	(BIAS - 56)	/* 2**-56 */
38 #define	ACOS_CONST	(BIAS - 113)	/* 2**-113 */
39 #define	ATAN_CONST	(BIAS + 113)	/* 2**113 */
40 #define	ATAN_LINEAR	(BIAS - 56)	/* 2**-56 */
41 
42 /* 0.95 */
43 #define	THRESH	((0xe666666666666666ULL>>(64-(MANH_SIZE-1)))|LDBL_NBIT)
44 
45 /* Constants shared by the long double inverse trig functions. */
46 #define	pS0	_ItL_pS0
47 #define	pS1	_ItL_pS1
48 #define	pS2	_ItL_pS2
49 #define	pS3	_ItL_pS3
50 #define	pS4	_ItL_pS4
51 #define	pS5	_ItL_pS5
52 #define	pS6	_ItL_pS6
53 #define	pS7	_ItL_pS7
54 #define	pS8	_ItL_pS8
55 #define	pS9	_ItL_pS9
56 #define	qS1	_ItL_qS1
57 #define	qS2	_ItL_qS2
58 #define	qS3	_ItL_qS3
59 #define	qS4	_ItL_qS4
60 #define	qS5	_ItL_qS5
61 #define	qS6	_ItL_qS6
62 #define	qS7	_ItL_qS7
63 #define	qS8	_ItL_qS8
64 #define	qS9	_ItL_qS9
65 #define	atanhi	_ItL_atanhi
66 #define	atanlo	_ItL_atanlo
67 #define	aT	_ItL_aT
68 #define	pi_lo	_ItL_pi_lo
69 
70 #define	pio2_hi	atanhi[3]
71 #define	pio2_lo	atanlo[3]
72 #define	pio4_hi	atanhi[1]
73 
74 /* Constants shared by the long double inverse trig functions. */
75 extern const long double pS0, pS1, pS2, pS3, pS4, pS5, pS6, pS7, pS8, pS9;
76 extern const long double qS1, qS2, qS3, qS4, qS5, qS6, qS7, qS8, qS9;
77 extern const long double atanhi[], atanlo[], aT[];
78 extern const long double pi_lo;
79 
80 static inline long double
P(long double x)81 P(long double x)
82 {
83 
84 	return (x * (pS0 + x * (pS1 + x * (pS2 + x * (pS3 + x * \
85 		(pS4 + x * (pS5 + x * (pS6 + x * (pS7 + x * (pS8 + x * \
86 		pS9))))))))));
87 }
88 
89 static inline long double
Q(long double x)90 Q(long double x)
91 {
92 
93 	return (1.0 + x * (qS1 + x * (qS2 + x * (qS3 + x * (qS4 + x * \
94 		(qS5 + x * (qS6 + x * (qS7 + x * (qS8 + x * qS9)))))))));
95 }
96 
97 static inline long double
T_even(long double x)98 T_even(long double x)
99 {
100 
101 	return (aT[0] + x * (aT[2] + x * (aT[4] + x * (aT[6] + x * \
102 		(aT[8] + x * (aT[10] + x * (aT[12] + x * (aT[14] + x * \
103 		(aT[16] + x * (aT[18] + x * (aT[20] + x * aT[22])))))))))));
104 }
105 
106 static inline long double
T_odd(long double x)107 T_odd(long double x)
108 {
109 
110 	return (aT[1] + x * (aT[3] + x * (aT[5] + x * (aT[7] + x * \
111 		(aT[9] + x * (aT[11] + x * (aT[13] + x * (aT[15] + x * \
112 		(aT[17] + x * (aT[19] + x * (aT[21] + x * aT[23])))))))))));
113 }
114