mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-22 04:36:42 +00:00
Implementation of DIV & MOD with gmp library
This commit is contained in:
parent
de186f1ed1
commit
100de855ec
2 changed files with 52 additions and 6 deletions
|
|
@ -1,8 +1,10 @@
|
|||
#include "Arith256.h"
|
||||
#include "Runtime.h"
|
||||
#include "Type.h"
|
||||
#include "Endianness.h"
|
||||
|
||||
#include <llvm/IR/Function.h>
|
||||
#include <gmp.h>
|
||||
|
||||
namespace dev
|
||||
{
|
||||
|
|
@ -63,6 +65,8 @@ llvm::Value* Arith256::mul(llvm::Value* _arg1, llvm::Value* _arg2)
|
|||
|
||||
llvm::Value* Arith256::div(llvm::Value* _arg1, llvm::Value* _arg2)
|
||||
{
|
||||
|
||||
//return Endianness::toNative(m_builder, binaryOp(m_div, Endianness::toBE(m_builder, _arg1), Endianness::toBE(m_builder, _arg2)));
|
||||
return binaryOp(m_div, _arg1, _arg2);
|
||||
}
|
||||
|
||||
|
|
@ -168,6 +172,29 @@ namespace
|
|||
|
||||
return {lo, (uint64_t)mid, hi};
|
||||
}
|
||||
|
||||
bool isZero(i256 const* _n)
|
||||
{
|
||||
return _n->a == 0 && _n->b == 0 && _n->c == 0 && _n->d == 0;
|
||||
}
|
||||
|
||||
const auto nLimbs = sizeof(i256) / sizeof(mp_limb_t);
|
||||
|
||||
int countLimbs(i256 const* _n)
|
||||
{
|
||||
static const auto limbsInWord = sizeof(_n->a) / sizeof(mp_limb_t);
|
||||
static_assert(limbsInWord == 1, "E?");
|
||||
|
||||
int l = nLimbs;
|
||||
if (_n->d != 0) return l;
|
||||
l -= limbsInWord;
|
||||
if (_n->c != 0) return l;
|
||||
l -= limbsInWord;
|
||||
if (_n->b != 0) return l;
|
||||
l -= limbsInWord;
|
||||
if (_n->a != 0) return l;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -187,16 +214,34 @@ extern "C"
|
|||
|
||||
EXPORT void arith_div(i256* _arg1, i256* _arg2, i256* o_result)
|
||||
{
|
||||
auto arg1 = llvm2eth(*_arg1);
|
||||
auto arg2 = llvm2eth(*_arg2);
|
||||
*o_result = eth2llvm(arg2 == 0 ? arg2 : arg1 / arg2);
|
||||
*o_result = {};
|
||||
if (isZero(_arg2))
|
||||
return;
|
||||
|
||||
mpz_t x{nLimbs, countLimbs(_arg1), reinterpret_cast<mp_limb_t*>(_arg1)};
|
||||
mpz_t y{nLimbs, countLimbs(_arg2), reinterpret_cast<mp_limb_t*>(_arg2)};
|
||||
mpz_t z{nLimbs, 0, reinterpret_cast<mp_limb_t*>(o_result)};
|
||||
|
||||
mpz_tdiv_q(z, x, y);
|
||||
|
||||
// auto arg1 = llvm2eth(*_arg1);
|
||||
// auto arg2 = llvm2eth(*_arg2);
|
||||
// auto res = arg2 == 0 ? arg2 : arg1 / arg2;
|
||||
// std::cout << "DIV " << arg1 << "/" << arg2 << " = " << res << std::endl;
|
||||
// gmp_printf("GMP %Zd / %Zd = %Zd\n", x, y, z);
|
||||
}
|
||||
|
||||
EXPORT void arith_mod(i256* _arg1, i256* _arg2, i256* o_result)
|
||||
{
|
||||
auto arg1 = llvm2eth(*_arg1);
|
||||
auto arg2 = llvm2eth(*_arg2);
|
||||
*o_result = eth2llvm(arg2 == 0 ? arg2 : arg1 % arg2);
|
||||
*o_result = {};
|
||||
if (isZero(_arg2))
|
||||
return;
|
||||
|
||||
mpz_t x{nLimbs, countLimbs(_arg1), reinterpret_cast<mp_limb_t*>(_arg1)};
|
||||
mpz_t y{nLimbs, countLimbs(_arg2), reinterpret_cast<mp_limb_t*>(_arg2)};
|
||||
mpz_t z{nLimbs, 0, reinterpret_cast<mp_limb_t*>(o_result)};
|
||||
|
||||
mpz_tdiv_r(z, x, y);
|
||||
}
|
||||
|
||||
EXPORT void arith_sdiv(i256* _arg1, i256* _arg2, i256* o_result)
|
||||
|
|
|
|||
|
|
@ -18,6 +18,7 @@ include_directories(${LLVM_INCLUDE_DIRS})
|
|||
include_directories(${Boost_INCLUDE_DIRS})
|
||||
|
||||
target_link_libraries(${TARGET_NAME} PRIVATE ${LLVM_LIBS})
|
||||
target_link_libraries(${TARGET_NAME} PRIVATE gmp)
|
||||
|
||||
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue