common.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. Copyright (c) 2018 tevador
  3. This file is part of RandomX.
  4. RandomX is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #pragma once
  16. #include <cstdint>
  17. #include <new>
  18. #include "intrinPortable.h"
  19. namespace RandomX {
  20. using addr_t = uint32_t;
  21. constexpr int RoundToNearest = 0;
  22. constexpr int RoundDown = 1;
  23. constexpr int RoundUp = 2;
  24. constexpr int RoundToZero = 3;
  25. constexpr int SeedSize = 32;
  26. constexpr int CacheBlockSize = 1024;
  27. constexpr int BlockExpansionRatio = 64;
  28. constexpr uint32_t DatasetBlockSize = BlockExpansionRatio * CacheBlockSize;
  29. constexpr uint32_t DatasetBlockCount = 65536;
  30. constexpr uint32_t CacheSize = DatasetBlockCount * CacheBlockSize;
  31. constexpr uint64_t DatasetSize = (uint64_t)DatasetBlockCount * DatasetBlockSize;
  32. constexpr int ArgonIterations = 12;
  33. constexpr uint32_t ArgonMemorySize = 65536; //KiB
  34. constexpr int ArgonLanes = 1;
  35. const char ArgonSalt[] = "Monero\x1A$";
  36. constexpr int ArgonSaltSize = sizeof(ArgonSalt) - 1;
  37. #ifdef TRACE
  38. constexpr bool trace = true;
  39. #else
  40. constexpr bool trace = false;
  41. #endif
  42. typedef union {
  43. double f64;
  44. int64_t i64;
  45. uint64_t u64;
  46. int32_t i32;
  47. uint32_t u32;
  48. } convertible_t;
  49. constexpr int ProgramLength = 512;
  50. constexpr int InstructionCount = 1024 * 1024;
  51. constexpr uint32_t ScratchpadSize = 256 * 1024;
  52. constexpr uint32_t ScratchpadLength = ScratchpadSize / sizeof(convertible_t);
  53. constexpr uint32_t ScratchpadL1 = ScratchpadSize / 16 / sizeof(convertible_t);
  54. constexpr uint32_t ScratchpadL2 = ScratchpadSize / sizeof(convertible_t);
  55. constexpr int RegistersCount = 8;
  56. struct LightClientMemory {
  57. uint8_t* cache;
  58. uint8_t* block;
  59. uint32_t blockNumber;
  60. alignas(16) __m128i keys[10];
  61. void* operator new(size_t size) {
  62. void* ptr = _mm_malloc(size, sizeof(__m128i));
  63. if (ptr == nullptr)
  64. throw std::bad_alloc();
  65. return ptr;
  66. }
  67. void operator delete(void* ptr) {
  68. _mm_free(ptr);
  69. }
  70. };
  71. struct MemoryRegisters {
  72. addr_t ma, mx;
  73. union {
  74. uint8_t* dataset;
  75. LightClientMemory* lcm;
  76. };
  77. };
  78. static_assert(sizeof(MemoryRegisters) == 2 * sizeof(addr_t) + sizeof(uintptr_t), "Invalid alignment of struct RandomX::MemoryRegisters");
  79. struct RegisterFile {
  80. convertible_t r[RegistersCount];
  81. convertible_t f[RegistersCount];
  82. };
  83. static_assert(sizeof(RegisterFile) == 2 * RegistersCount * sizeof(convertible_t), "Invalid alignment of struct RandomX::RegisterFile");
  84. typedef convertible_t(*DatasetReadFunc)(addr_t, MemoryRegisters&);
  85. extern "C" {
  86. void executeProgram(RegisterFile& registerFile, MemoryRegisters& memory, DatasetReadFunc readFunc, convertible_t* scratchpad);
  87. }
  88. }