solidity-math-utils 2.0.0 → 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
@@ -54,15 +54,19 @@ Function `floorCbrt(n)` computes the largest integer smaller than or equal to th
54
54
 
55
55
  Function `ceilCbrt(n)` computes the smallest integer larger than or equal to the cubic root of `n`.
56
56
 
57
- Function `roundDiv(n, d)` computes the nearest integer to the quotient of `n` and `d` (or `n / d`).
57
+ Function `roundDiv(n, d)` computes the nearest integer (half being rounded upwards) to `n / d`.
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`.
64
68
 
65
- Function `mulDivR(x, y, z)` computes the nearest integer smaller than or larger than `x * y / z`.
69
+ Function `mulDivR(x, y, z)` computes the nearest integer (half being rounded upwards) to `x * y / z`.
66
70
 
67
71
  Function `mulDivExF(x, y, z, w)` computes the largest integer smaller than or equal to `(x * y) / (z * w)`.
68
72
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solidity-math-utils",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Solidity Mathematical Utilities",
5
5
  "repository": {
6
6
  "type": "git",
@@ -262,38 +262,38 @@ library AdvancedMath {
262
262
  uint256 res = 0;
263
263
  uint256 xi = x;
264
264
 
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!)
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 = (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!)
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
  }}
@@ -388,8 +388,8 @@ library AdvancedMath {
388
388
  function lambertPos3(uint256 x) internal pure returns (uint256) { unchecked {
389
389
  uint256 a = AnalyticMath.fixedLog(x);
390
390
  uint256 b = AnalyticMath.fixedLog(a);
391
- uint256 c = IntegralMath.mulDivF(b, b + (a - FIXED_1) * 2, a);
392
- uint256 d = IntegralMath.mulDivF(FIXED_1 / 2, c, a);
391
+ uint256 c = IntegralMath.mulDivF(a, a, FIXED_1 / 2);
392
+ uint256 d = IntegralMath.mulDivF(b, b + (a - FIXED_1) * 2, c);
393
393
  uint256 e = IntegralMath.mulDivF(FIXED_1, a - b + d, x);
394
394
  return e;
395
395
  }}
@@ -34,16 +34,16 @@ library IntegralMath {
34
34
  * @dev Compute the largest integer smaller than or equal to the square root of `n`
35
35
  */
36
36
  function floorSqrt(uint256 n) internal pure returns (uint256) { unchecked {
37
- if (n > 0) {
38
- uint256 x = n / 2 + 1;
39
- uint256 y = (x + n / x) / 2;
37
+ if (n > 63) {
38
+ uint256 x = n;
39
+ uint256 y = 1 << (floorLog2(n) / 2 + 1);
40
40
  while (x > y) {
41
41
  x = y;
42
- y = (x + n / x) / 2;
42
+ y = (x + n / x) >> 1;
43
43
  }
44
44
  return x;
45
45
  }
46
- return 0;
46
+ return 0x7777777777777776666666666666555555555554444444443333333222221110 >> (n * 4) & 0xf;
47
47
  }}
48
48
 
49
49
  /**
@@ -58,16 +58,16 @@ library IntegralMath {
58
58
  * @dev Compute the largest integer smaller than or equal to the cubic root of `n`
59
59
  */
60
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;
61
+ if (n > 84) {
62
+ uint256 x = n;
63
+ uint256 y = 1 << (floorLog2(n) / 3 + 1);
64
+ while (x > y) {
65
+ x = y;
66
+ y = ((x << 1) + n / x ** 2) / 3;
68
67
  }
68
+ return x;
69
69
  }
70
- return x;
70
+ return 0x49249249249249246db6db6db6db6db6db6db6db6db692492492492492249248 >> (n * 3) & 0x7;
71
71
  }}
72
72
 
73
73
  /**
@@ -79,7 +79,7 @@ library IntegralMath {
79
79
  }}
80
80
 
81
81
  /**
82
- * @dev Compute the nearest integer to the quotient of `n` and `d` (or `n / d`)
82
+ * @dev Compute the nearest integer (half being rounded upwards) to `n / d`
83
83
  */
84
84
  function roundDiv(uint256 n, uint256 d) internal pure returns (uint256) { unchecked {
85
85
  return n / d + (n % d) / (d - d / 2);
@@ -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
  */
@@ -177,7 +196,7 @@ library IntegralMath {
177
196
  }}
178
197
 
179
198
  /**
180
- * @dev Compute the nearest integer smaller than or larger than `x * y / z`
199
+ * @dev Compute the nearest integer (half being rounded upwards) to `x * y / z`
181
200
  */
182
201
  function mulDivR(uint256 x, uint256 y, uint256 z) internal pure returns (uint256) { unchecked {
183
202
  uint256 w = mulDivF(x, y, z);