solidity-math-utils 2.0.1 → 2.1.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
CHANGED
|
@@ -58,6 +58,10 @@ Function `roundDiv(n, d)` computes the nearest integer (half being rounded upwar
|
|
|
58
58
|
|
|
59
59
|
Function `minFactor(x, y)` computes the smallest integer `z` such that `x * y / z <= 2 ^ 256 - 1`.
|
|
60
60
|
|
|
61
|
+
Function `mulShrF(x, y, s)` computes the largest integer smaller than or equal to `x * y / 2 ^ s`.
|
|
62
|
+
|
|
63
|
+
Function `mulShrC(x, y, s)` computes the smallest integer larger than or equal to `x * y / 2 ^ s`.
|
|
64
|
+
|
|
61
65
|
Function `mulDivF(x, y, z)` computes the largest integer smaller than or equal to `x * y / z`.
|
|
62
66
|
|
|
63
67
|
Function `mulDivC(x, y, z)` computes the smallest integer larger than or equal to `x * y / z`.
|
package/package.json
CHANGED
|
@@ -262,38 +262,38 @@ library AdvancedMath {
|
|
|
262
262
|
uint256 res = 0;
|
|
263
263
|
uint256 xi = x;
|
|
264
264
|
|
|
265
|
-
xi =
|
|
266
|
-
xi =
|
|
267
|
-
xi =
|
|
268
|
-
xi =
|
|
269
|
-
xi =
|
|
270
|
-
xi =
|
|
271
|
-
xi =
|
|
272
|
-
xi =
|
|
273
|
-
xi =
|
|
274
|
-
xi =
|
|
275
|
-
xi =
|
|
276
|
-
xi =
|
|
277
|
-
xi =
|
|
278
|
-
xi =
|
|
279
|
-
xi =
|
|
280
|
-
xi =
|
|
281
|
-
xi =
|
|
282
|
-
xi =
|
|
283
|
-
xi =
|
|
284
|
-
xi =
|
|
285
|
-
xi =
|
|
286
|
-
xi =
|
|
287
|
-
xi =
|
|
288
|
-
xi =
|
|
289
|
-
xi =
|
|
290
|
-
xi =
|
|
291
|
-
xi =
|
|
292
|
-
xi =
|
|
293
|
-
xi =
|
|
294
|
-
xi =
|
|
295
|
-
xi =
|
|
296
|
-
xi =
|
|
265
|
+
xi = xi * x / FIXED_1; res += xi * 0x00000000014d29a73a6e7b02c3668c7b0880000000; // add x^(03-1) * (34! * 03^(03-1) / 03!)
|
|
266
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000000002504a0cd9a7f7215b60f9be4800000000; // add x^(04-1) * (34! * 04^(04-1) / 04!)
|
|
267
|
+
xi = xi * x / FIXED_1; res += xi * 0x000000000484d0a1191c0ead267967c7a4a0000000; // add x^(05-1) * (34! * 05^(05-1) / 05!)
|
|
268
|
+
xi = xi * x / FIXED_1; res += xi * 0x00000000095ec580d7e8427a4baf26a90a00000000; // add x^(06-1) * (34! * 06^(06-1) / 06!)
|
|
269
|
+
xi = xi * x / FIXED_1; res += xi * 0x000000001440b0be1615a47dba6e5b3b1f10000000; // add x^(07-1) * (34! * 07^(07-1) / 07!)
|
|
270
|
+
xi = xi * x / FIXED_1; res += xi * 0x000000002d207601f46a99b4112418400000000000; // add x^(08-1) * (34! * 08^(08-1) / 08!)
|
|
271
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000000066ebaac4c37c622dd8288a7eb1b2000000; // add x^(09-1) * (34! * 09^(09-1) / 09!)
|
|
272
|
+
xi = xi * x / FIXED_1; res += xi * 0x00000000ef17240135f7dbd43a1ba10cf200000000; // add x^(10-1) * (34! * 10^(10-1) / 10!)
|
|
273
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000000233c33c676a5eb2416094a87b3657000000; // add x^(11-1) * (34! * 11^(11-1) / 11!)
|
|
274
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000000541cde48bc0254bed49a9f8700000000000; // add x^(12-1) * (34! * 12^(12-1) / 12!)
|
|
275
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000000cae1fad2cdd4d4cb8d73abca0d19a400000; // add x^(13-1) * (34! * 13^(13-1) / 13!)
|
|
276
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000001edb2aa2f760d15c41ceedba956400000000; // add x^(14-1) * (34! * 14^(14-1) / 14!)
|
|
277
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000004ba8d20d2dabd386c9529659841a2e200000; // add x^(15-1) * (34! * 15^(15-1) / 15!)
|
|
278
|
+
xi = xi * x / FIXED_1; res += xi * 0x000000bac08546b867cdaa20000000000000000000; // add x^(16-1) * (34! * 16^(16-1) / 16!)
|
|
279
|
+
xi = xi * x / FIXED_1; res += xi * 0x000001cfa8e70c03625b9db76c8ebf5bbf24820000; // add x^(17-1) * (34! * 17^(17-1) / 17!)
|
|
280
|
+
xi = xi * x / FIXED_1; res += xi * 0x000004851d99f82060df265f3309b26f8200000000; // add x^(18-1) * (34! * 18^(18-1) / 18!)
|
|
281
|
+
xi = xi * x / FIXED_1; res += xi * 0x00000b550d19b129d270c44f6f55f027723cbb0000; // add x^(19-1) * (34! * 19^(19-1) / 19!)
|
|
282
|
+
xi = xi * x / FIXED_1; res += xi * 0x00001c877dadc761dc272deb65d4b0000000000000; // add x^(20-1) * (34! * 20^(20-1) / 20!)
|
|
283
|
+
xi = xi * x / FIXED_1; res += xi * 0x000048178ece97479f33a77f2ad22a81b64406c000; // add x^(21-1) * (34! * 21^(21-1) / 21!)
|
|
284
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000b6ca8268b9d810fedf6695ef2f8a6c00000000; // add x^(22-1) * (34! * 22^(22-1) / 22!)
|
|
285
|
+
xi = xi * x / FIXED_1; res += xi * 0x0001d0e76631a5b05d007b8cb72a7c7f11ec36e000; // add x^(23-1) * (34! * 23^(23-1) / 23!)
|
|
286
|
+
xi = xi * x / FIXED_1; res += xi * 0x0004a1c37bd9f85fd9c6c780000000000000000000; // add x^(24-1) * (34! * 24^(24-1) / 24!)
|
|
287
|
+
xi = xi * x / FIXED_1; res += xi * 0x000bd8369f1b702bf491e2ebfcee08250313b65400; // add x^(25-1) * (34! * 25^(25-1) / 25!)
|
|
288
|
+
xi = xi * x / FIXED_1; res += xi * 0x001e5c7c32a9f6c70ab2cb59d9225764d400000000; // add x^(26-1) * (34! * 26^(26-1) / 26!)
|
|
289
|
+
xi = xi * x / FIXED_1; res += xi * 0x004dff5820e165e910f95120a708e742496221e600; // add x^(27-1) * (34! * 27^(27-1) / 27!)
|
|
290
|
+
xi = xi * x / FIXED_1; res += xi * 0x00c8c8f66db1fced378ee50e536000000000000000; // add x^(28-1) * (34! * 28^(28-1) / 28!)
|
|
291
|
+
xi = xi * x / FIXED_1; res += xi * 0x0205db8dffff45bfa2938f128f599dbf16eb11d880; // add x^(29-1) * (34! * 29^(29-1) / 29!)
|
|
292
|
+
xi = xi * x / FIXED_1; res += xi * 0x053a044ebd984351493e1786af38d39a0800000000; // add x^(30-1) * (34! * 30^(30-1) / 30!)
|
|
293
|
+
xi = xi * x / FIXED_1; res += xi * 0x0d86dae2a4cc0f47633a544479735869b487b59c40; // add x^(31-1) * (34! * 31^(31-1) / 31!)
|
|
294
|
+
xi = xi * x / FIXED_1; res += xi * 0x231000000000000000000000000000000000000000; // add x^(32-1) * (34! * 32^(32-1) / 32!)
|
|
295
|
+
xi = xi * x / FIXED_1; res += xi * 0x5b0485a76f6646c2039db1507cdd51b08649680822; // add x^(33-1) * (34! * 33^(33-1) / 33!)
|
|
296
|
+
xi = xi * x / FIXED_1; res += xi * 0xec983c46c49545bc17efa6b5b0055e242200000000; // add x^(34-1) * (34! * 34^(34-1) / 34!)
|
|
297
297
|
|
|
298
298
|
return res / 0xde1bc4d19efcac82445da75b00000000 + FIXED_1 + x; // divide by 34! and then add x^(1-1) * (1^(1-1) / 1!) + x^(2-1) * (2^(2-1) / 2!)
|
|
299
299
|
}}
|
|
@@ -307,38 +307,38 @@ library AdvancedMath {
|
|
|
307
307
|
uint256 res = 0;
|
|
308
308
|
uint256 xi = x;
|
|
309
309
|
|
|
310
|
-
xi =
|
|
311
|
-
xi =
|
|
312
|
-
xi =
|
|
313
|
-
xi =
|
|
314
|
-
xi =
|
|
315
|
-
xi =
|
|
316
|
-
xi =
|
|
317
|
-
xi =
|
|
318
|
-
xi =
|
|
319
|
-
xi =
|
|
320
|
-
xi =
|
|
321
|
-
xi =
|
|
322
|
-
xi =
|
|
323
|
-
xi =
|
|
324
|
-
xi =
|
|
325
|
-
xi =
|
|
326
|
-
xi =
|
|
327
|
-
xi =
|
|
328
|
-
xi =
|
|
329
|
-
xi =
|
|
330
|
-
xi =
|
|
331
|
-
xi =
|
|
332
|
-
xi =
|
|
333
|
-
xi =
|
|
334
|
-
xi =
|
|
335
|
-
xi =
|
|
336
|
-
xi =
|
|
337
|
-
xi =
|
|
338
|
-
xi =
|
|
339
|
-
xi =
|
|
340
|
-
xi =
|
|
341
|
-
xi =
|
|
310
|
+
xi = xi * x / FIXED_1; res += xi * 0x00000000014d29a73a6e7b02c3668c7b0880000000; // add x^(03-1) * (34! * 03^(03-1) / 03!)
|
|
311
|
+
xi = xi * x / FIXED_1; res -= xi * 0x0000000002504a0cd9a7f7215b60f9be4800000000; // sub x^(04-1) * (34! * 04^(04-1) / 04!)
|
|
312
|
+
xi = xi * x / FIXED_1; res += xi * 0x000000000484d0a1191c0ead267967c7a4a0000000; // add x^(05-1) * (34! * 05^(05-1) / 05!)
|
|
313
|
+
xi = xi * x / FIXED_1; res -= xi * 0x00000000095ec580d7e8427a4baf26a90a00000000; // sub x^(06-1) * (34! * 06^(06-1) / 06!)
|
|
314
|
+
xi = xi * x / FIXED_1; res += xi * 0x000000001440b0be1615a47dba6e5b3b1f10000000; // add x^(07-1) * (34! * 07^(07-1) / 07!)
|
|
315
|
+
xi = xi * x / FIXED_1; res -= xi * 0x000000002d207601f46a99b4112418400000000000; // sub x^(08-1) * (34! * 08^(08-1) / 08!)
|
|
316
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000000066ebaac4c37c622dd8288a7eb1b2000000; // add x^(09-1) * (34! * 09^(09-1) / 09!)
|
|
317
|
+
xi = xi * x / FIXED_1; res -= xi * 0x00000000ef17240135f7dbd43a1ba10cf200000000; // sub x^(10-1) * (34! * 10^(10-1) / 10!)
|
|
318
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000000233c33c676a5eb2416094a87b3657000000; // add x^(11-1) * (34! * 11^(11-1) / 11!)
|
|
319
|
+
xi = xi * x / FIXED_1; res -= xi * 0x0000000541cde48bc0254bed49a9f8700000000000; // sub x^(12-1) * (34! * 12^(12-1) / 12!)
|
|
320
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000000cae1fad2cdd4d4cb8d73abca0d19a400000; // add x^(13-1) * (34! * 13^(13-1) / 13!)
|
|
321
|
+
xi = xi * x / FIXED_1; res -= xi * 0x0000001edb2aa2f760d15c41ceedba956400000000; // sub x^(14-1) * (34! * 14^(14-1) / 14!)
|
|
322
|
+
xi = xi * x / FIXED_1; res += xi * 0x0000004ba8d20d2dabd386c9529659841a2e200000; // add x^(15-1) * (34! * 15^(15-1) / 15!)
|
|
323
|
+
xi = xi * x / FIXED_1; res -= xi * 0x000000bac08546b867cdaa20000000000000000000; // sub x^(16-1) * (34! * 16^(16-1) / 16!)
|
|
324
|
+
xi = xi * x / FIXED_1; res += xi * 0x000001cfa8e70c03625b9db76c8ebf5bbf24820000; // add x^(17-1) * (34! * 17^(17-1) / 17!)
|
|
325
|
+
xi = xi * x / FIXED_1; res -= xi * 0x000004851d99f82060df265f3309b26f8200000000; // sub x^(18-1) * (34! * 18^(18-1) / 18!)
|
|
326
|
+
xi = xi * x / FIXED_1; res += xi * 0x00000b550d19b129d270c44f6f55f027723cbb0000; // add x^(19-1) * (34! * 19^(19-1) / 19!)
|
|
327
|
+
xi = xi * x / FIXED_1; res -= xi * 0x00001c877dadc761dc272deb65d4b0000000000000; // sub x^(20-1) * (34! * 20^(20-1) / 20!)
|
|
328
|
+
xi = xi * x / FIXED_1; res += xi * 0x000048178ece97479f33a77f2ad22a81b64406c000; // add x^(21-1) * (34! * 21^(21-1) / 21!)
|
|
329
|
+
xi = xi * x / FIXED_1; res -= xi * 0x0000b6ca8268b9d810fedf6695ef2f8a6c00000000; // sub x^(22-1) * (34! * 22^(22-1) / 22!)
|
|
330
|
+
xi = xi * x / FIXED_1; res += xi * 0x0001d0e76631a5b05d007b8cb72a7c7f11ec36e000; // add x^(23-1) * (34! * 23^(23-1) / 23!)
|
|
331
|
+
xi = xi * x / FIXED_1; res -= xi * 0x0004a1c37bd9f85fd9c6c780000000000000000000; // sub x^(24-1) * (34! * 24^(24-1) / 24!)
|
|
332
|
+
xi = xi * x / FIXED_1; res += xi * 0x000bd8369f1b702bf491e2ebfcee08250313b65400; // add x^(25-1) * (34! * 25^(25-1) / 25!)
|
|
333
|
+
xi = xi * x / FIXED_1; res -= xi * 0x001e5c7c32a9f6c70ab2cb59d9225764d400000000; // sub x^(26-1) * (34! * 26^(26-1) / 26!)
|
|
334
|
+
xi = xi * x / FIXED_1; res += xi * 0x004dff5820e165e910f95120a708e742496221e600; // add x^(27-1) * (34! * 27^(27-1) / 27!)
|
|
335
|
+
xi = xi * x / FIXED_1; res -= xi * 0x00c8c8f66db1fced378ee50e536000000000000000; // sub x^(28-1) * (34! * 28^(28-1) / 28!)
|
|
336
|
+
xi = xi * x / FIXED_1; res += xi * 0x0205db8dffff45bfa2938f128f599dbf16eb11d880; // add x^(29-1) * (34! * 29^(29-1) / 29!)
|
|
337
|
+
xi = xi * x / FIXED_1; res -= xi * 0x053a044ebd984351493e1786af38d39a0800000000; // sub x^(30-1) * (34! * 30^(30-1) / 30!)
|
|
338
|
+
xi = xi * x / FIXED_1; res += xi * 0x0d86dae2a4cc0f47633a544479735869b487b59c40; // add x^(31-1) * (34! * 31^(31-1) / 31!)
|
|
339
|
+
xi = xi * x / FIXED_1; res -= xi * 0x231000000000000000000000000000000000000000; // sub x^(32-1) * (34! * 32^(32-1) / 32!)
|
|
340
|
+
xi = xi * x / FIXED_1; res += xi * 0x5b0485a76f6646c2039db1507cdd51b08649680822; // add x^(33-1) * (34! * 33^(33-1) / 33!)
|
|
341
|
+
xi = xi * x / FIXED_1; res -= xi * 0xec983c46c49545bc17efa6b5b0055e242200000000; // sub x^(34-1) * (34! * 34^(34-1) / 34!)
|
|
342
342
|
|
|
343
343
|
return res / 0xde1bc4d19efcac82445da75b00000000 + FIXED_1 - x; // divide by 34! and then add x^(1-1) * (1^(1-1) / 1!) - x^(2-1) * (2^(2-1) / 2!)
|
|
344
344
|
}}
|
|
@@ -144,6 +144,25 @@ library IntegralMath {
|
|
|
144
144
|
// `z = H + 1`
|
|
145
145
|
}}
|
|
146
146
|
|
|
147
|
+
/**
|
|
148
|
+
* @dev Compute the largest integer smaller than or equal to `x * y / 2 ^ s`
|
|
149
|
+
*/
|
|
150
|
+
function mulShrF(uint256 x, uint256 y, uint8 s) internal pure returns (uint256) { unchecked {
|
|
151
|
+
(uint256 xyh, uint256 xyl) = mul512(x, y);
|
|
152
|
+
require(xyh < 1 << s);
|
|
153
|
+
return (xyh << (256 - s)) | (xyl >> s);
|
|
154
|
+
}}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @dev Compute the smallest integer larger than or equal to `x * y / 2 ^ s`
|
|
158
|
+
*/
|
|
159
|
+
function mulShrC(uint256 x, uint256 y, uint8 s) internal pure returns (uint256) { unchecked {
|
|
160
|
+
uint256 w = mulShrF(x, y, s);
|
|
161
|
+
if (mulmod(x, y, 1 << s) > 0)
|
|
162
|
+
return safeAdd(w, 1);
|
|
163
|
+
return w;
|
|
164
|
+
}}
|
|
165
|
+
|
|
147
166
|
/**
|
|
148
167
|
* @dev Compute the largest integer smaller than or equal to `x * y / z`
|
|
149
168
|
*/
|