From 5926dee35471a910e218f8a021d77e69514aa18e Mon Sep 17 00:00:00 2001 From: SChernykh Date: Wed, 12 Aug 2020 16:45:16 +0200 Subject: [PATCH] RandomX JIT: optimized address mask calculation --- src/crypto/randomx/bytecode_machine.cpp | 20 ++++++++++---------- src/crypto/randomx/common.hpp | 5 +---- src/crypto/randomx/jit_compiler_x86.cpp | 4 ++-- src/crypto/randomx/randomx.cpp | 12 ++++++++---- src/crypto/randomx/randomx.h | 5 +---- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/crypto/randomx/bytecode_machine.cpp b/src/crypto/randomx/bytecode_machine.cpp index 55a639358..f0b95c30e 100644 --- a/src/crypto/randomx/bytecode_machine.cpp +++ b/src/crypto/randomx/bytecode_machine.cpp @@ -108,7 +108,7 @@ namespace randomx { ibc.imm = signExtend2sCompl(instr.getImm32()); if (src != dst) { ibc.isrc = &nreg->r[src]; - ibc.memMask = (instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask); + ibc.memMask = AddressMask[instr.getModMem()]; } else { ibc.isrc = &zero; @@ -142,7 +142,7 @@ namespace randomx { ibc.imm = signExtend2sCompl(instr.getImm32()); if (src != dst) { ibc.isrc = &nreg->r[src]; - ibc.memMask = (instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask); + ibc.memMask = AddressMask[instr.getModMem()]; } else { ibc.isrc = &zero; @@ -176,7 +176,7 @@ namespace randomx { ibc.imm = signExtend2sCompl(instr.getImm32()); if (src != dst) { ibc.isrc = &nreg->r[src]; - ibc.memMask = (instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask); + ibc.memMask = AddressMask[instr.getModMem()]; } else { ibc.isrc = &zero; @@ -204,7 +204,7 @@ namespace randomx { ibc.imm = signExtend2sCompl(instr.getImm32()); if (src != dst) { ibc.isrc = &nreg->r[src]; - ibc.memMask = (instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask); + ibc.memMask = AddressMask[instr.getModMem()]; } else { ibc.isrc = &zero; @@ -232,7 +232,7 @@ namespace randomx { ibc.imm = signExtend2sCompl(instr.getImm32()); if (src != dst) { ibc.isrc = &nreg->r[src]; - ibc.memMask = (instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask); + ibc.memMask = AddressMask[instr.getModMem()]; } else { ibc.isrc = &zero; @@ -290,7 +290,7 @@ namespace randomx { ibc.imm = signExtend2sCompl(instr.getImm32()); if (src != dst) { ibc.isrc = &nreg->r[src]; - ibc.memMask = (instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask); + ibc.memMask = AddressMask[instr.getModMem()]; } else { ibc.isrc = &zero; @@ -373,7 +373,7 @@ namespace randomx { ibc.type = InstructionType::FADD_M; ibc.fdst = &nreg->f[dst]; ibc.isrc = &nreg->r[src]; - ibc.memMask = (instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask); + ibc.memMask = AddressMask[instr.getModMem()]; ibc.imm = signExtend2sCompl(instr.getImm32()); return; } @@ -393,7 +393,7 @@ namespace randomx { ibc.type = InstructionType::FSUB_M; ibc.fdst = &nreg->f[dst]; ibc.isrc = &nreg->r[src]; - ibc.memMask = (instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask); + ibc.memMask = AddressMask[instr.getModMem()]; ibc.imm = signExtend2sCompl(instr.getImm32()); return; } @@ -420,7 +420,7 @@ namespace randomx { ibc.type = InstructionType::FDIV_M; ibc.fdst = &nreg->e[dst]; ibc.isrc = &nreg->r[src]; - ibc.memMask = (instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask); + ibc.memMask = AddressMask[instr.getModMem()]; ibc.imm = signExtend2sCompl(instr.getImm32()); return; } @@ -466,7 +466,7 @@ namespace randomx { ibc.isrc = &nreg->r[src]; ibc.imm = signExtend2sCompl(instr.getImm32()); if (instr.getModCond() < StoreL3Condition) - ibc.memMask = (instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask); + ibc.memMask = AddressMask[instr.getModMem()]; else ibc.memMask = ScratchpadL3Mask; return; diff --git a/src/crypto/randomx/common.hpp b/src/crypto/randomx/common.hpp index cea5f5be6..34c8477c3 100644 --- a/src/crypto/randomx/common.hpp +++ b/src/crypto/randomx/common.hpp @@ -126,10 +126,7 @@ namespace randomx { double hi; }; - #define ScratchpadL1Mask RandomX_CurrentConfig.ScratchpadL1Mask_Calculated - #define ScratchpadL1Mask16 RandomX_CurrentConfig.ScratchpadL1Mask16_Calculated - #define ScratchpadL2Mask RandomX_CurrentConfig.ScratchpadL2Mask_Calculated - #define ScratchpadL2Mask16 RandomX_CurrentConfig.ScratchpadL2Mask16_Calculated + #define AddressMask RandomX_CurrentConfig.AddressMask_Calculated #define ScratchpadL3Mask RandomX_CurrentConfig.ScratchpadL3Mask_Calculated #define ScratchpadL3Mask64 RandomX_CurrentConfig.ScratchpadL3Mask64_Calculated constexpr int RegistersCount = 8; diff --git a/src/crypto/randomx/jit_compiler_x86.cpp b/src/crypto/randomx/jit_compiler_x86.cpp index 178d35a5c..7aae54fd4 100644 --- a/src/crypto/randomx/jit_compiler_x86.cpp +++ b/src/crypto/randomx/jit_compiler_x86.cpp @@ -508,7 +508,7 @@ namespace randomx { *(uint32_t*)(code + codePos) = 0xe181; codePos += 2; } - emit32(instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask, code, codePos); + emit32(AddressMask[instr.getModMem()], code, codePos); } template void JitCompilerX86::genAddressReg(const Instruction& instr, const uint32_t src, uint8_t* code, uint32_t& codePos); @@ -522,7 +522,7 @@ namespace randomx { emit32(instr.getImm32(), code, codePos); emitByte(0x25, code, codePos); if (instr.getModCond() < StoreL3Condition) { - emit32(instr.getModMem() ? ScratchpadL1Mask : ScratchpadL2Mask, code, codePos); + emit32(AddressMask[instr.getModMem()], code, codePos); } else { emit32(ScratchpadL3Mask, code, codePos); diff --git a/src/crypto/randomx/randomx.cpp b/src/crypto/randomx/randomx.cpp index 7bb7df7fb..765c7fb27 100644 --- a/src/crypto/randomx/randomx.cpp +++ b/src/crypto/randomx/randomx.cpp @@ -211,10 +211,14 @@ static uint32_t Log2(size_t value) { return (value > 1) ? (Log2(value / 2) + 1) void RandomX_ConfigurationBase::Apply() { - ScratchpadL1Mask_Calculated = (ScratchpadL1_Size / sizeof(uint64_t) - 1) * 8; - ScratchpadL1Mask16_Calculated = (ScratchpadL1_Size / sizeof(uint64_t) / 2 - 1) * 16; - ScratchpadL2Mask_Calculated = (ScratchpadL2_Size / sizeof(uint64_t) - 1) * 8; - ScratchpadL2Mask16_Calculated = (ScratchpadL2_Size / sizeof(uint64_t) / 2 - 1) * 16; + const uint32_t ScratchpadL1Mask_Calculated = (ScratchpadL1_Size / sizeof(uint64_t) - 1) * 8; + const uint32_t ScratchpadL2Mask_Calculated = (ScratchpadL2_Size / sizeof(uint64_t) - 1) * 8; + + AddressMask_Calculated[0] = ScratchpadL2Mask_Calculated; + AddressMask_Calculated[1] = ScratchpadL1Mask_Calculated; + AddressMask_Calculated[2] = ScratchpadL1Mask_Calculated; + AddressMask_Calculated[3] = ScratchpadL1Mask_Calculated; + ScratchpadL3Mask_Calculated = (((ScratchpadL3_Size / sizeof(uint64_t)) - 1) * 8); ScratchpadL3Mask64_Calculated = ((ScratchpadL3_Size / sizeof(uint64_t)) / 8 - 1) * 64; diff --git a/src/crypto/randomx/randomx.h b/src/crypto/randomx/randomx.h index 96b67cd1c..ce3f6ebb3 100644 --- a/src/crypto/randomx/randomx.h +++ b/src/crypto/randomx/randomx.h @@ -129,10 +129,7 @@ struct RandomX_ConfigurationBase uint32_t CacheLineAlignMask_Calculated; uint32_t DatasetExtraItems_Calculated; - uint32_t ScratchpadL1Mask_Calculated; - uint32_t ScratchpadL1Mask16_Calculated; - uint32_t ScratchpadL2Mask_Calculated; - uint32_t ScratchpadL2Mask16_Calculated; + uint32_t AddressMask_Calculated[4]; uint32_t ScratchpadL3Mask_Calculated; uint32_t ScratchpadL3Mask64_Calculated;