Skip to content

Commit 83f9c18

Browse files
Merge pull request #8 from CetusProtocol/optimization
integer mate optimization
2 parents 06660f7 + c502cff commit 83f9c18

File tree

9 files changed

+242
-51
lines changed

9 files changed

+242
-51
lines changed

aptos/sources/i128.move

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,15 @@ module integer_mate::i128 {
8585
}
8686

8787
public fun sub(num1: I128, num2: I128): I128 {
88-
let sub_num = wrapping_add(I128 {
89-
bits: u128_neg(num2.bits)
90-
}, from(1));
91-
add(num1, sub_num)
88+
let (v, overflow) = overflowing_sub(num1, num2);
89+
assert!(!overflow, EOverflow);
90+
v
9291
}
9392

9493
public fun overflowing_sub(num1: I128, num2: I128): (I128, bool) {
95-
let sub_num = wrapping_add(I128 {
96-
bits: u128_neg(num2.bits)
97-
}, from(1));
98-
let sum = wrapping_add(num1, sub_num);
99-
let overflow = (sign(num1) & sign(sub_num) & u8_neg(sign(sum))) + (u8_neg(sign(num1)) & u8_neg(sign(sub_num)) & sign(sum));
100-
(sum, overflow != 0)
94+
let v = wrapping_sub(num1, num2);
95+
let overflow = sign(num1) != sign(num2) && sign(num1) != sign(v);
96+
(v, overflow)
10197
}
10298

