Do not use SAR instead of SDIV in shifts because it rounds differently

This commit is contained in:
Alex Beregszaszi 2018-04-06 14:54:21 +02:00
parent c3608eaf90
commit 52c9441879
3 changed files with 10 additions and 7 deletions

View File

@ -1439,17 +1439,18 @@ string ABIFunctions::shiftRightFunction(size_t _numBits, bool _signed)
solAssert(_numBits < 256, ""); solAssert(_numBits < 256, "");
string functionName = "shift_right_" + to_string(_numBits) + (_signed ? "_signed" : "_unsigned"); string functionName = "shift_right_" + to_string(_numBits) + (_signed ? "_signed" : "_unsigned");
if (m_evmVersion.hasBitwiseShifting())
// NOTE: SAR rounds differently than SDIV
if (m_evmVersion.hasBitwiseShifting() && !_signed)
{ {
return createFunction(functionName, [&]() { return createFunction(functionName, [&]() {
return return
Whiskers(R"( Whiskers(R"(
function <functionName>(value) -> newValue { function <functionName>(value) -> newValue {
newValue := <shiftOp>(<numBits>, value) newValue := shr(<numBits>, value)
} }
)") )")
("functionName", functionName) ("functionName", functionName)
("shiftOp", _signed ? "sar" : "shr")
("numBits", to_string(_numBits)) ("numBits", to_string(_numBits))
.render(); .render();
}); });

View File

@ -1274,8 +1274,9 @@ void CompilerUtils::leftShiftNumberOnStack(unsigned _bits)
void CompilerUtils::rightShiftNumberOnStack(unsigned _bits, bool _isSigned) void CompilerUtils::rightShiftNumberOnStack(unsigned _bits, bool _isSigned)
{ {
solAssert(_bits < 256, ""); solAssert(_bits < 256, "");
if (m_context.evmVersion().hasBitwiseShifting()) // NOTE: SAR rounds differently than SDIV
m_context << _bits << (_isSigned ? Instruction::SAR : Instruction::SHR); if (m_context.evmVersion().hasBitwiseShifting() && !_isSigned)
m_context << _bits << Instruction::SHR;
else else
m_context << (u256(1) << _bits) << Instruction::SWAP1 << (_isSigned ? Instruction::SDIV : Instruction::DIV); m_context << (u256(1) << _bits) << Instruction::SWAP1 << (_isSigned ? Instruction::SDIV : Instruction::DIV);
} }

View File

@ -1718,8 +1718,9 @@ void ExpressionCompiler::appendShiftOperatorCode(Token::Value _operator, Type co
m_context << u256(2) << Instruction::EXP << Instruction::MUL; m_context << u256(2) << Instruction::EXP << Instruction::MUL;
break; break;
case Token::SAR: case Token::SAR:
if (m_context.evmVersion().hasBitwiseShifting()) // NOTE: SAR rounds differently than SDIV
m_context << (c_valueSigned ? Instruction::SAR : Instruction::SHR); if (m_context.evmVersion().hasBitwiseShifting() && !c_valueSigned)
m_context << Instruction::SHR;
else else
m_context << u256(2) << Instruction::EXP << Instruction::SWAP1 << (c_valueSigned ? Instruction::SDIV : Instruction::DIV); m_context << u256(2) << Instruction::EXP << Instruction::SWAP1 << (c_valueSigned ? Instruction::SDIV : Instruction::DIV);
break; break;