1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2018 TiKV Project Authors. Licensed under Apache-2.0.

const SIGN_MARK: u64 = 1 << 63;

#[inline]
pub fn encode_i64_to_comparable_u64(v: i64) -> u64 {
    (v as u64) ^ SIGN_MARK
}

#[inline]
pub fn decode_comparable_u64_to_i64(u: u64) -> i64 {
    (u ^ SIGN_MARK) as i64
}

#[inline]
pub fn encode_f64_to_comparable_u64(v: f64) -> u64 {
    let u: u64 = v.to_bits();
    if v.is_sign_positive() {
        u | SIGN_MARK
    } else {
        !u
    }
}

#[inline]
pub fn decode_comparable_u64_to_f64(u: u64) -> f64 {
    let u = if u & SIGN_MARK > 0 {
        u & (!SIGN_MARK)
    } else {
        !u
    };
    f64::from_bits(u)
}

#[test]
fn test_encode_i64_to_comparable_u64() {
    assert_eq!(encode_i64_to_comparable_u64(1 << 63), 0);
}

#[test]
fn test_decode_comparable_u64_to_i64() {
    assert_eq!(decode_comparable_u64_to_i64((1 << 63) + 10), 10);
}

#[test]
fn test_encode_f64_to_comparable_u64() {
    assert_eq!(encode_f64_to_comparable_u64(0.0), !(1 << 63) + 1);
    assert_eq!(encode_f64_to_comparable_u64(-0.0), !(1 << 63));
}

#[test]
#[allow(clippy::float_cmp)]
fn test_decode_comparable_u64_to_f64() {
    assert_eq!(decode_comparable_u64_to_f64(1 << 63), 0.0);
    assert_ne!(decode_comparable_u64_to_f64(0), 0.0);
}