From 131085be803138c30884f3785fad7d2f2b49da14 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Fri, 21 Feb 2020 19:00:58 +0100 Subject: [PATCH] Optimized CFROUND Shorter version using BMI2 instructionns --- src/crypto/randomx/jit_compiler_x86.cpp | 23 +++++++++++++++++++++++ src/crypto/randomx/jit_compiler_x86.hpp | 1 + src/crypto/randomx/randomx.cpp | 12 +++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/crypto/randomx/jit_compiler_x86.cpp b/src/crypto/randomx/jit_compiler_x86.cpp index 1307c9a59..83bb87ad3 100644 --- a/src/crypto/randomx/jit_compiler_x86.cpp +++ b/src/crypto/randomx/jit_compiler_x86.cpp @@ -1118,6 +1118,29 @@ namespace randomx { codePos = pos; } + void JitCompilerX86::h_CFROUND_BMI2(const Instruction& instr) { + uint8_t* const p = code; + int pos = codePos; + + const uint64_t src = instr.src; + + const uint64_t rotate = (static_cast(instr.getImm32() & 63) - 2) & 63; + *(uint64_t*)(p + pos) = 0xC0F0FBC3C4ULL | (src << 32) | (rotate << 40); + + if (vm_flags & RANDOMX_FLAG_AMD) { + *(uint64_t*)(p + pos + 6) = 0x742024443B0CE083ULL; + *(uint8_t*)(p + pos + 14) = 8; + *(uint64_t*)(p + pos + 15) = 0x202444890414AE0FULL; + pos += 23; + } + else { + *(uint64_t*)(p + pos + 6) = 0x0414AE0F0CE083ULL; + pos += 13; + } + + codePos = pos; + } + void JitCompilerX86::h_CBRANCH(const Instruction& instr) { uint8_t* const p = code; int pos = codePos; diff --git a/src/crypto/randomx/jit_compiler_x86.hpp b/src/crypto/randomx/jit_compiler_x86.hpp index c1a05c873..c37890ad5 100644 --- a/src/crypto/randomx/jit_compiler_x86.hpp +++ b/src/crypto/randomx/jit_compiler_x86.hpp @@ -147,6 +147,7 @@ namespace randomx { void h_FSQRT_R(const Instruction&); void h_CBRANCH(const Instruction&); void h_CFROUND(const Instruction&); + void h_CFROUND_BMI2(const Instruction&); void h_ISTORE(const Instruction&); void h_NOP(const Instruction&); }; diff --git a/src/crypto/randomx/randomx.cpp b/src/crypto/randomx/randomx.cpp index 9fcaec925..b283a6c23 100644 --- a/src/crypto/randomx/randomx.cpp +++ b/src/crypto/randomx/randomx.cpp @@ -279,7 +279,17 @@ void RandomX_ConfigurationBase::Apply() INST_HANDLE(FDIV_M, FMUL_R); INST_HANDLE(FSQRT_R, FDIV_M); INST_HANDLE(CBRANCH, FSQRT_R); - INST_HANDLE(CFROUND, CBRANCH); + +#if defined(_M_X64) || defined(__x86_64__) + if (xmrig::Cpu::info()->hasBMI2()) { + INST_HANDLE2(CFROUND, CFROUND_BMI2, CBRANCH); + } + else +#endif + { + INST_HANDLE(CFROUND, CBRANCH); + } + INST_HANDLE(ISTORE, CFROUND); INST_HANDLE(NOP, ISTORE); #undef INST_HANDLE