Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions compiler/noirc_evaluator/src/ssa/ir/instruction/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ pub(super) fn simplify_cast(
SimplifiedTo(value)
}
(
Type::Numeric(NumericType::Unsigned { .. }),
Type::Numeric(NumericType::Unsigned { .. } | NumericType::Signed { .. }),
Type::Numeric(NumericType::NativeField),
) => {
// Unsigned -> Field: redefine same constant as Field
// Unsigned/Signed -> Field: redefine same constant as Field
SimplifiedTo(dfg.make_constant(constant, dst_typ.clone()))
}
(
Expand All @@ -48,6 +48,24 @@ pub(super) fn simplify_cast(
let truncated = FieldElement::from_be_bytes_reduce(&truncated.to_bytes_be());
SimplifiedTo(dfg.make_constant(truncated, dst_typ.clone()))
}
(
Type::Numeric(
NumericType::NativeField
| NumericType::Unsigned { .. }
| NumericType::Signed { .. },
),
Type::Numeric(NumericType::Signed { bit_size }),
) => {
// Field/Unsigned -> signed
// We only simplify to signed when we are below the maximum signed integer of the destination type.
let integer_modulus = BigUint::from(2u128).pow(*bit_size - 1);
let constant_uint: BigUint = BigUint::from_bytes_be(&constant.to_be_bytes());
if constant_uint < integer_modulus {
SimplifiedTo(dfg.make_constant(constant, dst_typ.clone()))
} else {
None
}
}
_ => None,
}
} else if *dst_typ == dfg.type_of_value(value) {
Expand Down