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