feat(libraries): add constant-product and q112 fixed-point math

This commit is contained in:
dualfroz committed 2026-08-25 19:56:00 +02:00
1 parent 2214801143
commit 4b7a996e41
2 files changed
+40

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.36;
/// @title Math helpers for the constant-product core.
library Math {
function min(uint256 x, uint256 y) internal pure returns (uint256) {
return x < y ? x : y;
}
/// @notice Babylonian integer square root.
function sqrt(uint256 y) internal pure returns (uint256 z) {
if (y > 3) {
z = y;
uint256 x = y / 2 + 1;
while (x < z) {
z = x;
x = (y / x + x) / 2;
}
} else if (y != 0) {
z = 1;
}
}
}
+17
View File
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.36;
/// @title Fixed point unsigned Q112.112, used for price accumulators.
library UQ112x112 {
uint224 constant q112 = 2 ** 112;
function encode(uint112 y) internal pure returns (uint224 z) {
unchecked {
z = uint224(y) * q112;
}
}
function uqdiv(uint224 x, uint112 y) internal pure returns (uint224 z) {
z = x / uint224(y);
}
}