From 50bdaba526206b955a13eab897f25738b4d7b2a1 Mon Sep 17 00:00:00 2001 From: SChernykh Date: Tue, 27 Oct 2020 13:57:41 +0100 Subject: [PATCH] Fixed Debug build in Visual Studio --- src/crypto/randomx/dataset.cpp | 13 +---- src/crypto/randomx/dataset.hpp | 1 - src/crypto/randomx/jit_compiler_a64.cpp | 6 +-- src/crypto/randomx/jit_compiler_a64.hpp | 4 +- src/crypto/randomx/jit_compiler_fallback.hpp | 2 +- src/crypto/randomx/jit_compiler_x86.cpp | 52 +++++++++++--------- src/crypto/randomx/jit_compiler_x86.hpp | 4 +- src/crypto/randomx/superscalar.cpp | 7 +-- src/crypto/randomx/superscalar.hpp | 2 +- src/crypto/randomx/vm_compiled_light.cpp | 2 +- src/crypto/rx/Rx_win.cpp | 2 +- 11 files changed, 44 insertions(+), 51 deletions(-) diff --git a/src/crypto/randomx/dataset.cpp b/src/crypto/randomx/dataset.cpp index 38dc9f4ef..ade6f2b75 100644 --- a/src/crypto/randomx/dataset.cpp +++ b/src/crypto/randomx/dataset.cpp @@ -92,24 +92,15 @@ namespace randomx { argon2_ctx_mem(&context, Argon2_d, cache->memory, RandomX_CurrentConfig.ArgonMemory * 1024); - cache->reciprocalCache.clear(); randomx::Blake2Generator gen(key, keySize); for (uint32_t i = 0; i < RandomX_CurrentConfig.CacheAccesses; ++i) { randomx::generateSuperscalar(cache->programs[i], gen); - for (unsigned j = 0; j < cache->programs[i].getSize(); ++j) { - auto& instr = cache->programs[i](j); - if ((SuperscalarInstructionType)instr.opcode == SuperscalarInstructionType::IMUL_RCP) { - auto rcp = randomx_reciprocal(instr.getImm32()); - instr.setImm32(cache->reciprocalCache.size()); - cache->reciprocalCache.push_back(rcp); - } - } } } void initCacheCompile(randomx_cache* cache, const void* key, size_t keySize) { initCache(cache, key, keySize); - cache->jit->generateSuperscalarHash(cache->programs, cache->reciprocalCache); + cache->jit->generateSuperscalarHash(cache->programs); cache->jit->generateDatasetInitCode(); } @@ -144,7 +135,7 @@ namespace randomx { rx_prefetch_nta(mixBlock); SuperscalarProgram& prog = cache->programs[i]; - executeSuperscalar(rl, prog, &cache->reciprocalCache); + executeSuperscalar(rl, prog); for (unsigned q = 0; q < 8; ++q) rl[q] ^= load64_native(mixBlock + 8 * q); diff --git a/src/crypto/randomx/dataset.hpp b/src/crypto/randomx/dataset.hpp index 34b79d262..c15ab3799 100644 --- a/src/crypto/randomx/dataset.hpp +++ b/src/crypto/randomx/dataset.hpp @@ -47,7 +47,6 @@ struct randomx_cache { randomx::CacheInitializeFunc* initialize; randomx::DatasetInitFunc* datasetInit; randomx::SuperscalarProgram programs[RANDOMX_CACHE_MAX_ACCESSES]; - std::vector reciprocalCache; bool isInitialized() { return programs[0].getSize() != 0; diff --git a/src/crypto/randomx/jit_compiler_a64.cpp b/src/crypto/randomx/jit_compiler_a64.cpp index a3e308c8e..e4072381b 100644 --- a/src/crypto/randomx/jit_compiler_a64.cpp +++ b/src/crypto/randomx/jit_compiler_a64.cpp @@ -230,7 +230,7 @@ void JitCompilerA64::generateProgramLight(Program& program, ProgramConfiguration } template -void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[N], std::vector &reciprocalCache) +void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[N]) { uint32_t codePos = CodeSize; @@ -263,7 +263,7 @@ void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[N], s { const Instruction& instr = prog(j); if (static_cast(instr.opcode) == randomx::SuperscalarInstructionType::IMUL_RCP) - emit64(reciprocalCache[instr.getImm32()], code, codePos); + emit64(randomx_reciprocal(instr.getImm32()), code, codePos); } // Jump over literal pool @@ -345,7 +345,7 @@ void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[N], s clear_code_cache(reinterpret_cast(code + CodeSize), reinterpret_cast(code + codePos)); } -template void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[RANDOMX_CACHE_MAX_ACCESSES], std::vector &reciprocalCache); +template void JitCompilerA64::generateSuperscalarHash(SuperscalarProgram(&programs)[RANDOMX_CACHE_MAX_ACCESSES]); DatasetInitFunc* JitCompilerA64::getDatasetInitFunc() { diff --git a/src/crypto/randomx/jit_compiler_a64.hpp b/src/crypto/randomx/jit_compiler_a64.hpp index cfcb0f85f..29ce478d5 100644 --- a/src/crypto/randomx/jit_compiler_a64.hpp +++ b/src/crypto/randomx/jit_compiler_a64.hpp @@ -38,7 +38,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace randomx { class Program; - class ProgramConfiguration; + struct ProgramConfiguration; class SuperscalarProgram; class Instruction; @@ -54,7 +54,7 @@ namespace randomx { void generateProgramLight(Program&, ProgramConfiguration&, uint32_t); template - void generateSuperscalarHash(SuperscalarProgram(&programs)[N], std::vector &); + void generateSuperscalarHash(SuperscalarProgram(&programs)[N]); void generateDatasetInitCode() {} diff --git a/src/crypto/randomx/jit_compiler_fallback.hpp b/src/crypto/randomx/jit_compiler_fallback.hpp index 1e8a29e4a..7f643b174 100644 --- a/src/crypto/randomx/jit_compiler_fallback.hpp +++ b/src/crypto/randomx/jit_compiler_fallback.hpp @@ -52,7 +52,7 @@ namespace randomx { } template - void generateSuperscalarHash(SuperscalarProgram(&programs)[N], std::vector &) { + void generateSuperscalarHash(SuperscalarProgram(&programs)[N]) { } void generateDatasetInitCode() { diff --git a/src/crypto/randomx/jit_compiler_x86.cpp b/src/crypto/randomx/jit_compiler_x86.cpp index ea41c3b10..dc584d919 100644 --- a/src/crypto/randomx/jit_compiler_x86.cpp +++ b/src/crypto/randomx/jit_compiler_x86.cpp @@ -96,24 +96,30 @@ namespace randomx { */ - #define codePrefetchScratchpad ((uint8_t*)&randomx_prefetch_scratchpad) - #define codePrefetchScratchpadEnd ((uint8_t*)&randomx_prefetch_scratchpad_end) - #define codePrologue ((uint8_t*)&randomx_program_prologue) - #define codeLoopBegin ((uint8_t*)&randomx_program_loop_begin) - #define codeLoopLoad ((uint8_t*)&randomx_program_loop_load) - #define codeLoopLoadXOP ((uint8_t*)&randomx_program_loop_load_xop) - #define codeProgamStart ((uint8_t*)&randomx_program_start) - #define codeReadDatasetLightSshInit ((uint8_t*)&randomx_program_read_dataset_sshash_init) - #define codeReadDatasetLightSshFin ((uint8_t*)&randomx_program_read_dataset_sshash_fin) - #define codeDatasetInit ((uint8_t*)&randomx_dataset_init) - #define codeLoopStore ((uint8_t*)&randomx_program_loop_store) - #define codeLoopEnd ((uint8_t*)&randomx_program_loop_end) - #define codeEpilogue ((uint8_t*)&randomx_program_epilogue) - #define codeProgramEnd ((uint8_t*)&randomx_program_end) - #define codeShhLoad ((uint8_t*)&randomx_sshash_load) - #define codeShhPrefetch ((uint8_t*)&randomx_sshash_prefetch) - #define codeShhEnd ((uint8_t*)&randomx_sshash_end) - #define codeShhInit ((uint8_t*)&randomx_sshash_init) +# if defined(_MSC_VER) && defined(_DEBUG) + #define ADDR(x) ((((uint8_t*)&x)[0] == 0xE9) ? (((uint8_t*)&x) + *(const int32_t*)(((uint8_t*)&x) + 1) + 5) : ((uint8_t*)&x)) +# else + #define ADDR(x) ((uint8_t*)&x) +# endif + + #define codePrefetchScratchpad ADDR(randomx_prefetch_scratchpad) + #define codePrefetchScratchpadEnd ADDR(randomx_prefetch_scratchpad_end) + #define codePrologue ADDR(randomx_program_prologue) + #define codeLoopBegin ADDR(randomx_program_loop_begin) + #define codeLoopLoad ADDR(randomx_program_loop_load) + #define codeLoopLoadXOP ADDR(randomx_program_loop_load_xop) + #define codeProgamStart ADDR(randomx_program_start) + #define codeReadDatasetLightSshInit ADDR(randomx_program_read_dataset_sshash_init) + #define codeReadDatasetLightSshFin ADDR(randomx_program_read_dataset_sshash_fin) + #define codeDatasetInit ADDR(randomx_dataset_init) + #define codeLoopStore ADDR(randomx_program_loop_store) + #define codeLoopEnd ADDR(randomx_program_loop_end) + #define codeEpilogue ADDR(randomx_program_epilogue) + #define codeProgramEnd ADDR(randomx_program_end) + #define codeShhLoad ADDR(randomx_sshash_load) + #define codeShhPrefetch ADDR(randomx_sshash_prefetch) + #define codeShhEnd ADDR(randomx_sshash_end) + #define codeShhInit ADDR(randomx_sshash_init) #define prefetchScratchpadSize (codePrefetchScratchpadEnd - codePrefetchScratchpad) #define prologueSize (codeLoopBegin - codePrologue) @@ -264,14 +270,14 @@ namespace randomx { } template - void JitCompilerX86::generateSuperscalarHash(SuperscalarProgram(&programs)[N], std::vector &reciprocalCache) { + void JitCompilerX86::generateSuperscalarHash(SuperscalarProgram(&programs)[N]) { memcpy(code + superScalarHashOffset, codeShhInit, codeSshInitSize); codePos = superScalarHashOffset + codeSshInitSize; for (unsigned j = 0; j < RandomX_CurrentConfig.CacheAccesses; ++j) { SuperscalarProgram& prog = programs[j]; for (unsigned i = 0; i < prog.getSize(); ++i) { Instruction& instr = prog(i); - generateSuperscalarCode(instr, reciprocalCache); + generateSuperscalarCode(instr); } emit(codeShhLoad, codeSshLoadSize, code, codePos); if (j < RandomX_CurrentConfig.CacheAccesses - 1) { @@ -284,7 +290,7 @@ namespace randomx { } template - void JitCompilerX86::generateSuperscalarHash(SuperscalarProgram(&programs)[RANDOMX_CACHE_MAX_ACCESSES], std::vector &reciprocalCache); + void JitCompilerX86::generateSuperscalarHash(SuperscalarProgram(&programs)[RANDOMX_CACHE_MAX_ACCESSES]); void JitCompilerX86::generateDatasetInitCode() { memcpy(code, codeDatasetInit, datasetInitSize); @@ -366,7 +372,7 @@ namespace randomx { emit32(epilogueOffset - codePos - 4, code, codePos); } - void JitCompilerX86::generateSuperscalarCode(Instruction& instr, std::vector &reciprocalCache) { + void JitCompilerX86::generateSuperscalarCode(Instruction& instr) { static constexpr uint8_t REX_SUB_RR[] = { 0x4d, 0x2b }; static constexpr uint8_t REX_MOV_RR64[] = { 0x49, 0x8b }; static constexpr uint8_t REX_MOV_R64R[] = { 0x4c, 0x8b }; @@ -452,7 +458,7 @@ namespace randomx { break; case randomx::SuperscalarInstructionType::IMUL_RCP: emit(MOV_RAX_I, code, codePos); - emit64(reciprocalCache[instr.getImm32()], code, codePos); + emit64(randomx_reciprocal_fast(instr.getImm32()), code, codePos); emit(REX_IMUL_RM, code, codePos); emitByte(0xc0 + 8 * instr.dst, code, codePos); break; diff --git a/src/crypto/randomx/jit_compiler_x86.hpp b/src/crypto/randomx/jit_compiler_x86.hpp index e65838317..0a9148f97 100644 --- a/src/crypto/randomx/jit_compiler_x86.hpp +++ b/src/crypto/randomx/jit_compiler_x86.hpp @@ -53,7 +53,7 @@ namespace randomx { void generateProgram(Program&, ProgramConfiguration&, uint32_t); void generateProgramLight(Program&, ProgramConfiguration&, uint32_t); template - void generateSuperscalarHash(SuperscalarProgram (&programs)[N], std::vector &); + void generateSuperscalarHash(SuperscalarProgram (&programs)[N]); void generateDatasetInitCode(); ProgramFunc* getProgramFunc() { return (ProgramFunc*)code; @@ -92,7 +92,7 @@ namespace randomx { static void genAddressImm(const Instruction&, uint8_t* code, uint32_t& codePos); static void genSIB(int scale, int index, int base, uint8_t* code, uint32_t& codePos); - void generateSuperscalarCode(Instruction &, std::vector &); + void generateSuperscalarCode(Instruction &); static void emitByte(uint8_t val, uint8_t* code, uint32_t& codePos) { code[codePos] = val; diff --git a/src/crypto/randomx/superscalar.cpp b/src/crypto/randomx/superscalar.cpp index 7586c0282..2eca929fc 100644 --- a/src/crypto/randomx/superscalar.cpp +++ b/src/crypto/randomx/superscalar.cpp @@ -847,7 +847,7 @@ namespace randomx { }*/ } - void executeSuperscalar(int_reg_t(&r)[8], SuperscalarProgram& prog, std::vector *reciprocals) { + void executeSuperscalar(int_reg_t(&r)[8], SuperscalarProgram& prog) { for (unsigned j = 0; j < prog.getSize(); ++j) { Instruction& instr = prog(j); switch ((SuperscalarInstructionType)instr.opcode) @@ -884,10 +884,7 @@ namespace randomx { r[instr.dst] = smulh(r[instr.dst], r[instr.src]); break; case SuperscalarInstructionType::IMUL_RCP: - if (reciprocals != nullptr) - r[instr.dst] *= (*reciprocals)[instr.getImm32()]; - else - r[instr.dst] *= randomx_reciprocal(instr.getImm32()); + r[instr.dst] *= randomx_reciprocal(instr.getImm32()); break; default: UNREACHABLE; diff --git a/src/crypto/randomx/superscalar.hpp b/src/crypto/randomx/superscalar.hpp index fd2a593b4..30794e48e 100644 --- a/src/crypto/randomx/superscalar.hpp +++ b/src/crypto/randomx/superscalar.hpp @@ -56,5 +56,5 @@ namespace randomx { }; void generateSuperscalar(SuperscalarProgram& prog, Blake2Generator& gen); - void executeSuperscalar(uint64_t(&r)[8], SuperscalarProgram& prog, std::vector *reciprocals = nullptr); + void executeSuperscalar(uint64_t(&r)[8], SuperscalarProgram& prog); } diff --git a/src/crypto/randomx/vm_compiled_light.cpp b/src/crypto/randomx/vm_compiled_light.cpp index d4f6fe50b..12e8de0f9 100644 --- a/src/crypto/randomx/vm_compiled_light.cpp +++ b/src/crypto/randomx/vm_compiled_light.cpp @@ -36,7 +36,7 @@ namespace randomx { void CompiledLightVm::setCache(randomx_cache* cache) { cachePtr = cache; mem.memory = cache->memory; - compiler.generateSuperscalarHash(cache->programs, cache->reciprocalCache); + compiler.generateSuperscalarHash(cache->programs); } template diff --git a/src/crypto/rx/Rx_win.cpp b/src/crypto/rx/Rx_win.cpp index 5131c1b5f..91e34cff1 100644 --- a/src/crypto/rx/Rx_win.cpp +++ b/src/crypto/rx/Rx_win.cpp @@ -117,7 +117,7 @@ static HANDLE wrmsr_install_driver() return nullptr; } - for (auto it = dir.end(); it != dir.begin(); --it) { + for (auto it = dir.end() - 1; it != dir.begin(); --it) { if ((*it == L'\\') || (*it == L'/')) { ++it; *it = L'\0';