From 00f3c42d171cb8c1ae731ad0df0f7cb40726424a Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 13 Dec 2019 00:35:55 +0000 Subject: [PATCH] Use multiprecision::msb() in GasMeter and remove FixedHash dependency --- libevmasm/GasMeter.cpp | 11 ++++++++--- libsolutil/FixedHash.h | 16 ---------------- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/libevmasm/GasMeter.cpp b/libevmasm/GasMeter.cpp index 358d67c3a..6a07e9905 100644 --- a/libevmasm/GasMeter.cpp +++ b/libevmasm/GasMeter.cpp @@ -19,8 +19,6 @@ #include -#include - using namespace std; using namespace solidity; using namespace solidity::util; @@ -182,7 +180,14 @@ GasMeter::GasConsumption GasMeter::estimateMax(AssemblyItem const& _item, bool _ case Instruction::EXP: gas = GasCosts::expGas; if (u256 const* value = classes.knownConstant(m_state->relativeStackElement(-1))) - gas += GasCosts::expByteGas(m_evmVersion) * (32 - (h256(*value).firstBitSet() / 8)); + { + if (*value) + { + // Note: msb() counts from 0 and throws on 0 as input. + unsigned const significantByteCount = (boost::multiprecision::msb(*value) + 1 + 7) / 8; + gas += GasCosts::expByteGas(m_evmVersion) * significantByteCount; + } + } else gas += GasCosts::expByteGas(m_evmVersion) * 32; break; diff --git a/libsolutil/FixedHash.h b/libsolutil/FixedHash.h index f5a042d4c..4fa5a3072 100644 --- a/libsolutil/FixedHash.h +++ b/libsolutil/FixedHash.h @@ -104,22 +104,6 @@ public: /// @returns a copy of the object's data as a byte vector. bytes asBytes() const { return bytes(data(), data() + N); } - /// Returns the index of the first bit set to one, or size() * 8 if no bits are set. - inline unsigned firstBitSet() const - { - unsigned ret = 0; - for (auto d: m_data) - if (d) - { - for (;; ++ret, d <<= 1) - if (d & 0x80) - return ret; - } - else - ret += 8; - return ret; - } - private: std::array m_data; ///< The binary data. };