milli_core/facet/
value_encoding.rs1#[inline]
3pub fn f64_into_bytes(float: f64) -> Option<[u8; 8]> {
4 if float.is_finite() {
5 if float == 0.0 || float == -0.0 {
6 return Some(xor_first_bit(0.0_f64.to_be_bytes()));
7 } else if float.is_sign_negative() {
8 return Some(xor_all_bits(float.to_be_bytes()));
9 } else if float.is_sign_positive() {
10 return Some(xor_first_bit(float.to_be_bytes()));
11 }
12 }
13 None
14}
15
16#[inline]
17fn xor_first_bit(mut x: [u8; 8]) -> [u8; 8] {
18 x[0] ^= 0x80;
19 x
20}
21
22#[inline]
23fn xor_all_bits(mut x: [u8; 8]) -> [u8; 8] {
24 x.iter_mut().for_each(|b| *b ^= 0xff);
25 x
26}
27
28#[cfg(test)]
29mod tests {
30 use std::cmp::Ordering::Less;
31
32 use super::*;
33
34 fn is_sorted<T: Ord>(x: &[T]) -> bool {
35 x.windows(2).map(|x| x[0].cmp(&x[1])).all(|o| o == Less)
36 }
37
38 #[test]
39 fn ordered_f64_bytes() {
40 let a = -13_f64;
41 let b = -10.0;
42 let c = -0.0;
43 let d = 1.0;
44 let e = 43.0;
45
46 let vec: Vec<_> = [a, b, c, d, e].iter().cloned().map(f64_into_bytes).collect();
47 assert!(is_sorted(&vec), "{:?}", vec);
48 }
49}