Skip to content

Commit 13d8b38

Browse files
authored
Merge pull request #10 from clober-dex/feat/msb
feat: msb
2 parents 9ce159e + 68545c4 commit 13d8b38

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+39
-10587
lines changed

contracts/SignificantBit.sol

+32-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// SPDX-License-Identifier: -
2-
// License: https://license.clober.io/LICENSE.pdf
1+
// SPDX-License-Identifier: GPL-2.0-or-later
32

43
pragma solidity ^0.8.0;
54

@@ -22,4 +21,35 @@ library SignificantBit {
2221
}
2322
return uint8(DEBRUIJN_INDEX[index]); // can optimize with CODECOPY opcode
2423
}
24+
25+
function mostSignificantBit(uint256 x) internal pure returns (uint8) {
26+
require(x > 0);
27+
uint256 msb;
28+
assembly {
29+
let f := shl(7, gt(x, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))
30+
msb := or(msb, f)
31+
x := shr(f, x)
32+
f := shl(6, gt(x, 0xFFFFFFFFFFFFFFFF))
33+
msb := or(msb, f)
34+
x := shr(f, x)
35+
f := shl(5, gt(x, 0xFFFFFFFF))
36+
msb := or(msb, f)
37+
x := shr(f, x)
38+
f := shl(4, gt(x, 0xFFFF))
39+
msb := or(msb, f)
40+
x := shr(f, x)
41+
f := shl(3, gt(x, 0xFF))
42+
msb := or(msb, f)
43+
x := shr(f, x)
44+
f := shl(2, gt(x, 0xF))
45+
msb := or(msb, f)
46+
x := shr(f, x)
47+
f := shl(1, gt(x, 0x3))
48+
msb := or(msb, f)
49+
x := shr(f, x)
50+
f := gt(x, 0x1)
51+
msb := or(msb, f)
52+
}
53+
return uint8(msb);
54+
}
2555
}

contracts/mocks/SignificantBitWrapper.sol

+4
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ contract SignificantBitWrapper {
1111
function leastSignificantBit(uint256 x) external pure returns (uint8) {
1212
return x.leastSignificantBit();
1313
}
14+
15+
function mostSignificantBit(uint256 x) external pure returns (uint8) {
16+
return x.mostSignificantBit();
17+
}
1418
}

lib/forge-std

Submodule forge-std added at 4513bc2

lib/forge-std/.github/workflows/ci.yml

-92
This file was deleted.

lib/forge-std/.gitignore

-4
This file was deleted.

lib/forge-std/.gitmodules

-3
This file was deleted.

lib/forge-std/LICENSE-APACHE

-203
This file was deleted.

lib/forge-std/LICENSE-MIT

-25
This file was deleted.

0 commit comments

Comments
 (0)