-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
There are a lot of times when encoding AArch64 instructions that we need to represent an integer value with a custom fixed width. For example, the offset for a B instruction is 26 bits, so we store an i32 on the instruction struct and then mask it when we encode. We've been doing this masking everywhere, which has worked, but it's getting a bit copy-pasty all over the place. This commit centralizes that logic to make sure we stay consistent.
- Loading branch information
Showing
12 changed files
with
110 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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.into(); | ||
assert_eq!(0b0101, result); | ||
} | ||
|
||
#[test] | ||
fn test_truncate_imm_negative() { | ||
let inst = truncate_imm::<i32, 4>(-5); | ||
let result: u32 = inst.into(); | ||
assert_eq!(0b1011, result); | ||
} | ||
|
||
#[test] | ||
fn test_truncate_uimm() { | ||
let inst = truncate_uimm::<u32, 4>(5); | ||
let result: u32 = inst.into(); | ||
assert_eq!(0b0101, result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters