use super::{
Computed, Double, Exceptions, Extended, Precision, Relation, Rounded, Rounding, Single,
Unrounded, X86, indefinite,
};
use crate::nan::NanPolicy;
use crate::{Tininess, round};
pub const DEFAULT_NAN: Extended = Extended {
sign_exponent: 0xFFFF,
significand: 0xC000_0000_0000_0000,
};
#[inline]
pub fn is_signalling_nan(value: Extended) -> bool {
<X86 as NanPolicy<Extended>>::is_signalling(value)
}
#[inline]
pub fn add(a: Extended, b: Extended, rounding: Rounding) -> Computed<Extended> {
a.add::<X86>(b, rounding).map_exceptions(Exceptions::from)
}
#[inline]
pub fn subtract(a: Extended, b: Extended, rounding: Rounding) -> Computed<Extended> {
a.subtract::<X86>(b, rounding)
.map_exceptions(Exceptions::from)
}
#[inline]
pub fn multiply(a: Extended, b: Extended) -> Computed<Extended> {
a.multiply::<X86>(b).map_exceptions(Exceptions::from)
}
#[inline]
pub fn divide(a: Extended, b: Extended) -> Computed<Extended> {
a.divide::<X86>(b).map_exceptions(Exceptions::from)
}
#[inline]
pub fn sqrt(a: Extended) -> Computed<Extended> {
a.sqrt::<X86>().map_exceptions(Exceptions::from)
}
#[inline]
pub fn compare(a: Extended, b: Extended, signalling: bool) -> (Relation, Exceptions) {
let (relation, flags) = a.compare::<X86>(b, signalling);
(relation, Exceptions::from(flags))
}
#[inline]
pub fn propagate_nan(a: Extended, b: Extended) -> (Extended, Exceptions) {
let (value, flags) = a.propagate_nan::<X86>(b);
(value, Exceptions::from(flags))
}
#[inline]
pub fn round(
value: Unrounded<Extended>,
rounding: Rounding,
precision: Precision,
) -> Rounded<Extended> {
value
.round(rounding, precision, Tininess::AfterRounding)
.map_exceptions(Exceptions::from)
}
#[inline]
pub fn round_unmasked(
value: Unrounded<Extended>,
rounding: Rounding,
precision: Precision,
) -> Rounded<Extended> {
let (negative, exponent, significand, extra) = value.parts();
let parts = adjusted(negative, exponent, significand, extra, rounding, precision);
let (overflow, underflow, inexact) = parts.flags;
Rounded {
value: Extended {
sign_exponent: u16::from(negative) << 15 | parts.field,
significand: parts.significand,
},
exceptions: Exceptions::OE.when(overflow)
| Exceptions::UE.when(underflow)
| Exceptions::PE.when(inexact),
rounded_up: parts.rounded_up,
tiny: parts.tiny,
}
}
#[inline]
pub fn to_i16(a: Extended, rounding: Rounding) -> Rounded<i16> {
indefinite(a.to_i16(rounding), i16::MIN)
}
#[inline]
pub fn to_i32(a: Extended, rounding: Rounding) -> Rounded<i32> {
indefinite(a.to_i32(rounding), i32::MIN)
}
#[inline]
pub fn to_i64(a: Extended, rounding: Rounding) -> Rounded<i64> {
indefinite(a.to_i64(rounding), i64::MIN)
}
#[inline]
pub fn round_to_integral(a: Extended, rounding: Rounding) -> Rounded<Extended> {
a.round_to_integral::<X86>(rounding)
.map_exceptions(Exceptions::from)
}
#[inline]
pub fn from_single(value: Single) -> (Extended, Exceptions) {
let (converted, flags) = Extended::from_single::<X86>(value);
(converted, Exceptions::from(flags))
}
#[inline]
pub fn from_double(value: Double) -> (Extended, Exceptions) {
let (converted, flags) = Extended::from_double::<X86>(value);
(converted, Exceptions::from(flags))
}
#[inline]
pub fn to_single(a: Extended) -> Computed<Single> {
a.to_single::<X86>().map_exceptions(Exceptions::from)
}
#[inline]
pub fn to_double(a: Extended) -> Computed<Double> {
a.to_double::<X86>().map_exceptions(Exceptions::from)
}
const ADJUSTMENT: i32 = 24_576;
#[inline]
fn adjusted(
negative: bool,
exponent: i32,
significand: u64,
extra: u64,
rounding: Rounding,
precision: Precision,
) -> round::ExtendedParts<(bool, bool, bool)> {
let (exponent, significand, inexact, up) = round::unbounded(
negative,
exponent,
significand,
extra,
rounding,
precision.discarded(),
);
let overflow = exponent > Extended::MAX_EXPONENT;
let tiny = exponent < Extended::MIN_EXPONENT;
let adjusted = if overflow {
exponent - ADJUSTMENT
} else if tiny {
exponent + ADJUSTMENT
} else {
exponent
};
if adjusted > Extended::MAX_EXPONENT {
round::ExtendedParts {
field: Extended::EXPONENT_MASK,
significand: 1 << 63,
flags: (overflow, tiny, true),
rounded_up: true,
tiny,
}
} else if adjusted < Extended::MIN_EXPONENT {
round::ExtendedParts {
field: 0,
significand: 0,
flags: (overflow, tiny, true),
rounded_up: false,
tiny,
}
} else {
round::ExtendedParts {
field: (adjusted + Extended::BIAS) as u16,
significand,
flags: (overflow, tiny, inexact),
rounded_up: up,
tiny,
}
}
}