Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize toString #3573

Merged
merged 18 commits into from
Aug 31, 2022
10 changes: 5 additions & 5 deletions contracts/utils/Strings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,23 @@ library Strings {

// compute log256(value), and add it to length
uint256 valueCopy = value;
if (valueCopy >> 128 > 0) {
if (valueCopy > 1 << 128 ) {
valueCopy >>= 128;
length += 16;
}
if (valueCopy >> 64 > 0) {
if (valueCopy > 1 << 64 ) {
valueCopy >>= 64;
length += 8;
}
if (valueCopy >> 32 > 0) {
if (valueCopy > 1 << 32 ) {
valueCopy >>= 32;
length += 4;
}
if (valueCopy >> 16 > 0) {
if (valueCopy > 1 << 16 ) {
valueCopy >>= 16;
length += 2;
}
if (valueCopy >> 8 > 0) {
if (valueCopy > 1 << 8 ) {
Amxx marked this conversation as resolved.
Show resolved Hide resolved
valueCopy >>= 8;
length += 1;
}
Expand Down
14 changes: 7 additions & 7 deletions contracts/utils/math/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -169,31 +169,31 @@ library Math {
// good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1;
uint256 x = a;
if (x >> 128 > 0) {
if (x > 1 << 128) {
x >>= 128;
result <<= 64;
}
if (x >> 64 > 0) {
if (x > 1 << 64) {
x >>= 64;
result <<= 32;
}
if (x >> 32 > 0) {
if (x > 1 << 32) {
x >>= 32;
result <<= 16;
}
if (x >> 16 > 0) {
if (x > 1 << 16) {
x >>= 16;
result <<= 8;
}
if (x >> 8 > 0) {
if (x > 1 << 8) {
x >>= 8;
result <<= 4;
}
if (x >> 4 > 0) {
if (x > 1 << 4) {
x >>= 4;
result <<= 2;
}
if (x >> 2 > 0) {
if (x > 1 << 2) {
result <<= 1;
}

Expand Down