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
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/casting/Uint128.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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.
/// @notice Thrown when trying to cast a uint128 that doesn't fit in SD21x18.
error PRBMath_IntoSD21x18_Overflow(uint128 x);

/// @notice Thrown when trying to cast a uint128 that doesn't fit in UD2x18.
Expand All @@ -33,7 +33,7 @@ library PRBMathCastingUint128 {
result = SD1x18.wrap(int64(uint64(x)));
}

/// @notice Casts a uint256 number to SD21x18.
/// @notice Casts a uint128 number to SD21x18.
/// @dev Requirements:
/// - x must be less than or equal to `uMAX_SD21x18`.
function intoSD21x18(uint128 x) internal pure returns (SD21x18 result) {
Expand All @@ -60,7 +60,6 @@ library PRBMathCastingUint128 {
}

/// @notice Casts a uint128 number to UD21x18.
/// @dev There is no overflow check because the domain of uint128 is a subset of UD21x18.
function intoUD21x18(uint128 x) internal pure returns (UD21x18 result) {
result = UD21x18.wrap(x);
}
Expand Down
6 changes: 5 additions & 1 deletion src/casting/Uint256.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { SD21x18 } from "../sd21x18/ValueType.sol";
import { uMAX_SD59x18 } from "../sd59x18/Constants.sol";
import { SD59x18 } from "../sd59x18/ValueType.sol";
import { uMAX_UD2x18 } from "../ud2x18/Constants.sol";
import { uMAX_UD21x18 } from "../ud21x18/Constants.sol";
import { UD2x18 } from "../ud2x18/ValueType.sol";
import { uMAX_UD21x18 } from "../ud21x18/Constants.sol";
import { UD21x18 } from "../ud21x18/ValueType.sol";
import { UD60x18 } from "../ud60x18/ValueType.sol";

Expand Down Expand Up @@ -62,6 +62,8 @@ library PRBMathCastingUint256 {
}

/// @notice Casts a uint256 number to UD2x18.
/// @dev Requirements:
/// - x must be less than or equal to `uMAX_UD2x18`.
function intoUD2x18(uint256 x) internal pure returns (UD2x18 result) {
if (x > uint256(uMAX_UD2x18)) {
revert PRBMath_IntoUD2x18_Overflow(x);
Expand All @@ -70,6 +72,8 @@ library PRBMathCastingUint256 {
}

/// @notice Casts a uint256 number to UD2x18.
/// @dev Requirements:
/// - x must be less than or equal to `uMAX_UD21x18`.
function intoUD21x18(uint256 x) internal pure returns (UD21x18 result) {
if (x > uint256(uMAX_UD21x18)) {
revert PRBMath_IntoUD21x18_Overflow(x);
Expand Down
16 changes: 8 additions & 8 deletions src/sd1x18/Casting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,26 @@ function intoUD60x18(SD1x18 x) pure returns (UD60x18 result) {
result = UD60x18.wrap(uint64(xInt));
}

/// @notice Casts an SD1x18 number into uint256.
/// @notice Casts an SD1x18 number into uint128.
/// @dev Requirements:
/// - x must be positive.
function intoUint256(SD1x18 x) pure returns (uint256 result) {
function intoUint128(SD1x18 x) pure returns (uint128 result) {
int64 xInt = SD1x18.unwrap(x);
if (xInt < 0) {
revert CastingErrors.PRBMath_SD1x18_ToUint256_Underflow(x);
revert CastingErrors.PRBMath_SD1x18_ToUint128_Underflow(x);
}
result = uint256(uint64(xInt));
result = uint128(uint64(xInt));
}

/// @notice Casts an SD1x18 number into uint128.
/// @notice Casts an SD1x18 number into uint256.
/// @dev Requirements:
/// - x must be positive.
function intoUint128(SD1x18 x) pure returns (uint128 result) {
function intoUint256(SD1x18 x) pure returns (uint256 result) {
int64 xInt = SD1x18.unwrap(x);
if (xInt < 0) {
revert CastingErrors.PRBMath_SD1x18_ToUint128_Underflow(x);
revert CastingErrors.PRBMath_SD1x18_ToUint256_Underflow(x);
}
result = uint128(uint64(xInt));
result = uint256(uint64(xInt));
}

/// @notice Casts an SD1x18 number into uint40.
Expand Down
14 changes: 7 additions & 7 deletions src/sd1x18/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ pragma solidity >=0.8.19;

import { SD1x18 } from "./ValueType.sol";

/// @notice Thrown when trying to cast a SD1x18 number that doesn't fit in UD2x18.
/// @notice Thrown when trying to cast an SD1x18 number that doesn't fit in UD2x18.
error PRBMath_SD1x18_ToUD2x18_Underflow(SD1x18 x);

/// @notice Thrown when trying to cast a SD1x18 number that doesn't fit in UD21x18.
/// @notice Thrown when trying to cast an SD1x18 number that doesn't fit in UD21x18.
error PRBMath_SD1x18_ToUD21x18_Underflow(SD1x18 x);

/// @notice Thrown when trying to cast a SD1x18 number that doesn't fit in UD60x18.
/// @notice Thrown when trying to cast an SD1x18 number that doesn't fit in UD60x18.
error PRBMath_SD1x18_ToUD60x18_Underflow(SD1x18 x);

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

/// @notice Thrown when trying to cast a SD1x18 number that doesn't fit in uint256.
/// @notice Thrown when trying to cast an SD1x18 number that doesn't fit in uint256.
error PRBMath_SD1x18_ToUint256_Underflow(SD1x18 x);

/// @notice Thrown when trying to cast a SD1x18 number that doesn't fit in uint40.
/// @notice Thrown when trying to cast an SD1x18 number that doesn't fit in uint40.
error PRBMath_SD1x18_ToUint40_Overflow(SD1x18 x);

/// @notice Thrown when trying to cast a SD1x18 number that doesn't fit in uint40.
/// @notice Thrown when trying to cast an SD1x18 number that doesn't fit in uint40.
error PRBMath_SD1x18_ToUint40_Underflow(SD1x18 x);
2 changes: 1 addition & 1 deletion src/sd1x18/ValueType.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ using {
Casting.intoUD2x18,
Casting.intoUD21x18,
Casting.intoUD60x18,
Casting.intoUint256,
Casting.intoUint128,
Casting.intoUint256,
Casting.intoUint40,
Casting.unwrap
} for SD1x18 global;
43 changes: 24 additions & 19 deletions src/sd21x18/Casting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "./Errors.sol" as CastingErrors;
import { SD1x18 } from "../sd1x18/ValueType.sol";
import { uMAX_SD1x18, uMIN_SD1x18 } from "../sd1x18/Constants.sol";
import { SD59x18 } from "../sd59x18/ValueType.sol";
import { uMAX_UD2x18 } from "../ud2x18/Constants.sol";
import { UD2x18 } from "../ud2x18/ValueType.sol";
import { UD21x18 } from "../ud21x18/ValueType.sol";
import { UD60x18 } from "../ud60x18/ValueType.sol";
Expand Down Expand Up @@ -34,11 +35,15 @@ function intoSD59x18(SD21x18 x) pure returns (SD59x18 result) {

/// @notice Casts an SD21x18 number into UD2x18.
/// - x must be positive.
/// - x must be less than or equal to `uMAX_UD2x18`.
function intoUD2x18(SD21x18 x) pure returns (UD2x18 result) {
int128 xInt = SD21x18.unwrap(x);
if (xInt < 0) {
revert CastingErrors.PRBMath_SD21x18_ToUD2x18_Underflow(x);
}
if (xInt > int128(uint128(uMAX_UD2x18))) {
revert CastingErrors.PRBMath_SD21x18_ToUD2x18_Overflow(x);
}
result = UD2x18.wrap(uint64(uint128(xInt)));
}

Expand All @@ -63,17 +68,6 @@ function intoUD60x18(SD21x18 x) pure returns (UD60x18 result) {
result = UD60x18.wrap(uint128(xInt));
}

/// @notice Casts an SD21x18 number into uint256.
/// @dev Requirements:
/// - x must be positive.
function intoUint256(SD21x18 x) pure returns (uint256 result) {
int128 xInt = SD21x18.unwrap(x);
if (xInt < 0) {
revert CastingErrors.PRBMath_SD21x18_ToUint256_Underflow(x);
}
result = uint256(uint128(xInt));
}

/// @notice Casts an SD21x18 number into uint128.
/// @dev Requirements:
/// - x must be positive.
Expand All @@ -85,19 +79,15 @@ function intoUint128(SD21x18 x) pure returns (uint128 result) {
result = uint128(xInt);
}

/// @notice Casts an SD21x18 number into uint64.
/// @notice Casts an SD21x18 number into uint256.
/// @dev Requirements:
/// - x must be positive.
/// - x must be less than or equal to `MAX_UINT64`.
function intoUint64(SD21x18 x) pure returns (uint64 result) {
function intoUint256(SD21x18 x) pure returns (uint256 result) {
int128 xInt = SD21x18.unwrap(x);
if (xInt < 0) {
revert CastingErrors.PRBMath_SD21x18_ToUint64_Underflow(x);
}
if (xInt > int128(uint128(Common.MAX_UINT64))) {
revert CastingErrors.PRBMath_SD21x18_ToUint64_Overflow(x);
revert CastingErrors.PRBMath_SD21x18_ToUint256_Underflow(x);
}
result = uint64(uint128(xInt));
result = uint256(uint128(xInt));
}

/// @notice Casts an SD21x18 number into uint40.
Expand All @@ -115,6 +105,21 @@ function intoUint40(SD21x18 x) pure returns (uint40 result) {
result = uint40(uint128(xInt));
}

/// @notice Casts an SD21x18 number into uint64.
/// @dev Requirements:
/// - x must be positive.
/// - x must be less than or equal to `MAX_UINT64`.
function intoUint64(SD21x18 x) pure returns (uint64 result) {
int128 xInt = SD21x18.unwrap(x);
if (xInt < 0) {
revert CastingErrors.PRBMath_SD21x18_ToUint64_Underflow(x);
}
if (xInt > int128(uint128(Common.MAX_UINT64))) {
revert CastingErrors.PRBMath_SD21x18_ToUint64_Overflow(x);
}
result = uint64(uint128(xInt));
}

/// @notice Alias for {wrap}.
function sd21x18(int128 x) pure returns (SD21x18 result) {
result = SD21x18.wrap(x);
Expand Down
25 changes: 14 additions & 11 deletions src/sd21x18/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,38 @@ pragma solidity >=0.8.19;

import { SD21x18 } from "./ValueType.sol";

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in SD1x18.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in SD1x18.
error PRBMath_SD21x18_IntoSD1x18_Overflow(SD21x18 x);

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in SD1x18.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in SD1x18.
error PRBMath_SD21x18_IntoSD1x18_Underflow(SD21x18 x);

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in UD2x18.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in UD2x18.
error PRBMath_SD21x18_ToUD2x18_Overflow(SD21x18 x);

/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in UD2x18.
error PRBMath_SD21x18_ToUD2x18_Underflow(SD21x18 x);

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in UD21x18.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in UD21x18.
error PRBMath_SD21x18_ToUD21x18_Underflow(SD21x18 x);

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in UD60x18.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in UD60x18.
error PRBMath_SD21x18_ToUD60x18_Underflow(SD21x18 x);

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in uint256.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in uint256.
error PRBMath_SD21x18_ToUint256_Underflow(SD21x18 x);

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in uint128.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in uint128.
error PRBMath_SD21x18_ToUint128_Underflow(SD21x18 x);

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in uint64.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in uint64.
error PRBMath_SD21x18_ToUint64_Overflow(SD21x18 x);

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in uint64.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in uint64.
error PRBMath_SD21x18_ToUint64_Underflow(SD21x18 x);

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in uint40.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in uint40.
error PRBMath_SD21x18_ToUint40_Overflow(SD21x18 x);

/// @notice Thrown when trying to cast a SD21x18 number that doesn't fit in uint40.
/// @notice Thrown when trying to cast an SD21x18 number that doesn't fit in uint40.
error PRBMath_SD21x18_ToUint40_Underflow(SD21x18 x);
4 changes: 2 additions & 2 deletions src/sd21x18/ValueType.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pragma solidity >=0.8.19;

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
/// @notice The signed 21.18-decimal fixed-point number representation, which can have up to 21 digits and up to 18
/// decimals. The values of this are bound by the minimum and the maximum values permitted by the underlying Solidity
/// type int128. This is useful when end users want to use int128 to save gas, e.g. with tight variable packing in contract
/// storage.
Expand All @@ -19,8 +19,8 @@ using {
Casting.intoUD2x18,
Casting.intoUD21x18,
Casting.intoUD60x18,
Casting.intoUint256,
Casting.intoUint128,
Casting.intoUint256,
Casting.intoUint40,
Casting.unwrap
} for SD21x18 global;
6 changes: 5 additions & 1 deletion src/sd59x18/Casting.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { uMAX_SD21x18, uMIN_SD21x18 } from "../sd21x18/Constants.sol";
import { SD21x18 } from "../sd21x18/ValueType.sol";
import { uMAX_UD2x18 } from "../ud2x18/Constants.sol";
import { UD2x18 } from "../ud2x18/ValueType.sol";
import { uMAX_UD21x18 } from "../ud21x18/Constants.sol";
import { UD21x18 } from "../ud21x18/ValueType.sol";
import { UD60x18 } from "../ud60x18/ValueType.sol";
import { SD59x18 } from "./ValueType.sol";
Expand Down Expand Up @@ -67,12 +68,15 @@ function intoUD2x18(SD59x18 x) pure returns (UD2x18 result) {
/// @notice Casts an SD59x18 number into UD21x18.
/// @dev Requirements:
/// - x must be positive.
/// - x must be less than or equal to `uMAX_UD21x18`.
andreivladbrg marked this conversation as resolved.
Show resolved Hide resolved
function intoUD21x18(SD59x18 x) pure returns (UD21x18 result) {
int256 xInt = SD59x18.unwrap(x);
if (xInt < 0) {
revert CastingErrors.PRBMath_SD59x18_IntoUD21x18_Underflow(x);
}

if (xInt > int256(uint256(uMAX_UD21x18))) {
revert CastingErrors.PRBMath_SD59x18_IntoUD21x18_Overflow(x);
}
result = UD21x18.wrap(uint128(uint256(xInt)));
}

Expand Down
29 changes: 16 additions & 13 deletions src/sd59x18/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,46 @@ error PRBMath_SD59x18_Gm_NegativeProduct(SD59x18 x, SD59x18 y);
/// @notice Thrown when taking the geometric mean of two numbers and multiplying them overflows SD59x18.
error PRBMath_SD59x18_Gm_Overflow(SD59x18 x, SD59x18 y);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in SD1x18.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in SD1x18.
error PRBMath_SD59x18_IntoSD1x18_Overflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in SD1x18.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in SD1x18.
error PRBMath_SD59x18_IntoSD1x18_Underflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in SD21x18.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in SD21x18.
error PRBMath_SD59x18_IntoSD21x18_Overflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in SD21x18.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in SD21x18.
error PRBMath_SD59x18_IntoSD21x18_Underflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in UD2x18.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in UD2x18.
error PRBMath_SD59x18_IntoUD2x18_Overflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in UD2x18.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in UD2x18.
error PRBMath_SD59x18_IntoUD2x18_Underflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in UD21x18.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in UD21x18.
error PRBMath_SD59x18_IntoUD21x18_Overflow(SD59x18 x);

/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in UD21x18.
error PRBMath_SD59x18_IntoUD21x18_Underflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in UD60x18.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in UD60x18.
error PRBMath_SD59x18_IntoUD60x18_Underflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in uint128.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in uint128.
error PRBMath_SD59x18_IntoUint128_Overflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in uint128.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in uint128.
error PRBMath_SD59x18_IntoUint128_Underflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in uint256.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in uint256.
error PRBMath_SD59x18_IntoUint256_Underflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in uint40.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in uint40.
error PRBMath_SD59x18_IntoUint40_Overflow(SD59x18 x);

/// @notice Thrown when trying to cast a SD59x18 number that doesn't fit in uint40.
/// @notice Thrown when trying to cast an SD59x18 number that doesn't fit in uint40.
error PRBMath_SD59x18_IntoUint40_Underflow(SD59x18 x);

/// @notice Thrown when taking the logarithm of a number less than or equal to zero.
Expand Down
Loading