xref: /freebsd/lib/msun/src/s_asinpif.c (revision ae417b3194e76ce26065dc20281493ee83619879)
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_asinpi.c for implementation details.
31  */
32 
33 #include "math.h"
34 #include "math_private.h"
35 
36 #define _CC	(0x1p12F + 1)
37 #define _ROOT	sqrtf
38 
39 volatile static const float tiny = 1.e-30;
40 static const float half = 0.5f, one = 1.f;
41 
42 /* Full precision high and low parts of 1 / pi. */
43 static const float
44 invpihi = 3.18309873e-01f,
45 invpilo = 1.28412765e-08f;
46 
47 /*
48  * Prior to the leading multiplication by x^2, the rational approximation
49  * has an absolute minimax error less than 2.8e-10 over the [0x1p-12,0.5]
50  * domain (or log2(error) = -31.7).
51  */
52 static inline float
__r(float xs)53 __r(float xs)
54 {
55 	static const float
56 	    R0 =  5.30516468e-02f,
57 	    R1 = -3.80413085e-02f,
58 	    R2 =  1.74116367e-03f,
59 	    S1 = -1.16706085e+00f,
60 	    S2 =  2.90115148e-01f;
61 	float r, s;
62 	r = R0 + (R1 + R2 * xs) * xs;
63 	s =  1 + (S1 + S2 * xs) * xs;
64 	return (xs * (r / s));
65 }
66 
67 float
asinpif(float x)68 asinpif(float x)
69 {
70 	float ax, hi, lo, xh, xl, y, zh, zl;
71 	uint32_t hx, ix;
72 
73 	GET_FLOAT_WORD(hx, x);
74 	ix = hx & 0x7fffffff;
75 
76 	/* |x| > 1 */
77 	if (ix > 0x3f800000)
78 		return ((x - x) / (x - x));
79 
80 	SET_FLOAT_WORD(ax, ix);
81 
82 	if (ix <= 0x3f000000) {			/* |x| <= 0.5 */
83 		if (ix < 0x39800000) {		/* |x| < 0x1p-12 */
84 			if (ix < 0x03800000) {	/* |x| < 0x1p-120 */
85 				if (ix == 0)
86 					return (x);
87 				/* Scale for near subnormal. */
88 				ax *= 0x1p25f;
89 				_XMUL(ax, 0, invpihi, invpilo, hi, lo);
90 				y = (hi + lo) * 0x1p-25f;
91 			} else {
92 				_XMUL(ax, 0, invpihi, invpilo, hi, lo);
93 				y = hi + lo;
94 			}
95 		} else {
96 			y = __r(ax * ax);
97 			_XADD(invpihi, invpilo, y, 0, xh, xl);
98 			_XMUL(ax, 0, xh, xl, hi, lo);
99 			y = hi + lo;
100 		}
101 	} else if (ix < 0x3f800000) {		/* |x| < 1 */
102 		y = 1 - ax;
103 		x = __r(y / 2);
104 		_XADD(invpihi, invpilo, x, 0, xh, xl);
105 		_SQRT(2 * y, zh, zl);
106 		_XMUL(xh, xl, zh, zl, hi, lo);
107 		_XADD(half, 0, -hi, -lo, y, x);
108 	} else					/* |x| == 1 */
109 		y = half;
110 
111 	return ((hx & 0x80000000) ? -y : y);
112 }
113 
114 
115 /*
116  * See src/s_asinpi.c for implementation details.
117  */
118 
119 float
acospif(float x)120 acospif(float x)
121 {
122 	float ax, hi, lo, xh, xl, y, zh, zl;
123 	uint32_t hx, ix;
124 
125 	GET_FLOAT_WORD(hx, x);
126 	ix = hx & 0x7fffffff;
127 
128 	/* |x| > 1 */
129 	if (ix > 0x3f800000)
130 		return ((x - x) / (x - x));
131 
132 	if (ix <= 0x3f000000) {			/* |x| <= 0.5 */
133 		if (ix <= 0x3a800000) {		/* |x| <= 0x1p-10 */
134 			y = (ix == 0) ? half : ((ix < 0x33800000) ?
135 			    half - tiny : half - x * invpihi);
136 		} else {
137 			y = __r(x * x);
138 			_XADD(invpihi, invpilo, y, 0, xh, xl);
139 			_XMUL(x, 0, xh, xl, hi, lo);
140 			_XADD(half, 0, -hi, -lo, y, ax);
141 		}
142 	} else if (ix < 0x3f800000) {		/* |x| < 1 */
143 		SET_FLOAT_WORD(ax, ix);
144 		y = 1 - ax;
145 		ax = __r(y / 2);
146 		_XADD(invpihi, invpilo, ax, 0, xh, xl);
147 		_SQRT(2 * y, zh, zl);
148 		_XMUL(xh, xl, zh, zl, hi, lo);
149 		if (hx & 0x80000000)
150 			_XADD(one, 0, -hi, -lo, y, ax);
151 		else
152 			y = hi + lo;
153 	} else					/* |x| == 1 */
154 		y = hx & 0x80000000 ? 1 : 0;
155 
156 	return (y);
157 }
158