Skip to content

Commit

Permalink
Removed libfunc bool_eq. (#3406)
Browse files Browse the repository at this point in the history
  • Loading branch information
orizi authored Jun 14, 2023
2 parents 667657a + fec5227 commit 043002a
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 177 deletions.
11 changes: 8 additions & 3 deletions corelib/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ impl BoolBitXor of BitXor<bool> {
}
}

extern fn bool_eq(lhs: bool, rhs: bool) -> bool implicits() nopanic;
impl BoolPartialEq of PartialEq<bool> {
#[inline(always)]
fn eq(lhs: @bool, rhs: @bool) -> bool {
bool_eq(*lhs, *rhs)
match lhs {
bool::False(_) => !*rhs,
bool::True(_) => *rhs,
}
}
#[inline(always)]
fn ne(lhs: @bool, rhs: @bool) -> bool {
!(*lhs == *rhs)
match lhs {
bool::False(_) => *rhs,
bool::True(_) => !*rhs,
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ pub fn core_libfunc_ap_change<InfoProvider: InvocationApChangeInfoProvider>(
BoolConcreteLibfunc::Not(_) => vec![ApChange::Known(1)],
BoolConcreteLibfunc::Xor(_) => vec![ApChange::Known(1)],
BoolConcreteLibfunc::Or(_) => vec![ApChange::Known(2)],
BoolConcreteLibfunc::Equal(_) => vec![ApChange::Known(1), ApChange::Known(1)],
BoolConcreteLibfunc::ToFelt252(_) => vec![ApChange::Known(0)],
},
CoreConcreteLibfunc::Box(libfunc) => match libfunc {
Expand Down
1 change: 0 additions & 1 deletion crates/cairo-lang-sierra-gas/src/core_libfunc_cost_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ pub fn core_libfunc_cost(
BoolConcreteLibfunc::Not(_) => vec![steps(1).into()],
BoolConcreteLibfunc::Xor(_) => vec![steps(1).into()],
BoolConcreteLibfunc::Or(_) => vec![steps(2).into()],
BoolConcreteLibfunc::Equal(_) => vec![steps(2).into(), steps(3).into()],
BoolConcreteLibfunc::ToFelt252(_) => vec![steps(0).into()],
},
Cast(libfunc) => match libfunc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub fn build(
BoolConcreteLibfunc::Not(_) => build_bool_not(builder),
BoolConcreteLibfunc::Xor(_) => build_bool_xor(builder),
BoolConcreteLibfunc::Or(_) => build_bool_or(builder),
BoolConcreteLibfunc::Equal(_) => misc::build_cell_eq(builder),
BoolConcreteLibfunc::ToFelt252(_) => misc::build_identity(builder),
}
}
Expand Down
36 changes: 2 additions & 34 deletions crates/cairo-lang-sierra/src/extensions/modules/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::get_bool_type;
use crate::define_libfunc_hierarchy;
use crate::extensions::felt252::Felt252Type;
use crate::extensions::lib_func::{
BranchSignature, DeferredOutputKind, LibfuncSignature, OutputVarInfo, ParamSignature,
SierraApChange, SignatureSpecializationContext,
DeferredOutputKind, LibfuncSignature, OutputVarInfo, ParamSignature, SierraApChange,
SignatureSpecializationContext,
};
use crate::extensions::{
NamedType, NoGenericArgsGenericLibfunc, OutputVarReferenceInfo, SpecializationError,
Expand All @@ -15,7 +15,6 @@ define_libfunc_hierarchy! {
Not(BoolNotLibfunc),
Xor(BoolXorLibfunc),
Or(BoolOrLibfunc),
Equal(BoolEqualLibfunc),
ToFelt252(BoolToFelt252Libfunc),
}, BoolConcreteLibfunc
}
Expand Down Expand Up @@ -118,34 +117,3 @@ impl NoGenericArgsGenericLibfunc for BoolOrLibfunc {
boolean_libfunc_signature(context, false, false)
}
}

/// Libfunc for boolean equality.
#[derive(Default)]
pub struct BoolEqualLibfunc {}
impl NoGenericArgsGenericLibfunc for BoolEqualLibfunc {
const STR_ID: &'static str = "bool_eq";

fn specialize_signature(
&self,
context: &dyn SignatureSpecializationContext,
) -> Result<LibfuncSignature, SpecializationError> {
let bool_type = get_bool_type(context)?;
Ok(LibfuncSignature {
param_signatures: vec![
ParamSignature::new(bool_type.clone()),
ParamSignature::new(bool_type).with_allow_const(),
],
branch_signatures: vec![
BranchSignature {
vars: vec![],
ap_change: SierraApChange::Known { new_vars_only: false },
},
BranchSignature {
vars: vec![],
ap_change: SierraApChange::Known { new_vars_only: false },
},
],
fallthrough: Some(0),
})
}
}
8 changes: 0 additions & 8 deletions crates/cairo-lang-sierra/src/simulation/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,6 @@ fn simulate_bool_libfunc(
[_, _] => Err(LibfuncSimulationError::WrongArgType),
_ => Err(LibfuncSimulationError::WrongNumberOfArgs),
},
BoolConcreteLibfunc::Equal(_) => match inputs {
[CoreValue::Enum { index: a_index, .. }, CoreValue::Enum { index: b_index, .. }] => {
// The variant index defines the true/false "value". Index zero is false.
Ok((vec![], usize::from(*a_index == *b_index)))
}
[_, _] => Err(LibfuncSimulationError::MemoryLayoutMismatch),
_ => Err(LibfuncSimulationError::WrongNumberOfArgs),
},
BoolConcreteLibfunc::ToFelt252(_) => match inputs {
[CoreValue::Enum { index, .. }] => {
// The variant index defines the true/false "value". Index zero is false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"array_snapshot_pop_front",
"bitwise",
"bool_and_impl",
"bool_eq",
"bool_not_impl",
"bool_or_impl",
"bool_to_felt252",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"array_snapshot_pop_front",
"bitwise",
"bool_and_impl",
"bool_eq",
"bool_not_impl",
"bool_or_impl",
"bool_to_felt252",
Expand Down
127 changes: 0 additions & 127 deletions tests/e2e_test_data/libfuncs/bool
Original file line number Diff line number Diff line change
Expand Up @@ -139,133 +139,6 @@ test::foo@0([0]: core::bool) -> (core::bool);

//! > ==========================================================================

//! > bool_eq libfunc (a deref, b deref)

//! > test_runner_name
SmallE2ETestRunner

//! > cairo
fn foo(a: bool, b: bool) -> bool {
a == b
}

//! > casm
[fp + -4] = [ap + 0] + [fp + -3], ap++;
jmp rel 4 if [ap + -1] != 0;
jmp rel 6;
[ap + 0] = 0, ap++;
jmp rel 4;
[ap + 0] = 1, ap++;
ret;

//! > function_costs
test::foo: OrderedHashMap({Const: 400})

//! > sierra_code
type Unit = Struct<ut@Tuple>;
type core::bool = Enum<ut@core::bool, Unit, Unit>;

libfunc snapshot_take<core::bool> = snapshot_take<core::bool>;
libfunc drop<core::bool> = drop<core::bool>;
libfunc rename<core::bool> = rename<core::bool>;
libfunc bool_eq = bool_eq;
libfunc branch_align = branch_align;
libfunc struct_construct<Unit> = struct_construct<Unit>;
libfunc enum_init<core::bool, 0> = enum_init<core::bool, 0>;
libfunc store_temp<core::bool> = store_temp<core::bool>;
libfunc jump = jump;
libfunc enum_init<core::bool, 1> = enum_init<core::bool, 1>;

snapshot_take<core::bool>([0]) -> ([2], [3]);
drop<core::bool>([2]) -> ();
snapshot_take<core::bool>([1]) -> ([4], [5]);
drop<core::bool>([4]) -> ();
rename<core::bool>([3]) -> ([6]);
rename<core::bool>([5]) -> ([7]);
bool_eq([6], [7]) { fallthrough() 12() };
branch_align() -> ();
struct_construct<Unit>() -> ([8]);
enum_init<core::bool, 0>([8]) -> ([9]);
store_temp<core::bool>([9]) -> ([10]);
jump() { 16() };
branch_align() -> ();
struct_construct<Unit>() -> ([11]);
enum_init<core::bool, 1>([11]) -> ([12]);
store_temp<core::bool>([12]) -> ([10]);
rename<core::bool>([10]) -> ([13]);
return([13]);

test::foo@0([0]: core::bool, [1]: core::bool) -> (core::bool);

//! > ==========================================================================

//! > bool_eq libfunc (a immediate, b deref)

//! > test_comments
//! see comment of: bool_eq libfunc (a deref, b deref)

//! > test_runner_name
SmallE2ETestRunner

//! > cairo
fn foo(b: bool) -> bool {
true == b
}

//! > casm
[ap + 0] = 1, ap++;
[ap + -1] = [ap + 0] + [fp + -3], ap++;
jmp rel 4 if [ap + -1] != 0;
jmp rel 6;
[ap + 0] = 0, ap++;
jmp rel 4;
[ap + 0] = 1, ap++;
ret;

//! > function_costs
test::foo: OrderedHashMap({Const: 500})

//! > sierra_code
type Unit = Struct<ut@Tuple>;
type core::bool = Enum<ut@core::bool, Unit, Unit>;

libfunc struct_construct<Unit> = struct_construct<Unit>;
libfunc enum_init<core::bool, 1> = enum_init<core::bool, 1>;
libfunc snapshot_take<core::bool> = snapshot_take<core::bool>;
libfunc drop<core::bool> = drop<core::bool>;
libfunc rename<core::bool> = rename<core::bool>;
libfunc store_temp<core::bool> = store_temp<core::bool>;
libfunc bool_eq = bool_eq;
libfunc branch_align = branch_align;
libfunc enum_init<core::bool, 0> = enum_init<core::bool, 0>;
libfunc jump = jump;

struct_construct<Unit>() -> ([1]);
enum_init<core::bool, 1>([1]) -> ([2]);
snapshot_take<core::bool>([2]) -> ([3], [4]);
drop<core::bool>([3]) -> ();
snapshot_take<core::bool>([0]) -> ([5], [6]);
drop<core::bool>([5]) -> ();
rename<core::bool>([4]) -> ([7]);
rename<core::bool>([6]) -> ([8]);
store_temp<core::bool>([7]) -> ([7]);
bool_eq([7], [8]) { fallthrough() 15() };
branch_align() -> ();
struct_construct<Unit>() -> ([9]);
enum_init<core::bool, 0>([9]) -> ([10]);
store_temp<core::bool>([10]) -> ([11]);
jump() { 19() };
branch_align() -> ();
struct_construct<Unit>() -> ([12]);
enum_init<core::bool, 1>([12]) -> ([13]);
store_temp<core::bool>([13]) -> ([11]);
rename<core::bool>([11]) -> ([14]);
return([14]);

test::foo@0([0]: core::bool) -> (core::bool);

//! > ==========================================================================

//! > bool_to_felt252 libfunc.

//! > test_comments
Expand Down

0 comments on commit 043002a

Please sign in to comment.