#![deny(
missing_docs,
missing_debug_implementations,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces,
unused_qualifications
)]
use std::cmp::Ordering;
use std::fmt;
mod convert;
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct F32(u32);
impl F32 {
#[inline]
pub fn from_bits(value: u32) -> Self {
Self(value)
}
#[inline]
pub fn to_bits(self) -> u32 {
self.0
}
#[inline]
pub fn from_be_bytes(bytes: [u8; 4]) -> Self {
Self(u32::from_be_bytes(bytes))
}
#[inline]
pub fn to_be_bytes(self) -> [u8; 4] {
self.0.to_be_bytes()
}
}
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct F64(u64);
impl F64 {
#[inline]
pub fn from_bits(value: u64) -> Self {
Self(value)
}
#[inline]
pub fn to_bits(self) -> u64 {
self.0
}
#[inline]
pub fn from_be_bytes(bytes: [u8; 8]) -> Self {
Self(u64::from_be_bytes(bytes))
}
#[inline]
pub fn to_be_bytes(self) -> [u8; 8] {
self.0.to_be_bytes()
}
}
macro_rules! float {
($t:ty) => {
impl fmt::Debug for $t {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f64::from(*self).fmt(f)
}
}
impl PartialEq for $t {
fn eq(&self, other: &Self) -> bool {
f64::from(*self).eq(&f64::from(*other))
}
}
impl PartialOrd for $t {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
f64::from(*self).partial_cmp(&f64::from(*other))
}
}
};
}
float!(F32);
float!(F64);
impl From<F32> for f32 {
#[inline]
fn from(v: F32) -> Self {
f32::from_bits(convert::ibm32ieee32(v.0))
}
}
impl From<F32> for f64 {
#[inline]
fn from(v: F32) -> Self {
f64::from_bits(convert::ibm32ieee64(v.0))
}
}
impl From<F64> for f32 {
#[inline]
fn from(v: F64) -> Self {
f32::from_bits(convert::ibm64ieee32(v.0))
}
}
impl From<F64> for f64 {
#[inline]
fn from(v: F64) -> Self {
f64::from_bits(convert::ibm64ieee64(v.0))
}
}