solidity-math-utils 1.2.3 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -29
- package/package.json +4 -4
- package/project/contracts/AdvancedMath.sol +1 -1
- package/project/contracts/AnalyticMath.sol +1 -1
- package/project/contracts/BondingCurve.sol +1 -1
- package/project/contracts/DynamicCurve.sol +1 -1
- package/project/contracts/FractionMath.sol +87 -71
- package/project/contracts/IntegralMath.sol +64 -32
package/README.md
CHANGED
|
@@ -38,14 +38,14 @@ A library cannot utilize non-constant state variables, nor can it extend (inheri
|
|
|
38
38
|
## IntegralMath
|
|
39
39
|
|
|
40
40
|
This module implements the following interface:
|
|
41
|
-
- `function roundDiv(uint256 n, uint256 d)` => `(uint256)`
|
|
42
41
|
- `function floorLog2(uint256 n)` => `(uint8)`
|
|
43
42
|
- `function floorSqrt(uint256 n)` => `(uint256)`
|
|
44
43
|
- `function ceilSqrt(uint256 n)` => `(uint256)`
|
|
45
|
-
- `function
|
|
46
|
-
- `function
|
|
47
|
-
|
|
48
|
-
|
|
44
|
+
- `function floorCbrt(uint256 n)` => `(uint256)`
|
|
45
|
+
- `function ceilCbrt(uint256 n)` => `(uint256)`
|
|
46
|
+
- `function roundDiv(uint256 n, uint256 d)` => `(uint256)`
|
|
47
|
+
- `function mulDivF(uint256 x, uint256 y, uint256 z)` => `(uint256)`
|
|
48
|
+
- `function mulDivC(uint256 x, uint256 y, uint256 z)` => `(uint256)`
|
|
49
49
|
|
|
50
50
|
Function `floorLog2(n)` computes the largest integer smaller than or equal to the binary logarithm of `n`.
|
|
51
51
|
|
|
@@ -53,11 +53,17 @@ Function `floorSqrt(n)` computes the largest integer smaller than or equal to th
|
|
|
53
53
|
|
|
54
54
|
Function `ceilSqrt(n)` computes the smallest integer larger than or equal to the square root of `n`.
|
|
55
55
|
|
|
56
|
-
Function `
|
|
56
|
+
Function `floorCbrt(n)` computes the largest integer smaller than or equal to the cubic root of `n`.
|
|
57
|
+
|
|
58
|
+
Function `ceilCbrt(n)` computes the smallest integer larger than or equal to the cubic root of `n`.
|
|
59
|
+
|
|
60
|
+
Function `roundDiv(n, d)` computes the nearest integer to the quotient of `n` and `d` (or `n / d`).
|
|
57
61
|
|
|
58
|
-
Function `
|
|
62
|
+
Function `mulDivF(x, y, z)` computes the largest integer smaller than or equal to `x * y / z`.
|
|
59
63
|
|
|
60
|
-
|
|
64
|
+
Function `mulDivC(x, y, z)` computes the smallest integer larger than or equal to `x * y / z`.
|
|
65
|
+
|
|
66
|
+
Note that function `mulDivF` and function `mulDivC` revert when the **actual** result is larger than 256 bits.
|
|
61
67
|
|
|
62
68
|
Note that function `floorSqrt` and function `ceilSqrt` are guaranteed to return the correct output for every input.
|
|
63
69
|
|
|
@@ -65,6 +71,12 @@ However, when compared with the **actual** square root, smaller input generally
|
|
|
65
71
|
|
|
66
72
|
For example, `floorSqrt(3)` returns 1, but the actual square root of 3 is ~1.73, which yields a relative accuracy of only ~57%.
|
|
67
73
|
|
|
74
|
+
Note that function `floorCbrt` and function `ceilCbrt` are guaranteed to return the correct output for every input.
|
|
75
|
+
|
|
76
|
+
However, when compared with the **actual** cubic root, smaller input generally yields relatively lower accuracy of the output.
|
|
77
|
+
|
|
78
|
+
For example, `floorCbrt(7)` returns 1, but the actual cubic root of 7 is ~1.91, which yields a relative accuracy of only ~52%.
|
|
79
|
+
|
|
68
80
|
<br/><br/>
|
|
69
81
|
|
|
70
82
|
---
|
|
@@ -74,33 +86,33 @@ For example, `floorSqrt(3)` returns 1, but the actual square root of 3 is ~1.73,
|
|
|
74
86
|
## FractionMath
|
|
75
87
|
|
|
76
88
|
This module implements the following interface:
|
|
77
|
-
- `function
|
|
89
|
+
- `function poweredRatio(uint256 n, uint256 d, uint256 exp, bool fast)` => `(uint256, uint256)`
|
|
90
|
+
- `function productRatio(uint256 xn, uint256 yn, uint256 xd, uint256 yd)` => `(uint256, uint256)`
|
|
78
91
|
- `function reducedRatio(uint256 n, uint256 d, uint256 max)` => `(uint256, uint256)`
|
|
79
|
-
- `function
|
|
80
|
-
- `function productRatio(uint256 n1, uint256 n2, uint256 d1, uint256 d2)` => `(uint256, uint256)`
|
|
92
|
+
- `function normalizedRatio(uint256 n, uint256 d, uint256 scale)` => `(uint256, uint256)`
|
|
81
93
|
|
|
82
|
-
###
|
|
94
|
+
### Powered Ratio
|
|
83
95
|
|
|
84
|
-
Function `
|
|
96
|
+
Function `poweredRatio` computes the power of a given ratio by a given exponent.
|
|
85
97
|
|
|
86
|
-
|
|
98
|
+
In order to avoid multiplication overflow, it may truncate the intermediate result on each iteration.
|
|
87
99
|
|
|
88
|
-
|
|
89
|
-
- `normalizedRatio(12, 34, 100)` returns `(26 74)`; the output ratio is smaller than the input ratio (26 / 74 = 0.351 < 0.352 = 12 / 34)
|
|
90
|
-
- `normalizedRatio(1234, 5678, 100)` returns `(18 82)`; the output ratio is larger than the input ratio (18 / 82 = 0.219 > 0.217 = 1234 / 5678)
|
|
100
|
+
Subsequently, the larger the input exponent is, the lower the accuracy of the output is likely to be.
|
|
91
101
|
|
|
92
|
-
|
|
102
|
+
The input argument `fast` allows to opt for either performance (using `true`) or accuracy (using `false`).
|
|
93
103
|
|
|
94
|
-
|
|
104
|
+
This library defines a maximum exponent of 4 bits (i.e., 15), which can be customized to fit the system requirements.
|
|
105
|
+
|
|
106
|
+
### Product Ratio
|
|
95
107
|
|
|
96
|
-
|
|
108
|
+
Function `productRatio` computes the product of two ratios as a single ratio whose components are not larger than 256 bits.
|
|
109
|
+
|
|
110
|
+
If either one of the intermediate components is larger than 256 bits, then both of them are reduced based on the larger one.
|
|
97
111
|
|
|
98
112
|
### Reduced Ratio
|
|
99
113
|
|
|
100
114
|
Function `reducedRatio` computes the nearest ratio whose components (numerator and denominator) are not larger than the input threshold.
|
|
101
115
|
|
|
102
|
-
Internally, it calls function `normalizedRatio` with the input threshold, but only if one of the components is larger than that threshold.
|
|
103
|
-
|
|
104
116
|
Note that function `reducedRatio` is not meant to replace GCD, nor does it strive to achieve better accuracy.
|
|
105
117
|
|
|
106
118
|
GCD is not being used here, because the time-complexity of this method depends on the bit-length of the input.
|
|
@@ -113,19 +125,21 @@ Reducing an input ratio by its GCD in advance (at your own expense) can most cer
|
|
|
113
125
|
|
|
114
126
|
However, without knowing specific characteristics of that ratio (e.g., each one of its components is a multiple of `0x1000`), doing so is generally useless.
|
|
115
127
|
|
|
116
|
-
###
|
|
128
|
+
### Normalized Ratio
|
|
117
129
|
|
|
118
|
-
Function `
|
|
130
|
+
Function `normalizedRatio` computes the nearest ratio whose sum of components (numerator + denominator) equals the input scale.
|
|
119
131
|
|
|
120
|
-
Note that
|
|
132
|
+
Note that the output ratio can be larger than the input ratio in some cases, and smaller than the input ratio in other cases.
|
|
121
133
|
|
|
122
|
-
|
|
134
|
+
For example:
|
|
135
|
+
- `normalizedRatio(12, 34, 100)` returns `(26 74)`; the output ratio is smaller than the input ratio (26 / 74 = 0.351 < 0.352 = 12 / 34)
|
|
136
|
+
- `normalizedRatio(1234, 5678, 100)` returns `(18 82)`; the output ratio is larger than the input ratio (18 / 82 = 0.219 > 0.217 = 1234 / 5678)
|
|
123
137
|
|
|
124
|
-
|
|
138
|
+
Keep in mind that it is an important consideration to take when choosing to use this function.
|
|
125
139
|
|
|
126
|
-
|
|
140
|
+
For example, when designing a sustainable financial model, it is imperative to never entitle more than the actual entitlement.
|
|
127
141
|
|
|
128
|
-
|
|
142
|
+
The same consideration applies for all the other functions in this module.
|
|
129
143
|
|
|
130
144
|
<br/><br/>
|
|
131
145
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "solidity-math-utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
4
4
|
"description": "Solidity Mathematical Utilities",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -17,9 +17,9 @@
|
|
|
17
17
|
"verify": "truffle run coverage"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"decimal.js": "10.
|
|
20
|
+
"decimal.js": "10.3.1",
|
|
21
21
|
"solidity-coverage": "0.7.16",
|
|
22
|
-
"truffle": "5.
|
|
23
|
-
"web3": "1.
|
|
22
|
+
"truffle": "5.4.0",
|
|
23
|
+
"web3": "1.4.0"
|
|
24
24
|
}
|
|
25
25
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity 0.8.
|
|
2
|
+
pragma solidity 0.8.10;
|
|
3
3
|
|
|
4
4
|
import "./IntegralMath.sol";
|
|
5
5
|
|
|
@@ -8,131 +8,147 @@ library FractionMath {
|
|
|
8
8
|
|
|
9
9
|
uint256 internal constant MAX_EXP_BIT_LEN = 4;
|
|
10
10
|
uint256 internal constant MAX_EXP = 2 ** MAX_EXP_BIT_LEN - 1;
|
|
11
|
-
uint256 internal constant MAX_UINT128 =
|
|
12
|
-
uint256 internal constant MAX_UINT256 = 2 ** 256 - 1;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* @dev Compute the product of two given ratios
|
|
16
|
-
*
|
|
17
|
-
* @param xn The 1st ratio numerator
|
|
18
|
-
* @param yn The 2nd ratio numerator
|
|
19
|
-
* @param xd The 1st ratio denominator
|
|
20
|
-
* @param yd The 2nd ratio denominator
|
|
21
|
-
*
|
|
22
|
-
* @return The product ratio numerator
|
|
23
|
-
* @return The product ratio denominator
|
|
24
|
-
*/
|
|
25
|
-
function productRatio(uint256 xn, uint256 yn, uint256 xd, uint256 yd) internal pure returns (uint256, uint256) { unchecked {
|
|
26
|
-
uint256 n = IntegralMath.mulDivC(xn, yn, MAX_UINT256);
|
|
27
|
-
uint256 d = IntegralMath.mulDivC(xd, yd, MAX_UINT256);
|
|
28
|
-
uint256 z = n > d ? n : d;
|
|
29
|
-
if (z > 1)
|
|
30
|
-
return (IntegralMath.mulDivC(xn, yn, z), IntegralMath.mulDivC(xd, yd, z));
|
|
31
|
-
return (xn * yn, xd * yd);
|
|
32
|
-
}}
|
|
11
|
+
uint256 internal constant MAX_UINT128 = type(uint128).max;
|
|
33
12
|
|
|
34
13
|
/**
|
|
35
14
|
* @dev Compute the power of a given ratio
|
|
36
15
|
*
|
|
37
|
-
* @param
|
|
38
|
-
* @param
|
|
39
|
-
* @param exp
|
|
16
|
+
* @param n The ratio numerator
|
|
17
|
+
* @param d The ratio denominator
|
|
18
|
+
* @param exp The exponentiation value
|
|
19
|
+
* @param fast Opt for performance over accuracy
|
|
40
20
|
*
|
|
41
21
|
* @return The powered ratio numerator
|
|
42
22
|
* @return The powered ratio denominator
|
|
43
23
|
*/
|
|
44
|
-
function poweredRatio(uint256
|
|
24
|
+
function poweredRatio(uint256 n, uint256 d, uint256 exp, bool fast) internal pure returns (uint256, uint256) { unchecked {
|
|
45
25
|
require(exp <= MAX_EXP, "exp too large");
|
|
46
26
|
|
|
27
|
+
function (uint256, uint256, uint256, uint256) pure returns (uint256, uint256) safeRatio = fast ? mulRatio128 : productRatio;
|
|
28
|
+
|
|
47
29
|
uint256[MAX_EXP_BIT_LEN] memory ns;
|
|
48
30
|
uint256[MAX_EXP_BIT_LEN] memory ds;
|
|
49
31
|
|
|
50
|
-
(ns[0], ds[0]) =
|
|
32
|
+
(ns[0], ds[0]) = safeRatio(n, 1, d, 1);
|
|
51
33
|
for (uint256 i = 0; (exp >> i) > 1; ++i) {
|
|
52
|
-
(ns[i + 1], ds[i + 1]) =
|
|
34
|
+
(ns[i + 1], ds[i + 1]) = safeRatio(ns[i], ns[i], ds[i], ds[i]);
|
|
53
35
|
}
|
|
54
36
|
|
|
55
|
-
|
|
56
|
-
|
|
37
|
+
n = 1;
|
|
38
|
+
d = 1;
|
|
57
39
|
|
|
58
40
|
for (uint256 i = 0; (exp >> i) > 0; ++i) {
|
|
59
41
|
if (((exp >> i) & 1) > 0) {
|
|
60
|
-
(n, d) =
|
|
42
|
+
(n, d) = safeRatio(n, ns[i], d, ds[i]);
|
|
61
43
|
}
|
|
62
44
|
}
|
|
63
45
|
|
|
64
46
|
return (n, d);
|
|
65
47
|
}}
|
|
66
48
|
|
|
49
|
+
/**
|
|
50
|
+
* @dev Compute the product of two given ratios
|
|
51
|
+
*
|
|
52
|
+
* @param xn The 1st ratio numerator
|
|
53
|
+
* @param yn The 2nd ratio numerator
|
|
54
|
+
* @param xd The 1st ratio denominator
|
|
55
|
+
* @param yd The 2nd ratio denominator
|
|
56
|
+
*
|
|
57
|
+
* @return The product ratio numerator
|
|
58
|
+
* @return The product ratio denominator
|
|
59
|
+
*/
|
|
60
|
+
function productRatio(uint256 xn, uint256 yn, uint256 xd, uint256 yd) internal pure returns (uint256, uint256) { unchecked {
|
|
61
|
+
uint256 n = IntegralMath.mulDivC(xn, yn, MAX_VAL);
|
|
62
|
+
uint256 d = IntegralMath.mulDivC(xd, yd, MAX_VAL);
|
|
63
|
+
uint256 z = n > d ? n : d;
|
|
64
|
+
if (z > 1) {
|
|
65
|
+
return (IntegralMath.mulDivC(xn, yn, z), IntegralMath.mulDivC(xd, yd, z));
|
|
66
|
+
}
|
|
67
|
+
return (xn * yn, xd * yd);
|
|
68
|
+
}}
|
|
69
|
+
|
|
67
70
|
/**
|
|
68
71
|
* @dev Reduce the components of a given ratio
|
|
69
72
|
*
|
|
70
|
-
* @param
|
|
71
|
-
* @param
|
|
72
|
-
* @param max
|
|
73
|
+
* @param n The ratio numerator
|
|
74
|
+
* @param d The ratio denominator
|
|
75
|
+
* @param max The maximum desired value
|
|
73
76
|
*
|
|
74
77
|
* @return The reduced ratio numerator
|
|
75
78
|
* @return The reduced ratio denominator
|
|
76
79
|
*/
|
|
77
|
-
function reducedRatio(uint256
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
}
|
|
81
|
-
return (baseN, baseD);
|
|
80
|
+
function reducedRatio(uint256 n, uint256 d, uint256 max) internal pure returns (uint256, uint256) { unchecked {
|
|
81
|
+
uint256 scale = ((n > d ? n : d) - 1) / max + 1;
|
|
82
|
+
return (n / scale, d / scale);
|
|
82
83
|
}}
|
|
83
84
|
|
|
84
85
|
/**
|
|
85
86
|
* @dev Compute a normalized ratio as `scale * n / (n + d)` and `scale * d / (n + d)`
|
|
86
87
|
*
|
|
87
|
-
* @param
|
|
88
|
-
* @param
|
|
88
|
+
* @param n The ratio numerator
|
|
89
|
+
* @param d The ratio denominator
|
|
89
90
|
* @param scale The desired scale
|
|
90
91
|
*
|
|
91
92
|
* @return The normalized ratio numerator
|
|
92
93
|
* @return The normalized ratio denominator
|
|
93
94
|
*/
|
|
94
|
-
function normalizedRatio(uint256
|
|
95
|
-
if (
|
|
96
|
-
return
|
|
95
|
+
function normalizedRatio(uint256 n, uint256 d, uint256 scale) internal pure returns (uint256, uint256) { unchecked {
|
|
96
|
+
if (n <= d) {
|
|
97
|
+
return estimatedRatio(n, d, scale);
|
|
97
98
|
}
|
|
98
|
-
(
|
|
99
|
+
(d, n) = estimatedRatio(d, n, scale);
|
|
99
100
|
return (n, d);
|
|
100
101
|
}}
|
|
101
102
|
|
|
102
103
|
/**
|
|
103
|
-
* @dev Compute
|
|
104
|
+
* @dev Compute an estimated ratio as `scale * n / (n + d)` and `scale * d / (n + d)`, assuming that `n <= d`
|
|
104
105
|
*
|
|
105
|
-
* @param
|
|
106
|
-
* @param
|
|
106
|
+
* @param n The ratio numerator
|
|
107
|
+
* @param d The ratio denominator
|
|
107
108
|
* @param scale The desired scale
|
|
108
109
|
*
|
|
109
|
-
* @return The
|
|
110
|
-
* @return The
|
|
110
|
+
* @return The estimated ratio numerator
|
|
111
|
+
* @return The estimated ratio denominator
|
|
111
112
|
*/
|
|
112
|
-
function
|
|
113
|
-
uint256
|
|
114
|
-
if (
|
|
115
|
-
// `
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
113
|
+
function estimatedRatio(uint256 n, uint256 d, uint256 scale) private pure returns (uint256, uint256) { unchecked {
|
|
114
|
+
uint256 x = MAX_VAL / scale;
|
|
115
|
+
if (n > x) {
|
|
116
|
+
// `n * scale` will overflow
|
|
117
|
+
uint256 y = (n - 1) / x + 1;
|
|
118
|
+
n /= y;
|
|
119
|
+
d /= y;
|
|
120
|
+
// `n * scale` will not overflow
|
|
120
121
|
}
|
|
121
122
|
|
|
122
|
-
if (
|
|
123
|
-
uint256
|
|
124
|
-
uint256
|
|
125
|
-
if (
|
|
126
|
-
// `
|
|
127
|
-
uint256
|
|
128
|
-
|
|
129
|
-
return (x, y);
|
|
123
|
+
if (n < d) {
|
|
124
|
+
uint256 p = n * scale;
|
|
125
|
+
uint256 q = unsafeAdd(n, d); // `n + d` can overflow
|
|
126
|
+
if (q >= n) {
|
|
127
|
+
// `n + d` did not overflow
|
|
128
|
+
uint256 r = IntegralMath.roundDiv(p, q);
|
|
129
|
+
return (r, scale - r); // `r = n * scale / (n + d) < scale`
|
|
130
130
|
}
|
|
131
|
-
if (
|
|
132
|
-
return (0, scale); // `
|
|
131
|
+
if (p < d - (d - n) / 2) {
|
|
132
|
+
return (0, scale); // `n * scale < (n + d) / 2 < MAX_VAL < n + d`
|
|
133
133
|
}
|
|
134
|
-
return (1, scale - 1); // `(
|
|
134
|
+
return (1, scale - 1); // `(n + d) / 2 < n * scale < MAX_VAL < n + d`
|
|
135
135
|
}
|
|
136
|
-
return (scale / 2, scale - scale / 2); // reflect the fact that `
|
|
136
|
+
return (scale / 2, scale - scale / 2); // reflect the fact that initially `n <= d`
|
|
137
|
+
}}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @dev Compute the product of two ratios and reduce the components of the result to 128 bits,
|
|
141
|
+
* under the implicit assumption that the components of the product are not larger than 256 bits
|
|
142
|
+
*
|
|
143
|
+
* @param xn The 1st ratio numerator
|
|
144
|
+
* @param yn The 2nd ratio numerator
|
|
145
|
+
* @param xd The 1st ratio denominator
|
|
146
|
+
* @param yd The 2nd ratio denominator
|
|
147
|
+
*
|
|
148
|
+
* @return The product ratio numerator
|
|
149
|
+
* @return The product ratio denominator
|
|
150
|
+
*/
|
|
151
|
+
function mulRatio128(uint256 xn, uint256 yn, uint256 xd, uint256 yd) private pure returns (uint256, uint256) { unchecked {
|
|
152
|
+
return reducedRatio(xn * yn, xd * yd, MAX_UINT128);
|
|
137
153
|
}}
|
|
138
154
|
}
|
|
@@ -1,16 +1,9 @@
|
|
|
1
1
|
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
|
|
2
|
-
pragma solidity 0.8.
|
|
2
|
+
pragma solidity 0.8.10;
|
|
3
3
|
|
|
4
4
|
import "./common/Uint.sol";
|
|
5
5
|
|
|
6
6
|
library IntegralMath {
|
|
7
|
-
/**
|
|
8
|
-
* @dev Compute the nearest integer to the quotient of `n / d`
|
|
9
|
-
*/
|
|
10
|
-
function roundDiv(uint256 n, uint256 d) internal pure returns (uint256) { unchecked {
|
|
11
|
-
return n / d + (n % d) / (d - d / 2);
|
|
12
|
-
}}
|
|
13
|
-
|
|
14
7
|
/**
|
|
15
8
|
* @dev Compute the largest integer smaller than or equal to the binary logarithm of `n`
|
|
16
9
|
*/
|
|
@@ -58,7 +51,38 @@ library IntegralMath {
|
|
|
58
51
|
*/
|
|
59
52
|
function ceilSqrt(uint256 n) internal pure returns (uint256) { unchecked {
|
|
60
53
|
uint256 x = floorSqrt(n);
|
|
61
|
-
return x
|
|
54
|
+
return x ** 2 == n ? x : x + 1;
|
|
55
|
+
}}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @dev Compute the largest integer smaller than or equal to the cubic root of `n`
|
|
59
|
+
*/
|
|
60
|
+
function floorCbrt(uint256 n) internal pure returns (uint256) { unchecked {
|
|
61
|
+
uint256 x = 0;
|
|
62
|
+
for (uint256 y = 1 << 255; y > 0; y >>= 3) {
|
|
63
|
+
x <<= 1;
|
|
64
|
+
uint256 z = 3 * x * (x + 1) + 1;
|
|
65
|
+
if (n / y >= z) {
|
|
66
|
+
n -= y * z;
|
|
67
|
+
x += 1;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return x;
|
|
71
|
+
}}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @dev Compute the smallest integer larger than or equal to the cubic root of `n`
|
|
75
|
+
*/
|
|
76
|
+
function ceilCbrt(uint256 n) internal pure returns (uint256) { unchecked {
|
|
77
|
+
uint256 x = floorCbrt(n);
|
|
78
|
+
return x ** 3 == n ? x : x + 1;
|
|
79
|
+
}}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @dev Compute the nearest integer to the quotient of `n` and `d` (or `n / d`)
|
|
83
|
+
*/
|
|
84
|
+
function roundDiv(uint256 n, uint256 d) internal pure returns (uint256) { unchecked {
|
|
85
|
+
return n / d + (n % d) / (d - d / 2);
|
|
62
86
|
}}
|
|
63
87
|
|
|
64
88
|
/**
|
|
@@ -66,9 +90,21 @@ library IntegralMath {
|
|
|
66
90
|
*/
|
|
67
91
|
function mulDivF(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) { unchecked {
|
|
68
92
|
(uint256 xyh, uint256 xyl) = mul512(x, y);
|
|
69
|
-
if (xyh
|
|
70
|
-
return
|
|
71
|
-
|
|
93
|
+
if (xyh == 0) { // `x * y < 2 ^ 256`
|
|
94
|
+
return xyl / z;
|
|
95
|
+
}
|
|
96
|
+
if (xyh < z) { // `x * y / z < 2 ^ 256`
|
|
97
|
+
uint256 m = mulMod(x, y, z); // `m = x * y % z`
|
|
98
|
+
(uint256 nh, uint256 nl) = sub512(xyh, xyl, m); // `n = x * y - m` hence `n / z = floor(x * y / z)`
|
|
99
|
+
if (nh == 0) { // `n < 2 ^ 256`
|
|
100
|
+
return nl / z;
|
|
101
|
+
}
|
|
102
|
+
uint256 p = unsafeSub(0, z) & z; // `p` is the largest power of 2 which `z` is divisible by
|
|
103
|
+
uint256 q = div512(nh, nl, p); // `n` is divisible by `p` because `n` is divisible by `z` and `z` is divisible by `p`
|
|
104
|
+
uint256 r = inv256(z / p); // `z / p = 1 mod 2` hence `inverse(z / p) = 1 mod 2 ^ 256`
|
|
105
|
+
return unsafeMul(q, r); // `q * r = (n / p) * inverse(z / p) = n / z`
|
|
106
|
+
}
|
|
107
|
+
revert(); // `x * y / z >= 2 ^ 256`
|
|
72
108
|
}}
|
|
73
109
|
|
|
74
110
|
/**
|
|
@@ -93,34 +129,30 @@ library IntegralMath {
|
|
|
93
129
|
}}
|
|
94
130
|
|
|
95
131
|
/**
|
|
96
|
-
* @dev Compute the
|
|
132
|
+
* @dev Compute the value of `2 ^ 256 * xh + xl - y`, where `2 ^ 256 * xh + xl >= y`
|
|
97
133
|
*/
|
|
98
|
-
function
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
while (xh > 0) {
|
|
103
|
-
uint256 bits = floorLog2(xh) + length;
|
|
104
|
-
result += 1 << bits;
|
|
105
|
-
(uint256 yh, uint256 yl) = shl512(y, bits);
|
|
106
|
-
(xh, xl) = sub512(xh, xl, yh, yl);
|
|
107
|
-
}
|
|
108
|
-
return result + xl / y;
|
|
134
|
+
function sub512(uint256 xh, uint256 xl, uint256 y) private pure returns (uint256, uint256) { unchecked {
|
|
135
|
+
if (xl >= y)
|
|
136
|
+
return (xh, xl - y);
|
|
137
|
+
return (xh - 1, unsafeSub(xl, y));
|
|
109
138
|
}}
|
|
110
139
|
|
|
111
140
|
/**
|
|
112
|
-
* @dev Compute the value of `
|
|
141
|
+
* @dev Compute the value of `(2 ^ 256 * xh + xl) / pow2n`, where `xl` is divisible by `pow2n`
|
|
113
142
|
*/
|
|
114
|
-
function
|
|
115
|
-
|
|
143
|
+
function div512(uint256 xh, uint256 xl, uint256 pow2n) private pure returns (uint256) { unchecked {
|
|
144
|
+
uint256 pow2nInv = unsafeAdd(unsafeSub(0, pow2n) / pow2n, 1); // `1 << (256 - n)`
|
|
145
|
+
return unsafeMul(xh, pow2nInv) | (xl / pow2n); // `(xh << (256 - n)) | (xl >> n)`
|
|
116
146
|
}}
|
|
117
147
|
|
|
118
148
|
/**
|
|
119
|
-
* @dev Compute the
|
|
149
|
+
* @dev Compute the inverse of `d` modulo `2 ^ 256`, where `d` is congruent to `1` modulo `2`
|
|
120
150
|
*/
|
|
121
|
-
function
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
151
|
+
function inv256(uint256 d) private pure returns (uint256) { unchecked {
|
|
152
|
+
// approximate the root of `f(x) = 1 / x - d` using the newton–raphson convergence method
|
|
153
|
+
uint256 x = 1;
|
|
154
|
+
for (uint256 i = 0; i < 8; ++i)
|
|
155
|
+
x = unsafeMul(x, unsafeSub(2, unsafeMul(x, d))); // `x = x * (2 - x * d) mod 2 ^ 256`
|
|
156
|
+
return x;
|
|
125
157
|
}}
|
|
126
158
|
}
|