-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathFixed.sol
772 lines (679 loc) · 25.5 KB
/
Fixed.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
// SPDX-License-Identifier: BlueOak-1.0.0
// solhint-disable func-name-mixedcase func-visibility
// slither-disable-start divide-before-multiply
pragma solidity ^0.8.19;
/// @title FixedPoint, a fixed-point arithmetic library defining the custom type uint192
/// @author Matt Elder <[email protected]> and the Reserve Team <https://reserve.org>
/** The logical type `uint192 ` is a 192 bit value, representing an 18-decimal Fixed-point
fractional value. This is what's described in the Solidity documentation as
"fixed192x18" -- a value represented by 192 bits, that makes 18 digits available to
the right of the decimal point.
The range of values that uint192 can represent is about [-1.7e20, 1.7e20].
Unless a function explicitly says otherwise, it will fail on overflow.
To be clear, the following should hold:
toFix(0) == 0
toFix(1) == 1e18
*/
// Analysis notes:
// Every function should revert iff its result is out of bounds.
// Unless otherwise noted, when a rounding mode is given, that mode is applied to
// a single division that may happen as the last step in the computation.
// Unless otherwise noted, when a rounding mode is *not* given but is needed, it's FLOOR.
// For each, we comment:
// - @return is the value expressed in "value space", where uint192(1e18) "is" 1.0
// - as-ints: is the value expressed in "implementation space", where uint192(1e18) "is" 1e18
// The "@return" expression is suitable for actually using the library
// The "as-ints" expression is suitable for testing
// A uint value passed to this library was out of bounds for uint192 operations
error UIntOutOfBounds();
bytes32 constant UIntOutofBoundsHash = keccak256(abi.encodeWithSignature("UIntOutOfBounds()"));
// Used by P1 implementation for easier casting
uint256 constant FIX_ONE_256 = 1e18;
uint8 constant FIX_DECIMALS = 18;
// If a particular uint192 is represented by the uint192 n, then the uint192 represents the
// value n/FIX_SCALE.
uint64 constant FIX_SCALE = 1e18;
// FIX_SCALE Squared:
uint128 constant FIX_SCALE_SQ = 1e36;
// The largest integer that can be converted to uint192 .
// This is a bit bigger than 3.1e39
uint192 constant FIX_MAX_INT = type(uint192).max / FIX_SCALE;
uint192 constant FIX_ZERO = 0; // The uint192 representation of zero.
uint192 constant FIX_ONE = FIX_SCALE; // The uint192 representation of one.
uint192 constant FIX_MAX = type(uint192).max; // The largest uint192. (Not an integer!)
uint192 constant FIX_MIN = 0; // The smallest uint192.
/// An enum that describes a rounding approach for converting to ints
enum RoundingMode {
FLOOR, // Round towards zero
ROUND, // Round to the nearest int
CEIL // Round away from zero
}
RoundingMode constant FLOOR = RoundingMode.FLOOR;
RoundingMode constant ROUND = RoundingMode.ROUND;
RoundingMode constant CEIL = RoundingMode.CEIL;
/* @dev Solidity 0.8.x only allows you to change one of type or size per type conversion.
Thus, all the tedious-looking double conversions like uint256(uint256 (foo))
See: https://docs.soliditylang.org/en/v0.8.17/080-breaking-changes.html#new-restrictions
*/
/// Explicitly convert a uint256 to a uint192. Revert if the input is out of bounds.
function _safeWrap(uint256 x) pure returns (uint192) {
if (FIX_MAX < x) revert UIntOutOfBounds();
return uint192(x);
}
/// Convert a uint to its Fix representation.
/// @return x
// as-ints: x * 1e18
function toFix(uint256 x) pure returns (uint192) {
return _safeWrap(x * FIX_SCALE);
}
/// Convert a uint to its fixed-point representation, and left-shift its value `shiftLeft`
/// decimal digits.
/// @return x * 10**shiftLeft
// as-ints: x * 10**(shiftLeft + 18)
function shiftl_toFix(uint256 x, int8 shiftLeft) pure returns (uint192) {
return shiftl_toFix(x, shiftLeft, FLOOR);
}
/// @return x * 10**shiftLeft
// as-ints: x * 10**(shiftLeft + 18)
function shiftl_toFix(
uint256 x,
int8 shiftLeft,
RoundingMode rounding
) pure returns (uint192) {
// conditions for avoiding overflow
if (x == 0) return 0;
if (shiftLeft <= -96) return (rounding == CEIL ? 1 : 0); // 0 < uint.max / 10**77 < 0.5
if (40 <= shiftLeft) revert UIntOutOfBounds(); // 10**56 < FIX_MAX < 10**57
shiftLeft += 18;
uint256 coeff = 10**abs(shiftLeft);
uint256 shifted = (shiftLeft >= 0) ? x * coeff : _divrnd(x, coeff, rounding);
return _safeWrap(shifted);
}
/// Divide a uint by a uint192, yielding a uint192
/// This may also fail if the result is MIN_uint192! not fixing this for optimization's sake.
/// @return x / y
// as-ints: x * 1e36 / y
function divFix(uint256 x, uint192 y) pure returns (uint192) {
// If we didn't have to worry about overflow, we'd just do `return x * 1e36 / _y`
// If it's safe to do this operation the easy way, do it:
if (x < uint256(type(uint256).max / FIX_SCALE_SQ)) {
return _safeWrap(uint256(x * FIX_SCALE_SQ) / y);
} else {
return _safeWrap(mulDiv256(x, FIX_SCALE_SQ, y));
}
}
/// Divide a uint by a uint, yielding a uint192
/// @return x / y
// as-ints: x * 1e18 / y
function divuu(uint256 x, uint256 y) pure returns (uint192) {
return _safeWrap(mulDiv256(FIX_SCALE, x, y));
}
/// @return min(x,y)
// as-ints: min(x,y)
function fixMin(uint192 x, uint192 y) pure returns (uint192) {
return x < y ? x : y;
}
/// @return max(x,y)
// as-ints: max(x,y)
function fixMax(uint192 x, uint192 y) pure returns (uint192) {
return x > y ? x : y;
}
/// @return absoluteValue(x,y)
// as-ints: absoluteValue(x,y)
function abs(int256 x) pure returns (uint256) {
return x < 0 ? uint256(-x) : uint256(x);
}
/// Divide two uints, returning a uint, using rounding mode `rounding`.
/// @return numerator / divisor
// as-ints: numerator / divisor
function _divrnd(
uint256 numerator,
uint256 divisor,
RoundingMode rounding
) pure returns (uint256) {
uint256 result = numerator / divisor;
if (rounding == FLOOR) return result;
if (rounding == ROUND) {
if (numerator % divisor > (divisor - 1) / 2) {
result++;
}
} else {
if (numerator % divisor != 0) {
result++;
}
}
return result;
}
library FixLib {
/// Again, all arithmetic functions fail if and only if the result is out of bounds.
/// Convert this fixed-point value to a uint. Round towards zero if needed.
/// @return x
// as-ints: x / 1e18
function toUint(uint192 x) internal pure returns (uint136) {
return toUint(x, FLOOR);
}
/// Convert this uint192 to a uint
/// @return x
// as-ints: x / 1e18 with rounding
function toUint(uint192 x, RoundingMode rounding) internal pure returns (uint136) {
return uint136(_divrnd(uint256(x), FIX_SCALE, rounding));
}
/// Return the uint192 shifted to the left by `decimal` digits
/// (Similar to a bitshift but in base 10)
/// @return x * 10**decimals
// as-ints: x * 10**decimals
function shiftl(uint192 x, int8 decimals) internal pure returns (uint192) {
return shiftl(x, decimals, FLOOR);
}
/// Return the uint192 shifted to the left by `decimal` digits
/// (Similar to a bitshift but in base 10)
/// @return x * 10**decimals
// as-ints: x * 10**decimals
function shiftl(
uint192 x,
int8 decimals,
RoundingMode rounding
) internal pure returns (uint192) {
// Handle overflow cases
if (x == 0) return 0;
if (decimals <= -59) return (rounding == CEIL ? 1 : 0); // 59, because 1e58 > 2**192
if (58 <= decimals) revert UIntOutOfBounds(); // 58, because x * 1e58 > 2 ** 192 if x != 0
uint256 coeff = uint256(10**abs(decimals));
return _safeWrap(decimals >= 0 ? x * coeff : _divrnd(x, coeff, rounding));
}
/// Add a uint192 to this uint192
/// @return x + y
// as-ints: x + y
function plus(uint192 x, uint192 y) internal pure returns (uint192) {
return x + y;
}
/// Add a uint to this uint192
/// @return x + y
// as-ints: x + y*1e18
function plusu(uint192 x, uint256 y) internal pure returns (uint192) {
return _safeWrap(x + y * FIX_SCALE);
}
/// Subtract a uint192 from this uint192
/// @return x - y
// as-ints: x - y
function minus(uint192 x, uint192 y) internal pure returns (uint192) {
return x - y;
}
/// Subtract a uint from this uint192
/// @return x - y
// as-ints: x - y*1e18
function minusu(uint192 x, uint256 y) internal pure returns (uint192) {
return _safeWrap(uint256(x) - uint256(y * FIX_SCALE));
}
/// Multiply this uint192 by a uint192
/// Round truncated values to the nearest available value. 5e-19 rounds away from zero.
/// @return x * y
// as-ints: x * y/1e18 [division using ROUND, not FLOOR]
function mul(uint192 x, uint192 y) internal pure returns (uint192) {
return mul(x, y, ROUND);
}
/// Multiply this uint192 by a uint192
/// @return x * y
// as-ints: x * y/1e18
function mul(
uint192 x,
uint192 y,
RoundingMode rounding
) internal pure returns (uint192) {
return _safeWrap(_divrnd(uint256(x) * uint256(y), FIX_SCALE, rounding));
}
/// Multiply this uint192 by a uint
/// @return x * y
// as-ints: x * y
function mulu(uint192 x, uint256 y) internal pure returns (uint192) {
return _safeWrap(x * y);
}
/// Divide this uint192 by a uint192
/// @return x / y
// as-ints: x * 1e18 / y
function div(uint192 x, uint192 y) internal pure returns (uint192) {
return div(x, y, FLOOR);
}
/// Divide this uint192 by a uint192
/// @return x / y
// as-ints: x * 1e18 / y
function div(
uint192 x,
uint192 y,
RoundingMode rounding
) internal pure returns (uint192) {
// Multiply-in FIX_SCALE before dividing by y to preserve precision.
return _safeWrap(_divrnd(uint256(x) * FIX_SCALE, y, rounding));
}
/// Divide this uint192 by a uint
/// @return x / y
// as-ints: x / y
function divu(uint192 x, uint256 y) internal pure returns (uint192) {
return divu(x, y, FLOOR);
}
/// Divide this uint192 by a uint
/// @return x / y
// as-ints: x / y
function divu(
uint192 x,
uint256 y,
RoundingMode rounding
) internal pure returns (uint192) {
return _safeWrap(_divrnd(x, y, rounding));
}
uint64 constant FIX_HALF = uint64(FIX_SCALE) / 2;
/// Raise this uint192 to a nonnegative integer power. Requires that x_ <= FIX_ONE
/// Gas cost is O(lg(y)), precision is +- 1e-18.
/// @return x_ ** y
// as-ints: x_ ** y / 1e18**(y-1) <- technically correct for y = 0. :D
function powu(uint192 x_, uint48 y) internal pure returns (uint192) {
require(x_ <= FIX_ONE);
if (y == 1) return x_;
if (x_ == FIX_ONE || y == 0) return FIX_ONE;
uint256 x = uint256(x_) * FIX_SCALE; // x is D36
uint256 result = FIX_SCALE_SQ; // result is D36
while (true) {
if (y & 1 == 1) result = (result * x + FIX_SCALE_SQ / 2) / FIX_SCALE_SQ;
if (y <= 1) break;
y = (y >> 1);
x = (x * x + FIX_SCALE_SQ / 2) / FIX_SCALE_SQ;
}
return _safeWrap(result / FIX_SCALE);
}
function sqrt(uint192 x) internal pure returns (uint192) {
return _safeWrap(sqrt256(x * FIX_ONE_256)); // FLOOR
}
/// Comparison operators...
function lt(uint192 x, uint192 y) internal pure returns (bool) {
return x < y;
}
function lte(uint192 x, uint192 y) internal pure returns (bool) {
return x <= y;
}
function gt(uint192 x, uint192 y) internal pure returns (bool) {
return x > y;
}
function gte(uint192 x, uint192 y) internal pure returns (bool) {
return x >= y;
}
function eq(uint192 x, uint192 y) internal pure returns (bool) {
return x == y;
}
function neq(uint192 x, uint192 y) internal pure returns (bool) {
return x != y;
}
/// Return whether or not this uint192 is less than epsilon away from y.
/// @return |x - y| < epsilon
// as-ints: |x - y| < epsilon
function near(
uint192 x,
uint192 y,
uint192 epsilon
) internal pure returns (bool) {
uint192 diff = x <= y ? y - x : x - y;
return diff < epsilon;
}
// ================ Chained Operations ================
// The operation foo_bar() always means:
// Do foo() followed by bar(), and overflow only if the _end_ result doesn't fit in an uint192
/// Shift this uint192 left by `decimals` digits, and convert to a uint
/// @return x * 10**decimals
// as-ints: x * 10**(decimals - 18)
function shiftl_toUint(uint192 x, int8 decimals) internal pure returns (uint256) {
return shiftl_toUint(x, decimals, FLOOR);
}
/// Shift this uint192 left by `decimals` digits, and convert to a uint.
/// @return x * 10**decimals
// as-ints: x * 10**(decimals - 18)
function shiftl_toUint(
uint192 x,
int8 decimals,
RoundingMode rounding
) internal pure returns (uint256) {
// Handle overflow cases
if (x == 0) return 0; // always computable, no matter what decimals is
if (decimals <= -42) return (rounding == CEIL ? 1 : 0);
if (96 <= decimals) revert UIntOutOfBounds();
decimals -= 18; // shift so that toUint happens at the same time.
uint256 coeff = uint256(10**abs(decimals));
return decimals >= 0 ? uint256(x * coeff) : uint256(_divrnd(x, coeff, rounding));
}
/// Multiply this uint192 by a uint, and output the result as a uint
/// @return x * y
// as-ints: x * y / 1e18
function mulu_toUint(uint192 x, uint256 y) internal pure returns (uint256) {
return mulDiv256(uint256(x), y, FIX_SCALE);
}
/// Multiply this uint192 by a uint, and output the result as a uint
/// @return x * y
// as-ints: x * y / 1e18
function mulu_toUint(
uint192 x,
uint256 y,
RoundingMode rounding
) internal pure returns (uint256) {
return mulDiv256(uint256(x), y, FIX_SCALE, rounding);
}
/// Multiply this uint192 by a uint192 and output the result as a uint
/// @return x * y
// as-ints: x * y / 1e36
function mul_toUint(uint192 x, uint192 y) internal pure returns (uint256) {
return mulDiv256(uint256(x), uint256(y), FIX_SCALE_SQ);
}
/// Multiply this uint192 by a uint192 and output the result as a uint
/// @return x * y
// as-ints: x * y / 1e36
function mul_toUint(
uint192 x,
uint192 y,
RoundingMode rounding
) internal pure returns (uint256) {
return mulDiv256(uint256(x), uint256(y), FIX_SCALE_SQ, rounding);
}
/// Compute x * y / z avoiding intermediate overflow
/// @dev Only use if you need to avoid overflow; costlier than x * y / z
/// @return x * y / z
// as-ints: x * y / z
function muluDivu(
uint192 x,
uint256 y,
uint256 z
) internal pure returns (uint192) {
return muluDivu(x, y, z, FLOOR);
}
/// Compute x * y / z, avoiding intermediate overflow
/// @dev Only use if you need to avoid overflow; costlier than x * y / z
/// @return x * y / z
// as-ints: x * y / z
function muluDivu(
uint192 x,
uint256 y,
uint256 z,
RoundingMode rounding
) internal pure returns (uint192) {
return _safeWrap(mulDiv256(x, y, z, rounding));
}
/// Compute x * y / z on Fixes, avoiding intermediate overflow
/// @dev Only use if you need to avoid overflow; costlier than x * y / z
/// @return x * y / z
// as-ints: x * y / z
function mulDiv(
uint192 x,
uint192 y,
uint192 z
) internal pure returns (uint192) {
return mulDiv(x, y, z, FLOOR);
}
/// Compute x * y / z on Fixes, avoiding intermediate overflow
/// @dev Only use if you need to avoid overflow; costlier than x * y / z
/// @return x * y / z
// as-ints: x * y / z
function mulDiv(
uint192 x,
uint192 y,
uint192 z,
RoundingMode rounding
) internal pure returns (uint192) {
return _safeWrap(mulDiv256(x, y, z, rounding));
}
// === safe*() ===
/// Multiply two fixes, rounding up to FIX_MAX and down to 0
/// @param a First param to multiply
/// @param b Second param to multiply
function safeMul(
uint192 a,
uint192 b,
RoundingMode rounding
) internal pure returns (uint192) {
// untestable:
// a will never = 0 here because of the check in _price()
if (a == 0 || b == 0) return 0;
// untestable:
// a = FIX_MAX iff b = 0
if (a == FIX_MAX || b == FIX_MAX) return FIX_MAX;
// return FIX_MAX instead of throwing overflow errors.
unchecked {
// p and mul *are* Fix values, so have 18 decimals (D18)
uint256 rawDelta = uint256(b) * a; // {D36} = {D18} * {D18}
// if we overflowed, then return FIX_MAX
if (rawDelta / b != a) return FIX_MAX;
uint256 shiftDelta = rawDelta;
// add in rounding
if (rounding == RoundingMode.ROUND) shiftDelta += (FIX_ONE / 2);
else if (rounding == RoundingMode.CEIL) shiftDelta += FIX_ONE - 1;
// untestable (here there be dragons):
// (below explanation is for the ROUND case, but it extends to the FLOOR/CEIL too)
// A) shiftDelta = rawDelta + (FIX_ONE / 2)
// shiftDelta overflows if:
// B) shiftDelta = MAX_UINT256 - FIX_ONE/2 + 1
// rawDelta + (FIX_ONE/2) = MAX_UINT256 - FIX_ONE/2 + 1
// b * a = MAX_UINT256 - FIX_ONE + 1
// therefore shiftDelta overflows if:
// C) b = (MAX_UINT256 - FIX_ONE + 1) / a
// MAX_UINT256 ~= 1e77 , FIX_MAX ~= 6e57 (6e20 difference in magnitude)
// a <= 1e21 (MAX_TARGET_AMT)
// a must be between 1e19 & 1e20 in order for b in (C) to be uint192,
// but a would have to be < 1e18 in order for (A) to overflow
if (shiftDelta < rawDelta) return FIX_MAX;
// return FIX_MAX if return result would truncate
if (shiftDelta / FIX_ONE > FIX_MAX) return FIX_MAX;
// return _div(rawDelta, FIX_ONE, rounding)
return uint192(shiftDelta / FIX_ONE); // {D18} = {D36} / {D18}
}
}
/// Divide two fixes, rounding up to FIX_MAX and down to 0
/// @param a Numerator
/// @param b Denominator
function safeDiv(
uint192 a,
uint192 b,
RoundingMode rounding
) internal pure returns (uint192) {
if (a == 0) return 0;
if (b == 0) return FIX_MAX;
uint256 raw = _divrnd(FIX_ONE_256 * a, uint256(b), rounding);
if (raw >= FIX_MAX) return FIX_MAX;
return uint192(raw); // don't need _safeWrap
}
/// Multiplies two fixes and divide by a third
/// @param a First to multiply
/// @param b Second to multiply
/// @param c Denominator
function safeMulDiv(
uint192 a,
uint192 b,
uint192 c,
RoundingMode rounding
) internal pure returns (uint192 result) {
if (a == 0 || b == 0) return 0;
if (a == FIX_MAX || b == FIX_MAX || c == 0) return FIX_MAX;
uint256 result_256;
unchecked {
(uint256 hi, uint256 lo) = fullMul(a, b);
if (hi >= c) return FIX_MAX;
uint256 mm = mulmod(a, b, c);
if (mm > lo) hi -= 1;
lo -= mm;
uint256 pow2 = c & (0 - c);
uint256 c_256 = uint256(c);
// Warning: Should not access c below this line
c_256 /= pow2;
lo /= pow2;
lo += hi * ((0 - pow2) / pow2 + 1);
uint256 r = 1;
r *= 2 - c_256 * r;
r *= 2 - c_256 * r;
r *= 2 - c_256 * r;
r *= 2 - c_256 * r;
r *= 2 - c_256 * r;
r *= 2 - c_256 * r;
r *= 2 - c_256 * r;
r *= 2 - c_256 * r;
result_256 = lo * r;
// Apply rounding
if (rounding == CEIL) {
if (mm != 0) result_256 += 1;
} else if (rounding == ROUND) {
if (mm > ((c_256 - 1) / 2)) result_256 += 1;
}
}
if (result_256 >= FIX_MAX) return FIX_MAX;
return uint192(result_256);
}
}
// ================ a couple pure-uint helpers================
// as-ints comments are omitted here, because they're the same as @return statements, because
// these are all pure uint functions
/// Return (x*y/z), avoiding intermediate overflow.
// Adapted from sources:
// https://medium.com/coinmonks/4db014e080b1, https://medium.com/wicketh/afa55870a65
// and quite a few of the other excellent "Mathemagic" posts from https://medium.com/wicketh
/// @dev Only use if you need to avoid overflow; costlier than x * y / z
/// @return result x * y / z
function mulDiv256(
uint256 x,
uint256 y,
uint256 z
) pure returns (uint256 result) {
unchecked {
(uint256 hi, uint256 lo) = fullMul(x, y);
if (hi >= z) revert UIntOutOfBounds();
uint256 mm = mulmod(x, y, z);
if (mm > lo) hi -= 1;
lo -= mm;
uint256 pow2 = z & (0 - z);
z /= pow2;
lo /= pow2;
lo += hi * ((0 - pow2) / pow2 + 1);
uint256 r = 1;
r *= 2 - z * r;
r *= 2 - z * r;
r *= 2 - z * r;
r *= 2 - z * r;
r *= 2 - z * r;
r *= 2 - z * r;
r *= 2 - z * r;
r *= 2 - z * r;
result = lo * r;
}
}
/// Return (x*y/z), avoiding intermediate overflow.
/// @dev Only use if you need to avoid overflow; costlier than x * y / z
/// @return x * y / z
function mulDiv256(
uint256 x,
uint256 y,
uint256 z,
RoundingMode rounding
) pure returns (uint256) {
uint256 result = mulDiv256(x, y, z);
if (rounding == FLOOR) return result;
uint256 mm = mulmod(x, y, z);
if (rounding == CEIL) {
if (mm != 0) result += 1;
} else {
if (mm > ((z - 1) / 2)) result += 1; // z should be z-1
}
return result;
}
/// Return (x*y) as a "virtual uint512" (lo, hi), representing (hi*2**256 + lo)
/// Adapted from sources:
/// https://medium.com/wicketh/27650fec525d, https://medium.com/coinmonks/4db014e080b1
/// @dev Intended to be internal to this library
/// @return hi (hi, lo) satisfies hi*(2**256) + lo == x * y
/// @return lo (paired with `hi`)
function fullMul(uint256 x, uint256 y) pure returns (uint256 hi, uint256 lo) {
unchecked {
uint256 mm = mulmod(x, y, uint256(0) - uint256(1));
lo = x * y;
hi = mm - lo;
if (mm < lo) hi -= 1;
}
}
// =============== from prbMath at commit 28055f6cd9a2367f9ad7ab6c8e01c9ac8e9acc61 ===============
/// @notice Calculates the square root of x using the Babylonian method.
///
/// @dev See https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Babylonian_method.
///
/// Notes:
/// - If x is not a perfect square, the result is rounded down.
/// - Credits to OpenZeppelin for the explanations in comments below.
///
/// @param x The uint256 number for which to calculate the square root.
/// @return result The result as a uint256.
function sqrt256(uint256 x) pure returns (uint256 result) {
if (x == 0) {
return 0;
}
// For our first guess, we calculate the biggest power of 2 which is smaller than the square root of x.
//
// We know that the "msb" (most significant bit) of x is a power of 2 such that we have:
//
// $$
// msb(x) <= x <= 2*msb(x)$
// $$
//
// We write $msb(x)$ as $2^k$, and we get:
//
// $$
// k = log_2(x)
// $$
//
// Thus, we can write the initial inequality as:
//
// $$
// 2^{log_2(x)} <= x <= 2*2^{log_2(x)+1} \\
// sqrt(2^k) <= sqrt(x) < sqrt(2^{k+1}) \\
// 2^{k/2} <= sqrt(x) < 2^{(k+1)/2} <= 2^{(k/2)+1}
// $$
//
// Consequently, $2^{log_2(x) /2} is a good first approximation of sqrt(x) with at least one correct bit.
uint256 xAux = uint256(x);
result = 1;
if (xAux >= 2**128) {
xAux >>= 128;
result <<= 64;
}
if (xAux >= 2**64) {
xAux >>= 64;
result <<= 32;
}
if (xAux >= 2**32) {
xAux >>= 32;
result <<= 16;
}
if (xAux >= 2**16) {
xAux >>= 16;
result <<= 8;
}
if (xAux >= 2**8) {
xAux >>= 8;
result <<= 4;
}
if (xAux >= 2**4) {
xAux >>= 4;
result <<= 2;
}
if (xAux >= 2**2) {
result <<= 1;
}
// At this point, `result` is an estimation with at least one bit of precision. We know the true value has at
// most 128 bits, since it is the square root of a uint256. Newton's method converges quadratically (precision
// doubles at every iteration). We thus need at most 7 iteration to turn our partial result with one bit of
// precision into the expected uint128 result.
unchecked {
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
result = (result + x / result) >> 1;
// If x is not a perfect square, round the result toward zero.
uint256 roundedResult = x / result;
if (result >= roundedResult) {
result = roundedResult;
}
}
}
// slither-disable-end divide-before-multiply