Skip to content

Commit

Permalink
Not trait implementation for U128 and U256 (FuelLabs#4077)
Browse files Browse the repository at this point in the history
## Description

Closes FuelLabs#4078
## Checklist

- [x] I have linked to any relevant issues.
- [x] I have commented my code, particularly in hard-to-understand
areas.
- [x] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [x] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [x] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Mohammad Fawaz <[email protected]>
  • Loading branch information
rostyslavtyshko and mohammadfawaz authored Feb 15, 2023
1 parent 77a1eea commit 472d220
Showing 4 changed files with 122 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sway-lib-std/src/u128.sw
Original file line number Diff line number Diff line change
@@ -256,6 +256,15 @@ impl core::ops::Shift for U128 {
}
}

impl core::ops::Not for U128 {
fn not(self) -> Self {
Self {
upper: !self.upper,
lower: !self.lower,
}
}
}

impl core::ops::Add for U128 {
/// Add a `U128` to a `U128`. Panics on overflow.
fn add(self, other: Self) -> Self {
11 changes: 11 additions & 0 deletions sway-lib-std/src/u256.sw
Original file line number Diff line number Diff line change
@@ -326,6 +326,17 @@ impl core::ops::Shift for U256 {
}
}

impl core::ops::Not for U256 {
fn not(self) -> Self {
Self {
a: !self.a,
b: !self.b,
c: !self.c,
d: !self.d,
}
}
}

impl core::ops::Add for U256 {
/// Add a `U256` to a `U256`. Panics on overflow.
fn add(self, other: Self) -> Self {
Original file line number Diff line number Diff line change
@@ -76,6 +76,18 @@ fn main() -> bool {
assert(three_left_shift_one.upper == 0);
assert(three_left_shift_one.lower == 6);

let not_0_3 = !U128::from((0, 3));
assert(not_0_3.upper == u64::max());
assert(not_0_3.lower == u64::max() - 3);

let not_3_3 = !U128::from((3, 3));
assert(not_3_3.upper == u64::max() - 3);
assert(not_3_3.lower == u64::max() - 3);

let not_3_0 = !U128::from((3, 0));
assert(not_3_0.upper == u64::max() - 3);
assert(not_3_0.lower == u64::max());

// test as_u64()
let eleven = U128::from((0, 11));
let unwrapped = eleven.as_u64().unwrap();
Original file line number Diff line number Diff line change
@@ -75,5 +75,95 @@ fn main() -> bool {
assert(last_left_shift_one.a == 0);
assert(last_left_shift_one.b == 1 << 63);

let not_1_0_0_0 = !U256::from((1, 0, 0, 0));
assert(not_1_0_0_0.a == u64::max() - 1);
assert(not_1_0_0_0.b == u64::max());
assert(not_1_0_0_0.c == u64::max());
assert(not_1_0_0_0.d == u64::max());

let not_0_1_0_0 = !U256::from((0, 1, 0, 0));
assert(not_0_1_0_0.a == u64::max());
assert(not_0_1_0_0.b == u64::max() - 1);
assert(not_0_1_0_0.c == u64::max());
assert(not_0_1_0_0.d == u64::max());

let not_0_0_1_0 = !U256::from((0, 0, 1, 0));
assert(not_0_0_1_0.a == u64::max());
assert(not_0_0_1_0.b == u64::max());
assert(not_0_0_1_0.c == u64::max() - 1);
assert(not_0_0_1_0.d == u64::max());

let not_0_0_0_1 = !U256::from((0, 0, 0, 1));
assert(not_0_0_0_1.a == u64::max());
assert(not_0_0_0_1.b == u64::max());
assert(not_0_0_0_1.c == u64::max());
assert(not_0_0_0_1.d == u64::max() - 1);

let not_1_1_0_0 = !U256::from((1, 1, 0, 0));
assert(not_1_1_0_0.a == u64::max() - 1);
assert(not_1_1_0_0.b == u64::max() - 1);
assert(not_1_1_0_0.c == u64::max());
assert(not_1_1_0_0.d == u64::max());

let not_0_1_1_0 = !U256::from((0, 1, 1, 0));
assert(not_0_1_1_0.a == u64::max());
assert(not_0_1_1_0.b == u64::max() - 1);
assert(not_0_1_1_0.c == u64::max() - 1);
assert(not_0_1_1_0.d == u64::max());

let not_0_0_1_1 = !U256::from((0, 0, 1, 1));
assert(not_0_0_1_1.a == u64::max());
assert(not_0_0_1_1.b == u64::max());
assert(not_0_0_1_1.c == u64::max() - 1);
assert(not_0_0_1_1.d == u64::max() - 1);

let not_1_0_1_0 = !U256::from((1, 0, 1, 0));
assert(not_1_0_1_0.a == u64::max() - 1);
assert(not_1_0_1_0.b == u64::max());
assert(not_1_0_1_0.c == u64::max() - 1);
assert(not_1_0_1_0.d == u64::max());

let not_1_0_0_1 = !U256::from((1, 0, 0, 1));
assert(not_1_0_0_1.a == u64::max() - 1);
assert(not_1_0_0_1.b == u64::max());
assert(not_1_0_0_1.c == u64::max());
assert(not_1_0_0_1.d == u64::max() - 1);

let not_0_1_0_1 = !U256::from((0, 1, 0, 1));
assert(not_0_1_0_1.a == u64::max());
assert(not_0_1_0_1.b == u64::max() - 1);
assert(not_0_1_0_1.c == u64::max());
assert(not_0_1_0_1.d == u64::max() - 1);

let not_1_1_1_0 = !U256::from((1, 1, 1, 0));
assert(not_1_1_1_0.a == u64::max() - 1);
assert(not_1_1_1_0.b == u64::max() - 1);
assert(not_1_1_1_0.c == u64::max() - 1);
assert(not_1_1_1_0.d == u64::max());

let not_0_1_1_1 = !U256::from((0, 1, 1, 1));
assert(not_0_1_1_1.a == u64::max());
assert(not_0_1_1_1.b == u64::max() - 1);
assert(not_0_1_1_1.c == u64::max() - 1);
assert(not_0_1_1_1.d == u64::max() - 1);

let not_1_0_1_1 = !U256::from((1, 0, 1, 1));
assert(not_1_0_1_1.a == u64::max() - 1);
assert(not_1_0_1_1.b == u64::max());
assert(not_1_0_1_1.c == u64::max() - 1);
assert(not_1_0_1_1.d == u64::max() - 1);

let not_1_1_0_1 = !U256::from((1, 1, 0, 1));
assert(not_1_1_0_1.a == u64::max() - 1);
assert(not_1_1_0_1.b == u64::max() - 1);
assert(not_1_1_0_1.c == u64::max());
assert(not_1_1_0_1.d == u64::max() - 1);

let not_1_1_1_1 = !U256::from((1, 1, 1, 1));
assert(not_1_1_1_1.a == u64::max() - 1);
assert(not_1_1_1_1.b == u64::max() - 1);
assert(not_1_1_1_1.c == u64::max() - 1);
assert(not_1_1_1_1.d == u64::max() - 1);

true
}

0 comments on commit 472d220

Please sign in to comment.