Fix conversion.

This commit is contained in:
chriseth 2021-08-19 16:30:30 +02:00
parent c4c0d0502d
commit d33eaf48b8
3 changed files with 17 additions and 3 deletions

View File

@ -915,11 +915,13 @@ void CompilerUtils::convertType(
else
solAssert(false, "");
bigint factor = pow(bigint(10), static_cast<unsigned>(abs(digitDifference)));
solAssert(0 <= factor && factor <= std::numeric_limits<u256>::max(), "");
if (digitDifference > 0)
m_context << pow(u256(10), static_cast<unsigned>(digitDifference)) << Instruction::MUL;
m_context << u256(factor) << Instruction::MUL;
else if (digitDifference < 0)
m_context <<
pow(u256(10), static_cast<unsigned>(-digitDifference)) <<
u256(factor) <<
Instruction::SWAP1 <<
(isSigned ? Instruction::SDIV : Instruction::DIV);

View File

@ -499,10 +499,12 @@ string YulUtilFunctions::fixedPointShiftFunction(int _digits, bool _signed)
return m_functionCollector.createFunction(functionName, [&](vector<string>& _args, vector<string>& _ret) {
_args = {"value"};
_ret = {"result"};
bigint factor = bigint("1" + string(static_cast<unsigned>(abs(_digits)), '0'));
solAssert(factor < (bigint(1) << 256), "");
return
Whiskers("result := <op>(value, <factor>)")
("op", _digits >= 0 ? "mul" : _signed ? "sdiv" : "div")
("factor", ("1" + string(static_cast<unsigned>(abs(_digits)), '0')))
("factor", factor.str())
.render();
});
}

View File

@ -0,0 +1,10 @@
contract C {
function test() public pure returns (uint a) {
ufixed256x77 x = 1.0000001234;
return uint(x);
}
}
// ====
// compileViaYul: also
// ----
// test() -> 0