don't pack prices as much

This commit is contained in:
Noah Zinsmeister
2019-12-05 13:28:23 -05:00
parent 607b1c6e6e
commit b284b63118
10 changed files with 122 additions and 207 deletions

View File

@ -1,6 +1,20 @@
pragma solidity 0.5.12;
library Math {
function add512(uint x0, uint64 x1, uint y0, uint64 y1) internal pure returns (uint z0, uint64 z1) {
assembly {
z0 := add(x0, y0)
z1 := add(add(x1, y1), lt(z0, x0))
}
}
function mul512(uint x, uint64 y) internal pure returns (uint z0, uint64 z1) {
assembly {
let mm := mulmod(x, y, not(0))
z0 := mul(x, y)
z1 := sub(sub(mm, z0), lt(mm, z0))
}
}
function min(uint x, uint y) internal pure returns (uint z) {
return x <= y ? x : y;
}

View File

@ -14,14 +14,4 @@ library SafeMath {
function clamp128(uint y) internal pure returns (uint128 z) {
z = y <= uint128(-1) ? uint128(y) : uint128(-1);
}
function downcast128(uint y) internal pure returns (uint128 z) {
require(y <= uint128(-1), "downcast-128-overflow");
z = uint128(y);
}
function downcast32(uint y) internal pure returns (uint32 z) {
require(y <= uint32(-1), "downcast-32-overflow");
z = uint32(y);
}
}

View File

@ -1,13 +0,0 @@
pragma solidity 0.5.12;
library SafeMath128 {
function add(uint128 x, uint128 y) internal pure returns (uint128 z) {
require((z = x + y) >= x, "ds-math-add-overflow");
}
function sub(uint128 x, uint128 y) internal pure returns (uint128 z) {
require((z = x - y) <= x, "ds-math-sub-underflow");
}
function mul(uint128 x, uint128 y) internal pure returns (uint128 z) {
require(y == 0 || (z = x * y) / y == x, "ds-math-mul-overflow");
}
}

View File

@ -1,33 +0,0 @@
pragma solidity 0.5.12;
// helpful links
// https://en.wikipedia.org/wiki/Q_(number_format)
// https://github.com/abdk-consulting/abdk-libraries-solidity/blob/master/ABDKMath64x64.md
// https://github.com/gnosis/solidity-arithmetic
library UQ104x104 {
uint240 constant Q104 = 2**104;
// we want to encode a uint128 `y` s.t. `y := y_encoded / 2**104` (i.e. with a Q104 denominator).
// in other words, to encode `y` we simply multiply by `2**104`, aka Q104.
// in the case of a traditional UQ104.104, we'd store this output in a 208-bit slot,
// but since we're encoding a uint128, this would overflow for values of `y` in (`uint104(-1)`, `uint128(-1)`],
// so instead we need to store the output in at least 232 bits (we use 240 for compatibility later on)
function encode(uint128 y) internal pure returns (uint240 z) {
return uint240(y) * Q104;
}
// we want to divide a modified UQ104.104 (the output of encode above) by an unencoded uint128 and return another
// modified UQ104.104. to do this, it's sufficient to divide the UQ104.104 by the unencoded value.
// since we want our output to fit in 208 bits, and behave consistently at the margins, we clamp this quotient
// within [1, uint208(-1)]
function qdiv(uint240 x, uint128 y) internal pure returns (uint240 z) {
z = x / y;
if (z == 0) {
z = 1;
} else if (z > uint208(-1)) {
z = uint208(-1);
}
}
}

View File

@ -0,0 +1,22 @@
pragma solidity 0.5.12;
// helpful links
// https://en.wikipedia.org/wiki/Q_(number_format)
// https://github.com/abdk-consulting/abdk-libraries-solidity/blob/master/ABDKMath64x64.md
// https://github.com/gnosis/solidity-arithmetic
library UQ128x128 {
uint constant Q128 = 2**128;
// we want to encode a uint128 `y` s.t. `y := y_encoded / 2**128` (i.e. with a Q128 denominator).
// in other words, to encode `y` we simply multiply by `2**128`, aka Q104, and store this in a 208-bit slot.
function encode(uint128 y) internal pure returns (uint z) {
return uint(y) * Q128; // guaranteed not to overflow
}
// we want to divide a UQ128.128 (the output of encode above) by an unencoded uint128 and return another
// modified UQ128.128. to do this, it's sufficient to divide the UQ128.128 by the unencoded value.
function qdiv(uint x, uint128 y) internal pure returns (uint z) {
z = x / y;
}
}