10399
public fun mul(num1: I128, num2: I128): I128 {

aptos/sources/i32.move

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ module integer_mate::i32 {
7474
}
7575

7676
public fun sub(num1: I32, num2: I32): I32 {
77-
let sub_num = wrapping_add(I32 {
78-
bits: u32_neg(num2.bits)
79-
}, from(1));
80-
add(num1, sub_num)
77+
let v = wrapping_sub(num1, num2);
78+
let overflow = sign(num1) != sign(num2) && sign(num1) != sign(v);
79+
assert!(!overflow, EOverflow);
80+
v
8181
}
8282

8383
public fun mul(num1: I32, num2: I32): I32 {

aptos/sources/i64.move

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ module integer_mate::i64 {
7777
}
7878

7979
public fun sub(num1: I64, num2: I64): I64 {
80-
let sub_num = wrapping_add(I64 {
81-
bits: u64_neg(num2.bits)
82-
}, from(1));
83-
add(num1, sub_num)
80+
let v = wrapping_sub(num1, num2);
81+
let overflow = sign(num1) != sign(num2) && sign(num1) != sign(v);
82+
assert!(!overflow, EOverflow);
83+
v
8484
}
8585

8686
public fun mul(num1: I64, num2: I64): I64 {

sui/Move.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ dependencies = [
2121
]
2222

2323
[move.toolchain-version]
24-
compiler-version = "1.48.2"
24+
compiler-version = "1.48.4"
2525
edition = "2024"
2626
flavor = "sui"

sui/Move.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# -------------------------------
33
[package]
44
name = "IntegerMate"
5-
version = "1.1.2"
6-
published-at = "0x991a8ab5ccc7af04c5d1327aaff520a074e1444bf7fcde2fcfcb0ad58b9c2af4"
5+
version = "1.2.0"
6+
published-at = "0xf1e4fae2183dabebc4827b0a46ddae9c0d0a6afa06a416e1f887a4ffd33f5067"
77

88
[dependencies]
99
Sui = { git = "https://github.com/MystenLabs/sui.git", subdir = "crates/sui-framework/packages/sui-framework", rev = "mainnet-v1.48.2" }

sui/sources/i128.move

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,15 @@ module integer_mate::i128 {
8484
}
8585

8686
public fun sub(num1: I128, num2: I128): I128 {
87-
let sub_num = wrapping_add(I128 {
88-
bits: u128_neg(num2.bits)
89-
}, from(1));
90-
add(num1, sub_num)
87+
let (v, overflow) = overflowing_sub(num1, num2);
88+
assert!(!overflow, EOverflow);
89+
v
9190
}
9291

9392
public fun overflowing_sub(num1: I128, num2: I128): (I128, bool) {
94-
let sub_num = wrapping_add(I128 {
95-
bits: u128_neg(num2.bits)
96-
}, from(1));
97-
let sum = wrapping_add(num1, sub_num);
98-
let overflow = (sign(num1) & sign(sub_num) & u8_neg(sign(sum))) + (u8_neg(sign(num1)) & u8_neg(sign(sub_num)) & sign(sum));
99-
(sum, overflow != 0)
93+
let v = wrapping_sub(num1, num2);
94+
let overflow = sign(num1) != sign(num2) && sign(num1) != sign(v);
95+
(v, overflow)
10096
}
10197

10298
public fun mul(num1: I128, num2: I128): I128 {
@@ -336,16 +332,42 @@ module integer_mate::i128 {
336332
assert!(overflow == false && as_u128(result) == MAX_AS_U128 - 1, 1);
337333
let (_, overflow) = overflowing_add(from(MAX_AS_U128), from(1));
338334
assert!(overflow == true, 1);
335+
let (_, overflow) = overflowing_add(from(1), from(MAX_AS_U128));
336+
assert!(overflow == true, 1);
339337
let (_, overflow) = overflowing_add(neg_from(MIN_AS_U128), neg_from(1));
340338
assert!(overflow == true, 1);
339+
let (_, overflow) = overflowing_add(neg_from(1), neg_from(MIN_AS_U128));
340+
assert!(overflow == true, 1);
341+
}
342+
343+
#[test]
344+
fun test_overflowing_sub() {
345+
let (_result, overflow) = overflowing_sub(from(0), neg_from(MIN_AS_U128));
346+
assert!(overflow == true, 1);
347+
let (_result, overflow) = overflowing_sub(from(1), neg_from(MIN_AS_U128));
348+
assert!(overflow == true, 1);
349+
let (_result, overflow) = overflowing_sub(neg_from(MIN_AS_U128), from(1));
350+
assert!(overflow == true, 1);
351+
let (_result, overflow) = overflowing_sub(neg_from(MIN_AS_U128), from(0));
352+
assert!(overflow == false, 1);
353+
let (_result, overflow) = overflowing_sub(from(MAX_AS_U128), neg_from(1));
354+
assert!(overflow == true, 1);
355+
let (_result, overflow) = overflowing_sub(neg_from(2), from(MAX_AS_U128));
356+
assert!(overflow == true, 1);
341357
}
342358

343359
#[test]
344360
#[expected_failure]
345-
fun test_add_overflow() {
361+
fun test_add_overflow_max_1() {
346362
add(from(MAX_AS_U128), from(1));
347363
}
348364

365+
#[test]
366+
#[expected_failure]
367+
fun test_add_overflow_1_max() {
368+
add(from(1), from(MAX_AS_U128));
369+
}
370+
349371
#[test]
350372
#[expected_failure]
351373
fun test_add_underflow() {
@@ -400,16 +422,68 @@ module integer_mate::i128 {
400422

401423
#[test]
402424
#[expected_failure]
403-
fun test_sub_overflow() {
404-
sub(from(MAX_AS_U128), neg_from(1));
425+
fun test_sub_overflow_0_min() {
426+
// 1 - i32::MIN
427+
sub(from(0), neg_from(MIN_AS_U128));
405428
}
406429

407430
#[test]
408431
#[expected_failure]
409-
fun test_sub_underflow() {
432+
fun test_sub_overflow_1_min() {
433+
// 1 - i32::MIN
434+
sub(from(1), neg_from(MIN_AS_U128));
435+
}
436+
437+
#[test]
438+
#[expected_failure]
439+
fun test_sub_overflow_min_1() {
410440
sub(neg_from(MIN_AS_U128), from(1));
411441
}
412442

443+
#[test]
444+
#[expected_failure]
445+
fun test_sub_overflow_max_n1() {
446+
sub(from(MAX_AS_U128), neg_from(1));
447+
}
448+
449+
#[test]
450+
#[expected_failure]
451+
fun test_sub_overflow_n2_max() {
452+
sub(neg_from(2), from(MAX_AS_U128));
453+
}
454+
455+
456+
#[test]
457+
#[expected_failure]
458+
fun test_sub_overflow_10000_min() {
459+
//10000 - i32::MIN
460+
sub(from(10000), neg_from(MIN_AS_U128));
461+
}
462+
463+
#[test]
464+
#[expected_failure]
465+
fun test_sub_overflow_min_10000() {
466+
sub(neg_from(MIN_AS_U128), from(10000));
467+
}
468+
469+
#[test]
470+
#[expected_failure]
471+
fun test_sub_overflow_min_max() {
472+
sub(neg_from(MIN_AS_U128), from(MAX_AS_U128));
473+
}
474+
475+
#[test]
476+
#[expected_failure]
477+
fun test_add_overflow_n1_min() {
478+
add(neg_from(1), neg_from(MIN_AS_U128));
479+
}
480+
481+
#[test]
482+
#[expected_failure]
483+
fun test_add_overflow_min_n1() {
484+
add(neg_from(MIN_AS_U128), neg_from(1));
485+
}
486+
413487
#[test]
414488
fun test_mul() {
415489
assert!(as_u128(mul(from(1), from(1))) == 1, 0);

sui/sources/i32.move

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ module integer_mate::i32 {
7474
}
7575

7676
public fun sub(num1: I32, num2: I32): I32 {
77-
let sub_num = wrapping_add(I32 {
78-
bits: u32_neg(num2.bits)
79-
}, from(1));
80-
add(num1, sub_num)
77+
let v = wrapping_sub(num1, num2);
78+
let overflow = sign(num1) != sign(num2) && sign(num1) != sign(v);
79+
assert!(!overflow, EOverflow);
80+
v
8181
}
8282

8383
public fun mul(num1: I32, num2: I32): I32 {
@@ -335,6 +335,10 @@ module integer_mate::i32 {
335335

336336
#[test]
337337
fun test_sub() {
338+
assert!(as_u32(sub(neg_from(1), neg_from(MIN_AS_U32))) == 2147483647, 0);
339+
assert!(as_u32(sub(neg_from(MIN_AS_U32), neg_from(1))) == 2147483649, 0);
340+
assert!(as_u32(sub(neg_from(10), neg_from(MIN_AS_U32))) == 2147483638, 0);
341+
assert!(as_u32(sub(neg_from(MIN_AS_U32), neg_from(10))) == 2147483658, 0);
338342
assert!(as_u32(sub(from(0), from(0))) == 0, 0);
339343
assert!(as_u32(sub(from(1), from(0))) == 1, 0);
340344
assert!(as_u32(sub(from(0), from(1))) == as_u32(neg_from(1)), 0);
@@ -351,16 +355,68 @@ module integer_mate::i32 {
351355

352356
#[test]
353357
#[expected_failure]
354-
fun test_sub_overflow() {
355-
sub(from(MAX_AS_U32), neg_from(1));
358+
fun test_sub_overflow_0_min() {
359+
// 1 - i32::MIN
360+
sub(from(0), neg_from(MIN_AS_U32));
361+
}
362+
363+
#[test]
364+
#[expected_failure]
365+
fun test_sub_overflow_1_min() {
366+
// 1 - i32::MIN
367+
sub(from(1), neg_from(MIN_AS_U32));
356368
}
357369

358370
#[test]
359371
#[expected_failure]
360-
fun test_sub_underflow() {
372+
fun test_sub_overflow_min_1() {
361373
sub(neg_from(MIN_AS_U32), from(1));
362374
}
363375

376+
#[test]
377+
#[expected_failure]
378+
fun test_sub_overflow_max_n1() {
379+
sub(from(MAX_AS_U32), neg_from(1));
380+
}
381+
382+
#[test]
383+
#[expected_failure]
384+
fun test_sub_overflow_n2_max() {
385+
sub(neg_from(2), from(MAX_AS_U32));
386+
}
387+
388+
389+
#[test]
390+
#[expected_failure]
391+
fun test_sub_overflow_10000_min() {
392+
//10000 - i32::MIN
393+
sub(from(10000), neg_from(MIN_AS_U32));
394+
}
395+
396+
#[test]
397+
#[expected_failure]
398+
fun test_sub_overflow_min_10000() {
399+
sub(neg_from(MIN_AS_U32), from(10000));
400+
}
401+
402+
#[test]
403+
#[expected_failure]
404+
fun test_sub_overflow_min_max() {
405+
sub(neg_from(MIN_AS_U32), from(MAX_AS_U32));
406+
}
407+
408+
#[test]
409+
#[expected_failure]
410+
fun test_add_overflow_n1_min() {
411+
add(neg_from(1), neg_from(MIN_AS_U32));
412+
}
413+
414+
#[test]
415+
#[expected_failure]
416+
fun test_add_overflow_min_n1() {
417+
add(neg_from(MIN_AS_U32), neg_from(1));
418+
}
419+
364420
#[test]
365421
fun test_mul() {
366422
assert!(as_u32(mul(from(1), from(1))) == 1, 0);

0 commit comments

Comments
 (0)