-
Notifications
You must be signed in to change notification settings - Fork 14
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
10 changed files
with
94 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// 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 | ||
// structs represent the signed and unsigned versions of this kind of encoding, | ||
// and provide Into<u32> to allow the individual instruction encoding functions | ||
// to use them easily. | ||
|
||
/// Shorten a signed immediate to fit into a compile-time known width. | ||
pub fn fix_signed_immediate<const WIDTH: usize, T: Into<i32>>(value: T) -> u32 { | ||
let value: i32 = value.into(); | ||
(value as u32) & ((1 << WIDTH) - 1) | ||
} | ||
|
||
/// Shorten an unsigned immediate to fit into a compile-time known width. | ||
pub fn fix_unsigned_immediate<const WIDTH: usize, T: Into<u32>>(value: T) -> u32 { | ||
let value: u32 = value.into(); | ||
(value & ((1 << WIDTH) - 1)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_fix_signed_immediate_positive() { | ||
let inst = fix_signed_immediate::<4, i32>(5); | ||
let result: u32 = inst.into(); | ||
assert_eq!(0b0101, result); | ||
} | ||
|
||
#[test] | ||
fn test_fix_signed_immediate_negative() { | ||
let inst = fix_signed_immediate::<4, i32>(-5); | ||
let result: u32 = inst.into(); | ||
assert_eq!(0b1011, result); | ||
} | ||
|
||
#[test] | ||
fn test_fix_signed_immediate_truncated() { | ||
let inst = fix_signed_immediate::<4, i32>(-23); | ||
let result: u32 = inst.into(); | ||
assert_eq!(0b1001, result); | ||
} | ||
|
||
#[test] | ||
fn test_fix_unsigned_immediate() { | ||
let inst = fix_unsigned_immediate::<4, u32>(5); | ||
let result: u32 = inst.into(); | ||
assert_eq!(0b0101, result); | ||
} | ||
|
||
#[test] | ||
fn test_fix_unsigned_immediate_truncated() { | ||
let inst = fix_unsigned_immediate::<4, u32>(23); | ||
let result: u32 = inst.into(); | ||
assert_eq!(0b0111, 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