Use proper SAR for signed right shifts and emulate on pre-constantinople.

This commit is contained in:
Daniel Kirchner
2018-06-12 09:32:19 +01:00
committed by Alex Beregszaszi
parent 8999a2f375
commit f33dc92cbd
5 changed files with 152 additions and 70 deletions
+9 -2
View File
@@ -1084,9 +1084,16 @@ TypePointer RationalNumberType::binaryOperatorResult(Token::Value _operator, Typ
{
uint32_t exponent = other.m_value.numerator().convert_to<uint32_t>();
if (exponent > mostSignificantBit(boost::multiprecision::abs(m_value.numerator())))
value = 0;
value = m_value.numerator() < 0 ? -1 : 0;
else
value = rational(m_value.numerator() / boost::multiprecision::pow(bigint(2), exponent), 1);
{
if (m_value.numerator() < 0)
// add 1 to the negative value before dividing to get a result that is strictly too large
// subtract 1 afterwards to round towards negative infinity
value = rational((m_value.numerator() + 1) / boost::multiprecision::pow(bigint(2), exponent) - bigint(1), 1);
else
value = rational(m_value.numerator() / boost::multiprecision::pow(bigint(2), exponent), 1);
}
}
break;
}
+21 -4
View File
@@ -1737,11 +1737,28 @@ void ExpressionCompiler::appendShiftOperatorCode(Token::Value _operator, Type co
m_context << u256(2) << Instruction::EXP << Instruction::MUL;
break;
case Token::SAR:
// NOTE: SAR rounds differently than SDIV
if (m_context.evmVersion().hasBitwiseShifting() && !c_valueSigned)
m_context << Instruction::SHR;
if (m_context.evmVersion().hasBitwiseShifting())
m_context << (c_valueSigned ? Instruction::SAR : Instruction::SHR);
else
m_context << u256(2) << Instruction::EXP << Instruction::SWAP1 << (c_valueSigned ? Instruction::SDIV : Instruction::DIV);
{
if (c_valueSigned)
// For negative values xor_mask has all bits set and xor(value_to_shift, xor_mask) will be
// the bitwise complement of value_to_shift, i.e. abs(value_to_shift) - 1. Dividing this by
// exp(2, shift_amount) results in a value that is positive and strictly smaller than the
// absolute value of the desired result. Taking the complement again changes the sign
// back to negative and subtracts one, resulting in rounding towards negative infinity.
// For positive values xor_mask is zero and xor(value_to_shift, xor_mask) is again value_to_shift.
m_context.appendInlineAssembly(R"({
let xor_mask := sub(0, slt(value_to_shift, 0))
value_to_shift := xor(div(xor(value_to_shift, xor_mask), exp(2, shift_amount)), xor_mask)
})", {"value_to_shift", "shift_amount"});
else
m_context.appendInlineAssembly(R"({
value_to_shift := div(value_to_shift, exp(2, shift_amount))
})", {"value_to_shift", "shift_amount"});
m_context << Instruction::POP;
}
break;
case Token::SHR:
default: