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