Skip to content

Commit 6f26f20

Browse files
committed
👯 Add more uint128x2 methods
1 parent 49f2cc4 commit 6f26f20

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

kimlikdao/IDistroStage.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ enum DistroStage {
66
Presale1,
77
Presale2,
88
ProtocolSaleStart,
9-
ProtocolSaleEnd,
9+
ProtocolSaleEnd, // Unlocks Presale1
1010
ProtocolAMMStart,
11-
Presale2Unlock,
11+
Presale2Unlock, // Unlocks Presale2
1212
FinalMint,
1313
FinalUnlock
1414
}

types/uint128x2.sol

+15-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,22 @@ function dec(uint128x2 self, uint256 delta) pure returns (uint128x2) {
3434
}
3535
}
3636

37+
function sum(uint128x2 self) pure returns (uint256) {
38+
unchecked {
39+
return (uint128x2.unwrap(self) >> 128) + uint128(uint128x2.unwrap(self));
40+
}
41+
}
42+
43+
function clearLo(uint128x2 self) pure returns (uint128x2) {
44+
return uint128x2.wrap(uint128x2.unwrap(self) & ~type(uint128).max);
45+
}
46+
47+
function clearHi(uint128x2 self) pure returns (uint128x2) {
48+
return uint128x2.wrap(uint128x2.unwrap(self) & type(uint128).max);
49+
}
50+
3751
function equal(uint128x2 self, uint128x2 other) pure returns (bool) {
3852
return uint128x2.unwrap(self) == uint128x2.unwrap(other);
3953
}
4054

41-
using {hi, lo, decLo, inc, dec, equal as ==} for uint128x2 global;
55+
using {hi, lo, decLo, inc, dec, sum, clearLo, clearHi, equal as ==} for uint128x2 global;

types/uint128x2.t.sol

+24
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,28 @@ contract uint128x2Test is Test {
3131

3232
assert(pair2 == pair3);
3333
}
34+
35+
function tesetClear() public pure {
36+
uint128x2 i = uint128x2From(2, 1);
37+
38+
i = i.clearHi();
39+
assertEq(i.hi(), 0);
40+
assertEq(i.lo(), 1);
41+
42+
uint128x2 j = uint128x2From(3, 2);
43+
44+
j = j.clearLo();
45+
j = j.clearLo();
46+
assertEq(j.hi(), 3);
47+
assertEq(j.lo(), 0);
48+
}
49+
50+
function testSum() public pure {
51+
uint128x2 i = uint128x2From(type(uint128).max - 1, 1);
52+
53+
assertEq(i.sum(), type(uint128).max);
54+
assertEq(uint128x2From(1, 2).sum(), 3);
55+
assertEq(uint128x2From(2, 3).sum(), 5);
56+
assertEq(uint128x2From(1337, 1338).sum(), 2 * 1337 + 1);
57+
}
3458
}

0 commit comments

Comments
 (0)