/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.25/src/asinf.rs
Line | Count | Source |
1 | | /* |
2 | | * // Copyright (c) Radzivon Bartoshyk 6/2025. All rights reserved. |
3 | | * // |
4 | | * // Redistribution and use in source and binary forms, with or without modification, |
5 | | * // are permitted provided that the following conditions are met: |
6 | | * // |
7 | | * // 1. Redistributions of source code must retain the above copyright notice, this |
8 | | * // list of conditions and the following disclaimer. |
9 | | * // |
10 | | * // 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | * // this list of conditions and the following disclaimer in the documentation |
12 | | * // and/or other materials provided with the distribution. |
13 | | * // |
14 | | * // 3. Neither the name of the copyright holder nor the names of its |
15 | | * // contributors may be used to endorse or promote products derived from |
16 | | * // this software without specific prior written permission. |
17 | | * // |
18 | | * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | | * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | | * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
22 | | * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 | | * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
24 | | * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
25 | | * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
26 | | * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | | * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | use crate::acosf::poly12; |
30 | | use crate::common::{dd_fmlaf, f_fmla}; |
31 | | |
32 | | #[cold] |
33 | 0 | fn as_special(x: f32) -> f32 { |
34 | 0 | let t = x.to_bits(); |
35 | 0 | let ax = t.wrapping_shl(1); |
36 | 0 | if ax > (0xffu32 << 24) { |
37 | 0 | return x + x; |
38 | 0 | } // nan |
39 | 0 | f32::NAN |
40 | 0 | } |
41 | | |
42 | | /// Computes asin |
43 | | /// |
44 | | /// Max found ULP 0.49999928 |
45 | | #[inline] |
46 | 0 | pub fn f_asinf(x: f32) -> f32 { |
47 | | const PI2: f64 = f64::from_bits(0x3ff921fb54442d18); |
48 | 0 | let xs = x as f64; |
49 | | let mut r; |
50 | 0 | let t = x.to_bits(); |
51 | 0 | let ax = t.wrapping_shl(1); |
52 | 0 | if ax > 0x7f << 24 { |
53 | 0 | return as_special(x); |
54 | 0 | } |
55 | 0 | if ax < 0x7ec29000u32 { |
56 | | // |x| < 1.49029 |
57 | 0 | if ax < 115 << 24 { |
58 | | // |x| < 0.000244141 |
59 | 0 | return dd_fmlaf(x, f64::from_bits(0x3e60000000000000) as f32, x); |
60 | 0 | } |
61 | | const B: [u64; 16] = [ |
62 | | 0x3ff0000000000005, |
63 | | 0x3fc55557aeca105d, |
64 | | 0x3fb3314ec3db7d12, |
65 | | 0x3fa775738a5a6f92, |
66 | | 0x3f75d5f7ce1c8538, |
67 | | 0x3fd605c6d58740f0, |
68 | | 0xc005728b732d73c6, |
69 | | 0x402f152170f151eb, |
70 | | 0xc04f962ea3ca992e, |
71 | | 0x40671971e17375a0, |
72 | | 0xc07860512b4ba230, |
73 | | 0x40826a3b8d4bdb14, |
74 | | 0xc0836f2ea5698b51, |
75 | | 0x407b3d722aebfa2e, |
76 | | 0xc066cf89703b1289, |
77 | | 0x4041518af6a65e2d, |
78 | | ]; |
79 | 0 | let z = xs; |
80 | 0 | let z2 = z * z; |
81 | 0 | let w0 = f_fmla(z2, f64::from_bits(B[1]), f64::from_bits(B[0])); |
82 | 0 | let w1 = f_fmla(z2, f64::from_bits(B[3]), f64::from_bits(B[2])); |
83 | 0 | let w2 = f_fmla(z2, f64::from_bits(B[5]), f64::from_bits(B[4])); |
84 | 0 | let w3 = f_fmla(z2, f64::from_bits(B[7]), f64::from_bits(B[6])); |
85 | 0 | let w4 = f_fmla(z2, f64::from_bits(B[9]), f64::from_bits(B[8])); |
86 | 0 | let w5 = f_fmla(z2, f64::from_bits(B[11]), f64::from_bits(B[10])); |
87 | 0 | let w6 = f_fmla(z2, f64::from_bits(B[13]), f64::from_bits(B[12])); |
88 | 0 | let w7 = f_fmla(z2, f64::from_bits(B[15]), f64::from_bits(B[14])); |
89 | | |
90 | 0 | let z4 = z2 * z2; |
91 | 0 | let z8 = z4 * z4; |
92 | 0 | let z16 = z8 * z8; |
93 | | |
94 | 0 | r = z |
95 | 0 | * ((f_fmla(z4, w1, w0) + z8 * f_fmla(z4, w3, w2)) |
96 | 0 | + z16 * (f_fmla(z4, w5, w4) + z8 * f_fmla(z4, w7, w6))); |
97 | 0 | let ub = r; |
98 | 0 | let lb = r - z * f64::from_bits(0x3e0efa8eb0000000); |
99 | | // Ziv's accuracy test |
100 | 0 | if ub == lb { |
101 | 0 | return ub as f32; |
102 | 0 | } |
103 | 0 | } |
104 | 0 | if ax < (0x7eu32 << 24) { |
105 | | const C: [u64; 12] = [ |
106 | | 0x3fc555555555529c, |
107 | | 0x3fb333333337e0dd, |
108 | | 0x3fa6db6db3b4465e, |
109 | | 0x3f9f1c72e13ac306, |
110 | | 0x3f96e89cebe06bc4, |
111 | | 0x3f91c6dcf5289094, |
112 | | 0x3f8c6dbbcc7c6315, |
113 | | 0x3f88f8dc2615e996, |
114 | | 0x3f7a5833b7bf15e8, |
115 | | 0x3f943f44ace1665c, |
116 | | 0xbf90fb17df881c73, |
117 | | 0x3fa07520c026b2d6, |
118 | | ]; |
119 | 0 | let z = xs; |
120 | 0 | let z2 = z * z; |
121 | 0 | let c0 = poly12(z2, C); |
122 | 0 | r = z + (z * z2) * c0; |
123 | | } else { |
124 | 0 | if ax == 0x7e55688au32 { |
125 | 0 | return f32::copysign(f64::from_bits(0x3fe75b8a20000000) as f32, x) |
126 | 0 | + f32::copysign(f64::from_bits(0x3e50000000000000) as f32, x); |
127 | 0 | } |
128 | 0 | if ax == 0x7e107434u32 { |
129 | 0 | return f32::copysign(f64::from_bits(0x3fe1f4b640000000) as f32, x) |
130 | 0 | + f32::copysign(f64::from_bits(0x3e50000000000000) as f32, x); |
131 | 0 | } |
132 | 0 | let bx = xs.abs(); |
133 | 0 | let z = 1.0 - bx; |
134 | 0 | let s = z.sqrt(); |
135 | | const C: [u64; 12] = [ |
136 | | 0x3ff6a09e667f3bcb, |
137 | | 0x3fbe2b7dddff2db9, |
138 | | 0x3f9b27247ab42dbc, |
139 | | 0x3f802995cc4e0744, |
140 | | 0x3f65ffb0276ec8ea, |
141 | | 0x3f5033885a928dec, |
142 | | 0x3f3911f2be23f8c7, |
143 | | 0x3f24c3c55d2437fd, |
144 | | 0x3f0af477e1d7b461, |
145 | | 0x3f0abd6bdff67dcb, |
146 | | 0xbef1717e86d0fa28, |
147 | | 0x3ef6ff526de46023, |
148 | | ]; |
149 | 0 | r = PI2 - s * poly12(z, C); |
150 | 0 | r = f64::copysign(r, xs); |
151 | | } |
152 | 0 | r as f32 |
153 | 0 | } |
154 | | |
155 | | #[cfg(test)] |
156 | | mod tests { |
157 | | use super::*; |
158 | | |
159 | | #[test] |
160 | | fn test_asinf() { |
161 | | assert_eq!(f_asinf(-0.5), -std::f32::consts::FRAC_PI_6); |
162 | | assert_eq!(f_asinf(0.5), std::f32::consts::FRAC_PI_6); |
163 | | assert!(f_asinf(7.).is_nan()); |
164 | | } |
165 | | } |