2018-12-11 20:00:30 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2018 tevador
|
|
|
|
|
|
|
|
This file is part of RandomX.
|
|
|
|
|
|
|
|
RandomX is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
RandomX is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with RandomX. If not, see<http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
2018-12-31 18:06:45 +00:00
|
|
|
//#define STATS
|
2019-02-18 07:54:55 +00:00
|
|
|
#include <new>
|
2018-12-11 20:00:30 +00:00
|
|
|
#include "VirtualMachine.hpp"
|
|
|
|
#include "Program.hpp"
|
2019-02-09 14:45:26 +00:00
|
|
|
#include "intrinPortable.h"
|
2019-04-10 22:01:22 +00:00
|
|
|
#include <vector>
|
2018-12-11 20:00:30 +00:00
|
|
|
|
|
|
|
namespace RandomX {
|
|
|
|
|
2019-02-04 16:07:00 +00:00
|
|
|
struct InstructionByteCode;
|
2019-04-10 22:01:22 +00:00
|
|
|
template<bool superscalar> class InterpretedVirtualMachine;
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2019-04-10 22:01:22 +00:00
|
|
|
template<bool superscalar>
|
|
|
|
using InstructionHandler = void(InterpretedVirtualMachine<superscalar>::*)(Instruction&);
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2019-04-10 22:01:22 +00:00
|
|
|
struct InstructionByteCode {
|
2019-02-15 10:14:40 +00:00
|
|
|
union {
|
|
|
|
int_reg_t* idst;
|
|
|
|
__m128d* fdst;
|
|
|
|
};
|
|
|
|
union {
|
|
|
|
int_reg_t* isrc;
|
|
|
|
__m128d* fsrc;
|
|
|
|
};
|
2019-02-15 09:41:02 +00:00
|
|
|
union {
|
|
|
|
uint64_t imm;
|
|
|
|
int64_t simm;
|
|
|
|
};
|
2019-03-20 22:38:37 +00:00
|
|
|
int_reg_t* creg;
|
|
|
|
uint16_t condition;
|
|
|
|
int16_t target;
|
2019-02-04 16:07:00 +00:00
|
|
|
uint32_t memMask;
|
2019-03-20 22:38:37 +00:00
|
|
|
uint16_t type;
|
|
|
|
uint16_t shift;
|
2019-02-04 16:07:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
constexpr int asedwfagdewsa = sizeof(InstructionByteCode);
|
|
|
|
|
2019-04-10 22:01:22 +00:00
|
|
|
template<bool superscalar>
|
2018-12-11 20:00:30 +00:00
|
|
|
class InterpretedVirtualMachine : public VirtualMachine {
|
|
|
|
public:
|
2019-02-18 07:54:55 +00:00
|
|
|
void* operator new(size_t size) {
|
|
|
|
void* ptr = _mm_malloc(size, 64);
|
|
|
|
if (ptr == nullptr)
|
|
|
|
throw std::bad_alloc();
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
void operator delete(void* ptr) {
|
|
|
|
_mm_free(ptr);
|
|
|
|
}
|
2019-03-20 22:38:37 +00:00
|
|
|
InterpretedVirtualMachine(bool soft) : softAes(soft) {}
|
2019-04-10 22:01:22 +00:00
|
|
|
~InterpretedVirtualMachine() {}
|
2019-04-12 17:36:08 +00:00
|
|
|
void setDataset(dataset_t ds, uint64_t size, SuperscalarProgram(&programs)[RANDOMX_CACHE_ACCESSES]) override;
|
2019-02-09 14:45:26 +00:00
|
|
|
void initialize() override;
|
2019-01-14 23:01:11 +00:00
|
|
|
void execute() override;
|
2019-04-12 17:36:08 +00:00
|
|
|
static void executeSuperscalar(int_reg_t(&r)[8], SuperscalarProgram& prog, std::vector<uint64_t>& reciprocals);
|
2018-12-11 20:00:30 +00:00
|
|
|
private:
|
2019-04-10 22:01:22 +00:00
|
|
|
static InstructionHandler<superscalar> engine[256];
|
2019-02-09 14:45:26 +00:00
|
|
|
DatasetReadFunc readDataset;
|
2019-03-20 22:38:37 +00:00
|
|
|
bool softAes;
|
2019-03-08 14:34:34 +00:00
|
|
|
InstructionByteCode byteCode[RANDOMX_PROGRAM_SIZE];
|
2019-04-10 22:01:22 +00:00
|
|
|
std::vector<uint64_t> reciprocals;
|
2019-04-12 17:36:08 +00:00
|
|
|
alignas(64) SuperscalarProgram superScalarPrograms[RANDOMX_CACHE_ACCESSES];
|
2018-12-20 21:42:47 +00:00
|
|
|
#ifdef STATS
|
2018-12-28 11:09:37 +00:00
|
|
|
int count_ADD_64 = 0;
|
|
|
|
int count_ADD_32 = 0;
|
|
|
|
int count_SUB_64 = 0;
|
|
|
|
int count_SUB_32 = 0;
|
|
|
|
int count_MUL_64 = 0;
|
|
|
|
int count_MULH_64 = 0;
|
|
|
|
int count_MUL_32 = 0;
|
|
|
|
int count_IMUL_32 = 0;
|
|
|
|
int count_IMULH_64 = 0;
|
|
|
|
int count_DIV_64 = 0;
|
|
|
|
int count_IDIV_64 = 0;
|
|
|
|
int count_AND_64 = 0;
|
|
|
|
int count_AND_32 = 0;
|
|
|
|
int count_OR_64 = 0;
|
|
|
|
int count_OR_32 = 0;
|
|
|
|
int count_XOR_64 = 0;
|
|
|
|
int count_XOR_32 = 0;
|
|
|
|
int count_SHL_64 = 0;
|
|
|
|
int count_SHR_64 = 0;
|
|
|
|
int count_SAR_64 = 0;
|
|
|
|
int count_ROL_64 = 0;
|
|
|
|
int count_ROR_64 = 0;
|
2019-02-05 22:43:57 +00:00
|
|
|
int count_FADD = 0;
|
|
|
|
int count_FSUB = 0;
|
|
|
|
int count_FMUL = 0;
|
|
|
|
int count_FDIV = 0;
|
|
|
|
int count_FSQRT = 0;
|
2018-12-28 11:09:37 +00:00
|
|
|
int count_FPROUND = 0;
|
2019-01-14 23:01:11 +00:00
|
|
|
int count_JUMP_taken = 0;
|
|
|
|
int count_JUMP_not_taken = 0;
|
2018-12-21 21:41:35 +00:00
|
|
|
int count_jump_taken[8] = { 0 };
|
|
|
|
int count_jump_not_taken[8] = { 0 };
|
2018-12-28 11:09:37 +00:00
|
|
|
int count_max_stack = 0;
|
|
|
|
int count_retdepth = 0;
|
|
|
|
int count_retdepth_max = 0;
|
|
|
|
int count_endstack = 0;
|
2019-03-21 08:17:28 +00:00
|
|
|
int count_instructions[RANDOMX_PROGRAM_SIZE] = { 0 };
|
2019-02-05 22:43:57 +00:00
|
|
|
int count_FADD_nop = 0;
|
|
|
|
int count_FADD_nop2 = 0;
|
|
|
|
int count_FSUB_nop = 0;
|
|
|
|
int count_FSUB_nop2 = 0;
|
|
|
|
int count_FMUL_nop = 0;
|
|
|
|
int count_FMUL_nop2 = 0;
|
2019-01-14 23:01:11 +00:00
|
|
|
int datasetAccess[256] = { 0 };
|
2018-12-20 21:42:47 +00:00
|
|
|
#endif
|
2019-02-09 14:45:26 +00:00
|
|
|
void precompileProgram(int_reg_t(&r)[8], __m128d (&f)[4], __m128d (&e)[4], __m128d (&a)[4]);
|
2019-04-12 17:36:08 +00:00
|
|
|
void precompileSuperscalar(SuperscalarProgram*);
|
2019-02-09 14:45:26 +00:00
|
|
|
void executeBytecode(int_reg_t(&r)[8], __m128d (&f)[4], __m128d (&e)[4], __m128d (&a)[4]);
|
2019-03-20 22:38:37 +00:00
|
|
|
void executeBytecode(int& i, int_reg_t(&r)[8], __m128d (&f)[4], __m128d (&e)[4], __m128d (&a)[4]);
|
2019-04-10 22:01:22 +00:00
|
|
|
void executeSuperscalar(uint32_t blockNumber, int_reg_t(&r)[8]);
|
2019-04-16 16:58:44 +00:00
|
|
|
void* getScratchpadAddress(InstructionByteCode& ibc);
|
2019-04-17 14:18:02 +00:00
|
|
|
__m128d maskRegisterExponentMantissa(__m128d);
|
2018-12-11 20:00:30 +00:00
|
|
|
};
|
|
|
|
}
|