use crate::cold::{self, Return};
use crate::format::{Class, Format, Magnitude};
use crate::nan::{self, NanPolicy};
use crate::result::unfinished;
use crate::round::UnroundedMagnitude;
use crate::{Computed, Double, Extended, Flags, Single};
impl Single {
pub fn sqrt<N: nan::NanPolicy<Single>>(self) -> Computed<Single> {
sqrt::<Single, N>(self, single)
}
}
impl Double {
pub fn sqrt<N: nan::NanPolicy<Double>>(self) -> Computed<Double> {
sqrt::<Double, N>(self, double)
}
}
impl Extended {
pub fn sqrt<N: nan::NanPolicy<Extended>>(self) -> Computed<Extended> {
sqrt::<Extended, N>(self, extended)
}
}
#[inline]
fn sqrt<T: Format, N: NanPolicy<T>>(
x: T,
kernel: impl FnOnce(Magnitude) -> UnroundedMagnitude,
) -> Computed<T> {
if let Some((false, magnitude)) = x.finite() {
let root = kernel(magnitude);
return unfinished(false, root.exponent, root.significand, root.extra);
}
slow::<T, N>(x).into_computed()
}
#[cold]
#[inline(never)]
fn slow<T: Format, N: NanPolicy<T>>(x: T) -> Return {
let a = x.decode();
match a.class {
Class::Nan => {
let (value, flags) = N::quiet(x);
Return::finished(value, flags)
}
Class::Zero => Return::finished(x, Flags::NONE),
_ if a.negative => cold::invalid::<T, N>(),
_ => Return::finished(x, Flags::NONE),
}
}
const SEED: [u16; 1024] = seed_table();
const fn seed_table() -> [u16; 1024] {
let mut table = [0u16; 1024];
let mut i = 256u64;
while i < 1024 {
let wide = ((1u64 << 63) / (2 * i + 1)).isqrt();
table[i as usize] = ((wide + (1 << 10)) >> 11) as u16;
i += 1;
}
table
}
#[inline]
fn extended(operand: Magnitude) -> UnroundedMagnitude {
let odd = (operand.exponent & 1) as u32;
let x = operand.significand >> (1 - odd);
let low = (operand.significand << 63) & u64::from(odd).wrapping_sub(1);
let x_high = x >> 32;
let y0 = u64::from(SEED[(x >> 54) as usize]);
let y1 = ((3 * y0) << 15) - ((y0 * y0 * ((y0 * x_high) >> 16)) >> 31);
let square = y1 * y1;
let residual = ((x >> 23).wrapping_mul(square >> 23).wrapping_neg() as i64) >> 32;
let y2 = ((y1 << 8) as i64 + ((y1 as i64 * residual) >> 41)) as u64;
let g1 = (x_high * y1) >> 30;
let d1 = ((x << 2) | (low >> 62)).wrapping_sub(g1.wrapping_mul(g1)) as i64;
let g = ((g1 << 6) as i64 + (((d1 >> 20) * y1 as i64) >> 39)) as u64;
let d = ((x << 14) | (low >> 50)).wrapping_sub(g.wrapping_mul(g)) as i64;
let rounded = d * (y2 >> 20) as i64 + (1 << 31) + ((d * (y2 & ((1 << 20) - 1)) as i64) >> 20);
let fourths = (rounded >> 32) as u64;
let b = (g << 27).wrapping_add(fourths);
let high = (g << 25).wrapping_add((rounded >> 34) as u64);
let quarters = fourths & 3;
let l = b.wrapping_mul(b).wrapping_neg();
let w = rounded as u64 & 0xFFFF_FFFF;
let near = u64::from(w.wrapping_sub(0x7800_0000) < 0x1000_0000);
let far_below = (near ^ 1) & ((w >> 31) ^ 1);
let near_below = near & (l >> 63);
let exact = near & u64::from(l == 0);
let borrow = u64::from(quarters == 0);
UnroundedMagnitude {
exponent: operand.exponent >> 1,
significand: high - (far_below & borrow) - (near_below & borrow),
extra: (quarters.wrapping_sub(far_below | near_below) & 3) << 62 | (exact ^ 1),
}
}
#[inline]
fn double(operand: Magnitude) -> UnroundedMagnitude {
let odd = (operand.exponent & 1) as u32;
let x = operand.significand >> (1 - odd);
let x_high = x >> 32;
let y0 = u64::from(SEED[(x >> 54) as usize]);
let y1 = ((3 * y0) << 15) - ((y0 * y0 * ((y0 * x_high) >> 16)) >> 31);
let g1 = (x_high * y1) >> 30;
let d1 = (x << 2).wrapping_sub(g1.wrapping_mul(g1)) as i64;
let g = ((g1 << 6) as i64 + (((d1 >> 20) * y1 as i64) >> 39)) as u64;
let d = (x << 14).wrapping_sub(g.wrapping_mul(g)) as i64;
let estimate = (g << 25).wrapping_add((((d >> 12) * y1 as i64) >> 34) as u64);
let b = estimate.wrapping_add(256 + 64) >> 9;
let v = (x << 46).wrapping_sub(b.wrapping_mul(b));
UnroundedMagnitude {
exponent: operand.exponent >> 1,
significand: ((b - (v >> 63)) << 9) | u64::from(v != 0),
extra: 0,
}
}
#[inline]
fn single(operand: Magnitude) -> UnroundedMagnitude {
let odd = (operand.exponent & 1) as u32;
let x = operand.significand >> (1 - odd);
let x_high = x >> 32;
let y0 = u64::from(SEED[(x >> 54) as usize]);
let y1 = ((3 * y0) << 15) - ((y0 * y0 * ((y0 * x_high) >> 16)) >> 31);
let g1 = (x_high * y1) >> 30;
let d1 = (x << 2).wrapping_sub(g1.wrapping_mul(g1)) as i64;
let g = ((g1 << 6) as i64 + (((d1 >> 20) * y1 as i64) >> 39)) as u64;
let b = (g + (1 << 12)) >> 13;
let v = (x >> 12).wrapping_sub(b * b);
UnroundedMagnitude {
exponent: operand.exponent >> 1,
significand: ((b - (v >> 63)) << 38) | u64::from(v != 0),
extra: 0,
}
}