mirror of
https://github.com/wownero/RandomX.git
synced 2026-10-17 23:41:39 +02:00
Interpreter with bytecode
Fixed some undefined behavior with signed types Fixed different results on big endian systems Removed unused code files Restored FNEG_R instructions Updated documentation
This commit is contained in:
+54
-14
@@ -23,32 +23,72 @@ along with RandomX. If not, see<http://www.gnu.org/licenses/>.
|
||||
#include "blake2/blake2.h"
|
||||
#include <cstring>
|
||||
#include <iomanip>
|
||||
#include "intrinPortable.h"
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const RandomX::RegisterFile& rf) {
|
||||
for (int i = 0; i < RandomX::RegistersCount; ++i)
|
||||
os << std::hex << "r" << i << " = " << rf.r[i].u64 << std::endl << std::dec;
|
||||
os << std::hex << "r" << i << " = " << rf.r[i] << std::endl << std::dec;
|
||||
for (int i = 0; i < RandomX::RegistersCount; ++i)
|
||||
os << std::hex << "f" << i << " = " << rf.f[i].hi.u64 << " (" << rf.f[i].hi.f64 << ")" << std::endl
|
||||
<< " = " << rf.f[i].lo.u64 << " (" << rf.f[i].lo.f64 << ")" << std::endl << std::dec;
|
||||
os << std::hex << "f" << i << " = " << *(uint64_t*)&rf.f[i].hi << " (" << rf.f[i].hi << ")" << std::endl
|
||||
<< " = " << *(uint64_t*)&rf.f[i].lo << " (" << rf.f[i].lo << ")" << std::endl << std::dec;
|
||||
return os;
|
||||
}
|
||||
|
||||
namespace RandomX {
|
||||
|
||||
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;
|
||||
|
||||
static inline uint64_t getSmallPositiveFloatBits(uint64_t entropy) {
|
||||
auto exponent = entropy >> 59; //0..31
|
||||
auto mantissa = entropy & mantissaMask;
|
||||
exponent += exponentBias;
|
||||
exponent &= exponentMask;
|
||||
exponent <<= mantissaSize;
|
||||
return exponent | mantissa;
|
||||
}
|
||||
|
||||
VirtualMachine::VirtualMachine() {
|
||||
mem.ds.dataset = nullptr;
|
||||
}
|
||||
|
||||
void VirtualMachine::getResult(void* scratchpad, size_t scratchpadSize, void* out) {
|
||||
constexpr size_t smallStateLength = sizeof(RegisterFile) / sizeof(uint64_t) + 8;
|
||||
alignas(16) uint64_t smallState[smallStateLength];
|
||||
memcpy(smallState, ®, sizeof(RegisterFile));
|
||||
if (scratchpadSize > 0) {
|
||||
hashAes1Rx4<false>(scratchpad, scratchpadSize, smallState + 24);
|
||||
}
|
||||
else {
|
||||
memset(smallState + 24, 0, 64);
|
||||
}
|
||||
blake2b(out, ResultSize, smallState, sizeof(smallState), nullptr, 0);
|
||||
void VirtualMachine::resetRoundingMode() {
|
||||
initFpu();
|
||||
}
|
||||
|
||||
void VirtualMachine::initialize() {
|
||||
store64(®.a[0].lo, getSmallPositiveFloatBits(program.getEntropy(0)));
|
||||
store64(®.a[0].hi, getSmallPositiveFloatBits(program.getEntropy(1)));
|
||||
store64(®.a[1].lo, getSmallPositiveFloatBits(program.getEntropy(2)));
|
||||
store64(®.a[1].hi, getSmallPositiveFloatBits(program.getEntropy(3)));
|
||||
store64(®.a[2].lo, getSmallPositiveFloatBits(program.getEntropy(4)));
|
||||
store64(®.a[2].hi, getSmallPositiveFloatBits(program.getEntropy(5)));
|
||||
store64(®.a[3].lo, getSmallPositiveFloatBits(program.getEntropy(6)));
|
||||
store64(®.a[3].hi, getSmallPositiveFloatBits(program.getEntropy(7)));
|
||||
mem.ma = program.getEntropy(8) & CacheLineAlignMask;
|
||||
mem.mx = program.getEntropy(10);
|
||||
auto addressRegisters = program.getEntropy(12);
|
||||
readReg0 = 0 + (addressRegisters & 1);
|
||||
addressRegisters >>= 1;
|
||||
readReg1 = 2 + (addressRegisters & 1);
|
||||
addressRegisters >>= 1;
|
||||
readReg2 = 4 + (addressRegisters & 1);
|
||||
addressRegisters >>= 1;
|
||||
readReg3 = 6 + (addressRegisters & 1);
|
||||
}
|
||||
|
||||
template<bool softAes>
|
||||
void VirtualMachine::getResult(void* scratchpad, size_t scratchpadSize, void* outHash) {
|
||||
if (scratchpadSize > 0) {
|
||||
hashAes1Rx4<false>(scratchpad, scratchpadSize, ®.a);
|
||||
}
|
||||
blake2b(outHash, ResultSize, ®, sizeof(RegisterFile), nullptr, 0);
|
||||
}
|
||||
|
||||
template void VirtualMachine::getResult<false>(void* scratchpad, size_t scratchpadSize, void* outHash);
|
||||
template void VirtualMachine::getResult<true>(void* scratchpad, size_t scratchpadSize, void* outHash);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user