2018-12-13 23:11:55 +01: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "CompiledVirtualMachine.hpp"
|
|
|
|
#include "Pcg32.hpp"
|
|
|
|
#include "common.hpp"
|
|
|
|
#include "instructions.hpp"
|
2018-12-18 22:00:58 +01:00
|
|
|
#include <stdexcept>
|
2018-12-13 23:11:55 +01:00
|
|
|
|
|
|
|
namespace RandomX {
|
|
|
|
|
2019-01-24 19:29:59 +01:00
|
|
|
constexpr int mantissaSize = 52;
|
|
|
|
constexpr int exponentSize = 11;
|
|
|
|
constexpr uint64_t mantissaMask = (1ULL << mantissaSize) - 1;
|
|
|
|
constexpr uint64_t exponentMask = (1ULL << exponentSize) - 1;
|
|
|
|
constexpr int exponentBias = 1023;
|
|
|
|
|
2019-01-15 00:01:11 +01:00
|
|
|
CompiledVirtualMachine::CompiledVirtualMachine() {
|
2019-01-12 16:05:09 +01:00
|
|
|
totalSize = 0;
|
2018-12-18 22:00:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-15 00:01:11 +01:00
|
|
|
void CompiledVirtualMachine::setDataset(dataset_t ds) {
|
|
|
|
mem.ds = ds;
|
|
|
|
}
|
|
|
|
|
2019-01-20 00:44:01 +01:00
|
|
|
void CompiledVirtualMachine::initializeScratchpad(uint8_t* scratchpad, int32_t index) {
|
2019-01-15 00:01:11 +01:00
|
|
|
memcpy(scratchpad, mem.ds.dataset + ScratchpadSize * index, ScratchpadSize);
|
2018-12-18 22:00:58 +01:00
|
|
|
}
|
|
|
|
|
2019-01-24 19:29:59 +01:00
|
|
|
static uint64_t getSmallPositiveFloatBits(uint64_t entropy) {
|
|
|
|
auto exponent = entropy >> 60; //0..15
|
|
|
|
auto mantissa = entropy & mantissaMask;
|
|
|
|
exponent += exponentBias;
|
|
|
|
exponent &= exponentMask;
|
|
|
|
exponent <<= mantissaSize;
|
|
|
|
return exponent | mantissa;
|
|
|
|
}
|
|
|
|
|
2018-12-13 23:11:55 +01:00
|
|
|
void CompiledVirtualMachine::initializeProgram(const void* seed) {
|
|
|
|
Pcg32 gen(seed);
|
|
|
|
for (unsigned i = 0; i < sizeof(reg) / sizeof(Pcg32::result_type); ++i) {
|
|
|
|
*(((uint32_t*)®) + i) = gen();
|
|
|
|
}
|
2019-01-20 00:44:01 +01:00
|
|
|
FPINIT();
|
2019-01-24 19:29:59 +01:00
|
|
|
/*for (int i = 0; i < RegistersCount / 2; ++i) {
|
2019-01-20 00:44:01 +01:00
|
|
|
reg.f[i].lo.f64 = (double)reg.f[i].lo.i64;
|
|
|
|
reg.f[i].hi.f64 = (double)reg.f[i].hi.i64;
|
|
|
|
}
|
2019-01-24 19:29:59 +01:00
|
|
|
for (int i = 0; i < RegistersCount / 2; ++i) {
|
|
|
|
reg.g[i].lo.f64 = std::abs((double)reg.g[i].lo.i64);
|
|
|
|
reg.g[i].hi.f64 = std::abs((double)reg.g[i].hi.i64);
|
|
|
|
}*/
|
|
|
|
for (int i = 0; i < RegistersCount / 2; ++i) {
|
|
|
|
reg.a[i].lo.u64 = getSmallPositiveFloatBits(reg.f[i].lo.u64);
|
|
|
|
reg.a[i].hi.u64 = getSmallPositiveFloatBits(reg.f[i].hi.u64);
|
|
|
|
}
|
2018-12-18 22:00:58 +01:00
|
|
|
compiler.generateProgram(gen);
|
2018-12-13 23:11:55 +01:00
|
|
|
mem.ma = (gen() ^ *(((uint32_t*)seed) + 4)) & ~7;
|
|
|
|
mem.mx = *(((uint32_t*)seed) + 5);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CompiledVirtualMachine::execute() {
|
2019-01-24 19:29:59 +01:00
|
|
|
executeProgram(reg, mem, scratchpad, InstructionCount);
|
2019-01-12 16:05:09 +01:00
|
|
|
totalSize += compiler.getCodeSize();
|
2019-01-24 19:29:59 +01:00
|
|
|
//compiler.getProgramFunc()(reg, mem, scratchpad);
|
2018-12-31 19:06:45 +01:00
|
|
|
#ifdef TRACEVM
|
2018-12-15 23:13:17 +01:00
|
|
|
for (int32_t i = InstructionCount - 1; i >= 0; --i) {
|
|
|
|
std::cout << std::hex << tracepad[i].u64 << std::endl;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-13 23:11:55 +01:00
|
|
|
}
|
|
|
|
}
|