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

Feat/bit ops impls #3346

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
4 changes: 0 additions & 4 deletions corelib/src/hash.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,3 @@ impl TupleSize4LegacyHash<
E3LegacyHash::hash(state, e3)
}
}

fn foo(input: Span<u64>) -> starknet::SyscallResult<u256> {
starknet::syscalls::keccak_syscall(input)
}
62 changes: 33 additions & 29 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -247,23 +247,53 @@ impl U128BitAnd of BitAnd<u128> {
v
}
}
impl TBitAnd<
T, impl TIntoU128: Into<T, u128>, impl U128TryIntoT: TryInto<u128, T>, impl TDrop: Drop<T>,
> of BitAnd<T> {
#[inline(always)]
fn bitand(lhs: T, rhs: T) -> T {
let lhs_u128 = TIntoU128::into(lhs);
let rhs_u128 = TIntoU128::into(rhs);
U128TryIntoT::try_into(lhs_u128 & rhs_u128).unwrap()
}
}
impl U128BitXor of BitXor<u128> {
#[inline(always)]
fn bitxor(lhs: u128, rhs: u128) -> u128 {
let (_, v, _) = bitwise(lhs, rhs);
v
}
}
impl TBitXor<
T, impl TIntoU128: Into<T, u128>, impl U128TryIntoT: TryInto<u128, T>, impl TDrop: Drop<T>,
> of BitXor<T> {
#[inline(always)]
fn bitxor(lhs: T, rhs: T) -> T {
let lhs_u128 = TIntoU128::into(lhs);
let rhs_u128 = TIntoU128::into(rhs);
U128TryIntoT::try_into(lhs_u128 ^ rhs_u128).unwrap()
}
}
impl U128BitOr of BitOr<u128> {
#[inline(always)]
fn bitor(lhs: u128, rhs: u128) -> u128 {
let (_, _, v) = bitwise(lhs, rhs);
v
}
}
impl U128BitNot of BitNot<u128> {
fn bitnot(a: u128) -> u128 {
BoundedInt::max() - a
impl TBitOr<
T, impl TIntoU128: Into<T, u128>, impl U128TryIntoT: TryInto<u128, T>, impl TDrop: Drop<T>,
> of BitOr<T> {
#[inline(always)]
fn bitor(lhs: T, rhs: T) -> T {
let lhs_u128 = TIntoU128::into(lhs);
let rhs_u128 = TIntoU128::into(rhs);
U128TryIntoT::try_into(lhs_u128 | rhs_u128).unwrap()
}
}
impl TBitNot<T, impl TBounded: BoundedInt<T>, impl TSub: Sub<T>> of BitNot<T> {
fn bitnot(a: T) -> T {
TSub::sub(TBounded::max(), a)
}
}

Expand Down Expand Up @@ -440,11 +470,6 @@ impl U8DivRem of DivRem<u8> {
}
}

impl U8BitNot of BitNot<u8> {
fn bitnot(a: u8) -> u8 {
BoundedInt::max() - a
}
}

#[derive(Copy, Drop)]
extern type u16;
Expand Down Expand Up @@ -616,11 +641,6 @@ impl U16DivRem of DivRem<u16> {
}
}

impl U16BitNot of BitNot<u16> {
fn bitnot(a: u16) -> u16 {
BoundedInt::max() - a
}
}

#[derive(Copy, Drop)]
extern type u32;
Expand Down Expand Up @@ -792,11 +812,6 @@ impl U32DivRem of DivRem<u32> {
}
}

impl U32BitNot of BitNot<u32> {
fn bitnot(a: u32) -> u32 {
BoundedInt::max() - a
}
}

#[derive(Copy, Drop)]
extern type u64;
Expand Down Expand Up @@ -968,11 +983,6 @@ impl U64DivRem of DivRem<u64> {
}
}

impl U64BitNot of BitNot<u64> {
fn bitnot(a: u64) -> u64 {
BoundedInt::max() - a
}
}

#[derive(Copy, Drop, PartialEq, Serde, storage_access::StorageAccess)]
struct u256 {
Expand Down Expand Up @@ -1199,12 +1209,6 @@ impl U256DivRem of DivRem<u256> {
}
}

impl U256BitNot of BitNot<u256> {
fn bitnot(a: u256) -> u256 {
u256 { low: ~a.low, high: ~a.high }
}
}

