Skip to content

Commit

Permalink
fix pow
Browse files Browse the repository at this point in the history
  • Loading branch information
SwayStar123 committed Oct 16, 2024
1 parent f13595c commit f921fb0
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions sway-lib-std/src/u128.sw
Original file line number Diff line number Diff line change
Expand Up @@ -677,25 +677,38 @@ fn u64_checked_mul(a: u64, b: u64) -> Option<u64> {
Some(a * b)
}

fn u64_checked_add(a: u64, b: u64) -> Option<u64> {
let of = asm(a: a, b: b, res) {
add res a b;
of: u64
};

if of != 0 {
return None;
}

Some(a + b)
}

fn u128_checked_mul(a: U128, b: U128) -> Option<U128> {
// in case both of the `U128` upper parts are bigger than zero,
// it automatically means overflow, as any `U128` value
// is upper part multiplied by 2 ^ 64 + lower part
if a.upper == 0 || b.upper == 0 {
if a.upper != 0 || b.upper != 0 {
return None
}

let mut result = a.lower.overflowing_mul(b.lower);

if a.upper == 0 {
match u64_checked_mul(a.lower, b.upper) {
match u64_checked_add(result.upper, a.lower * b.upper) {
None => return None,
Some(v) => { result.upper += v }
Some(v) => { result.upper = v}
}
} else if b.upper == 0 {
match u64_checked_mul(a.upper, b.lower) {
match u64_checked_add(result.upper, a.upper * b.lower) {
None => return None,
Some(v) => { result.upper += v }
Some(v) => { result.upper = v}
}
}

Expand Down

0 comments on commit f921fb0

Please sign in to comment.