summaryrefslogtreecommitdiff
path: root/yjit/src/asm/arm64/arg/truncate.rs
blob: 85d56ff202693e576be90f87fb400f9b4d974c38 (plain)
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
57
58
59
60
61
62
63
64
65
66
// There are many instances in AArch64 instruction encoding where you represent
// an integer value with a particular bit width that isn't a power of 2. These
// functions represent truncating those integer values down to the appropriate
// number of bits.

/// Truncate a signed immediate to fit into a compile-time known width. It is
/// assumed before calling this function that the value fits into the correct
/// size. If it doesn't, then this function will panic.
///
/// When the value is positive, this should effectively be a no-op since we're
/// just dropping leading zeroes. When the value is negative we should only be
/// dropping leading ones.
pub fn truncate_imm<T: Into<i32>, const WIDTH: usize>(imm: T) -> u32 {
    let value: i32 = imm.into();
    let masked = (value as u32) & ((1 << WIDTH) - 1);

    // Assert that we didn't drop any bits by truncating.
    if value >= 0 {
        assert_eq!(value as u32, masked);
    } else {
        assert_eq!(value as u32, masked | (u32::MAX << WIDTH));
    }

    masked
}

/// Truncate an unsigned immediate to fit into a compile-time known width. It is
/// assumed before calling this function that the value fits into the correct
/// size. If it doesn't, then this function will panic.
///
/// This should effectively be a no-op since we're just dropping leading zeroes.
pub fn truncate_uimm<T: Into<u32>, const WIDTH: usize>(uimm: T) -> u32 {
    let value: u32 = uimm.into();
    let masked = value & ((1 << WIDTH) - 1);

    // Assert that we didn't drop any bits by truncating.
    assert_eq!(value, masked);

    masked
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_truncate_imm_positive() {
        let inst = truncate_imm::<i32, 4>(5);
        let result: u32 = inst;
        assert_eq!(0b0101, result);
    }

    #[test]
    fn test_truncate_imm_negative() {
        let inst = truncate_imm::<i32, 4>(-5);
        let result: u32 = inst;
        assert_eq!(0b1011, result);
    }

    #[test]
    fn test_truncate_uimm() {
        let inst = truncate_uimm::<u32, 4>(5);
        let result: u32 = inst;
        assert_eq!(0b0101, result);
    }
}