#[derive(Copy, Drop, PartialEq, Serde)]
struct u512 {
limb0: u128,
Expand Down
28 changes: 28 additions & 0 deletions corelib/src/test/integer_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ fn test_u8_operators() {
assert_ge(5_u8, 2_u8, '5 >= 2');
assert(!(3_u8 > 3_u8), '!(3 > 3)');
assert_ge(3_u8, 3_u8, '3 >= 3');
assert_eq(@(1_u8 | 2_u8), @3_u8, '1 | 2 == 3');
assert_eq(@(1_u8 & 2_u8), @0_u8, '1 & 2 == 0');
assert_eq(@(1_u8 ^ 2_u8), @3_u8, '1 ^ 2 == 3');
assert_eq(@(2_u8 | 2_u8), @2_u8, '2 | 2 == 2');
assert_eq(@(2_u8 & 2_u8), @2_u8, '2 & 2 == 2');
assert_eq(@(2_u8 & 3_u8), @2_u8, '2 & 3 == 2');
assert_eq(@(3_u8 ^ 6_u8), @5_u8, '3 ^ 6 == 5');
assert_eq(@u8_sqrt(9), @3, 'u8_sqrt(9) == 3');
assert_eq(@u8_sqrt(10), @3, 'u8_sqrt(10) == 3');
assert_eq(@u8_sqrt(0x40), @0x8, 'u8_sqrt(2^6) == 2^3');
Expand Down Expand Up @@ -124,6 +131,13 @@ fn test_u16_operators() {
assert_ge(5_u16, 2_u16, '5 >= 2');
assert(!(3_u16 > 3_u16), '!(3 > 3)');
assert_ge(3_u16, 3_u16, '3 >= 3');
assert_eq(@(1_u16 | 2_u16), @3_u16, '1 | 2 == 3');
assert_eq(@(1_u16 & 2_u16), @0_u16, '1 & 2 == 0');
assert_eq(@(1_u16 ^ 2_u16), @3_u16, '1 ^ 2 == 3');
assert_eq(@(2_u16 | 2_u16), @2_u16, '2 | 2 == 2');
assert_eq(@(2_u16 & 2_u16), @2_u16, '2 & 2 == 2');
assert_eq(@(2_u16 & 3_u16), @2_u16, '2 & 3 == 2');
assert_eq(@(3_u16 ^ 6_u16), @5_u16, '3 ^ 6 == 5');
assert_eq(@u16_sqrt(9), @3, 'u16_sqrt(9) == 3');
assert_eq(@u16_sqrt(10), @3, 'u16_sqrt(10) == 3');
assert_eq(@u16_sqrt(0x400), @0x20, 'u16_sqrt(2^10) == 2^5');
Expand Down Expand Up @@ -220,6 +234,13 @@ fn test_u32_operators() {
assert_ge(5_u32, 2_u32, '5 >= 2');
assert(!(3_u32 > 3_u32), '!(3 > 3)');
assert_ge(3_u32, 3_u32, '3 >= 3');
assert_eq(@(1_u32 | 2_u32), @3_u32, '1 | 2 == 3');
assert_eq(@(1_u32 & 2_u32), @0_u32, '1 & 2 == 0');
assert_eq(@(1_u32 ^ 2_u32), @3_u32, '1 ^ 2 == 3');
assert_eq(@(2_u32 | 2_u32), @2_u32, '2 | 2 == 2');
assert_eq(@(2_u32 & 2_u32), @2_u32, '2 & 2 == 2');
assert_eq(@(2_u32 & 3_u32), @2_u32, '2 & 3 == 2');
assert_eq(@(3_u32 ^ 6_u32), @5_u32, '3 ^ 6 == 5');
assert_eq(@u32_sqrt(9), @3, 'u32_sqrt(9) == 3');
assert_eq(@u32_sqrt(10), @3, 'u32_sqrt(10) == 3');
assert_eq(@u32_sqrt(0x100000), @0x400, 'u32_sqrt(2^20) == 2^10');
Expand Down Expand Up @@ -318,6 +339,13 @@ fn test_u64_operators() {
assert_ge(5_u64, 2_u64, '5 >= 2');
assert(!(3_u64 > 3_u64), '!(3 > 3)');
assert_ge(3_u64, 3_u64, '3 >= 3');
assert_eq(@(1_u64 | 2_u64), @3_u64, '1 | 2 == 3');
assert_eq(@(1_u64 & 2_u64), @0_u64, '1 & 2 == 0');
assert_eq(@(1_u64 ^ 2_u64), @3_u64, '1 ^ 2 == 3');
assert_eq(@(2_u64 | 2_u64), @2_u64, '2 | 2 == 2');
assert_eq(@(2_u64 & 2_u64), @2_u64, '2 & 2 == 2');
assert_eq(@(2_u64 & 3_u64), @2_u64, '2 & 3 == 2');
assert_eq(@(3_u64 ^ 6_u64), @5_u64, '3 ^ 6 == 5');
assert_eq(@u64_sqrt(9), @3, 'u64_sqrt(9) == 3');
assert_eq(@u64_sqrt(10), @3, 'u64_sqrt(10) == 3');
assert_eq(@u64_sqrt(0x10000000000), @0x100000, 'u64_sqrt(2^40) == 2^20');
Expand Down