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: Added Bits<T> in num module #4251

Merged
merged 17 commits into from
Nov 14, 2023
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
6 changes: 6 additions & 0 deletions corelib/src/bytes_31.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ impl Bytes31IndexView of IndexView<bytes31, usize, u8> {
}
}

impl Bytes31BitSize of core::num::traits::BitSize<bytes31> {
fn bits() -> usize {
248
}
}

impl Bytes31IntoFelt252 of Into<bytes31, felt252> {
fn into(self: bytes31) -> felt252 {
bytes31_to_felt252(self)
Expand Down
66 changes: 66 additions & 0 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ impl U128BitNot of BitNot<u128> {
}
}

impl U128BitSize of core::num::traits::BitSize<u128> {
fn bits() -> usize {
128
}
}

extern fn u128_is_zero(a: u128) -> IsZeroResult<u128> implicits() nopanic;

extern fn u128_byte_reverse(input: u128) -> u128 implicits(Bitwise) nopanic;
Expand Down Expand Up @@ -453,6 +459,12 @@ impl U8BitOr of BitOr<u8> {
}
}

impl U8BitSize of core::num::traits::BitSize<u8> {
fn bits() -> usize {
8
}
}

#[derive(Copy, Drop)]
extern type u16;
impl NumericLiteralu16 of NumericLiteral<u16>;
Expand Down Expand Up @@ -643,6 +655,12 @@ impl U16BitOr of BitOr<u16> {
}
}

impl U16BitSize of core::num::traits::BitSize<u16> {
fn bits() -> usize {
16
}
}

#[derive(Copy, Drop)]
extern type u32;
impl NumericLiteralu32 of NumericLiteral<u32>;
Expand Down Expand Up @@ -833,6 +851,12 @@ impl U32BitOr of BitOr<u32> {
}
}

impl U32BitSize of core::num::traits::BitSize<u32> {
fn bits() -> usize {
32
}
}

#[derive(Copy, Drop)]
extern type u64;
impl NumericLiteralu64 of NumericLiteral<u64>;
Expand Down Expand Up @@ -1023,6 +1047,12 @@ impl U64BitOr of BitOr<u64> {
}
}

impl U64BitSize of core::num::traits::BitSize<u64> {
fn bits() -> usize {
64
}
}

#[derive(Copy, Drop, Hash, PartialEq, Serde, starknet::Store)]
struct u256 {
low: u128,
Expand Down Expand Up @@ -1264,6 +1294,12 @@ impl U256BitNot of BitNot<u256> {
}
}

impl U256BitSize of core::num::traits::BitSize<u256> {
fn bits() -> usize {
256
}
}

#[derive(Copy, Drop, Hash, PartialEq, Serde)]
struct u512 {
limb0: u128,
Expand Down Expand Up @@ -1903,6 +1939,12 @@ impl I8PartialOrd of PartialOrd<i8> {
}
}

impl I8BitSize of core::num::traits::BitSize<i8> {
fn bits() -> usize {
8
}
}

#[derive(Copy, Drop)]
extern type i16;
impl NumericLiterali16 of NumericLiteral<i16>;
Expand Down Expand Up @@ -2004,6 +2046,12 @@ impl I16PartialOrd of PartialOrd<i16> {
}
}

impl I16BitSize of core::num::traits::BitSize<i16> {
fn bits() -> usize {
16
}
}

#[derive(Copy, Drop)]
extern type i32;
impl NumericLiterali32 of NumericLiteral<i32>;
Expand Down Expand Up @@ -2105,6 +2153,12 @@ impl I32PartialOrd of PartialOrd<i32> {
}
}

impl I32BitSize of core::num::traits::BitSize<i32> {
fn bits() -> usize {
32
}
}

#[derive(Copy, Drop)]
extern type i64;
impl NumericLiterali64 of NumericLiteral<i64>;
Expand Down Expand Up @@ -2206,6 +2260,12 @@ impl I64PartialOrd of PartialOrd<i64> {
}
}

impl I64BitSize of core::num::traits::BitSize<i64> {
fn bits() -> usize {
64
}
}

#[derive(Copy, Drop)]
extern type i128;
impl NumericLiterali128 of NumericLiteral<i128>;
Expand Down Expand Up @@ -2328,6 +2388,12 @@ impl U64Zeroable = core::zeroable::zero_based::ZeroableImpl<u64, U64Zero>;
impl U128Zeroable = core::zeroable::zero_based::ZeroableImpl<u128, U128Zero>;
impl U256Zeroable = core::zeroable::zero_based::ZeroableImpl<u256, U256Zero>;

impl I128BitSize of core::num::traits::BitSize<i128> {
fn bits() -> usize {
128
}
}

// Zero trait implementations
impl U8Zero of core::num::traits::Zero<u8> {
fn zero() -> u8 {
Expand Down
3 changes: 3 additions & 0 deletions corelib/src/num/traits.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ use zero::Zero;

mod one;
use one::One;

mod bit_size;
use bit_size::BitSize;
5 changes: 5 additions & 0 deletions corelib/src/num/traits/bit_size.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Trait used to retrieve the size in bits of a type.
trait BitSize<T> {
/// Returns the size in bits of T as usize.
fn bits() -> usize;
}
1 change: 1 addition & 0 deletions corelib/src/test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod fmt_test;
mod hash_test;
mod integer_test;
mod keccak_test;
mod num_test;
mod math_test;
mod nullable_test;
mod panics_test;
Expand Down
17 changes: 17 additions & 0 deletions corelib/src/test/num_test.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use core::num::traits::BitSize;

#[test]
fn test_bit_size() {
assert!(BitSize::<u8>::bits() == 8);
assert!(BitSize::<u16>::bits() == 16);
assert!(BitSize::<u32>::bits() == 32);
assert!(BitSize::<u64>::bits() == 64);
assert!(BitSize::<u128>::bits() == 128);
assert!(BitSize::<u256>::bits() == 256);
assert!(BitSize::<i8>::bits() == 8);
assert!(BitSize::<i16>::bits() == 16);
assert!(BitSize::<i32>::bits() == 32);
assert!(BitSize::<i64>::bits() == 64);
assert!(BitSize::<i128>::bits() == 128);
assert!(BitSize::<bytes31>::bits() == 248);
}
Loading