Skip to content

Commit

Permalink
Minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
3sGgpQ8H committed Sep 1, 2019
1 parent 5f081f1 commit 2b46e39
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
39 changes: 17 additions & 22 deletions ABDKMath64x64.sol
Original file line number Diff line number Diff line change
Expand Up @@ -366,31 +366,26 @@ library ABDKMath64x64 {
function log_2 (int128 x) internal pure returns (int128) {
require (x > 0);

int128 a = 0;
int128 b = 126;
while (a < b) {
int128 m = a + b >> 1;
int128 t = x >> m;
if (t == 0) b = m - 1;
else if (t > 1) a = m + 1;
else {
a = m;
break;
}
}

int128 result = a - 64 << 64;
uint256 ux = uint256 (x) << 127 - a;
for (int128 bit = 0x8000000000000000; bit > 0; bit >>= 1) {
int256 msb = 0;
int256 xc = x;
if (xc >= 0x10000000000000000) { xc >>= 64; msb += 64; }
if (xc >= 0x100000000) { xc >>= 32; msb += 32; }
if (xc >= 0x10000) { xc >>= 16; msb += 16; }
if (xc >= 0x100) { xc >>= 8; msb += 8; }
if (xc >= 0x10) { xc >>= 4; msb += 4; }
if (xc >= 0x4) { xc >>= 2; msb += 2; }
if (xc >= 0x2) msb += 1; // No need to shift xc anymore

int256 result = msb - 64 << 64;
uint256 ux = uint256 (x) << 127 - msb;
for (int256 bit = 0x8000000000000000; bit > 0; bit >>= 1) {
ux *= ux;
if (ux >=
0x8000000000000000000000000000000000000000000000000000000000000000) {
ux >>= 128;
result += bit;
} else ux >>= 127;
uint256 b = ux >> 255;
ux >>= 127 + b;
result += bit * int256 (b);
}

return result;
return int128 (result);
}

/**
Expand Down
39 changes: 16 additions & 23 deletions ABDKMathQuad.sol
Original file line number Diff line number Diff line change
Expand Up @@ -911,20 +911,15 @@ library ABDKMathQuad {
resultSignifier <<= shift;
resultExponent -= shift;
} else {
uint256 bb = resultNegative ? 1 : 0;
while (resultSignifier < 0x10000000000000000000000000000) {
resultSignifier <<= 1;
resultExponent -= 1;

xSignifier = xSignifier * xSignifier;
if (xSignifier >= 0x8000000000000000000000000000000000000000000000000000000000000000) {
if (!resultNegative)
resultSignifier += 1;
xSignifier >>= 128;
} else {
if (resultNegative)
resultSignifier += 1;
xSignifier >>= 127;
}
xSignifier *= xSignifier;
uint256 b = xSignifier >> 255;
resultSignifier += b ^ bb;
xSignifier >>= 127 + b;
}
}

Expand Down Expand Up @@ -1145,19 +1140,17 @@ library ABDKMathQuad {
function msb (uint256 x) private pure returns (uint256) {
require (x > 0);

uint256 a = 0;
uint256 b = 255;
while (a < b) {
uint256 m = a + b >> 1;
uint256 t = x >> m;
if (t == 0) b = m - 1;
else if (t > 1) a = m + 1;
else {
a = m;
break;
}
}
uint256 result = 0;

if (x >= 0x100000000000000000000000000000000) { x >>= 128; result += 128; }
if (x >= 0x10000000000000000) { x >>= 64; result += 64; }
if (x >= 0x100000000) { x >>= 32; result += 32; }
if (x >= 0x10000) { x >>= 16; result += 16; }
if (x >= 0x100) { x >>= 8; result += 8; }
if (x >= 0x10) { x >>= 4; result += 4; }
if (x >= 0x4) { x >>= 2; result += 2; }
if (x >= 0x2) result += 1; // No need to shift x anymore

return a;
return result;
}
}

0 comments on commit 2b46e39

Please sign in to comment.