RandomWOW/src/InterpretedVirtualMachine.cpp

139 lines
3.9 KiB
C++
Raw Normal View History

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/>.
*/
//#define TRACE
//#define FPUCHECK
#include "InterpretedVirtualMachine.hpp"
#include "Pcg32.hpp"
#include "instructions.hpp"
2019-01-14 23:01:11 +00:00
#include "dataset.hpp"
#include "Cache.hpp"
#include "LightClientAsyncWorker.hpp"
2018-12-11 20:00:30 +00:00
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <sstream>
#include <cmath>
2019-01-14 23:01:11 +00:00
#include <thread>
2018-12-28 11:09:37 +00:00
#ifdef STATS
#include <algorithm>
#endif
2018-12-11 20:00:30 +00:00
#ifdef FPUCHECK
constexpr bool fpuCheck = true;
#else
constexpr bool fpuCheck = false;
#endif
namespace RandomX {
2019-01-14 23:01:11 +00:00
InterpretedVirtualMachine::~InterpretedVirtualMachine() {
if (asyncWorker) {
delete mem.ds.asyncWorker;
}
}
void InterpretedVirtualMachine::setDataset(dataset_t ds) {
if (asyncWorker) {
if (softAes) {
mem.ds.asyncWorker = new LightClientAsyncWorker<true>(ds.cache);
}
else {
mem.ds.asyncWorker = new LightClientAsyncWorker<false>(ds.cache);
}
readDataset = &datasetReadLightAsync;
}
else {
mem.ds = ds;
if (softAes) {
readDataset = &datasetReadLight<true>;
}
else {
readDataset = &datasetReadLight<false>;
}
}
}
2019-01-19 23:44:01 +00:00
void InterpretedVirtualMachine::initializeScratchpad(uint8_t* scratchpad, int32_t index) {
2019-01-14 23:01:11 +00:00
uint32_t startingBlock = (ScratchpadSize / CacheLineSize) * index;
if (asyncWorker) {
ILightClientAsyncWorker* worker = mem.ds.asyncWorker;
const uint32_t blocksPerThread = (ScratchpadSize / CacheLineSize) / 2;
worker->prepareBlocks(scratchpad, startingBlock, blocksPerThread); //async first half
worker->getBlocks(scratchpad + ScratchpadLength / 2, startingBlock + blocksPerThread, blocksPerThread); //sync second half
worker->sync();
}
else {
auto cache = mem.ds.cache;
if (softAes) {
for (int i = 0; i < ScratchpadSize / CacheLineSize; ++i) {
initBlock<true>(cache->getCache(), ((uint8_t*)scratchpad) + CacheLineSize * i, (ScratchpadSize / CacheLineSize) * index + i, cache->getKeys());
}
}
else {
for (int i = 0; i < ScratchpadSize / CacheLineSize; ++i) {
initBlock<false>(cache->getCache(), ((uint8_t*)scratchpad) + CacheLineSize * i, (ScratchpadSize / CacheLineSize) * index + i, cache->getKeys());
}
}
}
}
2018-12-11 20:00:30 +00:00
void InterpretedVirtualMachine::initializeProgram(const void* seed) {
Pcg32 gen(seed);
for (unsigned i = 0; i < sizeof(reg) / sizeof(Pcg32::result_type); ++i) {
*(((uint32_t*)&reg) + i) = gen();
}
FPINIT();
for (int i = 0; i < RegistersCount; ++i) {
reg.f[i].lo.f64 = (double)reg.f[i].lo.i64;
reg.f[i].hi.f64 = (double)reg.f[i].hi.i64;
2018-12-11 20:00:30 +00:00
}
//std::cout << reg;
2018-12-11 20:00:30 +00:00
p.initialize(gen);
2019-01-14 23:01:11 +00:00
currentTransform = addressTransformations[gen.getUniform(0, TransformationCount - 1)];
2018-12-11 20:00:30 +00:00
mem.ma = (gen() ^ *(((uint32_t*)seed) + 4)) & ~7;
mem.mx = *(((uint32_t*)seed) + 5);
pc = 0;
ic = InstructionCount;
stack.clear();
}
void InterpretedVirtualMachine::execute() {
while (ic > 0) {
2018-12-28 11:09:37 +00:00
#ifdef STATS
count_instructions[pc]++;
#endif
2018-12-11 20:00:30 +00:00
auto& inst = p(pc);
if(trace) std::cout << inst.getName() << " (" << std::dec << pc << ")" << std::endl;
2018-12-11 20:00:30 +00:00
pc = (pc + 1) % ProgramLength;
auto handler = engine[inst.opcode];
(this->*handler)(inst);
ic--;
}
2018-12-28 11:09:37 +00:00
#ifdef STATS
count_endstack += stack.size();
#endif
2018-12-11 20:00:30 +00:00
}
#include "instructionWeights.hpp"
#define INST_HANDLE(x) REPN(&InterpretedVirtualMachine::h_##x, WT(x))
InstructionHandler InterpretedVirtualMachine::engine[256] = {
2018-12-11 20:00:30 +00:00
};
}