@@ -5,6 +5,7 @@ pragma solidity ^0.8.20;
55
66import {Address} from "../Address.sol " ;
77import {Panic} from "../Panic.sol " ;
8+ import {SafeCast} from "./SafeCast.sol " ;
89
910/**
1011 * @dev Standard math utilities missing in the Solidity language.
@@ -210,7 +211,7 @@ library Math {
210211 * @dev Calculates x * y / denominator with full precision, following the selected rounding direction.
211212 */
212213 function mulDiv (uint256 x , uint256 y , uint256 denominator , Rounding rounding ) internal pure returns (uint256 ) {
213- return mulDiv (x, y, denominator) + boolToUint (unsignedRoundsUp (rounding) && mulmod (x, y, denominator) > 0 );
214+ return mulDiv (x, y, denominator) + SafeCast. toUint (unsignedRoundsUp (rounding) && mulmod (x, y, denominator) > 0 );
214215 }
215216
216217 /**
@@ -379,7 +380,7 @@ library Math {
379380 function sqrt (uint256 a , Rounding rounding ) internal pure returns (uint256 ) {
380381 unchecked {
381382 uint256 result = sqrt (a);
382- return result + boolToUint (unsignedRoundsUp (rounding) && result * result < a);
383+ return result + SafeCast. toUint (unsignedRoundsUp (rounding) && result * result < a);
383384 }
384385 }
385386
@@ -391,35 +392,35 @@ library Math {
391392 uint256 result = 0 ;
392393 uint256 exp;
393394 unchecked {
394- exp = 128 * boolToUint (value > (1 << 128 ) - 1 );
395+ exp = 128 * SafeCast. toUint (value > (1 << 128 ) - 1 );
395396 value >>= exp;
396397 result += exp;
397398
398- exp = 64 * boolToUint (value > (1 << 64 ) - 1 );
399+ exp = 64 * SafeCast. toUint (value > (1 << 64 ) - 1 );
399400 value >>= exp;
400401 result += exp;
401402
402- exp = 32 * boolToUint (value > (1 << 32 ) - 1 );
403+ exp = 32 * SafeCast. toUint (value > (1 << 32 ) - 1 );
403404 value >>= exp;
404405 result += exp;
405406
406- exp = 16 * boolToUint (value > (1 << 16 ) - 1 );
407+ exp = 16 * SafeCast. toUint (value > (1 << 16 ) - 1 );
407408 value >>= exp;
408409 result += exp;
409410
410- exp = 8 * boolToUint (value > (1 << 8 ) - 1 );
411+ exp = 8 * SafeCast. toUint (value > (1 << 8 ) - 1 );
411412 value >>= exp;
412413 result += exp;
413414
414- exp = 4 * boolToUint (value > (1 << 4 ) - 1 );
415+ exp = 4 * SafeCast. toUint (value > (1 << 4 ) - 1 );
415416 value >>= exp;
416417 result += exp;
417418
418- exp = 2 * boolToUint (value > (1 << 2 ) - 1 );
419+ exp = 2 * SafeCast. toUint (value > (1 << 2 ) - 1 );
419420 value >>= exp;
420421 result += exp;
421422
422- result += boolToUint (value > 1 );
423+ result += SafeCast. toUint (value > 1 );
423424 }
424425 return result;
425426 }
@@ -431,7 +432,7 @@ library Math {
431432 function log2 (uint256 value , Rounding rounding ) internal pure returns (uint256 ) {
432433 unchecked {
433434 uint256 result = log2 (value);
434- return result + boolToUint (unsignedRoundsUp (rounding) && 1 << result < value);
435+ return result + SafeCast. toUint (unsignedRoundsUp (rounding) && 1 << result < value);
435436 }
436437 }
437438
@@ -480,7 +481,7 @@ library Math {
480481 function log10 (uint256 value , Rounding rounding ) internal pure returns (uint256 ) {
481482 unchecked {
482483 uint256 result = log10 (value);
483- return result + boolToUint (unsignedRoundsUp (rounding) && 10 ** result < value);
484+ return result + SafeCast. toUint (unsignedRoundsUp (rounding) && 10 ** result < value);
484485 }
485486 }
486487
@@ -494,23 +495,23 @@ library Math {
494495 uint256 result = 0 ;
495496 uint256 isGt;
496497 unchecked {
497- isGt = boolToUint (value > (1 << 128 ) - 1 );
498+ isGt = SafeCast. toUint (value > (1 << 128 ) - 1 );
498499 value >>= isGt * 128 ;
499500 result += isGt * 16 ;
500501
501- isGt = boolToUint (value > (1 << 64 ) - 1 );
502+ isGt = SafeCast. toUint (value > (1 << 64 ) - 1 );
502503 value >>= isGt * 64 ;
503504 result += isGt * 8 ;
504505
505- isGt = boolToUint (value > (1 << 32 ) - 1 );
506+ isGt = SafeCast. toUint (value > (1 << 32 ) - 1 );
506507 value >>= isGt * 32 ;
507508 result += isGt * 4 ;
508509
509- isGt = boolToUint (value > (1 << 16 ) - 1 );
510+ isGt = SafeCast. toUint (value > (1 << 16 ) - 1 );
510511 value >>= isGt * 16 ;
511512 result += isGt * 2 ;
512513
513- result += boolToUint (value > (1 << 8 ) - 1 );
514+ result += SafeCast. toUint (value > (1 << 8 ) - 1 );
514515 }
515516 return result;
516517 }
@@ -522,7 +523,7 @@ library Math {
522523 function log256 (uint256 value , Rounding rounding ) internal pure returns (uint256 ) {
523524 unchecked {
524525 uint256 result = log256 (value);
525- return result + boolToUint (unsignedRoundsUp (rounding) && 1 << (result << 3 ) < value);
526+ return result + SafeCast. toUint (unsignedRoundsUp (rounding) && 1 << (result << 3 ) < value);
526527 }
527528 }
528529
@@ -532,14 +533,4 @@ library Math {
532533 function unsignedRoundsUp (Rounding rounding ) internal pure returns (bool ) {
533534 return uint8 (rounding) % 2 == 1 ;
534535 }
535-
536- /**
537- * @dev Cast a boolean (false or true) to a uint256 (0 or 1) with no jump.
538- */
539- function boolToUint (bool b ) internal pure returns (uint256 u ) {
540- /// @solidity memory-safe-assembly
541- assembly {
542- u := iszero (iszero (b))
543- }
544- }
545536}
0 commit comments