Skip to content

Commit

Permalink
fails only on even 85 shifts
Browse files Browse the repository at this point in the history
  • Loading branch information
wzmuda committed Feb 8, 2025
1 parent 5c2acc7 commit c64abd4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
32 changes: 16 additions & 16 deletions compiler-rt/src/alu/sshl_sat.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod sshl_sat_i8;

use crate::utils::{assert_fits_in_type, negate_twos_complement};
use crate::utils::{assert_fits_in_type, negate_twos_complement, extend_sign};
use crate::alu::shl::shl;
use core::num::traits::{BitSize, Bounded};

Expand Down Expand Up @@ -54,31 +54,31 @@ fn sshl_sat<
// Check if the shifted value is negative
let sign_bit_mask = shl::<u128>(1, bit_size - 1);
let shifted_sign_bit = (shifted & sign_bit_mask) != 0;
let n_sign_bit = (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 (shifted > max_value, shifted_sign_bit) {
(true, _) => {
// The value after shifting exceeds the maximum value of the signed
// type. Clamp to the maximum possible value.
let result = match (n_sign_bit, shifted_sign_bit) {
(false, false) => {
println!("shifted {:x} extend_sign(shifted) {:x}, negate_twos_complement(shifted) {:x}, negate_twos_complement(extend_sign(shifted)) {:x}", shifted, extend_sign(shifted, sign_bit_mask), negate_twos_complement(shifted), negate_twos_complement(extend_sign(shifted, sign_bit_mask)));
shifted
},
(false, true) => {
max_value
},
(_, true) => {
// The value after shifting is negative. Since we're operating in
// u128, calculate compare its absolute value agains the minimum
// allowed value.
let shifted_abs = negate_twos_complement(shifted);
if shifted_abs > min_value {
// The value after shifting exceeds the minimum value of the
// signed type. Clamp to the minimum possible value.
min_value
} else {
(true, false) => {
min_value
},
(true, true) => {
println!("shifted {:x} extend_sign(shifted) {:x}, negate_twos_complement(shifted) {:x}, negate_twos_complement(extend_sign(shifted)) {:x}", shifted, extend_sign(shifted, sign_bit_mask), negate_twos_complement(shifted), negate_twos_complement(extend_sign(shifted, sign_bit_mask)));
if (extend_sign(shifted, shl::<u128>(1, bit_size - 1 + shift)) & ~Bounded::<T>::MAX.into()) == ~Bounded::<T>::MAX.into() {
shifted
} else {
min_value
}
},
_ => shifted,
};

result & Bounded::<T>::MAX.into()
Expand Down
1 change: 0 additions & 1 deletion compiler-rt/src/alu/sshl_sat/sshl_sat_i8.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ mod tests {
#[should_panic(expected: "Requested shift by more bits than input word size")]
fn test_i8_panic() {
for case in test_cases_panic.span() {
println!("lhs {} rhs {} expected {}", *case.lhs, *case.rhs, *case.expected);
assert_eq!(__llvm_sshl_sat_i8_i8(*case.lhs, *case.rhs), *case.expected);
}
}
Expand Down

0 comments on commit c64abd4

Please sign in to comment.