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

fix: u256 log suggestions #6645

Merged
merged 6 commits into from
Oct 16, 2024
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
8 changes: 8 additions & 0 deletions sway-lib-std/src/math.sw
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,18 @@ impl Logarithm for u256 {
assert(base >= 2);
// Logarithm is undefined for 0
assert(self != 0);
} else {
// Logarithm is undefined for bases less than 2
// Logarithm is undefined for 0
if (base < 2) || (self == 0) {
set_flags(flags);
return 0x00u256;
}
}

// Decimals rounded to 0
if self < base {
set_flags(flags);
return 0x00u256;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ fn bytes_append() {

assert(bytes2.len() == second_length);
assert(!bytes2.is_empty());

assert(bytes2.get(0).unwrap() == d);
assert(bytes2.get(1).unwrap() == e);
assert(bytes2.get(2).unwrap() == f);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library;

use std::flags::disable_panic_on_overflow;
use std::{flags::{disable_panic_on_overflow, disable_panic_on_unsafe_math}, registers::flags};

#[test]
fn math_root_u256() {
Expand Down Expand Up @@ -272,13 +272,21 @@ fn math_log_u8() {
#[test]
fn math_log_u256() {
let max_u256 = u256::max();
let prior_flags = flags();
assert(0x2u256.log(0x2u256) == 0x1u256);
assert(0x1u256.log(0x3u256) == 0);
assert(0x8u256.log(0x2u256) == 0x3u256);
assert(0x64u256.log(0xau256) == 0x2u256);
assert(0x64u256.log(0x2u256) == 0x6u256);
assert(0x64u256.log(0x9u256) == 0x2u256);
assert(max_u256.log(0x2u256) == 0xffu256);
assert(prior_flags == flags());

let _ = disable_panic_on_unsafe_math();
let prior_flags = flags();
assert(0x1u256.log(0x1u256) == 0);
assert(0x0u256.log(0x3u256) == 0);
assert(prior_flags == flags());
}

#[test]
Expand Down
Loading