Coverage Report

Created: 2024-10-10 15:24

/home/runner/actions-runner/_work/fuel-vm/fuel-vm/fuel-asm/src/unpack.rs
Line
Count
Source
1
//! Functions for unpacking instruction data from bytes or u32s.
2
3
use crate::{
4
    Imm06,
5
    Imm12,
6
    Imm18,
7
    Imm24,
8
    RegId,
9
};
10
11
208k
pub(super) fn rd_from_bytes(bs: [u8; 3]) -> RegId {
12
208k
    rd_from_u32(u32::from_be_bytes(u8x4_from_u8x3(bs)))
13
208k
}
14
15
9.21k
pub(super) fn imm06_from_bytes(bs: [u8; 3]) -> Imm06 {
16
9.21k
    imm06_from_u32(u32::from_be_bytes(u8x4_from_u8x3(bs)))
17
9.21k
}
18
19
309k
pub(super) fn imm12_from_bytes(bs: [u8; 3]) -> Imm12 {
20
309k
    imm12_from_u32(u32::from_be_bytes(u8x4_from_u8x3(bs)))
21
309k
}
22
23
495k
pub(super) fn imm18_from_bytes(bs: [u8; 3]) -> Imm18 {
24
495k
    imm18_from_u32(u32::from_be_bytes(u8x4_from_u8x3(bs)))
25
495k
}
26
27
202k
pub(super) fn imm24_from_bytes(bs: [u8; 3]) -> Imm24 {
28
202k
    imm24_from_u32(u32::from_be_bytes(u8x4_from_u8x3(bs)))
29
202k
}
30
31
2.37k
pub(super) fn ra_rb_from_bytes(bs: [u8; 3]) -> (RegId, RegId) {
32
2.37k
    (ra_from_bytes(bs), rb_from_bytes(bs))
33
2.37k
}
34
35
7.99k
pub(super) fn ra_rb_rc_from_bytes(bs: [u8; 3]) -> (RegId, RegId, RegId) {
36
7.99k
    (ra_from_bytes(bs), rb_from_bytes(bs), rc_from_bytes(bs))
37
7.99k
}
38
39
208k
pub(super) fn ra_rb_rc_rd_from_bytes(bs: [u8; 3]) -> (RegId, RegId, RegId, RegId) {
40
208k
    (
41
208k
        ra_from_bytes(bs),
42
208k
        rb_from_bytes(bs),
43
208k
        rc_from_bytes(bs),
44
208k
        rd_from_bytes(bs),
45
208k
    )
46
208k
}
47
48
9.21k
pub(super) fn ra_rb_rc_imm06_from_bytes(bs: [u8; 3]) -> (RegId, RegId, RegId, Imm06) {
49
9.21k
    (
50
9.21k
        ra_from_bytes(bs),
51
9.21k
        rb_from_bytes(bs),
52
9.21k
        rc_from_bytes(bs),
53
9.21k
        imm06_from_bytes(bs),
54
9.21k
    )
55
9.21k
}
56
57
309k
pub(super) fn ra_rb_imm12_from_bytes(bs: [u8; 3]) -> (RegId, RegId, Imm12) {
58
309k
    (ra_from_bytes(bs), rb_from_bytes(bs), imm12_from_bytes(bs))
59
309k
}
60
61
495k
pub(super) fn ra_imm18_from_bytes(bs: [u8; 3]) -> (RegId, Imm18) {
62
495k
    (ra_from_bytes(bs), imm18_from_bytes(bs))
63
495k
}
64
65
#[allow(clippy::cast_possible_truncation)]
66
1.03M
fn ra_from_u32(u: u32) -> RegId {
67
1.03M
    RegId::new((u >> 18) as u8)
68
1.03M
}
69
70
#[allow(clippy::cast_possible_truncation)]
71
537k
fn rb_from_u32(u: u32) -> RegId {
72
537k
    RegId::new((u >> 12) as u8)
73
537k
}
74
75
#[allow(clippy::cast_possible_truncation)]
76
225k
fn rc_from_u32(u: u32) -> RegId {
77
225k
    RegId::new((u >> 6) as u8)
78
225k
}
79
80
#[allow(clippy::cast_possible_truncation)]
81
208k
fn rd_from_u32(u: u32) -> RegId {
82
208k
    RegId::new(u as u8)
83
208k
}
84
85
#[allow(clippy::cast_possible_truncation)]
86
9.21k
fn imm06_from_u32(u: u32) -> Imm06 {
87
9.21k
    Imm06::new(u as u8)
88
9.21k
}
89
90
#[allow(clippy::cast_possible_truncation)]
91
309k
fn imm12_from_u32(u: u32) -> Imm12 {
92
309k
    Imm12::new(u as u16)
93
309k
}
94
95
495k
fn imm18_from_u32(u: u32) -> Imm18 {
96
495k
    Imm18::new(u)
97
495k
}
98
99
202k
fn imm24_from_u32(u: u32) -> Imm24 {
100
202k
    Imm24::new(u)
101
202k
}
102
103
1.03M
pub(super) fn ra_from_bytes(bs: [u8; 3]) -> RegId {
104
1.03M
    ra_from_u32(u32::from_be_bytes(u8x4_from_u8x3(bs)))
105
1.03M
}
106
107
537k
pub(super) fn rb_from_bytes(bs: [u8; 3]) -> RegId {
108
537k
    rb_from_u32(u32::from_be_bytes(u8x4_from_u8x3(bs)))
109
537k
}
110
111
225k
pub(super) fn rc_from_bytes(bs: [u8; 3]) -> RegId {
112
225k
    rc_from_u32(u32::from_be_bytes(u8x4_from_u8x3(bs)))
113
225k
}
114
115
// Produce the big-endian bytes for an instruction's data, with a zeroed opcode byte.
116
3.02M
fn u8x4_from_u8x3([a, b, c]: [u8; 3]) -> [u8; 4] {
117
3.02M
    [0, a, b, c]
118
3.02M
}