use std::cmp::Ordering;
use std::convert::TryInto;
use crate::compressor::Compressor;
use crate::decompressor::Decompressor;
use crate::types::NumberLike;
const SIGN_BIT_MASK: u32 = 1_u32 << 31;
fn f32_to_u32(x: f32) -> u32 {
let mem_layout_x_u32 = x.to_bits();
if mem_layout_x_u32 & SIGN_BIT_MASK > 0 {
!mem_layout_x_u32
} else {
mem_layout_x_u32 ^ SIGN_BIT_MASK
}
}
fn from_u32(x: u32) -> f32 {
if x & SIGN_BIT_MASK > 0 {
f32::from_bits(x ^ SIGN_BIT_MASK)
} else {
f32::from_bits(!x)
}
}
impl NumberLike for f32 {
const HEADER_BYTE: u8 = 6;
const PHYSICAL_BITS: usize = 32;
type Diff = u32;
fn num_eq(&self, other: &f32) -> bool {
self.to_bits() == other.to_bits()
}
fn num_cmp(&self, other: &f32) -> Ordering {
f32_to_u32(*self).cmp(&f32_to_u32(*other))
}
fn offset_diff(upper: f32, lower: f32) -> u32 {
f32_to_u32(upper) - f32_to_u32(lower)
}
fn add_offset(lower: f32, off: u32) -> f32 {
from_u32(f32_to_u32(lower) + off)
}
fn bytes_from(num: f32) -> Vec<u8> {
num.to_be_bytes().to_vec()
}
fn from_bytes(bytes: Vec<u8>) -> f32 {
f32::from_be_bytes(bytes.try_into().unwrap())
}
}
pub type F32Compressor = Compressor<f32>;
pub type F32Decompressor = Decompressor<f32>;