#[inline(always)]
pub const fn encode_f64(val: f64) -> [u8; 8] {
let bits = val.to_bits();
let mask = (((bits as i64) >> 63) as u64) | (1u64 << 63);
(bits ^ mask).to_be_bytes()
}
#[inline(always)]
pub const fn decode_f64(bytes: [u8; 8]) -> f64 {
let sortable = u64::from_be_bytes(bytes);
let mask = ((((!sortable) as i64) >> 63) as u64) | (1u64 << 63);
f64::from_bits(sortable ^ mask)
}
#[inline(always)]
pub const fn encode_f32(val: f32) -> [u8; 4] {
let bits = val.to_bits();
let mask = (((bits as i32) >> 31) as u32) | (1u32 << 31);
(bits ^ mask).to_be_bytes()
}
#[inline(always)]
pub const fn decode_f32(bytes: [u8; 4]) -> f32 {
let sortable = u32::from_be_bytes(bytes);
let mask = ((((!sortable) as i32) >> 31) as u32) | (1u32 << 31);
f32::from_bits(sortable ^ mask)
}
#[inline(always)]
pub fn encode_f64_to_slice(val: f64, dst: &mut [u8]) -> bool {
if let Some(chunk) = dst.first_chunk_mut::<8>() {
*chunk = encode_f64(val);
true
} else {
false
}
}
#[inline(always)]
pub const fn decode_f64_from_slice(src: &[u8]) -> Option<f64> {
match src {
[b0, b1, b2, b3, b4, b5, b6, b7, ..] => {
Some(decode_f64([*b0, *b1, *b2, *b3, *b4, *b5, *b6, *b7]))
}
_ => None,
}
}
#[inline(always)]
pub fn encode_f32_to_slice(val: f32, dst: &mut [u8]) -> bool {
if let Some(chunk) = dst.first_chunk_mut::<4>() {
*chunk = encode_f32(val);
true
} else {
false
}
}
#[inline(always)]
pub const fn decode_f32_from_slice(src: &[u8]) -> Option<f32> {
match src {
[b0, b1, b2, b3, ..] => Some(decode_f32([*b0, *b1, *b2, *b3])),
_ => None,
}
}