use core::cmp::Ordering;
use crate::float::{Float128, SIGN_MASK};
impl PartialEq for Float128 {
fn eq(&self, other: &Self) -> bool {
if self.is_nan() || other.is_nan() {
return false;
}
if self.is_zero() && other.is_zero() {
return true;
}
self.to_bits() == other.to_bits()
}
}
impl PartialOrd for Float128 {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.is_nan() || other.is_nan() {
return None;
}
if self.is_zero() && other.is_zero() {
return Some(Ordering::Equal);
}
Some(ordered_key(*self).cmp(&ordered_key(*other)))
}
}
impl Float128 {
#[must_use]
pub fn total_cmp(self, other: Self) -> Ordering {
ordered_key(self).cmp(&ordered_key(other))
}
#[must_use]
pub fn total_max(self, other: Self) -> Self {
if self.total_cmp(other) == Ordering::Less {
other
} else {
self
}
}
#[must_use]
pub fn total_min(self, other: Self) -> Self {
if self.total_cmp(other) == Ordering::Greater {
other
} else {
self
}
}
}
fn ordered_key(value: Float128) -> u128 {
let bits = value.to_bits();
if bits & SIGN_MASK == 0 {
bits | SIGN_MASK
} else {
!bits
}
}