Skip to content

Commit

Permalink
Merge pull request #9366 from ethereum/wasm-clz
Browse files Browse the repository at this point in the history
Fix yulInterpreter to correctly handle i32.clz
  • Loading branch information
ekpyron authored Jul 10, 2020
2 parents 8d4ec27 + f5ae9c5 commit 65ed93d
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions test/tools/yulInterpreter/EwasmBuiltinInterpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ void copyZeroExtended(
_target[_targetOffset + i] = _sourceOffset + i < _source.size() ? _source[_sourceOffset + i] : 0;
}

/// Count leading zeros for uint64
uint64_t clz(uint64_t _v)
/// Count leading zeros for uint64. Following WebAssembly rules, it returns 64 for @a _v being zero.
/// NOTE: the clz builtin of the compiler may or may not do this
uint64_t clz64(uint64_t _v)
{
if (_v == 0)
return 64;
Expand Down Expand Up @@ -133,7 +134,11 @@ u256 EwasmBuiltinInterpreter::evalBuiltin(YulString _fun, vector<u256> const& _a
accessMemory(arg[0], 4);
return readMemoryHalfWord(arg[0]);
}

else if (_fun == "i32.clz"_yulstring)
// NOTE: the clz implementation assumes 64-bit inputs, hence the adjustment
return clz64(arg[0] & uint32_t(-1)) - 32;
else if (_fun == "i64.clz"_yulstring)
return clz64(arg[0]);

string prefix = _fun.str();
string suffix;
Expand Down Expand Up @@ -202,8 +207,6 @@ u256 EwasmBuiltinInterpreter::evalWasmBuiltin(string const& _fun, vector<Word> c
return arg[0] != arg[1] ? 1 : 0;
else if (_fun == "eqz")
return arg[0] == 0 ? 1 : 0;
else if (_fun == "clz")
return clz(arg[0]);
else if (_fun == "lt_u")
return arg[0] < arg[1] ? 1 : 0;
else if (_fun == "gt_u")
Expand Down

0 comments on commit 65ed93d

Please sign in to comment.