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 16 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 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 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 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 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 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 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 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 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 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 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 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 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
24 changes: 24 additions & 0 deletions corelib/src/test/num_test.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use bytes_31::Bytes31BitSize;
use integer::{
U8BitSize, U16BitSize, U32BitSize, U64BitSize, U128BitSize, U256BitSize, I8BitSize, I16BitSize,
I32BitSize, I64BitSize, I128BitSize,
};

use test::test_utils::assert_eq;


#[test]
fn test_bit_size() {
assert_eq(@U8BitSize::bits(), @8, 'U8 bit size != 8');
assert_eq(@U16BitSize::bits(), @16, 'U16 bit size != 16');
assert_eq(@U32BitSize::bits(), @32, 'U32 bit size != 32');
assert_eq(@U64BitSize::bits(), @64, 'U64 bit size != 64');
assert_eq(@U128BitSize::bits(), @128, 'U128 bit size != 128');
assert_eq(@U256BitSize::bits(), @256, 'U256 bit size != 256');
assert_eq(@I8BitSize::bits(), @8, 'I8 bit size != 8');
assert_eq(@I16BitSize::bits(), @16, 'I16 bit size != 16');
assert_eq(@I32BitSize::bits(), @32, 'I32 bit size != 32');
assert_eq(@I64BitSize::bits(), @64, 'I64 bit size != 64');
assert_eq(@I128BitSize::bits(), @128, 'I128 bit size != 128');
assert_eq(@Bytes31BitSize::bits(), @248, 'Bytes31 bit size != 248');
}