Skip to content

Commit

Permalink
Add as_u256 for U128 (#6951)
Browse files Browse the repository at this point in the history
## Description
Adds the conversion helper for U128

## 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).
- [ ] 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
SwayStar123 authored Feb 25, 2025
1 parent a879ffb commit a090937
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
22 changes: 22 additions & 0 deletions sway-lib-std/src/u128.sw
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ impl U128 {
}
}

// TODO: Rename to `try_as_u64` to be consistent with all other downcasts
/// Safely downcast to `u64` without loss of precision.
///
/// # Additional Information
Expand Down Expand Up @@ -333,6 +334,27 @@ impl U128 {
}
}

/// Upcasts a `U128` to a `u256`.
///
/// # Returns
///
/// * [u256] - The `u256` representation of the `U128` value.
///
/// # Examples
///
/// ```sway
/// use std::u128::U128;
///
/// fn foo() {
/// let u128_value = U128::from(0u64);
/// let u256_value = u128_value.as_u256();
/// }
pub fn as_u256(self) -> u256 {
asm(nums: (0, 0, self.upper, self.lower)) {
nums: u256
}
}

/// The smallest value that can be represented by this integer type.
///
/// # Returns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1188,3 +1188,39 @@ fn u128_unsafemath_log2() {

set_flags(prior_flags);
}

#[test]
fn u128_as_u256() {
let mut vals = Vec::new();
vals.push(0);
vals.push(1);
vals.push(2);
vals.push(u64::max() - 1);
vals.push(u64::max());

for val in vals.iter() {
// Ensure parity with u256::from(val)
let u128_val = U128::from(val);
let u256_val = u128_val.as_u256();
assert(u256_val == u256::from(val));

// Ensure parity with asm u256 conversion
let asm_val = asm(nums: (0, 0, 0, val)) {
nums: u256
};
assert(u256_val == asm_val);

for val2 in vals.iter() {
// Ensure parity with u256::from(0, 0, val, val2)
let u128_val = U128::from((val, val2));
let u256_val = u128_val.as_u256();
assert(u256_val == u256::from((0, 0, val, val2)));

// Ensure parity with asm u256 conversion
let asm_val = asm(nums: (0, 0, val, val2)) {
nums: u256
};
assert(u256_val == asm_val);
}
}
}

0 comments on commit a090937

Please sign in to comment.