use std::cmp::Ordering;
use super::dint::Dint;
use super::pow_dint::{add, cmp_abs};
const FRACTION: u64 = (1 << 52) - 1;
const XMAX: [u64; 34] = [
0,
u64::MAX,
94_906_265,
208_063,
9741,
1551,
455,
189,
97,
59,
39,
27,
21,
15,
13,
11,
9,
7,
7,
5,
5,
5,
5,
4,
3,
3,
3,
3,
3,
3,
3,
3,
3,
3,
];
#[inline(always)]
pub fn is_int(x: f64) -> bool {
let bits = x.to_bits() & (u64::MAX >> 1);
let biased = bits >> 52;
if biased < 0x3ff {
return bits == 0;
}
if biased >= 0x433 {
return true;
}
bits & ((1u64 << (0x433 - biased)) - 1) == 0
}
#[inline(always)]
pub fn is_odd_int(y: f64) -> bool {
is_int(y) && !is_int(y * 0.5)
}
#[inline(always)]
fn extract(x: f64) -> (i64, u64) {
let bits = x.to_bits();
let biased = ((bits >> 52) & 0x7ff) as i64;
let m = (bits & FRACTION) + if biased != 0 { 1 << 52 } else { 0 };
let t = m.trailing_zeros();
(biased + t as i64 - (0x433 - i64::from(biased == 0)), m >> t)
}
#[inline(always)]
fn odd_split(bits: u64) -> (u64, i64) {
let mut m = bits & FRACTION;
let mut e = ((bits << 1) >> 53) as i64 - 0x433;
if e >= -1074 {
m |= 1 << 52;
} else {
e += 1;
}
let t = m.trailing_zeros();
(m >> t, e + t as i64)
}
#[inline(always)]
fn pow2(x: f64, e: i64) -> f64 {
let x = if e & 1 != 0 { x * 2.0 } else { x };
let e2 = f64::from_bits((((e >> 1) + 0x3ff) as u64 & 0x7ff) << 52);
(x * e2) * e2
}
pub fn is_exact(x: f64, y: f64) -> bool {
let (v, w) = (x.to_bits(), y.to_bits());
if (v << 1) != 0x7fe0_0000_0000_0000 && (w << 22) != 0 {
return false;
}
if (v << 1) == 0x7fe0_0000_0000_0000 {
return true;
}
let (mut m, mut e) = odd_split(v);
if y >= 0.0 && is_int(y) {
if y == 0.0 || y == 1.0 {
return true;
}
if m == 1 {
let ye = y * e as f64;
return (-1074.0..1024.0).contains(&ye);
}
if !(0.0..=33.0).contains(&y) {
return false;
}
let yi = y as usize;
if m > XMAX[yi] {
return false;
}
let t = 64 - m.leading_zeros() as i64;
let ez = e * yi as i64 + t;
if ez <= -1074 || 1024 < ez {
return false;
}
return e * yi as i64 >= -1074;
}
let (n, mut f) = odd_split(w);
if y < 0.0 {
if m != 1 {
return false;
}
let ez = if f >= 0 {
if f < 12 {
(-(n as i64)).wrapping_mul(e).wrapping_shl(f as u32)
} else {
1024
}
} else {
if -f > e.trailing_zeros() as i64 {
return false;
}
(-e >> -f) * n as i64
};
return (-1074..1024).contains(&ez);
}
while f != 0 {
f += 1;
if e & 1 != 0 {
return false;
}
e /= 2;
let s = m.isqrt();
if s * s != m {
return false;
}
m = s;
}
if m > 1 && (33 < n || m > XMAX[n as usize]) {
return false;
}
let mut my = m;
for _ in 1..n {
my = my.wrapping_mul(m);
}
let t = 64 - my.leading_zeros() as i64;
let en = e * n as i64;
-1074 <= en && en + t <= 1024
}
pub fn exact_pow(x: f64, y: f64, z: Dint, exact: bool) -> Option<f64> {
let sign: i64 = if z.sgn != 0 { -1 } else { 1 };
let (e, m) = extract(x);
if m == 1 {
let g = e as f64 * y;
if !is_int(g) {
return None;
}
let g = (g as i64).clamp(-1100, 1100);
return Some(pow2(sign as f64, g));
}
if !(0.0..=34.0).contains(&y) {
return None;
}
let (f, n) = extract(y);
if n > 34 || f < -5 {
return None;
}
if f < 0 {
if (e as u64) & (u64::MAX >> (64 + f)) != 0 {
return None;
}
let g = (e >> -f) * n as i64;
let big_g = z.ex - 53;
let k = ((z.hi >> 10) + ((z.hi >> 9) & 1)) as i64;
if !exact {
let cnt = (k as u64).leading_zeros();
let rounded = Dint {
hi: (k as u64) << cnt,
lo: 0,
ex: big_g + 63 - cnt as i64,
sgn: 1 - z.sgn,
};
let mut d = add(z, rounded);
d.ex += 116;
if cmp_abs(d, z) != Ordering::Less {
return None;
}
}
if big_g > g {
return None;
}
let shift = (g - big_g) as u32;
if shift >= 64 {
return None;
}
let k = k as u64;
if k & !(!1u64 << shift) == 1u64 << shift {
return Some(pow2(((k >> shift) as i64 * sign) as f64, g));
}
return None;
}
let mut t = n << f;
let mut k: i64 = 1;
let mut base = m;
while t != 0 {
if t & 1 != 0 {
k = i64::try_from(base as i128 * k as i128).ok()?;
}
t >>= 1;
if t != 0 {
base = base.checked_mul(base)?;
}
}
if k >> 54 != 0 {
return None;
}
Some(pow2((k * sign) as f64, e * (n << f) as i64))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_int_reads_bits() {
for (x, want) in [
(0.0, true),
(-0.0, true),
(1.0, true),
(-3.0, true),
(0.5, false),
(2.5, false),
(4_503_599_627_370_495.5, false),
(4_503_599_627_370_496.0, true),
(1.0e300, true),
(f64::from_bits(1), false),
(f64::MAX, true),
] {
assert_eq!(is_int(x), want, "{x}");
}
assert!(is_odd_int(-3.0));
assert!(is_odd_int(9_007_199_254_740_991.0));
assert!(!is_odd_int(9_007_199_254_740_992.0));
assert!(!is_odd_int(1.0e300));
assert!(!is_odd_int(0.5));
}
#[test]
fn test_extract_returns_odd_significand() {
assert_eq!(extract(12.0), (2, 3));
assert_eq!(extract(-0.375), (-3, 3));
assert_eq!(extract(f64::from_bits(1)), (-1074, 1));
assert_eq!(extract(f64::from_bits(6)), (-1073, 3));
assert_eq!(extract(1.0), (0, 1));
}
#[test]
fn test_pow2_rounds_once() {
assert_eq!(pow2(3.0, 5), 96.0);
assert_eq!(pow2(1.0, -1074).to_bits(), 1);
assert_eq!(pow2(1.0, -1075).to_bits(), 0);
assert_eq!(
pow2(-3.0, -1075).to_bits(),
(-2.0 * f64::from_bits(1)).to_bits()
);
assert_eq!(pow2(1.0, 1024), f64::INFINITY);
assert_eq!(pow2(-1.0, 1100), f64::NEG_INFINITY);
assert_eq!(pow2(1.0, -1100).to_bits(), 0);
}
#[test]
fn test_is_exact_classifies_cases() {
assert!(is_exact(1.0, 0.3));
assert!(is_exact(3.0, 2.0));
assert!(is_exact(9.0, 0.5));
assert!(is_exact(0.25, 1.5));
assert!(is_exact(4.0, -0.5));
assert!(!is_exact(3.0, 0.5));
assert!(!is_exact(2.0, 0.5));
assert!(!is_exact(3.0, -1.0));
assert!(!is_exact(3.0, 4.0 / 3.0));
}
#[test]
fn test_exact_pow_finds_exact_and_midpoint_results() {
let z = |v: f64| Dint::from_f64(v);
assert_eq!(exact_pow(4.0, 1.5, z(8.0), true), Some(8.0));
assert_eq!(exact_pow(2.0, -3.0, z(0.125), true), Some(0.125));
assert_eq!(
exact_pow(-2.0, 3.0, Dint { sgn: 1, ..z(8.0) }, true),
Some(-8.0)
);
assert_eq!(exact_pow(3.0, 2.0, z(9.0), true), Some(9.0));
let r = exact_pow(3.0, 34.0, z(16_677_181_699_666_569.0), false);
assert_eq!(r, Some(16_677_181_699_666_568.0));
assert_eq!(exact_pow(3.0, 0.5, z(1.732_050_807_568_877_2), false), None);
assert_eq!(exact_pow(3.0, 40.0, z(1.0e19), false), None);
}
}