Skip to content

Commit d0e8afa

Browse files
authored
feat(framework): add fun bitwise_not and macro fun max_value (#5589)
* feat(move-stdlib): add fun bitwise_not and macro fun max_value * chore: update compiled binaries and published api * chore: update snapshot * chore: update iota-swarm-config snapshot * chore: update manifest * fix: remove duplicate macro fun * chore: update compiled packages and swarm snapshot * chore: update snapshot and manifest * chore: update swarm snapshot * chore: update manifest
1 parent b52711c commit d0e8afa

File tree

19 files changed

+413
-19
lines changed

19 files changed

+413
-19
lines changed

crates/iota-framework-snapshot/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
]
4141
},
4242
"5": {
43-
"git_revision": "5195d8fc4cc4",
43+
"git_revision": "385270f268e5",
4444
"package_ids": [
4545
"0x0000000000000000000000000000000000000000000000000000000000000001",
4646
"0x0000000000000000000000000000000000000000000000000000000000000002",

crates/iota-framework/packages/move-stdlib/sources/macros.move

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,36 @@ module std::macros {
117117
range_do_eq!(0, $stop, $f)
118118
}
119119

120+
public macro fun try_as_u8($x: _): Option<u8> {
121+
let x = $x;
122+
if (x > 0xFF) option::none()
123+
else option::some(x as u8)
124+
}
125+
126+
public macro fun try_as_u16($x: _): Option<u16> {
127+
let x = $x;
128+
if (x > 0xFFFF) option::none()
129+
else option::some(x as u16)
130+
}
131+
132+
public macro fun try_as_u32($x: _): Option<u32> {
133+
let x = $x;
134+
if (x > 0xFFFF_FFFF) option::none()
135+
else option::some(x as u32)
136+
}
137+
138+
public macro fun try_as_u64($x: _): Option<u64> {
139+
let x = $x;
140+
if (x > 0xFFFF_FFFF_FFFF_FFFF) option::none()
141+
else option::some(x as u64)
142+
}
143+
144+
public macro fun try_as_u128($x: _): Option<u128> {
145+
let x = $x;
146+
if (x > 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF) option::none()
147+
else option::some(x as u128)
148+
}
149+
120150
/// Creates a fixed-point value from a quotient specified by its numerator and denominator.
121151
/// `$T` is the underlying integer type for the fixed-point value, where `$T` has `$t_bits` bits.
122152
/// `$U` is the type used for intermediate calculations, where `$U` is the next larger integer type.

crates/iota-framework/packages/move-stdlib/sources/u128.move

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
module std::u128 {
77
use std::string::String;
88

9+
/// Returns the bitwise not of the value.
10+
/// Each bit that is 1 becomes 0. Each bit that is 0 becomes 1.
11+
public fun bitwise_not(x: u128): u128 {
12+
x ^ max_value!()
13+
}
14+
915
/// Return the larger of `x` and `y`
1016
public fun max(x: u128, y: u128): u128 {
1117
std::macros::num_max!(x, y)
@@ -60,11 +66,36 @@ module std::u128 {
6066
std::macros::num_sqrt!<u128, u256>(x, 128)
6167
}
6268

69+
/// Try to convert a `u128` to a `u8`. Returns `None` if the value is too large.
70+
public fun try_as_u8(x: u128): Option<u8> {
71+
std::macros::try_as_u8!(x)
72+
}
73+
74+
/// Try to convert a `u128` to a `u16`. Returns `None` if the value is too large.
75+
public fun try_as_u16(x: u128): Option<u16> {
76+
std::macros::try_as_u16!(x)
77+
}
78+
79+
/// Try to convert a `u128` to a `u32`. Returns `None` if the value is too large.
80+
public fun try_as_u32(x: u128): Option<u32> {
81+
std::macros::try_as_u32!(x)
82+
}
83+
84+
/// Try to convert a `u128` to a `u64`. Returns `None` if the value is too large.
85+
public fun try_as_u64(x: u128): Option<u64> {
86+
std::macros::try_as_u64!(x)
87+
}
88+
6389
/// Convert `u128` value to string
6490
public fun to_string(x: u128): String {
6591
std::macros::num_to_string!(x)
6692
}
6793

94+
/// Maximum value for a `u128`
95+
public macro fun max_value(): u128 {
96+
0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF
97+
}
98+
6899
/// Loops applying `$f` to each number from `$start` to `$stop` (exclusive)
69100
public macro fun range_do($start: u128, $stop: u128, $f: |u128|) {
70101
std::macros::range_do!($start, $stop, $f)
@@ -85,8 +116,4 @@ module std::u128 {
85116
std::macros::do_eq!($stop, $f)
86117
}
87118

88-
/// Maximum value for a `u128`
89-
public macro fun max_value(): u128 {
90-
0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF
91-
}
92119
}

crates/iota-framework/packages/move-stdlib/sources/u16.move

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
module std::u16 {
77
use std::string::String;
88

9+
/// Returns the bitwise not of the value.
10+
/// Each bit that is 1 becomes 0. Each bit that is 0 becomes 1.
11+
public fun bitwise_not(x: u16): u16 {
12+
x ^ max_value!()
13+
}
14+
915
/// Return the larger of `x` and `y`
1016
public fun max(x: u16, y: u16): u16 {
1117
std::macros::num_max!(x, y)
@@ -60,11 +66,21 @@ module std::u16 {
6066
std::macros::num_sqrt!<u16, u32>(x, 16)
6167
}
6268

69+
/// Try to convert a `u16` to a `u8`. Returns `None` if the value is too large.
70+
public fun try_as_u8(x: u16): Option<u8> {
71+
std::macros::try_as_u8!(x)
72+
}
73+
6374
/// Convert `u16` value to string
6475
public fun to_string(x: u16): String {
6576
std::macros::num_to_string!(x)
6677
}
6778

79+
/// Maximum value for a `u16`
80+
public macro fun max_value(): u16 {
81+
0xFFFF
82+
}
83+
6884
/// Loops applying `$f` to each number from `$start` to `$stop` (exclusive)
6985
public macro fun range_do($start: u16, $stop: u16, $f: |u16|) {
7086
std::macros::range_do!($start, $stop, $f)

crates/iota-framework/packages/move-stdlib/sources/u256.move

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
module std::u256 {
77
use std::string::String;
88

9+
/// Returns the bitwise not of the value.
10+
/// Each bit that is 1 becomes 0. Each bit that is 0 becomes 1.
11+
public fun bitwise_not(x: u256): u256 {
12+
x ^ max_value!()
13+
}
14+
915
/// Return the larger of `x` and `y`
1016
public fun max(x: u256, y: u256): u256 {
1117
std::macros::num_max!(x, y)
@@ -31,11 +37,41 @@ module std::u256 {
3137
std::macros::num_pow!(base, exponent)
3238
}
3339

40+
/// Try to convert a `u256` to a `u8`. Returns `None` if the value is too large.
41+
public fun try_as_u8(x: u256): Option<u8> {
42+
std::macros::try_as_u8!(x)
43+
}
44+
45+
/// Try to convert a `u256` to a `u16`. Returns `None` if the value is too large.
46+
public fun try_as_u16(x: u256): Option<u16> {
47+
std::macros::try_as_u16!(x)
48+
}
49+
50+
/// Try to convert a `u256` to a `u32`. Returns `None` if the value is too large.
51+
public fun try_as_u32(x: u256): Option<u32> {
52+
std::macros::try_as_u32!(x)
53+
}
54+
55+
/// Try to convert a `u256` to a `u64`. Returns `None` if the value is too large.
56+
public fun try_as_u64(x: u256): Option<u64> {
57+
std::macros::try_as_u64!(x)
58+
}
59+
60+
/// Try to convert a `u256` to a `u128`. Returns `None` if the value is too large.
61+
public fun try_as_u128(x: u256): Option<u128> {
62+
std::macros::try_as_u128!(x)
63+
}
64+
3465
/// Convert `u256` value to string
3566
public fun to_string(x: u256): String {
3667
std::macros::num_to_string!(x)
3768
}
3869

70+
/// Maximum value for a `u256`
71+
public macro fun max_value(): u256 {
72+
0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF
73+
}
74+
3975
/// Loops applying `$f` to each number from `$start` to `$stop` (exclusive)
4076
public macro fun range_do($start: u256, $stop: u256, $f: |u256|) {
4177
std::macros::range_do!($start, $stop, $f)

crates/iota-framework/packages/move-stdlib/sources/u32.move

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
module std::u32 {
77
use std::string::String;
88

9+
/// Returns the bitwise not of the value.
10+
/// Each bit that is 1 becomes 0. Each bit that is 0 becomes 1.
11+
public fun bitwise_not(x: u32): u32 {
12+
x ^ max_value!()
13+
}
14+
915
/// Return the larger of `x` and `y`
1016
public fun max(x: u32, y: u32): u32 {
1117
std::macros::num_max!(x, y)
@@ -60,6 +66,16 @@ module std::u32 {
6066
std::macros::num_sqrt!<u32, u64>(x, 32)
6167
}
6268

69+
/// Try to convert a `u32` to a `u8`. Returns `None` if the value is too large.
70+
public fun try_as_u8(x: u32): Option<u8> {
71+
std::macros::try_as_u8!(x)
72+
}
73+
74+
/// Try to convert a `u32` to a `u16`. Returns `None` if the value is too large.
75+
public fun try_as_u16(x: u32): Option<u16> {
76+
std::macros::try_as_u16!(x)
77+
}
78+
6379
/// Convert `u32` value to string
6480
public fun to_string(x: u32): String {
6581
std::macros::num_to_string!(x)

crates/iota-framework/packages/move-stdlib/sources/u64.move

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
module std::u64 {
77
use std::string::String;
88

9+
/// Returns the bitwise not of the value.
10+
/// Each bit that is 1 becomes 0. Each bit that is 0 becomes 1.
11+
public fun bitwise_not(x: u64): u64 {
12+
x ^ max_value!()
13+
}
14+
915
/// Return the larger of `x` and `y`
1016
public fun max(x: u64, y: u64): u64 {
1117
std::macros::num_max!(x, y)
@@ -60,6 +66,21 @@ module std::u64 {
6066
std::macros::num_sqrt!<u64, u128>(x, 64)
6167
}
6268

69+
/// Try to convert a `u64` to a `u8`. Returns `None` if the value is too large.
70+
public fun try_as_u8(x: u64): Option<u8> {
71+
std::macros::try_as_u8!(x)
72+
}
73+
74+
/// Try to convert a `u64` to a `u16`. Returns `None` if the value is too large.
75+
public fun try_as_u16(x: u64): Option<u16> {
76+
std::macros::try_as_u16!(x)
77+
}
78+
79+
/// Try to convert a `u64` to a `u32`. Returns `None` if the value is too large.
80+
public fun try_as_u32(x: u64): Option<u32> {
81+
std::macros::try_as_u32!(x)
82+
}
83+
6384
/// Convert `u64` value to string
6485
public fun to_string(x: u64): String {
6586
std::macros::num_to_string!(x)

crates/iota-framework/packages/move-stdlib/sources/u8.move

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
module std::u8 {
77
use std::string::String;
88

9+
/// Returns the bitwise not of the value.
10+
/// Each bit that is 1 becomes 0. Each bit that is 0 becomes 1.
11+
public fun bitwise_not(x: u8): u8 {
12+
x ^ max_value!()
13+
}
14+
915
/// Return the larger of `x` and `y`
1016
public fun max(x: u8, y: u8): u8 {
1117
std::macros::num_max!(x, y)
@@ -65,6 +71,11 @@ module std::u8 {
6571
std::macros::num_to_string!(x)
6672
}
6773

74+
/// Maximum value for a `u8`
75+
public macro fun max_value(): u8 {
76+
0xFF
77+
}
78+
6879
/// Loops applying `$f` to each number from `$start` to `$stop` (exclusive)
6980
public macro fun range_do($start: u8, $stop: u8, $f: |u8|) {
7081
std::macros::range_do!($start, $stop, $f)

crates/iota-framework/packages/move-stdlib/tests/integer_tests.move

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,28 @@ module std::integer_tests {
1818
}
1919
}
2020

21+
public(package) macro fun test_bitwise_not($max: _, $cases: vector<_>) {
22+
let max = $max;
23+
let cases = $cases;
24+
assert_eq!(max.bitwise_not(), 0);
25+
cases!(max, cases, |case_pred, case, case_succ| {
26+
assert_eq!(case_pred.bitwise_not().bitwise_not(), case_pred);
27+
assert_eq!(case_pred.bitwise_not() | case_pred, max);
28+
assert_eq!(case_pred.bitwise_not() ^ case_pred, max);
29+
assert_eq!(case_pred.bitwise_not() & case_pred, 0);
30+
31+
assert_eq!(case.bitwise_not().bitwise_not(), case);
32+
assert_eq!(case.bitwise_not() | case, max);
33+
assert_eq!(case.bitwise_not() ^ case, max);
34+
assert_eq!(case.bitwise_not() & case, 0);
35+
36+
assert_eq!(case_succ.bitwise_not().bitwise_not(), case_succ);
37+
assert_eq!(case_succ.bitwise_not() | case_succ, max);
38+
assert_eq!(case_succ.bitwise_not() ^ case_succ, max);
39+
assert_eq!(case_succ.bitwise_not() & case_succ, 0);
40+
})
41+
}
42+
2143
public(package) macro fun test_max($max: _, $cases: vector<_>) {
2244
let max = $max;
2345
let cases = $cases;
@@ -161,6 +183,57 @@ module std::integer_tests {
161183
}
162184
}
163185

186+
public(package) macro fun test_try_as_u8<$T>($max: $T) {
187+
assert_eq!((0: $T).try_as_u8(), option::some(0));
188+
assert_eq!((1: $T).try_as_u8(), option::some(1));
189+
assert_eq!((0xFF: $T).try_as_u8(), option::some(0xFF));
190+
assert_eq!((0xFF + 1: $T).try_as_u8(), option::none());
191+
let max = $max;
192+
assert_eq!(max.try_as_u8(), option::none());
193+
}
194+
195+
public(package) macro fun test_try_as_u16<$T>($max: $T) {
196+
assert_eq!((0: $T).try_as_u16(), option::some(0));
197+
assert_eq!((1: $T).try_as_u16(), option::some(1));
198+
assert_eq!((0xFFFF: $T).try_as_u16(), option::some(0xFFFF));
199+
assert_eq!((0xFFFF + 1: $T).try_as_u16(), option::none());
200+
let max = $max;
201+
assert_eq!(max.try_as_u16(), option::none());
202+
}
203+
204+
public(package) macro fun test_try_as_u32<$T>($max: $T) {
205+
assert_eq!((0: $T).try_as_u32(), option::some(0));
206+
assert_eq!((1: $T).try_as_u32(), option::some(1));
207+
assert_eq!((0xFFFF_FFFF: $T).try_as_u32(), option::some(0xFFFF_FFFF));
208+
assert_eq!((0xFFFF_FFFF + 1: $T).try_as_u32(), option::none());
209+
let max = $max;
210+
assert_eq!(max.try_as_u32(), option::none());
211+
}
212+
213+
public(package) macro fun test_try_as_u64<$T>($max: $T) {
214+
assert_eq!((0: $T).try_as_u64(), option::some(0));
215+
assert_eq!((1: $T).try_as_u64(), option::some(1));
216+
assert_eq!((0xFFFF_FFFF_FFFF_FFFF: $T).try_as_u64(), option::some(0xFFFF_FFFF_FFFF_FFFF));
217+
assert_eq!((0xFFFF_FFFF_FFFF_FFFF + 1: $T).try_as_u64(), option::none());
218+
let max = $max;
219+
assert_eq!(max.try_as_u64(), option::none());
220+
}
221+
222+
public(package) macro fun test_try_as_u128<$T>($max: $T) {
223+
assert_eq!((0: $T).try_as_u128(), option::some(0));
224+
assert_eq!((1: $T).try_as_u128(), option::some(1));
225+
assert_eq!(
226+
(0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF: $T).try_as_u128(),
227+
option::some(0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF)
228+
);
229+
assert_eq!(
230+
(0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF + 1: $T).try_as_u128(),
231+
option::none()
232+
);
233+
let max = $max;
234+
assert_eq!(max.try_as_u128(), option::none());
235+
}
236+
164237
public(package) macro fun sum_range<$T>($n: $T): $T {
165238
let n = $n;
166239
(n * (n + 1)) / 2

0 commit comments

Comments
 (0)