pxfm/cube_roots/
rcbrtf.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 8/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 */
29use crate::common::f_fmla;
30
31// // y1 = y0 * (2+x*y0^3)/(1+2*x*y0^3)
32// #[inline(always)]
33// fn halley_refine_d(x: f64, a: f64) -> f64 {
34//     let tx = x * x * x;
35//     x * f_fmla(tx, a, 2.0) / f_fmla(2. * a, tx, 1.0)
36// }
37
38#[inline(always)]
39fn rapshon_refine_inv_cbrt(x: f64, a: f64) -> f64 {
40    x * f_fmla(-1. / 3. * a, x * x * x, 4. / 3.)
41}
42
43// y1 = y0(k1 − c(k2 − k3c), c = x*y0*y0*y0
44// k1 = 14/9 , k2 = 7/9 , k3 = 2/9
45#[inline(always)]
46fn halleys_div_free(x: f64, a: f64) -> f64 {
47    const K3: f64 = 2. / 9.;
48    const K2: f64 = 7. / 9.;
49    const K1: f64 = 14. / 9.;
50    let c = a * x * x * x;
51    let mut y = f_fmla(-K3, c, K2);
52    y = f_fmla(-c, y, K1);
53    y * x
54}
55
56/// Computes 1/cbrt(x)
57///
58/// ULP 0.5
59#[inline]
60pub fn f_rcbrtf(x: f32) -> f32 {
61    let u = x.to_bits();
62    let au = u.wrapping_shl(1);
63    if au < (1u32 << 24) || au >= (0xffu32 << 24) {
64        if x.is_infinite() {
65            return if x.is_sign_negative() { -0.0 } else { 0.0 };
66        }
67        if au >= (0xffu32 << 24) {
68            return x + x; /* inf, nan */
69        }
70        if x == 0. {
71            return if x.is_sign_positive() {
72                f32::INFINITY
73            } else {
74                f32::NEG_INFINITY
75            }; /* +-inf */
76        }
77    }
78
79    let mut ui: u32 = x.to_bits();
80    let mut hx: u32 = ui & 0x7fffffff;
81
82    if hx < 0x00800000 {
83        /* zero or subnormal? */
84        if hx == 0 {
85            return x; /* cbrt(+-0) is itself */
86        }
87        const TWO_EXP_24: f32 = f32::from_bits(0x4b800000);
88        ui = (x * TWO_EXP_24).to_bits();
89        hx = ui & 0x7fffffff;
90        const B: u32 = 0x54a21d2au32 + (8u32 << 23);
91        hx = B.wrapping_sub(hx / 3);
92    } else {
93        hx = 0x54a21d2au32.wrapping_sub(hx / 3);
94    }
95    ui &= 0x80000000;
96    ui |= hx;
97
98    let t = f32::from_bits(ui) as f64;
99    let dx = x as f64;
100    let mut t = halleys_div_free(t, dx);
101    t = halleys_div_free(t, dx);
102    t = rapshon_refine_inv_cbrt(t, dx);
103    t as f32
104}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn test_fcbrtf() {
112        assert_eq!(f_rcbrtf(0.0), f32::INFINITY);
113        assert_eq!(f_rcbrtf(-0.0), f32::NEG_INFINITY);
114        assert_eq!(f_rcbrtf(-27.0), -1. / 3.);
115        assert_eq!(f_rcbrtf(27.0), 1. / 3.);
116        assert_eq!(f_rcbrtf(64.0), 0.25);
117        assert_eq!(f_rcbrtf(-64.0), -0.25);
118        assert_eq!(f_rcbrtf(f32::NEG_INFINITY), -0.0);
119        assert_eq!(f_rcbrtf(f32::INFINITY), 0.0);
120        assert!(f_rcbrtf(f32::NAN).is_nan());
121    }
122}