feat(libraries): add fixed-point and low-level math primitives

This commit is contained in:
dualfroz committed 2026-08-23 11:44:00 +02:00
1 parent 3f335c47ec
commit d83d3c1e96
4 files changed
+48

No files matched your search

+7
View File
@@ -0,0 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.36;
/// @title Q128.128 fixed point constant used by the fee-growth accounting.
library FixedPoint128 {
uint256 internal constant q128 = 0x100000000000000000000000000000000;
}
+8
View File
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.36;
/// @title Q64.96 fixed point constants used by the sqrt-price math.
library FixedPoint96 {
uint8 internal constant resolution = 96;
uint256 internal constant q96 = 0x1000000000000000000000000;
}
+22
View File
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.36;
/// @title Checked narrowing casts between integer widths.
library SafeCast {
function toUint160(uint256 y) internal pure returns (uint160 z) {
require((z = uint160(y)) == y, "SafeCast: U160");
}
function toUint128(uint256 y) internal pure returns (uint128 z) {
require((z = uint128(y)) == y, "SafeCast: U128");
}
function toInt128(int256 y) internal pure returns (int128 z) {
require((z = int128(y)) == y, "SafeCast: I128");
}
function toInt256(uint256 y) internal pure returns (int256 z) {
require(y < 2 ** 255, "SafeCast: I256");
z = int256(y);
}
}
+11
View File
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.36;
/// @title Rounding-up integer division that trusts its non-zero denominator.
library UnsafeMath {
function divRoundingUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
z := add(div(x, y), gt(mod(x, y), 0))
}
}
}