Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UD21x18 and SD21x18 types #212

Merged
merged 5 commits into from
Jun 28, 2024

Conversation

andreivladbrg
Copy link
Contributor

Closes #207

I've also added support for SD21x18, which is the type for int128, its maximum value has the same number of decimals as uint128

@andreivladbrg andreivladbrg force-pushed the feat/UD21x18-SD21x18 branch from 149e1ea to f402d59 Compare June 14, 2024 12:35
@andreivladbrg
Copy link
Contributor Author

@PaulRBerg just wanted to let you know that i've rebased from main and fixed the conflicts

@PaulRBerg
Copy link
Owner

@andreivladbrg I will review this shortly. In the meantime, it would be helpful if you could write up a changelog and put it in the PR's comment body. This would help me when I release a new version containing the changes in this PR.

Copy link
Owner

@PaulRBerg PaulRBerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feedback below.

Accepted because I resolved all issues myself.


import "./Casting.sol" as Casting;

/// @notice The signed 1.18-decimal fixed-point number representation, which can have up to 1 digit and up to 18
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should say The signed 21.18-decimal

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

import { UD60x18 } from "../ud60x18/ValueType.sol";

/// @notice Thrown when trying to cast a uint128 that doesn't fit in SD1x18.
error PRBMath_IntoSD1x18_Overflow(uint128 x);

/// @notice Thrown when trying to cast a uint128 that doesn't fit in SD1x18.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SD21x18

function intoSD1x18(uint128 x) internal pure returns (SD1x18 result) {
if (x > uint256(int256(uMAX_SD1x18))) {
revert PRBMath_IntoSD1x18_Overflow(x);
}
result = SD1x18.wrap(int64(uint64(x)));
}

/// @notice Casts a uint256 number to SD21x18.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uint128

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

function intoUD2x18(uint128 x) internal pure returns (UD2x18 result) {
if (x > uint64(uMAX_UD2x18)) {
revert PRBMath_IntoUD2x18_Overflow(x);
}
result = UD2x18.wrap(uint64(x));
}

/// @notice Casts a uint128 number to UD21x18.
/// @dev There is no overflow check because the domain of uint128 is a subset of UD21x18.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's remove this line - uint128 is not a 'subset' of UD21x18.

uint128 is UD21x18.

Comment on lines 35 to 43
/// @notice Casts an SD21x18 number into UD2x18.
/// - x must be positive.
function intoUD2x18(SD21x18 x) pure returns (UD2x18 result) {
int128 xInt = SD21x18.unwrap(x);
if (xInt < 0) {
revert CastingErrors.PRBMath_SD21x18_ToUD2x18_Underflow(x);
}
result = UD2x18.wrap(uint64(uint128(xInt)));
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing check that x is not greater than uMAX_UD2x18.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, missed this

@@ -22,6 +26,17 @@ function intoSD1x18(UD60x18 x) pure returns (SD1x18 result) {
result = SD1x18.wrap(int64(uint64(xUint)));
}

/// @notice Casts a UD60x18 number into SD21x18.
/// @dev Requirements:
/// - x must be less than or equal to `uMAX_SD1x18`.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uMAX_SD21x18

/// @notice Casts a UD2x18 number into SD59x18.
/// @dev There is no overflow check because the domain of UD2x18 is a subset of SD59x18.
function intoSD59x18(UD2x18 x) pure returns (SD59x18 result) {
result = SD59x18.wrap(int256(uint256(UD2x18.unwrap(x))));
}

/// @notice Casts a UD2x18 number into UD21x18.
/// @dev There is no overflow check because the domain of UD2x18 is a subset of UD60x18.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of UD21x18.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fuzz test for intoSD21x18 not written.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, missed them

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fuzz test for intoSD21x18 not written.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fuzz test for intoSD21x18 not written.

src/sd59x18/Casting.sol Show resolved Hide resolved
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, missed them


import "./Casting.sol" as Casting;

/// @notice The signed 1.18-decimal fixed-point number representation, which can have up to 1 digit and up to 18
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

function intoSD1x18(uint128 x) internal pure returns (SD1x18 result) {
if (x > uint256(int256(uMAX_SD1x18))) {
revert PRBMath_IntoSD1x18_Overflow(x);
}
result = SD1x18.wrap(int64(uint64(x)));
}

/// @notice Casts a uint256 number to SD21x18.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct

Comment on lines 35 to 43
/// @notice Casts an SD21x18 number into UD2x18.
/// - x must be positive.
function intoUD2x18(SD21x18 x) pure returns (UD2x18 result) {
int128 xInt = SD21x18.unwrap(x);
if (xInt < 0) {
revert CastingErrors.PRBMath_SD21x18_ToUD2x18_Underflow(x);
}
result = UD2x18.wrap(uint64(uint128(xInt)));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, missed this

@PaulRBerg PaulRBerg merged commit ebe0395 into PaulRBerg:main Jun 28, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for uint128 tpye
2 participants