Skip to content

Commit

Permalink
Rolled back PVE002, added further optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
The3D committed Oct 22, 2020
1 parent 19756cd commit d05a2d1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3,145 deletions.
32 changes: 8 additions & 24 deletions contracts/libraries/math/WadRayMath.sol
Expand Up @@ -54,17 +54,13 @@ library WadRayMath {
* @return the result of a*b, in wad
**/
function wadMul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0 || b == 0) {
if (a == 0) {
return 0;
}

uint256 result = a * b;

require(result / a == b, Errors.MULTIPLICATION_OVERFLOW);
uint256 result = a * b + halfWAD;

result += halfWAD;

require(result >= halfWAD, Errors.ADDITION_OVERFLOW);
require((result - halfWAD) / a == b, Errors.MULTIPLICATION_OVERFLOW);

return result / WAD;
}
Expand Down Expand Up @@ -102,17 +98,13 @@ library WadRayMath {
* @return the result of a*b, in ray
**/
function rayMul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0 || b == 0) {
if (a == 0) {
return 0;
}

uint256 result = a * b;
uint256 result = a * b + halfRAY;

require(result / a == b, Errors.MULTIPLICATION_OVERFLOW);

result += halfRAY;

require(result >= halfRAY, Errors.ADDITION_OVERFLOW);
require((result - halfRAY) / a == b, Errors.MULTIPLICATION_OVERFLOW);

return result / RAY;
}
Expand All @@ -126,19 +118,11 @@ library WadRayMath {
function rayDiv(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, Errors.DIVISION_BY_ZERO);

if (a == 0) {
return 0;
}

uint256 halfB = b / 2;

uint256 result = a * RAY;

require(result / RAY == a, Errors.MULTIPLICATION_OVERFLOW);
uint256 result = a * RAY + halfB;

result += halfB;

require(result >= halfB, Errors.ADDITION_OVERFLOW);
require((result - halfB) / RAY == a, Errors.MULTIPLICATION_OVERFLOW);

return result / b;
}
Expand Down

0 comments on commit d05a2d1

Please sign in to comment.