@@ -5,6 +5,7 @@ pragma solidity ^0.8.20;
5
5
6
6
import {Address} from "../Address.sol " ;
7
7
import {Panic} from "../Panic.sol " ;
8
+ import {SafeCast} from "./SafeCast.sol " ;
8
9
9
10
/**
10
11
* @dev Standard math utilities missing in the Solidity language.
@@ -210,11 +211,7 @@ library Math {
210
211
* @dev Calculates x * y / denominator with full precision, following the selected rounding direction.
211
212
*/
212
213
function mulDiv (uint256 x , uint256 y , uint256 denominator , Rounding rounding ) internal pure returns (uint256 ) {
213
- uint256 result = mulDiv (x, y, denominator);
214
- if (unsignedRoundsUp (rounding) && mulmod (x, y, denominator) > 0 ) {
215
- result += 1 ;
216
- }
217
- return result;
214
+ return mulDiv (x, y, denominator) + SafeCast.toUint (unsignedRoundsUp (rounding) && mulmod (x, y, denominator) > 0 );
218
215
}
219
216
220
217
/**
@@ -383,7 +380,7 @@ library Math {
383
380
function sqrt (uint256 a , Rounding rounding ) internal pure returns (uint256 ) {
384
381
unchecked {
385
382
uint256 result = sqrt (a);
386
- return result + (unsignedRoundsUp (rounding) && result * result < a ? 1 : 0 );
383
+ return result + SafeCast. toUint (unsignedRoundsUp (rounding) && result * result < a);
387
384
}
388
385
}
389
386
@@ -393,38 +390,37 @@ library Math {
393
390
*/
394
391
function log2 (uint256 value ) internal pure returns (uint256 ) {
395
392
uint256 result = 0 ;
393
+ uint256 exp;
396
394
unchecked {
397
- if (value >> 128 > 0 ) {
398
- value >>= 128 ;
399
- result += 128 ;
400
- }
401
- if (value >> 64 > 0 ) {
402
- value >>= 64 ;
403
- result += 64 ;
404
- }
405
- if (value >> 32 > 0 ) {
406
- value >>= 32 ;
407
- result += 32 ;
408
- }
409
- if (value >> 16 > 0 ) {
410
- value >>= 16 ;
411
- result += 16 ;
412
- }
413
- if (value >> 8 > 0 ) {
414
- value >>= 8 ;
415
- result += 8 ;
416
- }
417
- if (value >> 4 > 0 ) {
418
- value >>= 4 ;
419
- result += 4 ;
420
- }
421
- if (value >> 2 > 0 ) {
422
- value >>= 2 ;
423
- result += 2 ;
424
- }
425
- if (value >> 1 > 0 ) {
426
- result += 1 ;
427
- }
395
+ exp = 128 * SafeCast.toUint (value > (1 << 128 ) - 1 );
396
+ value >>= exp;
397
+ result += exp;
398
+
399
+ exp = 64 * SafeCast.toUint (value > (1 << 64 ) - 1 );
400
+ value >>= exp;
401
+ result += exp;
402
+
403
+ exp = 32 * SafeCast.toUint (value > (1 << 32 ) - 1 );
404
+ value >>= exp;
405
+ result += exp;
406
+
407
+ exp = 16 * SafeCast.toUint (value > (1 << 16 ) - 1 );
408
+ value >>= exp;
409
+ result += exp;
410
+
411
+ exp = 8 * SafeCast.toUint (value > (1 << 8 ) - 1 );
412
+ value >>= exp;
413
+ result += exp;
414
+
415
+ exp = 4 * SafeCast.toUint (value > (1 << 4 ) - 1 );
416
+ value >>= exp;
417
+ result += exp;
418
+
419
+ exp = 2 * SafeCast.toUint (value > (1 << 2 ) - 1 );
420
+ value >>= exp;
421
+ result += exp;
422
+
423
+ result += SafeCast.toUint (value > 1 );
428
424
}
429
425
return result;
430
426
}
@@ -436,7 +432,7 @@ library Math {
436
432
function log2 (uint256 value , Rounding rounding ) internal pure returns (uint256 ) {
437
433
unchecked {
438
434
uint256 result = log2 (value);
439
- return result + (unsignedRoundsUp (rounding) && 1 << result < value ? 1 : 0 );
435
+ return result + SafeCast. toUint (unsignedRoundsUp (rounding) && 1 << result < value);
440
436
}
441
437
}
442
438
@@ -485,7 +481,7 @@ library Math {
485
481
function log10 (uint256 value , Rounding rounding ) internal pure returns (uint256 ) {
486
482
unchecked {
487
483
uint256 result = log10 (value);
488
- return result + (unsignedRoundsUp (rounding) && 10 ** result < value ? 1 : 0 );
484
+ return result + SafeCast. toUint (unsignedRoundsUp (rounding) && 10 ** result < value);
489
485
}
490
486
}
491
487
@@ -497,26 +493,25 @@ library Math {
497
493
*/
498
494
function log256 (uint256 value ) internal pure returns (uint256 ) {
499
495
uint256 result = 0 ;
496
+ uint256 isGt;
500
497
unchecked {
501
- if (value >> 128 > 0 ) {
502
- value >>= 128 ;
503
- result += 16 ;
504
- }
505
- if (value >> 64 > 0 ) {
506
- value >>= 64 ;
507
- result += 8 ;
508
- }
509
- if (value >> 32 > 0 ) {
510
- value >>= 32 ;
511
- result += 4 ;
512
- }
513
- if (value >> 16 > 0 ) {
514
- value >>= 16 ;
515
- result += 2 ;
516
- }
517
- if (value >> 8 > 0 ) {
518
- result += 1 ;
519
- }
498
+ isGt = SafeCast.toUint (value > (1 << 128 ) - 1 );
499
+ value >>= isGt * 128 ;
500
+ result += isGt * 16 ;
501
+
502
+ isGt = SafeCast.toUint (value > (1 << 64 ) - 1 );
503
+ value >>= isGt * 64 ;
504
+ result += isGt * 8 ;
505
+
506
+ isGt = SafeCast.toUint (value > (1 << 32 ) - 1 );
507
+ value >>= isGt * 32 ;
508
+ result += isGt * 4 ;
509
+
510
+ isGt = SafeCast.toUint (value > (1 << 16 ) - 1 );
511
+ value >>= isGt * 16 ;
512
+ result += isGt * 2 ;
513
+
514
+ result += SafeCast.toUint (value > (1 << 8 ) - 1 );
520
515
}
521
516
return result;
522
517
}
@@ -528,7 +523,7 @@ library Math {
528
523
function log256 (uint256 value , Rounding rounding ) internal pure returns (uint256 ) {
529
524
unchecked {
530
525
uint256 result = log256 (value);
531
- return result + (unsignedRoundsUp (rounding) && 1 << (result << 3 ) < value ? 1 : 0 );
526
+ return result + SafeCast. toUint (unsignedRoundsUp (rounding) && 1 << (result << 3 ) < value);
532
527
}
533
528
}
534
529
0 commit comments