Skip to content

Commit a090937

Browse files
authored
Add as_u256 for U128 (#6951)
## 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.
1 parent a879ffb commit a090937

File tree

2 files changed

+58
-0
lines changed
  • sway-lib-std/src
  • test/src/in_language_tests/test_programs/u128_inline_tests/src

2 files changed

+58
-0
lines changed

sway-lib-std/src/u128.sw

+22
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ impl U128 {
299299
}
300300
}
301301

302+
// TODO: Rename to `try_as_u64` to be consistent with all other downcasts
302303
/// Safely downcast to `u64` without loss of precision.
303304
///
304305
/// # Additional Information
@@ -333,6 +334,27 @@ impl U128 {
333334
}
334335
}
335336

337+
/// Upcasts a `U128` to a `u256`.
338+
///
339+
/// # Returns
340+
///
341+
/// * [u256] - The `u256` representation of the `U128` value.
342+
///
343+
/// # Examples
344+
///
345+
/// ```sway
346+
/// use std::u128::U128;
347+
///
348+
/// fn foo() {
349+
/// let u128_value = U128::from(0u64);
350+
/// let u256_value = u128_value.as_u256();
351+
/// }
352+
pub fn as_u256(self) -> u256 {
353+
asm(nums: (0, 0, self.upper, self.lower)) {
354+
nums: u256
355+
}
356+
}
357+
336358
/// The smallest value that can be represented by this integer type.
337359
///
338360
/// # Returns

test/src/in_language_tests/test_programs/u128_inline_tests/src/main.sw

+36
Original file line numberDiff line numberDiff line change
@@ -1188,3 +1188,39 @@ fn u128_unsafemath_log2() {
11881188

11891189
set_flags(prior_flags);
11901190
}
1191+
1192+
#[test]
1193+
fn u128_as_u256() {
1194+
let mut vals = Vec::new();
1195+
vals.push(0);
1196+
vals.push(1);
1197+
vals.push(2);
1198+
vals.push(u64::max() - 1);
1199+
vals.push(u64::max());
1200+
1201+
for val in vals.iter() {
1202+
// Ensure parity with u256::from(val)
1203+
let u128_val = U128::from(val);
1204+
let u256_val = u128_val.as_u256();
1205+
assert(u256_val == u256::from(val));
1206+
1207+
// Ensure parity with asm u256 conversion
1208+
let asm_val = asm(nums: (0, 0, 0, val)) {
1209+
nums: u256
1210+
};
1211+
assert(u256_val == asm_val);
1212+
1213+
for val2 in vals.iter() {
1214+
// Ensure parity with u256::from(0, 0, val, val2)
1215+
let u128_val = U128::from((val, val2));
1216+
let u256_val = u128_val.as_u256();
1217+
assert(u256_val == u256::from((0, 0, val, val2)));
1218+
1219+
// Ensure parity with asm u256 conversion
1220+
let asm_val = asm(nums: (0, 0, val, val2)) {
1221+
nums: u256
1222+
};
1223+
assert(u256_val == asm_val);
1224+
}
1225+
}
1226+
}

0 commit comments

Comments
 (0)