-
Notifications
You must be signed in to change notification settings - Fork 0
compiler-rt: alu: add saturated shift left for i8 #134 #140
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+458
−76
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
690a5f1
compiler-rt: alu: simplify shl for 128 bit values
wzmuda 62e4f5b
compiler-rt: alu: ashr: simplify test case for too broad shifts
wzmuda e3baba3
compiler-rt: alu: add unsigned saturating shift left for i8
wzmuda cd31da1
compiler-rt: alu: move sign extension to an utilty function
wzmuda e9312da
compiler-rt: alu: add signed saturating shift left for i8
wzmuda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,103 @@ | ||
pub mod sshl_sat_i8; | ||
|
||
use crate::utils::{assert_fits_in_type, extend_sign}; | ||
use crate::alu::shl::shl; | ||
use core::num::traits::{BitSize, Bounded}; | ||
|
||
// Perform the `sshl_sat` operation. | ||
// | ||
// This function performs signed saturating shift left. It behaves like a regular | ||
// bitwise shift left with the additional behavior of: | ||
// - clamping the output to the minimum possible value of a type, if the shifted | ||
// value is less than the minimum possible value of a type. | ||
// - clamping the output to the maximum possible value of a type, if the shifted | ||
// value is larger than the maximum possible value of a type. | ||
// | ||
// The minimum and maximum values are determined with the assumption of the input | ||
// value being a signed number. Therefore the MSB of the type is the sign bit. | ||
// Bitwise, the minimum value is 0b10..00 and the maximum value is 0b01..11. | ||
// | ||
// The shift value cannot be equal higher than the bit width of the concrete type. | ||
// E.g. for `n` being an 8-bit value, the maximum allowed `shift` is 7. In LLVM IR | ||
// shifting by more bits than the bit width of the input value results in returning | ||
// a poison value. As for now, Hieratika support poisons values by panicking. | ||
// | ||
// This is a generic implementation for every data type. Its specialized versions | ||
// are defined and tested in the sshl_sat/sshl_sat_<type>.cairo files. | ||
fn sshl_sat< | ||
iamrecursion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
T, | ||
// The trait bounds are chosen so that: | ||
// | ||
// - BitSize<T>: we can determine the length of the data type in bits, | ||
// - Bounded<T>: we can determine min and max value of the type, | ||
// - TryInto<u128, T>, Into<T, u128> - we can convert the type from/to u128, | ||
// - Destruct<T>: the type can be dropped as the result of the downcasting check. | ||
// | ||
// Overall these trait bounds allow any unsigned integer to be used as the concrete type. | ||
impl TBitSize: BitSize<T>, | ||
impl TBounded: Bounded<T>, | ||
impl TTryInto: TryInto<u128, T>, | ||
impl TInto: Into<T, u128>, | ||
impl TDestruct: Destruct<T>, | ||
>( | ||
n: u128, shift: u128, | ||
) -> u128 { | ||
// Make sure the value passed in the u128 arguments can fit in the concrete type. | ||
assert_fits_in_type::<T>(n); | ||
|
||
// As per the LLVM Language Reference Manual: | ||
// | ||
// If b is (statically or dynamically) equal to or larger than the number of bits in op1, | ||
// this instruction returns a poison value. | ||
// | ||
// As per `docs/ALU Design.md`, poison values cause panics. | ||
let bit_size = BitSize::<T>::bits().into(); | ||
if shift >= bit_size { | ||
panic!("Requested shift by more bits than input word size") | ||
} | ||
|
||
if n == 0 { | ||
return 0; | ||
} | ||
|
||
if shift == 0 { | ||
return n; | ||
} | ||
|
||
let shifted = shl::<u128>(n, shift); | ||
|
||
// Check if the shifted value is negative | ||
let sign_bit_mask = shl::<u128>(1, bit_size - 1); | ||
let is_shifted_negative = (shifted & sign_bit_mask) != 0; | ||
let is_n_negative = (n & sign_bit_mask) != 0; | ||
|
||
// Min/max values of iN | ||
let max_value = sign_bit_mask - 1; | ||
let min_value = sign_bit_mask; | ||
#[cairofmt::skip] | ||
let result = match (is_n_negative, is_shifted_negative) { | ||
(false, false) => { | ||
if shifted > max_value { | ||
max_value | ||
} else { | ||
shifted | ||
} | ||
}, | ||
(false, true) => { | ||
max_value | ||
}, | ||
(true, false) => { | ||
min_value | ||
}, | ||
(true, true) => { | ||
let shifted_sign_bit_mask = shl::<u128>(1, bit_size - 1 + shift); | ||
if extend_sign(shifted, shifted_sign_bit_mask) > extend_sign(min_value, sign_bit_mask) { | ||
shifted | ||
} else { | ||
min_value | ||
} | ||
}, | ||
}; | ||
|
||
result & Bounded::<T>::MAX.into() | ||
} |
This file contains hidden or 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,126 @@ | ||
use crate::alu::sshl_sat::sshl_sat; | ||
|
||
pub fn __llvm_sshl_sat_i8_i8(n: u128, shift: u128) -> u128 { | ||
sshl_sat::<u8>(n, shift) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::__llvm_sshl_sat_i8_i8; | ||
use crate::alu::test_case::TestCaseTwoArgs; | ||
#[cairofmt::skip] | ||
pub const test_cases: [TestCaseTwoArgs; 64] = [ | ||
// All possible shifts of -128 from 0 throughout the whole input value length. | ||
// Since -128 is the lowest possible value, the output saturates | ||
// at the minimum value. | ||
TestCaseTwoArgs { lhs: 0b10000000, rhs: 0, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10000000, rhs: 1, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10000000, rhs: 2, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10000000, rhs: 3, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10000000, rhs: 4, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10000000, rhs: 5, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10000000, rhs: 6, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10000000, rhs: 7, expected: 0b10000000 }, | ||
|
||
// All possible shifts of -86 from 0 throughout the whole input value length. | ||
// -86 << 1 == -86 * 2 == -172 < -128, so the output saturates | ||
// at the minimum value. | ||
TestCaseTwoArgs { lhs: 0b10101010, rhs: 0, expected: 0b10101010 }, | ||
TestCaseTwoArgs { lhs: 0b10101010, rhs: 1, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10101010, rhs: 2, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10101010, rhs: 3, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10101010, rhs: 4, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10101010, rhs: 5, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10101010, rhs: 6, expected: 0b10000000 }, | ||
TestCaseTwoArgs { lhs: 0b10101010, rhs: 7, expected: 0b10000000 }, | ||
|
||
// All possible shifts of -1 from 0 throughout the whole input value length. | ||
// The value is shifted all the way to -128, so the saturation does not | ||
// occur (or it does, but it is s equal to the actual result of the shift). | ||
TestCaseTwoArgs { lhs: 0b11111111, rhs: 0, expected: 0b11111111 }, | ||
TestCaseTwoArgs { lhs: 0b11111111, rhs: 1, expected: 0b11111110 }, | ||
TestCaseTwoArgs { lhs: 0b11111111, rhs: 2, expected: 0b11111100 }, | ||
TestCaseTwoArgs { lhs: 0b11111111, rhs: 3, expected: 0b11111000 }, | ||
TestCaseTwoArgs { lhs: 0b11111111, rhs: 4, expected: 0b11110000 }, | ||
TestCaseTwoArgs { lhs: 0b11111111, rhs: 5, expected: 0b11100000 }, | ||
TestCaseTwoArgs { lhs: 0b11111111, rhs: 6, expected: 0b11000000 }, | ||
TestCaseTwoArgs { lhs: 0b11111111, rhs: 7, expected: 0b10000000 }, | ||
|
||
// All possible shifts of 0 from 0 throughout the whole input value length. | ||
// No saturation because the result is always zero. | ||
TestCaseTwoArgs { lhs: 0b00000000, rhs: 0, expected: 0b00000000 }, | ||
TestCaseTwoArgs { lhs: 0b00000000, rhs: 1, expected: 0b00000000 }, | ||
TestCaseTwoArgs { lhs: 0b00000000, rhs: 2, expected: 0b00000000 }, | ||
TestCaseTwoArgs { lhs: 0b00000000, rhs: 3, expected: 0b00000000 }, | ||
TestCaseTwoArgs { lhs: 0b00000000, rhs: 4, expected: 0b00000000 }, | ||
TestCaseTwoArgs { lhs: 0b00000000, rhs: 5, expected: 0b00000000 }, | ||
TestCaseTwoArgs { lhs: 0b00000000, rhs: 6, expected: 0b00000000 }, | ||
TestCaseTwoArgs { lhs: 0b00000000, rhs: 7, expected: 0b00000000 }, | ||
|
||
// All possible shifts of 1 from 0 throughout the whole input value length. | ||
// 1 << 7 == 128 > 127, so the output saturates at the maximum value. | ||
TestCaseTwoArgs { lhs: 0b00000001, rhs: 0, expected: 0b00000001 }, | ||
TestCaseTwoArgs { lhs: 0b00000001, rhs: 1, expected: 0b00000010 }, | ||
TestCaseTwoArgs { lhs: 0b00000001, rhs: 2, expected: 0b00000100 }, | ||
TestCaseTwoArgs { lhs: 0b00000001, rhs: 3, expected: 0b00001000 }, | ||
TestCaseTwoArgs { lhs: 0b00000001, rhs: 4, expected: 0b00010000 }, | ||
TestCaseTwoArgs { lhs: 0b00000001, rhs: 5, expected: 0b00100000 }, | ||
TestCaseTwoArgs { lhs: 0b00000001, rhs: 6, expected: 0b01000000 }, | ||
TestCaseTwoArgs { lhs: 0b00000001, rhs: 7, expected: 0b01111111 }, | ||
|
||
// All possible shifts of 15 from 0 throughout the whole input value length. | ||
// No saturation up to 15 << 3 == 120 < 127. Saturation at the maximum | ||
// value occurs at 15 << 4 == 240 > 127. | ||
TestCaseTwoArgs { lhs: 0b00001111, rhs: 0, expected: 0b00001111 }, | ||
TestCaseTwoArgs { lhs: 0b00001111, rhs: 1, expected: 0b00011110 }, | ||
TestCaseTwoArgs { lhs: 0b00001111, rhs: 2, expected: 0b00111100 }, | ||
TestCaseTwoArgs { lhs: 0b00001111, rhs: 3, expected: 0b01111000 }, | ||
TestCaseTwoArgs { lhs: 0b00001111, rhs: 4, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b00001111, rhs: 5, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b00001111, rhs: 6, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b00001111, rhs: 7, expected: 0b01111111 }, | ||
|
||
// All possible shifts of 85 from 0 throughout the whole input value length. | ||
// 85 << 1 == 85 * 2 == 170 > 127, so the output saturates | ||
// at the minimum value. | ||
TestCaseTwoArgs { lhs: 0b01010101, rhs: 0, expected: 0b01010101 }, | ||
TestCaseTwoArgs { lhs: 0b01010101, rhs: 1, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01010101, rhs: 2, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01010101, rhs: 3, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01010101, rhs: 4, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01010101, rhs: 5, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01010101, rhs: 6, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01010101, rhs: 7, expected: 0b01111111 }, | ||
|
||
// All possible shifts of 127 from 0 throughout the whole input value length. | ||
// Since 127 is the highest possible value, the output saturates | ||
// at the maximum value. | ||
TestCaseTwoArgs { lhs: 0b01111111, rhs: 0, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01111111, rhs: 1, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01111111, rhs: 2, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01111111, rhs: 3, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01111111, rhs: 4, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01111111, rhs: 5, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01111111, rhs: 6, expected: 0b01111111 }, | ||
TestCaseTwoArgs { lhs: 0b01111111, rhs: 7, expected: 0b01111111 }, | ||
]; | ||
#[test] | ||
fn test_i8() { | ||
for case in test_cases.span() { | ||
assert_eq!(__llvm_sshl_sat_i8_i8(*case.lhs, *case.rhs), *case.expected); | ||
} | ||
} | ||
|
||
// As per the LLVM Language Reference Manual: | ||
// | ||
// If b is (statically or dynamically) equal to or larger than the number of bits in op1, | ||
// this instruction returns a poison value. | ||
// | ||
// As per `docs/ALU Design.md`, poison values cause panics. | ||
#[test] | ||
#[should_panic(expected: "Requested shift by more bits than input word size")] | ||
fn test_i8_panic() { | ||
let case = TestCaseTwoArgs { lhs: 0b11111111, rhs: 8, expected: 0b00000000 }; | ||
assert_eq!(__llvm_sshl_sat_i8_i8(case.lhs, case.rhs), case.expected); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.