RandomWOW/src/common.hpp

144 lines
4.3 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/>.
*/
#pragma once
#include <cstdint>
#include <iostream>
#include "blake2/endian.h"
2018-12-11 20:00:30 +00:00
namespace RandomX {
using addr_t = uint32_t;
constexpr int SeedSize = 32;
constexpr int ResultSize = 64;
2018-12-11 20:00:30 +00:00
2019-01-18 17:44:06 +00:00
constexpr int ArgonIterations = 3;
constexpr uint32_t ArgonMemorySize = 262144; //KiB
2018-12-11 20:00:30 +00:00
constexpr int ArgonLanes = 1;
const char ArgonSalt[] = "Monero\x1A$";
constexpr int ArgonSaltSize = sizeof(ArgonSalt) - 1;
2019-01-18 16:57:47 +00:00
constexpr int CacheLineSize = 64;
2019-02-15 10:38:20 +00:00
2019-01-18 16:57:47 +00:00
constexpr uint64_t DatasetSize = 4ULL * 1024 * 1024 * 1024; //4 GiB
2019-02-15 10:38:20 +00:00
constexpr uint32_t CacheLineAlignMask = (DatasetSize - 1) & ~(CacheLineSize - 1);
2019-01-18 16:57:47 +00:00
constexpr uint32_t CacheSize = ArgonMemorySize * 1024;
constexpr int CacheBlockCount = CacheSize / CacheLineSize;
2019-02-15 10:38:20 +00:00
constexpr int DatasetExpansionRatio = DatasetSize / CacheSize;
constexpr int DatasetBlockCount = DatasetExpansionRatio * CacheBlockCount;
constexpr int DatasetIterations = DatasetExpansionRatio;
2019-01-18 16:57:47 +00:00
2018-12-11 20:00:30 +00:00
#ifdef TRACE
constexpr bool trace = true;
#else
constexpr bool trace = false;
#endif
#ifndef UNREACHABLE
#ifdef __GNUC__
#define UNREACHABLE __builtin_unreachable()
#elif _MSC_VER
#define UNREACHABLE __assume(false)
#else
#define UNREACHABLE
#endif
#endif
using int_reg_t = uint64_t;
struct fpu_reg_t {
double lo;
double hi;
2018-12-19 20:54:44 +00:00
};
2018-12-11 20:00:30 +00:00
constexpr int ProgramLength = 256;
constexpr uint32_t InstructionCount = 2048;
2019-02-11 17:57:42 +00:00
constexpr int ChainLength = 8;
constexpr uint32_t ScratchpadSize = 2 * 1024 * 1024;
constexpr uint32_t ScratchpadLength = ScratchpadSize / sizeof(int_reg_t);
constexpr uint32_t ScratchpadL1 = ScratchpadSize / 128 / sizeof(int_reg_t);
constexpr uint32_t ScratchpadL2 = ScratchpadSize / 8 / sizeof(int_reg_t);
constexpr uint32_t ScratchpadL3 = ScratchpadSize / sizeof(int_reg_t);
constexpr int ScratchpadL1Mask = (ScratchpadL1 - 1) * 8;
constexpr int ScratchpadL2Mask = (ScratchpadL2 - 1) * 8;
constexpr int ScratchpadL1Mask16 = (ScratchpadL1 / 2 - 1) * 16;
constexpr int ScratchpadL2Mask16 = (ScratchpadL2 / 2 - 1) * 16;
constexpr int ScratchpadL3Mask = (ScratchpadLength - 1) * 8;
constexpr int ScratchpadL3Mask64 = (ScratchpadLength / 8 - 1) * 64;
2019-01-10 21:04:55 +00:00
constexpr uint32_t TransformationCount = 90;
2018-12-11 20:00:30 +00:00
constexpr int RegistersCount = 8;
2018-12-19 20:54:44 +00:00
class Cache;
2018-12-18 21:00:58 +00:00
inline int wrapInstr(int i) {
return i % RandomX::ProgramLength;
}
2019-01-14 23:01:11 +00:00
class ILightClientAsyncWorker {
public:
virtual ~ILightClientAsyncWorker() {}
2019-01-14 23:01:11 +00:00
virtual void prepareBlock(addr_t) = 0;
virtual void prepareBlocks(void* out, uint32_t startBlock, uint32_t blockCount) = 0;
virtual const uint64_t* getBlock(addr_t) = 0;
virtual void getBlocks(void* out, uint32_t startBlock, uint32_t blockCount) = 0;
virtual void sync() = 0;
const Cache* getCache() {
return cache;
}
protected:
ILightClientAsyncWorker(const Cache* c) : cache(c) {}
const Cache* cache;
2018-12-19 20:54:44 +00:00
};
union dataset_t {
uint8_t* dataset;
Cache* cache;
2019-01-14 23:01:11 +00:00
ILightClientAsyncWorker* asyncWorker;
2018-12-11 20:00:30 +00:00
};
struct MemoryRegisters {
2019-01-07 16:44:43 +00:00
addr_t mx, ma;
2018-12-19 20:54:44 +00:00
dataset_t ds;
2018-12-11 20:00:30 +00:00
};
static_assert(sizeof(MemoryRegisters) == 2 * sizeof(addr_t) + sizeof(uintptr_t), "Invalid alignment of struct RandomX::MemoryRegisters");
struct RegisterFile {
int_reg_t r[RegistersCount];
fpu_reg_t f[RegistersCount / 2];
fpu_reg_t e[RegistersCount / 2];
fpu_reg_t a[RegistersCount / 2];
2018-12-11 20:00:30 +00:00
};
static_assert(sizeof(RegisterFile) == 256, "Invalid alignment of struct RandomX::RegisterFile");
2018-12-11 20:00:30 +00:00
typedef void(*DatasetReadFunc)(addr_t, MemoryRegisters&, int_reg_t(&reg)[RegistersCount]);
2018-12-18 21:00:58 +00:00
typedef void(*ProgramFunc)(RegisterFile&, MemoryRegisters&, uint8_t* /* scratchpad */, uint64_t);
2018-12-11 20:00:30 +00:00
extern "C" {
void executeProgram(RegisterFile&, MemoryRegisters&, uint8_t* /* scratchpad */, uint64_t);
2018-12-11 20:00:30 +00:00
}
}
std::ostream& operator<<(std::ostream& os, const RandomX::RegisterFile& rf);