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>
|
2018-12-31 18:06:45 +00:00
|
|
|
#include <iostream>
|
2019-02-09 14:45:26 +00:00
|
|
|
#include "blake2/endian.h"
|
2019-03-08 14:34:34 +00:00
|
|
|
#include "configuration.h"
|
2019-04-20 09:08:01 +00:00
|
|
|
#include "randomx.h"
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
namespace randomx {
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2019-03-08 14:34:34 +00:00
|
|
|
static_assert((RANDOMX_ARGON_MEMORY & (RANDOMX_ARGON_MEMORY - 1)) == 0, "RANDOMX_ARGON_MEMORY must be a power of 2.");
|
2019-04-26 14:05:30 +00:00
|
|
|
static_assert((RANDOMX_DATASET_BASE_SIZE & (RANDOMX_DATASET_BASE_SIZE - 1)) == 0, "RANDOMX_DATASET_BASE_SIZE must be a power of 2.");
|
|
|
|
static_assert(RANDOMX_DATASET_BASE_SIZE <= 4294967296ULL, "RANDOMX_DATASET_BASE_SIZE must not exceed 4294967296.");
|
|
|
|
static_assert(RANDOMX_DATASET_EXTRA_SIZE % 64 == 0, "RANDOMX_DATASET_EXTRA_SIZE must be divisible by 64.");
|
2019-03-08 14:34:34 +00:00
|
|
|
static_assert(RANDOMX_PROGRAM_SIZE > 0, "RANDOMX_PROGRAM_SIZE must be greater than 0");
|
|
|
|
static_assert(RANDOMX_PROGRAM_ITERATIONS > 0, "RANDOMX_PROGRAM_ITERATIONS must be greater than 0");
|
|
|
|
static_assert(RANDOMX_PROGRAM_COUNT > 0, "RANDOMX_PROGRAM_COUNT must be greater than 0");
|
|
|
|
static_assert((RANDOMX_SCRATCHPAD_L3 & (RANDOMX_SCRATCHPAD_L3 - 1)) == 0, "RANDOMX_SCRATCHPAD_L3 must be a power of 2.");
|
|
|
|
static_assert(RANDOMX_SCRATCHPAD_L3 >= RANDOMX_SCRATCHPAD_L2, "RANDOMX_SCRATCHPAD_L3 must be greater than or equal to RANDOMX_SCRATCHPAD_L2.");
|
|
|
|
static_assert((RANDOMX_SCRATCHPAD_L2 & (RANDOMX_SCRATCHPAD_L2 - 1)) == 0, "RANDOMX_SCRATCHPAD_L2 must be a power of 2.");
|
|
|
|
static_assert(RANDOMX_SCRATCHPAD_L2 >= RANDOMX_SCRATCHPAD_L1, "RANDOMX_SCRATCHPAD_L2 must be greater than or equal to RANDOMX_SCRATCHPAD_L1.");
|
|
|
|
static_assert((RANDOMX_SCRATCHPAD_L1 & (RANDOMX_SCRATCHPAD_L1 - 1)) == 0, "RANDOMX_SCRATCHPAD_L1 must be a power of 2.");
|
|
|
|
static_assert(RANDOMX_CACHE_ACCESSES > 1, "RANDOMX_CACHE_ACCESSES must be greater than 1");
|
|
|
|
|
2019-04-26 09:07:47 +00:00
|
|
|
constexpr int wtSum = RANDOMX_FREQ_IADD_RS + RANDOMX_FREQ_IADD_M + RANDOMX_FREQ_ISUB_R + \
|
|
|
|
RANDOMX_FREQ_ISUB_M + RANDOMX_FREQ_IMUL_R + RANDOMX_FREQ_IMUL_M + RANDOMX_FREQ_IMULH_R + \
|
2019-03-08 14:34:34 +00:00
|
|
|
RANDOMX_FREQ_IMULH_M + RANDOMX_FREQ_ISMULH_R + RANDOMX_FREQ_ISMULH_M + RANDOMX_FREQ_IMUL_RCP + \
|
|
|
|
RANDOMX_FREQ_INEG_R + RANDOMX_FREQ_IXOR_R + RANDOMX_FREQ_IXOR_M + RANDOMX_FREQ_IROR_R + RANDOMX_FREQ_ISWAP_R + \
|
|
|
|
RANDOMX_FREQ_FSWAP_R + RANDOMX_FREQ_FADD_R + RANDOMX_FREQ_FADD_M + RANDOMX_FREQ_FSUB_R + RANDOMX_FREQ_FSUB_M + \
|
|
|
|
RANDOMX_FREQ_FSCAL_R + RANDOMX_FREQ_FMUL_R + RANDOMX_FREQ_FDIV_M + RANDOMX_FREQ_FSQRT_R + RANDOMX_FREQ_COND_R + \
|
2019-04-26 09:07:47 +00:00
|
|
|
RANDOMX_FREQ_CFROUND + RANDOMX_FREQ_ISTORE + RANDOMX_FREQ_NOP;
|
2019-03-08 14:34:34 +00:00
|
|
|
|
|
|
|
static_assert(wtSum == 256, "Sum of instruction frequencies must be 256.");
|
|
|
|
|
2018-12-11 20:00:30 +00:00
|
|
|
using addr_t = uint32_t;
|
|
|
|
|
2019-03-10 22:14:03 +00:00
|
|
|
constexpr int ArgonBlockSize = 1024;
|
2019-03-08 14:34:34 +00:00
|
|
|
constexpr int ArgonSaltSize = sizeof(RANDOMX_ARGON_SALT) - 1;
|
2019-04-23 20:15:55 +00:00
|
|
|
constexpr int CacheLineSize = RANDOMX_DATASET_ITEM_SIZE;
|
2019-04-20 09:08:01 +00:00
|
|
|
constexpr int ScratchpadSize = RANDOMX_SCRATCHPAD_L3;
|
2019-04-26 14:05:30 +00:00
|
|
|
constexpr uint32_t CacheLineAlignMask = (RANDOMX_DATASET_BASE_SIZE - 1) & ~(CacheLineSize - 1);
|
|
|
|
constexpr uint32_t CacheSize = RANDOMX_ARGON_MEMORY * ArgonBlockSize;
|
|
|
|
constexpr uint64_t DatasetSize = RANDOMX_DATASET_BASE_SIZE + RANDOMX_DATASET_EXTRA_SIZE;
|
|
|
|
constexpr uint32_t DatasetExtraItems = RANDOMX_DATASET_EXTRA_SIZE / RANDOMX_DATASET_ITEM_SIZE;
|
2019-04-21 12:07:32 +00:00
|
|
|
|
2018-12-11 20:00:30 +00:00
|
|
|
#ifdef TRACE
|
|
|
|
constexpr bool trace = true;
|
|
|
|
#else
|
|
|
|
constexpr bool trace = false;
|
|
|
|
#endif
|
|
|
|
|
2019-02-09 14:45:26 +00:00
|
|
|
#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;
|
2018-12-31 18:06:45 +00:00
|
|
|
|
|
|
|
struct fpu_reg_t {
|
2019-02-09 14:45:26 +00:00
|
|
|
double lo;
|
|
|
|
double hi;
|
2018-12-19 20:54:44 +00:00
|
|
|
};
|
2018-12-11 20:00:30 +00:00
|
|
|
|
2019-03-08 14:34:34 +00:00
|
|
|
constexpr uint32_t ScratchpadL1 = RANDOMX_SCRATCHPAD_L1 / sizeof(int_reg_t);
|
|
|
|
constexpr uint32_t ScratchpadL2 = RANDOMX_SCRATCHPAD_L2 / sizeof(int_reg_t);
|
|
|
|
constexpr uint32_t ScratchpadL3 = RANDOMX_SCRATCHPAD_L3 / sizeof(int_reg_t);
|
2019-01-24 18:29:59 +00:00
|
|
|
constexpr int ScratchpadL1Mask = (ScratchpadL1 - 1) * 8;
|
|
|
|
constexpr int ScratchpadL2Mask = (ScratchpadL2 - 1) * 8;
|
2019-01-27 09:52:30 +00:00
|
|
|
constexpr int ScratchpadL1Mask16 = (ScratchpadL1 / 2 - 1) * 16;
|
|
|
|
constexpr int ScratchpadL2Mask16 = (ScratchpadL2 / 2 - 1) * 16;
|
2019-03-08 14:34:34 +00:00
|
|
|
constexpr int ScratchpadL3Mask = (ScratchpadL3 - 1) * 8;
|
|
|
|
constexpr int ScratchpadL3Mask64 = (ScratchpadL3 / 8 - 1) * 64;
|
2018-12-11 20:00:30 +00:00
|
|
|
constexpr int RegistersCount = 8;
|
2019-04-14 15:21:26 +00:00
|
|
|
constexpr int RegisterNeedsDisplacement = 5; //x86 r13 register
|
|
|
|
constexpr int RegisterNeedsSib = 4; //x86 r12 register
|
2018-12-11 20:00:30 +00:00
|
|
|
|
|
|
|
struct MemoryRegisters {
|
2019-01-07 16:44:43 +00:00
|
|
|
addr_t mx, ma;
|
2019-04-20 09:08:01 +00:00
|
|
|
uint8_t* memory = nullptr;
|
2018-12-11 20:00:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct RegisterFile {
|
2019-02-09 14:45:26 +00:00
|
|
|
int_reg_t r[RegistersCount];
|
2019-01-24 18:29:59 +00:00
|
|
|
fpu_reg_t f[RegistersCount / 2];
|
2019-02-09 14:45:26 +00:00
|
|
|
fpu_reg_t e[RegistersCount / 2];
|
2019-01-24 18:29:59 +00:00
|
|
|
fpu_reg_t a[RegistersCount / 2];
|
2018-12-11 20:00:30 +00:00
|
|
|
};
|
|
|
|
|
2019-02-09 14:45:26 +00:00
|
|
|
typedef void(*DatasetReadFunc)(addr_t, MemoryRegisters&, int_reg_t(®)[RegistersCount]);
|
|
|
|
typedef void(*ProgramFunc)(RegisterFile&, MemoryRegisters&, uint8_t* /* scratchpad */, uint64_t);
|
2019-04-20 09:08:01 +00:00
|
|
|
typedef void(*DatasetInitFunc)(randomx_cache* cache, uint8_t* dataset, uint32_t startBlock, uint32_t endBlock);
|
2018-12-31 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
2019-04-20 09:08:01 +00:00
|
|
|
std::ostream& operator<<(std::ostream& os, const randomx::RegisterFile& rf);
|