Branchless ternary, min and max methods (#4976)
Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: Ernesto García <ernestognw@gmail.com>
This commit is contained in:
committed by
GitHub
parent
60afc99053
commit
4032b42694
@ -3,22 +3,40 @@
|
||||
|
||||
pragma solidity ^0.8.20;
|
||||
|
||||
import {SafeCast} from "./SafeCast.sol";
|
||||
|
||||
/**
|
||||
* @dev Standard signed math utilities missing in the Solidity language.
|
||||
*/
|
||||
library SignedMath {
|
||||
/**
|
||||
* @dev Branchless ternary evaluation for `a ? b : c`. Gas costs are constant.
|
||||
*
|
||||
* IMPORTANT: This function may reduce bytecode size and consume less gas when used standalone.
|
||||
* However, the compiler may optimize Solidity ternary operations (i.e. `a ? b : c`) to only compute
|
||||
* one branch when needed, making this function more expensive.
|
||||
*/
|
||||
function ternary(bool condition, int256 a, int256 b) internal pure returns (int256) {
|
||||
unchecked {
|
||||
// branchless terinary works because:
|
||||
// b ^ (a ^ b) == a
|
||||
// b ^ 0 == b
|
||||
return b ^ ((a ^ b) * int256(SafeCast.toUint(condition)));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the largest of two signed numbers.
|
||||
*/
|
||||
function max(int256 a, int256 b) internal pure returns (int256) {
|
||||
return a > b ? a : b;
|
||||
return ternary(a > b, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev Returns the smallest of two signed numbers.
|
||||
*/
|
||||
function min(int256 a, int256 b) internal pure returns (int256) {
|
||||
return a < b ? a : b;
|
||||
return ternary(a < b, a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user