use std::cmp;
pub trait ToOrd: Sized + Copy {
type Ord: cmp::Ord;
fn to_ord(self) -> Self::Ord;
#[inline]
fn total_eq(self, rhs: Self) -> bool {
self.to_ord() == rhs.to_ord()
}
}
macro_rules! impl_to_ord {
($f: ty, $u: ty) => {
impl ToOrd for $f {
type Ord = $u;
#[inline]
fn to_ord(self) -> Self::Ord {
const MSB: $u = 1 << (std::mem::size_of::<$f>() * 8 - 1);
let bits = self.to_bits();
if bits & MSB == 0 {
bits | MSB
} else {
if bits << 1 == 0 {
#[cold]
fn zero() -> $u {
MSB }
zero()
}
else {
!bits
}
}
}
#[inline]
fn total_eq(self, rhs: Self) -> bool {
let a = self.to_bits();
let b = rhs.to_bits();
if a << 1 == 0 {
#[cold]
fn zero(b: $u) -> bool {
b << 1 == 0
}
zero(b)
}
else {
a == b
}
}
}
};
}
impl_to_ord!(f32, u32);
impl_to_ord!(f64, u64);