modelassetlib_native/util/
bits.rs

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
pub fn u8_to_u32(bits: (u8, u8, u8, u8)) -> u32 {
    let a0 = u32::from(bits.0);
    let a1 = u32::from(bits.1);
    let a2 = u32::from(bits.2);
    let a3 = u32::from(bits.3);

    (a0 << 24) + (a1 << 16) + (a2 << 8) + a3
}

pub fn u32_to_u8(num: u32) -> (u8, u8, u8, u8) {
    let mut num_mut = num;

    let a0 = num_mut & 0xFF;
    num_mut >>= 8;
    let a1 = num_mut & 0xFF;
    num_mut >>= 8;
    let a2 = num_mut & 0xFF;
    num_mut >>= 8;
    let a3 = num_mut;

    (a3 as u8, a2 as u8, a1 as u8, a0 as u8)
}

pub fn i8_to_i32(bits: (i8, i8, i8, i8)) -> i32 {
    u8_to_u32((bits.0 as u8,
               bits.1 as u8,
               bits.2 as u8,
               bits.3 as u8)) as i32
}

pub fn i32_to_i8(num: i32) -> (i8, i8, i8, i8) {
    let result = u32_to_u8(num as u32);
    (result.0 as i8, result.1 as i8, result.2 as i8, result.3 as i8)
}

pub fn u8_to_f32(bits: (u8, u8, u8, u8)) -> f32 {
    f32::from_bits(u8_to_u32(bits))
}

pub fn f32_to_u8(num: f32) -> (u8, u8, u8, u8) {
    u32_to_u8(num.to_bits())
}

pub fn i8_to_f32(bits: (i8, i8, i8, i8)) -> f32 {
    u8_to_f32((bits.0 as u8,
               bits.1 as u8,
               bits.2 as u8,
               bits.3 as u8))
}

pub fn f32_to_i8(num: f32) -> (i8, i8, i8, i8) {
    let result = f32_to_u8(num);
    (result.0 as i8, result.1 as i8,
     result.2 as i8, result.3 as i8)
}