1use crate::common::f_fmla;
30use crate::double_double::DoubleDouble;
31use crate::exponents::fast_ldexp;
32use crate::polyeval::f_polyeval6;
33
34#[inline(always)]
45fn halleys_div_free(x: f64, a: f64) -> f64 {
46 const K3: f64 = 2. / 9.;
47 const K2: f64 = 7. / 9.;
48 const K1: f64 = 14. / 9.;
49 let c = a * x * x * x;
50 let mut y = f_fmla(-K3, c, K2);
51 y = f_fmla(-c, y, K1);
52 y * x
53}
54
55pub fn f_rcbrt(a: f64) -> f64 {
59 let xu = a.to_bits();
61 let exp = ((xu >> 52) & 0x7ff) as i32;
62 let mut e = ((xu >> 52) & 0x7ff) as i32;
63 let mut mant = xu & ((1u64 << 52) - 1);
64
65 if exp == 0x7ff {
66 if a.is_infinite() {
67 return if a.is_sign_negative() { -0.0 } else { 0.0 };
68 }
69 return a + a;
70 }
71 if exp == 0 && a == 0. {
72 return if a.is_sign_negative() {
73 f64::NEG_INFINITY
74 } else {
75 f64::INFINITY
76 };
77 }
78
79 if exp == 0 {
81 let norm = a * f64::from_bits(0x4350000000000000); let norm_bits = norm.to_bits();
83 mant = norm_bits & ((1u64 << 52) - 1);
84 e = ((norm_bits >> 52) & 0x7ff) as i32 - 54;
85 }
86
87 e -= 1023;
88
89 mant |= 0x3ff << 52;
90 let m = f64::from_bits(mant);
91
92 let p = f_polyeval6(
100 m,
101 f64::from_bits(0x3ffc7f365bceaf71),
102 f64::from_bits(0xbff90e741fb9c896),
103 f64::from_bits(0x3ff3e68b9b2cd237),
104 f64::from_bits(0xbfe321c5eb24a185),
105 f64::from_bits(0x3fc3fa269b897f69),
106 f64::from_bits(0xbf916d6f13849fd1),
107 );
108
109 let q = e.div_euclid(3);
112 let rem_scale = e.rem_euclid(3);
113
114 static ESCALE: [u64; 3] = [1.0f64.to_bits(), 0x3fe965fea53d6e3d, 0x3fe428a2f98d728b];
116
117 let z = p * f64::from_bits(ESCALE[rem_scale as usize]);
118
119 let mm = fast_ldexp(m, rem_scale); let y0 = halleys_div_free(z, mm);
124
125 let d2y = DoubleDouble::from_exact_mult(y0, y0);
126 let d3y = DoubleDouble::quick_mult_f64(d2y, y0);
127 let hb = DoubleDouble::quick_mult_f64(d3y, mm);
128
129 let y: f64;
130
131 #[cfg(any(
132 all(
133 any(target_arch = "x86", target_arch = "x86_64"),
134 target_feature = "fma"
135 ),
136 all(target_arch = "aarch64", target_feature = "neon")
137 ))]
138 {
139 let r = f_fmla(-hb.lo, y0, f_fmla(hb.hi, -y0, y0));
142 y = f_fmla(1. / 3., r, y0);
144 }
145 #[cfg(not(any(
146 all(
147 any(target_arch = "x86", target_arch = "x86_64"),
148 target_feature = "fma"
149 ),
150 all(target_arch = "aarch64", target_feature = "neon")
151 )))]
152 {
153 let m_hb = DoubleDouble::full_add_f64(-hb, 1.0);
154 let r = DoubleDouble::quick_mult_f64(m_hb, y0);
155 y = f_fmla(1. / 3., r.to_f64(), y0);
156 }
157 f64::copysign(fast_ldexp(y, -q), a)
158}
159
160#[cfg(test)]
161mod tests {
162 use super::*;
163
164 #[test]
165 fn test_rcbrt() {
166 assert_eq!(f_rcbrt(0.9999999999999717), 1.0000000000000095);
167 assert_eq!(f_rcbrt(-68355745214719140000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.),
168 -0.000000000000000000000000000000000000000002445728958868668);
169 assert_eq!(f_rcbrt(-96105972807656840000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.),
170 -0.0000000000000000000000000000000000000000000000000000000002183148143573148);
171 assert_eq!(f_rcbrt(0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000139491540182158),
172 8949883389846071000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.);
173 assert_eq!(f_rcbrt(0.00008386280387617153), 22.846001824951983);
174 assert_eq!(f_rcbrt(-125.0), -0.2);
175 assert_eq!(f_rcbrt(125.0), 0.2);
176 assert_eq!(f_rcbrt(1.0), 1.0);
177 assert_eq!(f_rcbrt(-1.0), -1.0);
178 assert_eq!(f_rcbrt(0.0), f64::INFINITY);
179 assert_eq!(f_rcbrt(-27.0), -1. / 3.);
180 assert_eq!(
181 f_rcbrt(2417851639214765300000000.),
182 0.000000007450580596938716
183 );
184 assert_eq!(f_rcbrt(27.0), 1. / 3.);
185 assert_eq!(f_rcbrt(64.0), 0.25);
186 assert_eq!(f_rcbrt(-64.0), -0.25);
187 assert_eq!(f_rcbrt(f64::NEG_INFINITY), -0.0);
188 assert_eq!(f_rcbrt(f64::INFINITY), 0.0);
189 assert!(f_rcbrt(f64::NAN).is_nan());
190 }
191}