1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2026 Steven G. Kargl
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
12 * disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * src/s_atanpi.c for implemenation details.
31 */
32
33 #ifdef __i386__
34 #include <ieeefp.h>
35 #endif
36 #include <stdint.h>
37
38 #include "fpmath.h"
39 #include "math.h"
40 #include "math_private.h"
41
42 #define _CC (0x1p32L + 1)
43 #define _ROOT sqrtl
44 #define NBIT (0x8000000000000000ull)
45
46 volatile static const double tiny = 1.e-300;
47 static const double half = 0.5, one = 1., qrtr = 0.25;
48 static const double x0 = 0.625, x1 = 0.875, x2 = 1.5;
49
50 /* 53-bit high and low parts. */
51 static const double
52 invpihi = 3.1830988618379069e-01, /* 1/pi */
53 invpilo = -1.9678676675182486e-17, /* 1/pi */
54 a0hi = 1.7780768448935275e-01, /* atanpi(x0) */
55 a0lo = 6.7223942595197191e-18, /* atanpi(x0) */
56 a1hi = 2.2881069536505358e-01, /* atanpi(x1) */
57 a1lo = 8.7193139538130510e-18, /* atanpi(x1) */
58 a2hi = 3.1283295818900120e-01, /* atanpi(x2) */
59 a2lo = -1.4076885713501453e-17; /* atanpi(x2) */
60
61 /*
62 * Prior to the leading multiplication by x^2, the rational approximation
63 * has an absolute minimax error less than 1.22e-23 over the [0x1p-32,0.5]
64 * domain (or log2(error) = -76.1).
65 */
66 static inline long double
__r(long double xs)67 __r(long double xs)
68 {
69 static const union IEEEl2bits
70 R0u = LD80C(0xd94caf3dbdb01c38, -4, -1.06103295394596890513e-01L),
71 R1u = LD80C(0x845b6d12f35f0ccb, -2, -2.58510025561632250608e-01L),
72 R2u = LD80C(0xe679d75bf98585ae, -3, -2.25074162472429082081e-01L),
73 R3u = LD80C(0xaba63bb6f14e17e4, -4, -8.38131585316324449243e-02L),
74 R4u = LD80C(0xca768e4a7d2fd52e, -7, -1.23573674736290235872e-02L),
75 R5u = LD80C(0x802e09b751d4c6bc, -11, -4.88967268965928487608e-04L),
76 S1u = LD80C(0xc2545ef3d335e598, 1, 3.03639959155120062705e+00L),
77 S2u = LD80C(0xe0ee42f67b6b24bc, 1, 3.51454233236806805243e+00L),
78 S3u = LD80C(0xf7200820c8d271d1, 0, 1.93066503144077077959e+00L),
79 S4u = LD80C(0x820ec69663c6f1f3, -1, 5.08037959781883845928e-01L),
80 S5u = LD80C(0xe61a8fde47e2f517, -5, 5.61776752333507006525e-02L),
81 S6u = LD80C(0xe260573083b4d35b, -10, 1.72711433720654801154e-03L);
82
83 #define R0 (R0u.e)
84 #define R1 (R1u.e)
85 #define R2 (R2u.e)
86 #define R3 (R3u.e)
87 #define R4 (R4u.e)
88 #define R5 (R5u.e)
89 #define S1 (S1u.e)
90 #define S2 (S2u.e)
91 #define S3 (S3u.e)
92 #define S4 (S4u.e)
93 #define S5 (S5u.e)
94 #define S6 (S6u.e)
95
96 long double r, s;
97 r = R0 + (R1 + (R2 + (R3 + (R4 + R5 * xs) * xs) * xs) * xs) * xs;
98 s = 1 + (S1 + (S2 + (S3 + (S4 + (S5 + S6 * xs) * xs) * xs) *
99 xs) * xs) * xs;
100 return (xs * (r / s));
101 }
102
103 long double
atanpil(long double x)104 atanpil(long double x)
105 {
106 long double ax, hi, lo, xh, xl, y, zh, zl;
107 uint64_t lx;
108 uint16_t hx, ix;
109
110 EXTRACT_LDBL80_WORDS(hx, lx, x);
111 ix = hx & 0x7fff;
112
113 /* x = +-inf, nan */
114 if (ix >= 0x7fff && lx >= 0x8000000000000000ull) {
115 if (lx > 0x8000000000000000ull)
116 return (x + x);
117 return ((hx & 0x8000) ? -half : half);
118 }
119
120 ENTERI();
121
122 INSERT_LDBL80_WORDS(ax, ix, lx);
123
124 if (ix < 0x3ffe ) { /* |x| < 0.5 */
125 if (ix < 0x3fde) { /* |x| < 0x1p-33 */
126 if (ix < 0x002b) { /* |x| < 0x1p-16340 */
127 if ((ix | lx) == 0)
128 RETURNI(x);
129 /* Scale for near subnormal. */
130 ax *= 0x1p65;
131 _XMUL(ax, 0, invpihi, invpilo, hi, lo);
132 y = (hi + lo) * 0x1p-65;
133 } else {
134 _XMUL(ax, 0, invpihi, invpilo, hi, lo);
135 y = hi + lo;
136 }
137 } else {
138 y = __r(ax * ax);
139 _XADD(invpihi, invpilo, y, 0, xh, xl);
140 _XMUL(ax, 0, xh, xl, hi, lo);
141 y = hi + lo;
142 }
143 } else if (ax < 1) { /* |x| < 1 */
144 /* |x| < 0.75 */
145 if (ix == 0x3ffe && lx < 0xc000000000000000ull) {
146 x = (ax - x0) / (1 + x0 * ax);
147 y = __r(x * x);
148 _XADD(invpihi, invpilo, y, 0, xh, xl);
149 _XMUL(x, 0, xh, xl, hi, lo);
150 _XADD(a0hi, a0lo, hi, lo, y, xl);
151 } else {
152 x = (ax - x1) / (1 + x1 * ax);
153 y = __r(x * x);
154 _XADD(invpihi, invpilo, y, 0, xh, xl);
155 _XMUL(x, 0, xh, xl, hi, lo);
156 _XADD(a1hi, a1lo, hi, lo, y, xl);
157 }
158 } else if (ix < 0x4000) { /* |x| < 2 */
159 if (ix == 0x3fff && lx == NBIT)
160 return ((hx & 0x8000) ? -qrtr : qrtr);
161 x = (ax - x2) / (1 + x2 * ax);
162 y = __r(x * x);
163 _XADD(invpihi, invpilo, y, 0, xh, xl);
164 _XMUL(x, 0, xh, xl, hi, lo);
165 _XADD(a2hi, a2lo, hi, lo, y, xl);
166 } else { /* |x| > 2 */
167 x = 1 / ax;
168 y = __r(x * x);
169 _XADD(invpihi, invpilo, y, 0, xh, xl);
170 _XMUL(x, 0, xh, xl, hi, lo);
171 _XADD(half, 0, -hi, -lo, y, x);
172 }
173
174 RETURNI((hx & 0x8000) ? -y : y);
175 }
176