Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using default implementations for PartialOrd::{gt,le,ge}. #6195

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
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
128 changes: 18 additions & 110 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,6 @@ impl U128PartialEq of PartialEq<u128> {
}

impl U128PartialOrd of PartialOrd<u128> {
#[inline(always)]
fn le(lhs: u128, rhs: u128) -> bool {
u128_overflowing_sub(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: u128, rhs: u128) -> bool {
u128_overflowing_sub(lhs, rhs).into_is_ok()
Expand All @@ -194,10 +190,6 @@ impl U128PartialOrd of PartialOrd<u128> {
fn lt(lhs: u128, rhs: u128) -> bool {
u128_overflowing_sub(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: u128, rhs: u128) -> bool {
u128_overflowing_sub(rhs, lhs).into_is_err()
}
}

pub extern type Bitwise;
Expand Down Expand Up @@ -260,21 +252,13 @@ impl U8PartialEq of PartialEq<u8> {
}

impl U8PartialOrd of PartialOrd<u8> {
#[inline(always)]
fn le(lhs: u8, rhs: u8) -> bool {
u8_overflowing_sub(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: u8, rhs: u8) -> bool {
u8_overflowing_sub(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: u8, rhs: u8) -> bool {
u8_overflowing_sub(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: u8, rhs: u8) -> bool {
u8_overflowing_sub(rhs, lhs).into_is_err()
fn ge(lhs: u8, rhs: u8) -> bool {
u8_overflowing_sub(lhs, rhs).into_is_ok()
}
}

Expand Down Expand Up @@ -417,21 +401,13 @@ impl U16PartialEq of PartialEq<u16> {
}

impl U16PartialOrd of PartialOrd<u16> {
#[inline(always)]
fn le(lhs: u16, rhs: u16) -> bool {
u16_overflowing_sub(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: u16, rhs: u16) -> bool {
u16_overflowing_sub(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: u16, rhs: u16) -> bool {
u16_overflowing_sub(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: u16, rhs: u16) -> bool {
u16_overflowing_sub(rhs, lhs).into_is_err()
fn ge(lhs: u16, rhs: u16) -> bool {
u16_overflowing_sub(lhs, rhs).into_is_ok()
}
}

Expand Down Expand Up @@ -580,21 +556,13 @@ impl U32PartialEq of PartialEq<u32> {
}

impl U32PartialOrd of PartialOrd<u32> {
#[inline(always)]
fn le(lhs: u32, rhs: u32) -> bool {
u32_overflowing_sub(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: u32, rhs: u32) -> bool {
u32_overflowing_sub(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: u32, rhs: u32) -> bool {
u32_overflowing_sub(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: u32, rhs: u32) -> bool {
u32_overflowing_sub(rhs, lhs).into_is_err()
fn ge(lhs: u32, rhs: u32) -> bool {
u32_overflowing_sub(lhs, rhs).into_is_ok()
}
}

Expand Down Expand Up @@ -743,21 +711,13 @@ impl U64PartialEq of PartialEq<u64> {
}

impl U64PartialOrd of PartialOrd<u64> {
#[inline(always)]
fn le(lhs: u64, rhs: u64) -> bool {
u64_overflowing_sub(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: u64, rhs: u64) -> bool {
u64_overflowing_sub(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: u64, rhs: u64) -> bool {
u64_overflowing_sub(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: u64, rhs: u64) -> bool {
u64_overflowing_sub(rhs, lhs).into_is_err()
fn ge(lhs: u64, rhs: u64) -> bool {
u64_overflowing_sub(lhs, rhs).into_is_ok()
}
}

Expand Down Expand Up @@ -1017,14 +977,6 @@ impl U256Mul of Mul<u256> {
}

impl U256PartialOrd of PartialOrd<u256> {
#[inline(always)]
fn le(lhs: u256, rhs: u256) -> bool {
!(rhs < lhs)
}
#[inline(always)]
fn ge(lhs: u256, rhs: u256) -> bool {
!(lhs < rhs)
}
fn lt(lhs: u256, rhs: u256) -> bool {
if lhs.high < rhs.high {
true
Expand All @@ -1034,10 +986,6 @@ impl U256PartialOrd of PartialOrd<u256> {
false
}
}
#[inline(always)]
fn gt(lhs: u256, rhs: u256) -> bool {
rhs < lhs
}
}

impl U256BitAnd of BitAnd<u256> {
Expand Down Expand Up @@ -1704,21 +1652,13 @@ impl I8Mul of Mul<i8> {
/// If `lhs` >= `rhs` returns `Ok(lhs - rhs)` else returns `Err(2**8 + lhs - rhs)`.
pub extern fn i8_diff(lhs: i8, rhs: i8) -> Result<u8, u8> implicits(RangeCheck) nopanic;
impl I8PartialOrd of PartialOrd<i8> {
#[inline(always)]
fn le(lhs: i8, rhs: i8) -> bool {
i8_diff(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: i8, rhs: i8) -> bool {
i8_diff(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: i8, rhs: i8) -> bool {
i8_diff(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: i8, rhs: i8) -> bool {
i8_diff(rhs, lhs).into_is_err()
fn ge(lhs: i8, rhs: i8) -> bool {
i8_diff(lhs, rhs).into_is_ok()
}
}

Expand Down Expand Up @@ -1789,21 +1729,13 @@ impl I16Mul of Mul<i16> {
/// If `lhs` >= `rhs` returns `Ok(lhs - rhs)` else returns `Err(2**16 + lhs - rhs)`.
pub extern fn i16_diff(lhs: i16, rhs: i16) -> Result<u16, u16> implicits(RangeCheck) nopanic;
impl I16PartialOrd of PartialOrd<i16> {
#[inline(always)]
fn le(lhs: i16, rhs: i16) -> bool {
i16_diff(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: i16, rhs: i16) -> bool {
i16_diff(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: i16, rhs: i16) -> bool {
i16_diff(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: i16, rhs: i16) -> bool {
i16_diff(rhs, lhs).into_is_err()
fn ge(lhs: i16, rhs: i16) -> bool {
i16_diff(lhs, rhs).into_is_ok()
}
}

Expand Down Expand Up @@ -1874,21 +1806,13 @@ impl I32Mul of Mul<i32> {
/// If `lhs` >= `rhs` returns `Ok(lhs - rhs)` else returns `Err(2**32 + lhs - rhs)`.
pub extern fn i32_diff(lhs: i32, rhs: i32) -> Result<u32, u32> implicits(RangeCheck) nopanic;
impl I32PartialOrd of PartialOrd<i32> {
#[inline(always)]
fn le(lhs: i32, rhs: i32) -> bool {
i32_diff(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: i32, rhs: i32) -> bool {
i32_diff(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: i32, rhs: i32) -> bool {
i32_diff(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: i32, rhs: i32) -> bool {
i32_diff(rhs, lhs).into_is_err()
fn ge(lhs: i32, rhs: i32) -> bool {
i32_diff(lhs, rhs).into_is_ok()
}
}

Expand Down Expand Up @@ -1959,21 +1883,13 @@ impl I64Mul of Mul<i64> {
/// If `lhs` >= `rhs` returns `Ok(lhs - rhs)` else returns `Err(2**64 + lhs - rhs)`.
pub extern fn i64_diff(lhs: i64, rhs: i64) -> Result<u64, u64> implicits(RangeCheck) nopanic;
impl I64PartialOrd of PartialOrd<i64> {
#[inline(always)]
fn le(lhs: i64, rhs: i64) -> bool {
i64_diff(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: i64, rhs: i64) -> bool {
i64_diff(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: i64, rhs: i64) -> bool {
i64_diff(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: i64, rhs: i64) -> bool {
i64_diff(rhs, lhs).into_is_err()
fn ge(lhs: i64, rhs: i64) -> bool {
i64_diff(lhs, rhs).into_is_ok()
}
}

Expand Down Expand Up @@ -2057,21 +1973,13 @@ impl I128Mul of Mul<i128> {
/// If `lhs` >= `rhs` returns `Ok(lhs - rhs)` else returns `Err(2**128 + lhs - rhs)`.
pub extern fn i128_diff(lhs: i128, rhs: i128) -> Result<u128, u128> implicits(RangeCheck) nopanic;
impl I128PartialOrd of PartialOrd<i128> {
#[inline(always)]
fn le(lhs: i128, rhs: i128) -> bool {
i128_diff(rhs, lhs).into_is_ok()
}
#[inline(always)]
fn ge(lhs: i128, rhs: i128) -> bool {
i128_diff(lhs, rhs).into_is_ok()
}
#[inline(always)]
fn lt(lhs: i128, rhs: i128) -> bool {
i128_diff(lhs, rhs).into_is_err()
}
#[inline(always)]
fn gt(lhs: i128, rhs: i128) -> bool {
i128_diff(rhs, lhs).into_is_err()
fn ge(lhs: i128, rhs: i128) -> bool {
i128_diff(lhs, rhs).into_is_ok()
}
}

Expand Down
12 changes: 0 additions & 12 deletions corelib/src/starknet/contract_address.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,6 @@ impl ContractAddressPartialOrd of PartialOrd<ContractAddress> {
let lhs: u256 = contract_address_to_felt252(lhs).into();
lhs < contract_address_to_felt252(rhs).into()
}
#[inline(always)]
fn le(lhs: ContractAddress, rhs: ContractAddress) -> bool {
!(rhs < lhs)
}
#[inline(always)]
fn gt(lhs: ContractAddress, rhs: ContractAddress) -> bool {
rhs < lhs
}
#[inline(always)]
fn ge(lhs: ContractAddress, rhs: ContractAddress) -> bool {
!(lhs < rhs)
}
}

impl HashContractAddress<S, +HashStateTrait<S>, +Drop<S>> =
Expand Down
12 changes: 9 additions & 3 deletions corelib/src/traits.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,16 @@ pub trait BitNot<T> {
}

pub trait PartialOrd<T> {
fn le(lhs: T, rhs: T) -> bool;
fn ge(lhs: T, rhs: T) -> bool;
fn lt(lhs: T, rhs: T) -> bool;
fn gt(lhs: T, rhs: T) -> bool;
fn ge(lhs: T, rhs: T) -> bool {
!Self::lt(lhs, rhs)
}
fn gt(lhs: T, rhs: T) -> bool {
Self::lt(rhs, lhs)
}
fn le(lhs: T, rhs: T) -> bool {
Self::ge(rhs, lhs)
}
}

impl PartialOrdSnap<T, +PartialOrd<T>, +Copy<T>> of PartialOrd<@T> {
Expand Down
Loading