22
33use super :: LibfuncHelper ;
44use crate :: {
5- error:: Result ,
5+ error:: { panic:: ToNativeAssertError , Result } ,
6+ execution_result:: RANGE_CHECK_BUILTIN_SIZE ,
67 metadata:: MetadataStorage ,
78 native_assert,
89 types:: TypeBuilder ,
@@ -12,7 +13,8 @@ use cairo_lang_sierra::{
1213 extensions:: {
1314 bounded_int:: {
1415 BoundedIntConcreteLibfunc , BoundedIntConstrainConcreteLibfunc ,
15- BoundedIntDivRemConcreteLibfunc , BoundedIntTrimConcreteLibfunc ,
16+ BoundedIntDivRemAlgorithm , BoundedIntDivRemConcreteLibfunc ,
17+ BoundedIntTrimConcreteLibfunc ,
1618 } ,
1719 core:: { CoreLibfunc , CoreType } ,
1820 lib_func:: SignatureOnlyConcreteLibfunc ,
@@ -453,8 +455,6 @@ fn build_divrem<'ctx, 'this>(
453455 _metadata : & mut MetadataStorage ,
454456 info : & BoundedIntDivRemConcreteLibfunc ,
455457) -> Result < ( ) > {
456- let range_check = super :: increment_builtin_counter ( context, entry, location, entry. arg ( 0 ) ?) ?;
457-
458458 let lhs_value = entry. arg ( 1 ) ?;
459459 let rhs_value = entry. arg ( 2 ) ?;
460460
@@ -482,6 +482,12 @@ fn build_divrem<'ctx, 'this>(
482482 rhs_range. zero_based_bit_width ( )
483483 } ;
484484
485+ let div_rem_algorithm = BoundedIntDivRemAlgorithm :: try_new ( & lhs_range, & rhs_range)
486+ . to_native_assert_error ( & format ! (
487+ "div_rem of ranges: lhs = {:#?} and rhs= {:#?} is not supported yet" ,
488+ & lhs_range, & rhs_range
489+ ) ) ?;
490+
485491 // Calculate the computation range.
486492 let compute_range = Range {
487493 lower : ( & lhs_range. lower )
@@ -583,6 +589,32 @@ fn build_divrem<'ctx, 'this>(
583589 rem_value
584590 } ;
585591
592+ // Increase range check builtin by 3, regardless of `div_rem_algorithm`:
593+ // https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/bounded.rs#L100
594+ let range_check = match div_rem_algorithm {
595+ BoundedIntDivRemAlgorithm :: KnownSmallRhs => crate :: libfuncs:: increment_builtin_counter_by (
596+ context,
597+ entry,
598+ location,
599+ entry. arg ( 0 ) ?,
600+ 3 * RANGE_CHECK_BUILTIN_SIZE ,
601+ ) ?,
602+ BoundedIntDivRemAlgorithm :: KnownSmallQuotient { .. }
603+ | BoundedIntDivRemAlgorithm :: KnownSmallLhs { .. } => {
604+ // If `div_rem_algorithm` is `KnownSmallQuotient` or `KnownSmallLhs`, increase range check builtin by 1.
605+ //
606+ // Case KnownSmallQuotient: https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/bounded.rs#L129
607+ // Case KnownSmallLhs: https://github.com/starkware-libs/cairo/blob/v2.12.0-dev.1/crates/cairo-lang-sierra-to-casm/src/invocations/int/bounded.rs#L157
608+ crate :: libfuncs:: increment_builtin_counter_by (
609+ context,
610+ entry,
611+ location,
612+ entry. arg ( 0 ) ?,
613+ 4 * RANGE_CHECK_BUILTIN_SIZE ,
614+ ) ?
615+ }
616+ } ;
617+
586618 helper. br ( entry, 0 , & [ range_check, div_value, rem_value] , location)
587619}
588620
@@ -805,7 +837,7 @@ fn build_wrap_non_zero<'ctx, 'this>(
805837 "value must not be zero"
806838 ) ;
807839
808- super :: build_noop :: < 1 , true > (
840+ super :: build_noop :: < 1 , false > (
809841 context,
810842 registry,
811843 entry,
0 commit comments