Skip to content

Commit

Permalink
style: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
MathisGD committed Sep 4, 2023
1 parent 8baaeda commit 831e3a4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/irm/libraries/ErrorsLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.0;
library ErrorsLib {
string internal constant MAX_INT128_EXCEEDED = "max int128 exceeded";
string internal constant INPUT_TOO_LARGE = "input too large";
string internal constant WEXP_UNDERFLOW = "wExp underflow";
string internal constant WEXP_OVERFLOW = "wExp overflow";
string internal constant NOT_MORPHO = "not Morpho";
}
7 changes: 6 additions & 1 deletion src/irm/libraries/MathLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ library MathLib {
unchecked {
// Revert if x > ln(2^256-1) ~ 177.
require(x <= 177.44567822334599921 ether, ErrorsLib.WEXP_OVERFLOW);
// Revert if x > min / 1e18.
require(x >= type(int256).min + 1 ether, ErrorsLib.WEXP_UNDERFLOW);

// Decompose x as x = q * ln(2) + r with q an integer and -ln(2)/2 < r <= ln(2)/2.
int256 q = (x + (LN2_INT / 2)) / LN2_INT;
// q = x / ln(2) rounded half toward zero.
int256 offset = (x < 0) ? - (LN2_INT / 2) : (LN2_INT / 2);
// Safe unchecked because x is bounded.
int256 q = (x + offset) / LN2_INT;
// Safe unchecked because |q * LN2_INT| <= x.
int256 r = x - q * LN2_INT;

Expand Down
7 changes: 3 additions & 4 deletions test/irm/MathLibTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ contract MathLibTest is Test {
using MathLib for uint256;

function testWExp(int256 x) public {
vm.assume(x > -176 ether);
vm.assume(x < 135305999368893231589);
assertApproxEqRel(int256(MathLib.wExp(x)), wadExp(x), 0.05 ether);
x = bound(x, - 27 ether, 94 ether);
assertApproxEqRel(MathLib.wExp(x), uint256(wadExp(x)), 0.01 ether);
}

function testWExpSmall(int256 x) public {
vm.assume(x <= -178 ether);
x = bound(x, type(int256).min + 1 ether, -178 ether);
assertEq(MathLib.wExp(x), 0);
}

Expand Down

0 comments on commit 831e3a4

Please sign in to comment.