mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Unify style of checked integer division for sol->yul code generation.
This commit is contained in:
parent
5f6af8b374
commit
e4c884ae13
@ -439,8 +439,6 @@ string YulUtilFunctions::overflowCheckedUIntMulFunction(size_t _bits)
|
|||||||
|
|
||||||
string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
||||||
{
|
{
|
||||||
unsigned bits = _type.numBits();
|
|
||||||
solAssert(0 < bits && bits <= 256 && bits % 8 == 0, "");
|
|
||||||
string functionName = "checked_div_" + _type.identifier();
|
string functionName = "checked_div_" + _type.identifier();
|
||||||
return m_functionCollector->createFunction(functionName, [&]() {
|
return m_functionCollector->createFunction(functionName, [&]() {
|
||||||
return
|
return
|
||||||
@ -448,7 +446,7 @@ string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
|||||||
function <functionName>(x, y) -> r {
|
function <functionName>(x, y) -> r {
|
||||||
if iszero(y) { revert(0, 0) }
|
if iszero(y) { revert(0, 0) }
|
||||||
<?signed>
|
<?signed>
|
||||||
// x / -1 == x
|
// overflow for minVal / -1
|
||||||
if and(
|
if and(
|
||||||
eq(x, <minVal>),
|
eq(x, <minVal>),
|
||||||
eq(y, sub(0, 1))
|
eq(y, sub(0, 1))
|
||||||
@ -457,10 +455,10 @@ string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
|||||||
r := <?signed>s</signed>div(x, y)
|
r := <?signed>s</signed>div(x, y)
|
||||||
}
|
}
|
||||||
)")
|
)")
|
||||||
("functionName", functionName)
|
("functionName", functionName)
|
||||||
("signed", _type.isSigned())
|
("signed", _type.isSigned())
|
||||||
("minVal", (0 - (u256(1) << (bits - 1))).str())
|
("minVal", toCompactHexWithPrefix(u256(_type.minValue())))
|
||||||
.render();
|
.render();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user