use core::hint::select_unpredictable;
use crate::cold::{self, Return};
use crate::format::{Class, Format};
use crate::nan::{self, NanPolicy};
use crate::result::unfinished;
use crate::{Computed, Double, Extended, Flags, Rounding, Single};
macro_rules! impl_addition {
($format:ty) => {
impl $format {
pub fn add<N: nan::NanPolicy<$format>>(
self,
addend: $format,
rounding: Rounding,
) -> Computed<$format> {
add::<$format, N>(self, addend, false, rounding)
}
pub fn subtract<N: nan::NanPolicy<$format>>(
self,
subtrahend: $format,
rounding: Rounding,
) -> Computed<$format> {
add::<$format, N>(self, subtrahend, true, rounding)
}
}
};
}
impl_addition!(Single);
impl_addition!(Double);
impl_addition!(Extended);
trait Addition: Format {
fn inline(self, other: Self, _: bool) -> bool {
self.is_normal() & other.is_normal()
}
#[inline]
fn sum_inline(
a_negative: bool,
a_exponent: i32,
a: u64,
b_negative: bool,
b_exponent: i32,
b: u64,
) -> Option<(bool, i32, u64, u64)> {
sum64(a_negative, a_exponent, a, b_negative, b_exponent, b)
}
#[inline(always)]
fn sum(
a_negative: bool,
a_exponent: i32,
a: u64,
b_negative: bool,
b_exponent: i32,
b: u64,
) -> Option<(bool, i32, u64, u64)> {
sum64(a_negative, a_exponent, a, b_negative, b_exponent, b)
}
}
impl Addition for Single {}
impl Addition for Double {}
impl Addition for Extended {
#[inline]
fn inline(self, other: Extended, subtract: bool) -> bool {
let (a, b) = (
self.sign_exponent,
other.sign_exponent ^ u16::from(subtract) << 15,
);
let mask = Extended::EXPONENT_MASK;
let cancels = near(
a >> 15 != 0,
i32::from(a & mask),
b >> 15 != 0,
i32::from(b & mask),
);
let integers = (self.significand & other.significand) >> 63 != 0;
let fields =
((a & mask).wrapping_sub(1) < mask - 1) & ((b & mask).wrapping_sub(1) < mask - 1);
integers & fields & !cancels
}
#[inline]
fn sum_inline(
a_negative: bool,
a_exponent: i32,
a: u64,
b_negative: bool,
b_exponent: i32,
b: u64,
) -> Option<(bool, i32, u64, u64)> {
far128(a_negative, a_exponent, a, b_negative, b_exponent, b)
}
#[inline(always)]
fn sum(
a_negative: bool,
a_exponent: i32,
a: u64,
b_negative: bool,
b_exponent: i32,
b: u64,
) -> Option<(bool, i32, u64, u64)> {
sum128(a_negative, a_exponent, a, b_negative, b_exponent, b)
}
}
#[inline(always)]
fn far128(
a_negative: bool,
a_exponent: i32,
a: u64,
b_negative: bool,
b_exponent: i32,
b: u64,
) -> Option<(bool, i32, u64, u64)> {
let swap = b_exponent > a_exponent;
let large = select_unpredictable(swap, b, a);
let small = select_unpredictable(swap, a, b);
let exponent = a_exponent.max(b_exponent);
let negative = select_unpredictable(swap, b_negative, a_negative);
let apart = a_exponent.wrapping_sub(b_exponent).unsigned_abs();
let distance = apart.min(127);
let within = distance & 63;
let upper = small >> within;
let spill = (small << 1) << (63 - within);
let beyond = distance >= 64;
let high = if beyond { 0 } else { upper };
let low = if beyond { upper } else { spill };
let lost = u64::from(beyond & (spill != 0));
if a_negative == b_negative {
let (total, carry) = large.overflowing_add(high);
let down = u32::from(carry);
let carried = u64::from(carry).wrapping_neg();
let moved_low = ((total << 63 | low >> 1 | (low & 1)) & carried) | (low & !carried);
return Some((
negative,
exponent + down as i32,
total >> down | u64::from(carry) << 63,
moved_low,
));
}
{
let borrow = u64::from(low != 0);
let low = 0u64.wrapping_sub(low).wrapping_sub(lost) | lost;
let high = large.wrapping_sub(high).wrapping_sub(borrow);
let up = (high >> 63) as u32 ^ 1;
Some((
negative,
exponent - up as i32,
high << up | (low >> 63 & u64::from(up)),
low << up,
))
}
}
fn near(a_negative: bool, a_exponent: i32, b_negative: bool, b_exponent: i32) -> bool {
(a_negative != b_negative) & (a_exponent.wrapping_sub(b_exponent).wrapping_add(1) as u32 <= 2)
}
#[inline(always)]
fn sum128(
a_negative: bool,
a_exponent: i32,
a: u64,
b_negative: bool,
b_exponent: i32,
b: u64,
) -> Option<(bool, i32, u64, u64)> {
if !near(a_negative, a_exponent, b_negative, b_exponent) {
return far128(a_negative, a_exponent, a, b_negative, b_exponent, b);
}
near128(a_negative, b_negative, a_exponent, b_exponent, a, b)
}
#[inline(always)]
fn near128(
a_negative: bool,
b_negative: bool,
a_exponent: i32,
b_exponent: i32,
a: u64,
b: u64,
) -> Option<(bool, i32, u64, u64)> {
let (negative, mut exponent, mut high, mut low) = if a_exponent > b_exponent {
(
a_negative,
a_exponent,
a - (b >> 1) - (b & 1),
(b & 1) << 63,
)
} else if b_exponent > a_exponent {
(
b_negative,
b_exponent,
b - (a >> 1) - (a & 1),
(a & 1) << 63,
)
} else if b > a {
(b_negative, b_exponent, b - a, 0)
} else {
(a_negative, a_exponent, a - b, 0)
};
if high == 0 {
if low == 0 {
return None;
}
high = low;
low = 0;
exponent -= 64;
}
let shift = high.leading_zeros();
Some((
negative,
exponent - shift as i32,
high << shift | (low >> 1) >> (63 - shift),
low << shift,
))
}
#[inline(always)]
fn sum64(
a_negative: bool,
a_exponent: i32,
a: u64,
b_negative: bool,
b_exponent: i32,
b: u64,
) -> Option<(bool, i32, u64, u64)> {
let swap = b_exponent > a_exponent;
let large = select_unpredictable(swap, b, a);
let small = select_unpredictable(swap, a, b);
let exponent = a_exponent.max(b_exponent);
let negative = select_unpredictable(swap, b_negative, a_negative);
let distance = a_exponent.wrapping_sub(b_exponent).unsigned_abs();
let far = distance > 63;
let within = distance & 63;
let shifted = if far { 0 } else { small >> within };
let lost = u64::from(far | (small & ((1u64 << within) - 1) != 0));
if a_negative == b_negative {
let (total, carry) = large.overflowing_add(shifted);
let carry = u32::from(carry);
let moved = total >> carry | (total & u64::from(carry)) | lost | u64::from(carry) << 63;
return Some((negative, exponent + carry as i32, moved, 0));
}
if distance > 1 {
let difference = large.wrapping_sub(shifted).wrapping_sub(lost) | lost;
let shift = (difference >> 63) as u32 ^ 1;
return Some((negative, exponent - shift as i32, difference << shift, 0));
}
let (negative, difference) = if distance == 1 {
(negative, large - shifted)
} else if b > a {
(b_negative, b - a)
} else {
(a_negative, a - b)
};
if difference == 0 {
return None;
}
let shift = difference.leading_zeros();
Some((negative, exponent - shift as i32, difference << shift, 0))
}
#[inline(always)]
fn add<T: Addition, N: NanPolicy<T>>(
x: T,
y: T,
subtract: bool,
rounding: Rounding,
) -> Computed<T> {
if T::inline(x, y, subtract) {
let ((a_negative, a_exponent, a), (b_negative, b_exponent, b)) = (x.normal(), y.normal());
return match T::sum_inline(
a_negative,
a_exponent,
a,
b_negative ^ subtract,
b_exponent,
b,
) {
Some((negative, exponent, significand, extra)) => {
unfinished(negative, exponent, significand, extra)
}
None => Computed::Finished(T::zero(rounding == Rounding::Down), Flags::NONE),
};
}
slow::<T, N>(x, y, subtract, rounding).into_computed()
}
#[cold]
#[inline(never)]
fn slow<T: Addition, N: NanPolicy<T>>(x: T, y: T, subtract: bool, rounding: Rounding) -> Return {
let (a, b) = (x.decode(), y.decode());
let b_negative = b.negative ^ subtract;
match (a.class, b.class) {
(Class::Nan, _) | (_, Class::Nan) => cold::nan::<T, N>(x, y),
(Class::Infinity, Class::Infinity) if a.negative != b_negative => cold::invalid::<T, N>(),
(Class::Infinity, _) => Return::finished(T::infinity(a.negative), Flags::NONE),
(_, Class::Infinity) => Return::finished(T::infinity(b_negative), Flags::NONE),
(Class::Zero, Class::Zero) => {
let negative = if a.negative == b_negative {
a.negative
} else {
rounding == Rounding::Down
};
Return::finished(T::zero(negative), Flags::NONE)
}
(Class::Zero, _) => Return::unfinished(b_negative, b.exponent, b.significand, 0),
(_, Class::Zero) => Return::unfinished(a.negative, a.exponent, a.significand, 0),
_ => match T::sum(
a.negative,
a.exponent,
a.significand,
b_negative,
b.exponent,
b.significand,
) {
Some((negative, exponent, significand, extra)) => {
Return::unfinished(negative, exponent, significand, extra)
}
None => Return::finished(T::zero(rounding == Rounding::Down), Flags::NONE),
},
}
}