1181074Sdas/*-
2181074Sdas * Copyright (c) 2008 David Schultz <das@FreeBSD.ORG>
3181074Sdas * All rights reserved.
4181074Sdas *
5181074Sdas * Redistribution and use in source and binary forms, with or without
6181074Sdas * modification, are permitted provided that the following conditions
7181074Sdas * are met:
8181074Sdas * 1. Redistributions of source code must retain the above copyright
9181074Sdas *    notice, this list of conditions and the following disclaimer.
10181074Sdas * 2. Redistributions in binary form must reproduce the above copyright
11181074Sdas *    notice, this list of conditions and the following disclaimer in the
12181074Sdas *    documentation and/or other materials provided with the distribution.
13181074Sdas *
14181074Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15181074Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16181074Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17181074Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18181074Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19181074Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20181074Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21181074Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22181074Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23181074Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24181074Sdas * SUCH DAMAGE.
25181074Sdas *
26181074Sdas * $FreeBSD$
27181074Sdas */
28181074Sdas
29181074Sdas#include <float.h>
30181074Sdas
31181074Sdas#include "fpmath.h"
32181074Sdas
33181074Sdas#define	BIAS		(LDBL_MAX_EXP - 1)
34181074Sdas#define	MANH_SIZE	(LDBL_MANH_SIZE + 1)
35181074Sdas
36181074Sdas/* Approximation thresholds. */
37181074Sdas#define	ASIN_LINEAR	(BIAS - 56)	/* 2**-56 */
38181074Sdas#define	ACOS_CONST	(BIAS - 113)	/* 2**-113 */
39181074Sdas#define	ATAN_CONST	(BIAS + 113)	/* 2**113 */
40181074Sdas#define	ATAN_LINEAR	(BIAS - 56)	/* 2**-56 */
41181074Sdas
42181074Sdas/* 0.95 */
43181074Sdas#define	THRESH	((0xe666666666666666ULL>>(64-(MANH_SIZE-1)))|LDBL_NBIT)
44181074Sdas
45181074Sdas/* Constants shared by the long double inverse trig functions. */
46181074Sdas#define	pS0	_ItL_pS0
47181074Sdas#define	pS1	_ItL_pS1
48181074Sdas#define	pS2	_ItL_pS2
49181074Sdas#define	pS3	_ItL_pS3
50181074Sdas#define	pS4	_ItL_pS4
51181074Sdas#define	pS5	_ItL_pS5
52181074Sdas#define	pS6	_ItL_pS6
53181074Sdas#define	pS7	_ItL_pS7
54181074Sdas#define	pS8	_ItL_pS8
55181074Sdas#define	pS9	_ItL_pS9
56181074Sdas#define	qS1	_ItL_qS1
57181074Sdas#define	qS2	_ItL_qS2
58181074Sdas#define	qS3	_ItL_qS3
59181074Sdas#define	qS4	_ItL_qS4
60181074Sdas#define	qS5	_ItL_qS5
61181074Sdas#define	qS6	_ItL_qS6
62181074Sdas#define	qS7	_ItL_qS7
63181074Sdas#define	qS8	_ItL_qS8
64181074Sdas#define	qS9	_ItL_qS9
65181074Sdas#define	atanhi	_ItL_atanhi
66181074Sdas#define	atanlo	_ItL_atanlo
67181074Sdas#define	aT	_ItL_aT
68181074Sdas#define	pi_lo	_ItL_pi_lo
69181074Sdas
70181074Sdas#define	pio2_hi	atanhi[3]
71181074Sdas#define	pio2_lo	atanlo[3]
72181074Sdas#define	pio4_hi	atanhi[1]
73181074Sdas
74181074Sdas/* Constants shared by the long double inverse trig functions. */
75181074Sdasextern const long double pS0, pS1, pS2, pS3, pS4, pS5, pS6, pS7, pS8, pS9;
76181074Sdasextern const long double qS1, qS2, qS3, qS4, qS5, qS6, qS7, qS8, qS9;
77181074Sdasextern const long double atanhi[], atanlo[], aT[];
78181074Sdasextern const long double pi_lo;
79181074Sdas
80181074Sdasstatic inline long double
81181074SdasP(long double x)
82181074Sdas{
83181074Sdas
84181074Sdas	return (x * (pS0 + x * (pS1 + x * (pS2 + x * (pS3 + x * \
85181074Sdas		(pS4 + x * (pS5 + x * (pS6 + x * (pS7 + x * (pS8 + x * \
86181074Sdas		pS9))))))))));
87181074Sdas}
88181074Sdas
89181074Sdasstatic inline long double
90181074SdasQ(long double x)
91181074Sdas{
92181074Sdas
93181074Sdas	return (1.0 + x * (qS1 + x * (qS2 + x * (qS3 + x * (qS4 + x * \
94181074Sdas		(qS5 + x * (qS6 + x * (qS7 + x * (qS8 + x * qS9)))))))));
95181074Sdas}
96181074Sdas
97181074Sdasstatic inline long double
98181074SdasT_even(long double x)
99181074Sdas{
100181074Sdas
101181074Sdas	return (aT[0] + x * (aT[2] + x * (aT[4] + x * (aT[6] + x * \
102181074Sdas		(aT[8] + x * (aT[10] + x * (aT[12] + x * (aT[14] + x * \
103181074Sdas		(aT[16] + x * (aT[18] + x * (aT[20] + x * aT[22])))))))))));
104181074Sdas}
105181074Sdas
106181074Sdasstatic inline long double
107181074SdasT_odd(long double x)
108181074Sdas{
109181074Sdas
110181074Sdas	return (aT[1] + x * (aT[3] + x * (aT[5] + x * (aT[7] + x * \
111181074Sdas		(aT[9] + x * (aT[11] + x * (aT[13] + x * (aT[15] + x * \
112181074Sdas		(aT[17] + x * (aT[19] + x * (aT[21] + x * aT[23])))))))))));
113181074Sdas}
114