Skip to content

Commit

Permalink
Fix integer casting (#605)
Browse files Browse the repository at this point in the history
* Separate variables for better legibility

* Fix proper_cast test
  • Loading branch information
JulianGCalderon authored May 23, 2024
1 parent 0b3fe0a commit b9bf1b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 0 additions & 2 deletions cairo-tests/src/integer_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -1088,9 +1088,7 @@ fn validate_cast_bounds_overlapping<
validate_max_strictly_contained::<A, B>(err);
}

// TODO: fails
#[test]
#[ignore]
fn proper_cast() {
validate_cast_bounds_contained_same_min::<u8, u16>('u8 u16 casts');
validate_cast_bounds_contained_same_min::<u8, u32>('u8 u32 casts');
Expand Down
20 changes: 12 additions & 8 deletions src/libfuncs/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,27 @@ pub fn build_downcast<'ctx, 'this>(
location,
);

let is_signed = src_type.is_integer_signed().ok_or_else(|| {
let src_is_signed = src_type.is_integer_signed().ok_or_else(|| {
Error::SierraAssert("casts always happen between numerical types".to_string())
})? || dst_type.is_integer_signed().ok_or_else(|| {
})?;
let dst_is_signed = dst_type.is_integer_signed().ok_or_else(|| {
Error::SierraAssert("casts always happen between numerical types".to_string())
})?;
let is_felt = matches!(src_type, CoreTypeConcrete::Felt252(_));

let is_signed = src_is_signed || dst_is_signed;
let src_is_felt = matches!(src_type, CoreTypeConcrete::Felt252(_));

let src_value: melior::ir::Value = entry.argument(1)?.into();

let mut block = entry;

let (is_in_range, result) = if src_ty == dst_ty {
let (is_in_range, result) = if info.from_ty == info.to_ty {
let k0 = block.const_int(context, location, 0, 1)?;
(k0, src_value)
} else {
// make unsigned felt into signed felt
// felt > half prime = negative
let src_value = if is_felt {
let src_value = if src_is_felt {
let attr_halfprime_i252 = metadata
.get::<PrimeModuloMeta<Felt>>()
.ok_or(Error::MissingMetadata)?
Expand Down Expand Up @@ -184,7 +187,8 @@ pub fn build_downcast<'ctx, 'this>(
info.to_range
.intersection(&info.from_range)
.ok_or_else(|| Error::SierraAssert("range should always interesct".to_string()))?
.upper,
.upper
- 1,
compare_ty,
)?;

Expand All @@ -201,9 +205,9 @@ pub fn build_downcast<'ctx, 'this>(
let is_in_range_upper = block.append_op_result(arith::cmpi(
context,
if is_signed {
CmpiPredicate::Slt
CmpiPredicate::Sle
} else {
CmpiPredicate::Ult
CmpiPredicate::Ule
},
compare_value,
max_value,
Expand Down

0 comments on commit b9bf1b4

Please sign in to comment.