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
35181074Sdas
36181074Sdas/* Approximation thresholds. */
37181074Sdas#define	ASIN_LINEAR	(BIAS - 32)	/* 2**-32 */
38181074Sdas#define	ACOS_CONST	(BIAS - 65)	/* 2**-65 */
39181074Sdas#define	ATAN_CONST	(BIAS + 65)	/* 2**65 */
40181074Sdas#define	ATAN_LINEAR	(BIAS - 32)	/* 2**-32 */
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	qS1	_ItL_qS1
54181074Sdas#define	qS2	_ItL_qS2
55181074Sdas#define	qS3	_ItL_qS3
56181074Sdas#define	qS4	_ItL_qS4
57181074Sdas#define	qS5	_ItL_qS5
58181074Sdas#define	atanhi	_ItL_atanhi
59181074Sdas#define	atanlo	_ItL_atanlo
60181074Sdas#define	aT	_ItL_aT
61181074Sdas#define	pi_lo	_ItL_pi_lo
62181074Sdas
63181074Sdas#define	pio2_hi	atanhi[3]
64181074Sdas#define	pio2_lo	atanlo[3]
65181074Sdas#define	pio4_hi	atanhi[1]
66181074Sdas
67181152Sdas#ifdef STRUCT_DECLS
68181152Sdastypedef struct longdouble {
69181152Sdas	uint64_t mant;
70181152Sdas	uint16_t expsign;
71181152Sdas} LONGDOUBLE;
72181152Sdas#else
73181152Sdastypedef long double LONGDOUBLE;
74181152Sdas#endif
75181074Sdas
76181152Sdasextern const LONGDOUBLE pS0, pS1, pS2, pS3, pS4, pS5, pS6;
77181152Sdasextern const LONGDOUBLE qS1, qS2, qS3, qS4, qS5;
78181152Sdasextern const LONGDOUBLE atanhi[], atanlo[], aT[];
79181152Sdasextern const LONGDOUBLE pi_lo;
80181152Sdas
81181152Sdas#ifndef STRUCT_DECLS
82181152Sdas
83181074Sdasstatic inline long double
84181074SdasP(long double x)
85181074Sdas{
86181074Sdas
87181074Sdas	return (x * (pS0 + x * (pS1 + x * (pS2 + x * (pS3 + x * \
88181074Sdas		(pS4 + x * (pS5 + x * pS6)))))));
89181074Sdas}
90181074Sdas
91181074Sdasstatic inline long double
92181074SdasQ(long double x)
93181074Sdas{
94181074Sdas
95181074Sdas	return (1.0 + x * (qS1 + x * (qS2 + x * (qS3 + x * (qS4 + x * qS5)))));
96181074Sdas}
97181074Sdas
98181074Sdasstatic inline long double
99181074SdasT_even(long double x)
100181074Sdas{
101181074Sdas
102181074Sdas	return (aT[0] + x * (aT[2] + x * (aT[4] + x * (aT[6] + x * \
103181074Sdas		(aT[8] + x * (aT[10] + x * aT[12]))))));
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])))));
112181074Sdas}
113181152Sdas
114181152Sdas#endif
115