Skip to content

Commit

Permalink
fix: u256 log suggestions (#6645)
Browse files Browse the repository at this point in the history
## Description

u256.log() now always recovers the flag register. Additionally the
method now returns 0u256 with invalid args when unsafe math panic is
disabled.

Closes #6644 

## 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] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [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.
  • Loading branch information
K1-R1 authored Oct 16, 2024
1 parent cbfd962 commit e216061
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
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

0 comments on commit e216061

Please sign in to comment.