Skip to content

Commit c971040

Browse files
committed
feat: add value types SD1x18 and UD2x18
1 parent bd866b9 commit c971040

File tree

8 files changed

+130
-1
lines changed

8 files changed

+130
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Common Changelog](https://common-changelog.org/), and this project adheres to
66
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
[3.1.0]: https://github.com/paulrberg/prb-math/compare/v3.0.0...v3.1.0
89
[3.0.0]: https://github.com/paulrberg/prb-math/compare/v2.5.0...v3.0.0
910
[2.5.0]: https://github.com/paulrberg/prb-math/compare/v2.4.3...v2.5.0
1011
[2.4.3]: https://github.com/paulrberg/prb-math/compare/v2.4.2...v2.4.3
@@ -24,6 +25,12 @@ The format is based on [Common Changelog](https://common-changelog.org/), and th
2425
[1.0.1]: https://github.com/paulrberg/prb-math/compare/v1.0.0...v1.0.1
2526
[1.0.0]: https://github.com/paulrberg/prb-math/releases/tag/v1.0.0
2627

28+
## [3.1.0] - 2022-12-13
29+
30+
### Added
31+
32+
- Value types `SD1x18` and `UD2x18` (@paulrberg)
33+
2734
## [3.0.0] - 2022-11-29
2835

2936
[1b82ea]: https://github.com/paulrberg/prb-math/commit/1b82ea

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,18 @@ function addRshiftEq() pure returns (bool result) {
193193
194194
```
195195

196+
### Adjacent Value Types
197+
198+
PRBMath provides adjacent value types that serve as abstractions over other vanilla types such as `int64`. The types currently available are:
199+
200+
- `SD1x18` (int64)
201+
- `UD2x18` (uint64)
202+
203+
These are useful if you want to save gas by using a lower bit width integer, e.g. in a struct.
204+
205+
Note that these types don't have any mathematical functionality. For that, you will have to unwrap them into a simple integer and then to the core
206+
types `SD59x18` and `UD60x18`.
207+
196208
### Assertions
197209

198210
PRBMath is shipped with typed assertions that you can use for writing tests with [PRBTest](https://github.com/paulrberg/prb-test), which is based on

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@prb/math",
33
"description": "Solidity library for advanced fixed-point math",
4-
"version": "3.0.0",
4+
"version": "3.1.0",
55
"author": {
66
"name": "Paul Razvan Berg",
77
"url": "https://github.com/paulrberg"

src/SD1x18.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.8.13;
3+
4+
/// @notice The signed 1.18-decimal fixed-point number representation, which can have up to 1 digit and up to 18 decimals.
5+
/// The values of this are bound by the minimum and the maximum values permitted by the underlying Solidity type int64.
6+
/// This is useful when end users want to use int64 to save gas, e.g. with tight variable packing in contract storage.
7+
type SD1x18 is int64;
8+
9+
/*//////////////////////////////////////////////////////////////////////////
10+
CONVERSION FUNCTIONS
11+
//////////////////////////////////////////////////////////////////////////*/
12+
13+
/// @notice Wraps a signed integer into the SD1x18 type.
14+
function sd1x18(int64 x) pure returns (SD1x18 result) {
15+
result = SD1x18.wrap(x);
16+
}

src/UD2x18.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity >=0.8.13;
3+
4+
/// @notice The unsigned 2.18-decimal fixed-point number representation, which can have up to 2 digits and up to 18 decimals.
5+
/// The values of this are bound by the minimum and the maximum values permitted by the underlying Solidity type uint64.
6+
/// This is useful when end users want to use uint64 to save gas, e.g. with tight variable packing in contract storage.
7+
type UD2x18 is uint64;
8+
9+
/*//////////////////////////////////////////////////////////////////////////
10+
CONVERSION FUNCTIONS
11+
//////////////////////////////////////////////////////////////////////////*/
12+
13+
/// @notice Wraps a signed integer into the UD2x18 type.
14+
function ud2x18(uint64 x) pure returns (UD2x18 result) {
15+
result = UD2x18.wrap(x);
16+
}

src/test/Assertions.sol

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,104 @@ pragma solidity >=0.8.13;
33

44
import { PRBTest } from "@prb/test/PRBTest.sol";
55

6+
import { SD1x18 } from "../SD1x18.sol";
67
import { SD59x18 } from "../SD59x18.sol";
8+
import { UD2x18 } from "../UD2x18.sol";
79
import { UD60x18 } from "../UD60x18.sol";
810

911
contract Assertions is PRBTest {
12+
function assertEq(SD1x18 a, SD1x18 b) internal {
13+
assertEq(SD1x18.unwrap(a), SD1x18.unwrap(b));
14+
}
15+
1016
function assertEq(SD59x18 a, SD59x18 b) internal {
1117
assertEq(SD59x18.unwrap(a), SD59x18.unwrap(b));
1218
}
1319

20+
function assertEq(SD1x18 a, SD1x18 b, string memory err) internal {
21+
assertEq(SD1x18.unwrap(a), SD1x18.unwrap(b), err);
22+
}
23+
1424
function assertEq(SD59x18 a, SD59x18 b, string memory err) internal {
1525
assertEq(SD59x18.unwrap(a), SD59x18.unwrap(b), err);
1626
}
1727

28+
function assertEq(SD1x18 a, int256 b) internal {
29+
assertEq(SD1x18.unwrap(a), b);
30+
}
31+
1832
function assertEq(SD59x18 a, int256 b) internal {
1933
assertEq(SD59x18.unwrap(a), b);
2034
}
2135

36+
function assertEq(SD1x18 a, int256 b, string memory err) internal {
37+
assertEq(SD1x18.unwrap(a), b, err);
38+
}
39+
2240
function assertEq(SD59x18 a, int256 b, string memory err) internal {
2341
assertEq(SD59x18.unwrap(a), b, err);
2442
}
2543

44+
function assertEq(int256 a, SD1x18 b) internal {
45+
assertEq(a, SD1x18.unwrap(b));
46+
}
47+
2648
function assertEq(int256 a, SD59x18 b) internal {
2749
assertEq(a, SD59x18.unwrap(b));
2850
}
2951

52+
function assertEq(int256 a, SD1x18 b, string memory err) internal {
53+
assertEq(a, SD1x18.unwrap(b), err);
54+
}
55+
3056
function assertEq(int256 a, SD59x18 b, string memory err) internal {
3157
assertEq(a, SD59x18.unwrap(b), err);
3258
}
3359

60+
function assertEq(UD2x18 a, UD2x18 b) internal {
61+
assertEq(UD2x18.unwrap(a), UD2x18.unwrap(b));
62+
}
63+
3464
function assertEq(UD60x18 a, UD60x18 b) internal {
3565
assertEq(UD60x18.unwrap(a), UD60x18.unwrap(b));
3666
}
3767

68+
function assertEq(UD2x18 a, UD2x18 b, string memory err) internal {
69+
assertEq(UD2x18.unwrap(a), UD2x18.unwrap(b), err);
70+
}
71+
3872
function assertEq(UD60x18 a, UD60x18 b, string memory err) internal {
3973
assertEq(UD60x18.unwrap(a), UD60x18.unwrap(b), err);
4074
}
4175

76+
function assertEq(UD2x18 a, uint256 b) internal {
77+
assertEq(UD2x18.unwrap(a), b);
78+
}
79+
4280
function assertEq(UD60x18 a, uint256 b) internal {
4381
assertEq(UD60x18.unwrap(a), b);
4482
}
4583

84+
function assertEq(UD2x18 a, uint256 b, string memory err) internal {
85+
assertEq(UD2x18.unwrap(a), b, err);
86+
}
87+
4688
function assertEq(UD60x18 a, uint256 b, string memory err) internal {
4789
assertEq(UD60x18.unwrap(a), b, err);
4890
}
4991

92+
function assertEq(uint256 a, UD2x18 b) internal {
93+
assertEq(a, UD2x18.unwrap(b));
94+
}
95+
5096
function assertEq(uint256 a, UD60x18 b) internal {
5197
assertEq(a, UD60x18.unwrap(b));
5298
}
5399

100+
function assertEq(uint256 a, UD2x18 b, string memory err) internal {
101+
assertEq(a, UD2x18.unwrap(b), err);
102+
}
103+
54104
function assertEq(uint256 a, UD60x18 b, string memory err) internal {
55105
assertEq(a, UD60x18.unwrap(b), err);
56106
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.13;
3+
4+
import "src/SD1x18.sol";
5+
import { BaseTest } from "../../BaseTest.t.sol";
6+
7+
/// @dev Collection of tests for the conversion functions available in the SD1x18 type.
8+
contract SD1x18__ConversionTest is BaseTest {
9+
function testSd1x18(int64 x) external {
10+
SD1x18 actual = sd1x18(x);
11+
SD1x18 expected = SD1x18.wrap(x);
12+
assertEq(actual, expected);
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity >=0.8.13;
3+
4+
import "src/UD2x18.sol";
5+
import { BaseTest } from "../../BaseTest.t.sol";
6+
7+
/// @dev Collection of tests for the conversion functions available in the UD2x18 type.
8+
contract UD2x18__ConversionTest is BaseTest {
9+
function testUd2x18(uint64 x) external {
10+
UD2x18 actual = ud2x18(x);
11+
UD2x18 expected = UD2x18.wrap(x);
12+
assertEq(actual, expected);
13+
}
14+
}

0 commit comments

Comments
 (0)