bytecode_machine.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. Copyright (c) 2019, tevador <tevador@gmail.com>
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of the copyright holder nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #pragma once
  26. #include "common.hpp"
  27. #include "intrin_portable.h"
  28. #include "instruction.hpp"
  29. #include "program.hpp"
  30. namespace randomx {
  31. //register file in machine byte order
  32. struct NativeRegisterFile {
  33. int_reg_t r[RegistersCount] = { 0 };
  34. rx_vec_f128 f[RegisterCountFlt];
  35. rx_vec_f128 e[RegisterCountFlt];
  36. rx_vec_f128 a[RegisterCountFlt];
  37. };
  38. struct InstructionByteCode {
  39. union {
  40. int_reg_t* idst;
  41. rx_vec_f128* fdst;
  42. };
  43. union {
  44. const int_reg_t* isrc;
  45. const rx_vec_f128* fsrc;
  46. };
  47. union {
  48. uint64_t imm;
  49. int64_t simm;
  50. };
  51. InstructionType type;
  52. union {
  53. int16_t target;
  54. uint16_t shift;
  55. };
  56. uint32_t memMask;
  57. };
  58. #define OPCODE_CEIL_DECLARE(curr, prev) constexpr int ceil_ ## curr = ceil_ ## prev + RANDOMX_FREQ_ ## curr;
  59. constexpr int ceil_NULL = 0;
  60. OPCODE_CEIL_DECLARE(IADD_RS, NULL);
  61. OPCODE_CEIL_DECLARE(IADD_M, IADD_RS);
  62. OPCODE_CEIL_DECLARE(ISUB_R, IADD_M);
  63. OPCODE_CEIL_DECLARE(ISUB_M, ISUB_R);
  64. OPCODE_CEIL_DECLARE(IMUL_R, ISUB_M);
  65. OPCODE_CEIL_DECLARE(IMUL_M, IMUL_R);
  66. OPCODE_CEIL_DECLARE(IMULH_R, IMUL_M);
  67. OPCODE_CEIL_DECLARE(IMULH_M, IMULH_R);
  68. OPCODE_CEIL_DECLARE(ISMULH_R, IMULH_M);
  69. OPCODE_CEIL_DECLARE(ISMULH_M, ISMULH_R);
  70. OPCODE_CEIL_DECLARE(IMUL_RCP, ISMULH_M);
  71. OPCODE_CEIL_DECLARE(INEG_R, IMUL_RCP);
  72. OPCODE_CEIL_DECLARE(IXOR_R, INEG_R);
  73. OPCODE_CEIL_DECLARE(IXOR_M, IXOR_R);
  74. OPCODE_CEIL_DECLARE(IROR_R, IXOR_M);
  75. OPCODE_CEIL_DECLARE(IROL_R, IROR_R);
  76. OPCODE_CEIL_DECLARE(ISWAP_R, IROL_R);
  77. OPCODE_CEIL_DECLARE(FSWAP_R, ISWAP_R);
  78. OPCODE_CEIL_DECLARE(FADD_R, FSWAP_R);
  79. OPCODE_CEIL_DECLARE(FADD_M, FADD_R);
  80. OPCODE_CEIL_DECLARE(FSUB_R, FADD_M);
  81. OPCODE_CEIL_DECLARE(FSUB_M, FSUB_R);
  82. OPCODE_CEIL_DECLARE(FSCAL_R, FSUB_M);
  83. OPCODE_CEIL_DECLARE(FMUL_R, FSCAL_R);
  84. OPCODE_CEIL_DECLARE(FDIV_M, FMUL_R);
  85. OPCODE_CEIL_DECLARE(FSQRT_R, FDIV_M);
  86. OPCODE_CEIL_DECLARE(CBRANCH, FSQRT_R);
  87. OPCODE_CEIL_DECLARE(CFROUND, CBRANCH);
  88. OPCODE_CEIL_DECLARE(ISTORE, CFROUND);
  89. OPCODE_CEIL_DECLARE(NOP, ISTORE);
  90. #undef OPCODE_CEIL_DECLARE
  91. #define RANDOMX_EXE_ARGS InstructionByteCode& ibc, int& pc, uint8_t* scratchpad, ProgramConfiguration& config
  92. #define RANDOMX_GEN_ARGS Instruction& instr, int i, InstructionByteCode& ibc
  93. class BytecodeMachine;
  94. typedef void(BytecodeMachine::*InstructionGenBytecode)(RANDOMX_GEN_ARGS);
  95. class BytecodeMachine {
  96. public:
  97. void beginCompilation(NativeRegisterFile& regFile) {
  98. for (unsigned i = 0; i < RegistersCount; ++i) {
  99. registerUsage[i] = -1;
  100. }
  101. nreg = &regFile;
  102. }
  103. void compileProgram(Program& program, InstructionByteCode bytecode[RANDOMX_PROGRAM_SIZE], NativeRegisterFile& regFile) {
  104. beginCompilation(regFile);
  105. for (unsigned i = 0; i < RANDOMX_PROGRAM_SIZE; ++i) {
  106. auto& instr = program(i);
  107. auto& ibc = bytecode[i];
  108. compileInstruction(instr, i, ibc);
  109. }
  110. }
  111. static void executeBytecode(InstructionByteCode bytecode[RANDOMX_PROGRAM_SIZE], uint8_t* scratchpad, ProgramConfiguration& config) {
  112. for (int pc = 0; pc < RANDOMX_PROGRAM_SIZE; ++pc) {
  113. auto& ibc = bytecode[pc];
  114. executeInstruction(ibc, pc, scratchpad, config);
  115. }
  116. }
  117. void compileInstruction(RANDOMX_GEN_ARGS)
  118. #ifdef RANDOMX_GEN_TABLE
  119. {
  120. auto generator = genTable[instr.opcode];
  121. (this->*generator)(instr, i, ibc);
  122. }
  123. #else
  124. ;
  125. #endif
  126. static void executeInstruction(RANDOMX_EXE_ARGS);
  127. static void exe_IADD_RS(RANDOMX_EXE_ARGS) {
  128. *ibc.idst += (*ibc.isrc << ibc.shift) + ibc.imm;
  129. }
  130. static void exe_IADD_M(RANDOMX_EXE_ARGS) {
  131. *ibc.idst += load64(getScratchpadAddress(ibc, scratchpad));
  132. }
  133. static void exe_ISUB_R(RANDOMX_EXE_ARGS) {
  134. *ibc.idst -= *ibc.isrc;
  135. }
  136. static void exe_ISUB_M(RANDOMX_EXE_ARGS) {
  137. *ibc.idst -= load64(getScratchpadAddress(ibc, scratchpad));
  138. }
  139. static void exe_IMUL_R(RANDOMX_EXE_ARGS) {
  140. *ibc.idst *= *ibc.isrc;
  141. }
  142. static void exe_IMUL_M(RANDOMX_EXE_ARGS) {
  143. *ibc.idst *= load64(getScratchpadAddress(ibc, scratchpad));
  144. }
  145. static void exe_IMULH_R(RANDOMX_EXE_ARGS) {
  146. *ibc.idst = mulh(*ibc.idst, *ibc.isrc);
  147. }
  148. static void exe_IMULH_M(RANDOMX_EXE_ARGS) {
  149. *ibc.idst = mulh(*ibc.idst, load64(getScratchpadAddress(ibc, scratchpad)));
  150. }
  151. static void exe_ISMULH_R(RANDOMX_EXE_ARGS) {
  152. *ibc.idst = smulh(unsigned64ToSigned2sCompl(*ibc.idst), unsigned64ToSigned2sCompl(*ibc.isrc));
  153. }
  154. static void exe_ISMULH_M(RANDOMX_EXE_ARGS) {
  155. *ibc.idst = smulh(unsigned64ToSigned2sCompl(*ibc.idst), unsigned64ToSigned2sCompl(load64(getScratchpadAddress(ibc, scratchpad))));
  156. }
  157. static void exe_INEG_R(RANDOMX_EXE_ARGS) {
  158. *ibc.idst = ~(*ibc.idst) + 1; //two's complement negative
  159. }
  160. static void exe_IXOR_R(RANDOMX_EXE_ARGS) {
  161. *ibc.idst ^= *ibc.isrc;
  162. }
  163. static void exe_IXOR_M(RANDOMX_EXE_ARGS) {
  164. *ibc.idst ^= load64(getScratchpadAddress(ibc, scratchpad));
  165. }
  166. static void exe_IROR_R(RANDOMX_EXE_ARGS) {
  167. *ibc.idst = rotr(*ibc.idst, *ibc.isrc & 63);
  168. }
  169. static void exe_IROL_R(RANDOMX_EXE_ARGS) {
  170. *ibc.idst = rotl(*ibc.idst, *ibc.isrc & 63);
  171. }
  172. static void exe_ISWAP_R(RANDOMX_EXE_ARGS) {
  173. int_reg_t temp = *ibc.isrc;
  174. *(int_reg_t*)ibc.isrc = *ibc.idst;
  175. *ibc.idst = temp;
  176. }
  177. static void exe_FSWAP_R(RANDOMX_EXE_ARGS) {
  178. *ibc.fdst = rx_swap_vec_f128(*ibc.fdst);
  179. }
  180. static void exe_FADD_R(RANDOMX_EXE_ARGS) {
  181. *ibc.fdst = rx_add_vec_f128(*ibc.fdst, *ibc.fsrc);
  182. }
  183. static void exe_FADD_M(RANDOMX_EXE_ARGS) {
  184. rx_vec_f128 fsrc = rx_cvt_packed_int_vec_f128(getScratchpadAddress(ibc, scratchpad));
  185. *ibc.fdst = rx_add_vec_f128(*ibc.fdst, fsrc);
  186. }
  187. static void exe_FSUB_R(RANDOMX_EXE_ARGS) {
  188. *ibc.fdst = rx_sub_vec_f128(*ibc.fdst, *ibc.fsrc);
  189. }
  190. static void exe_FSUB_M(RANDOMX_EXE_ARGS) {
  191. rx_vec_f128 fsrc = rx_cvt_packed_int_vec_f128(getScratchpadAddress(ibc, scratchpad));
  192. *ibc.fdst = rx_sub_vec_f128(*ibc.fdst, fsrc);
  193. }
  194. static void exe_FSCAL_R(RANDOMX_EXE_ARGS) {
  195. const rx_vec_f128 mask = rx_set1_vec_f128(0x80F0000000000000);
  196. *ibc.fdst = rx_xor_vec_f128(*ibc.fdst, mask);
  197. }
  198. static void exe_FMUL_R(RANDOMX_EXE_ARGS) {
  199. *ibc.fdst = rx_mul_vec_f128(*ibc.fdst, *ibc.fsrc);
  200. }
  201. static void exe_FDIV_M(RANDOMX_EXE_ARGS) {
  202. rx_vec_f128 fsrc = maskRegisterExponentMantissa(
  203. config,
  204. rx_cvt_packed_int_vec_f128(getScratchpadAddress(ibc, scratchpad))
  205. );
  206. *ibc.fdst = rx_div_vec_f128(*ibc.fdst, fsrc);
  207. }
  208. static void exe_FSQRT_R(RANDOMX_EXE_ARGS) {
  209. *ibc.fdst = rx_sqrt_vec_f128(*ibc.fdst);
  210. }
  211. static void exe_CBRANCH(RANDOMX_EXE_ARGS) {
  212. *ibc.idst += ibc.imm;
  213. if ((*ibc.idst & ibc.memMask) == 0) {
  214. pc = ibc.target;
  215. }
  216. }
  217. static void exe_CFROUND(RANDOMX_EXE_ARGS) {
  218. rx_set_rounding_mode(rotr(*ibc.isrc, ibc.imm) % 4);
  219. }
  220. static void exe_ISTORE(RANDOMX_EXE_ARGS) {
  221. store64(scratchpad + ((*ibc.idst + ibc.imm) & ibc.memMask), *ibc.isrc);
  222. }
  223. protected:
  224. static rx_vec_f128 maskRegisterExponentMantissa(ProgramConfiguration& config, rx_vec_f128 x) {
  225. const rx_vec_f128 xmantissaMask = rx_set_vec_f128(dynamicMantissaMask, dynamicMantissaMask);
  226. const rx_vec_f128 xexponentMask = rx_load_vec_f128((const double*)&config.eMask);
  227. x = rx_and_vec_f128(x, xmantissaMask);
  228. x = rx_or_vec_f128(x, xexponentMask);
  229. return x;
  230. }
  231. private:
  232. static const int_reg_t zero;
  233. int registerUsage[RegistersCount];
  234. NativeRegisterFile* nreg;
  235. static void* getScratchpadAddress(InstructionByteCode& ibc, uint8_t* scratchpad) {
  236. uint32_t addr = (*ibc.isrc + ibc.imm) & ibc.memMask;
  237. return scratchpad + addr;
  238. }
  239. #ifdef RANDOMX_GEN_TABLE
  240. static InstructionGenBytecode genTable[256];
  241. void gen_IADD_RS(RANDOMX_GEN_ARGS);
  242. void gen_IADD_M(RANDOMX_GEN_ARGS);
  243. void gen_ISUB_R(RANDOMX_GEN_ARGS);
  244. void gen_ISUB_M(RANDOMX_GEN_ARGS);
  245. void gen_IMUL_R(RANDOMX_GEN_ARGS);
  246. void gen_IMUL_M(RANDOMX_GEN_ARGS);
  247. void gen_IMULH_R(RANDOMX_GEN_ARGS);
  248. void gen_IMULH_M(RANDOMX_GEN_ARGS);
  249. void gen_ISMULH_R(RANDOMX_GEN_ARGS);
  250. void gen_ISMULH_M(RANDOMX_GEN_ARGS);
  251. void gen_IMUL_RCP(RANDOMX_GEN_ARGS);
  252. void gen_INEG_R(RANDOMX_GEN_ARGS);
  253. void gen_IXOR_R(RANDOMX_GEN_ARGS);
  254. void gen_IXOR_M(RANDOMX_GEN_ARGS);
  255. void gen_IROR_R(RANDOMX_GEN_ARGS);
  256. void gen_IROL_R(RANDOMX_GEN_ARGS);
  257. void gen_ISWAP_R(RANDOMX_GEN_ARGS);
  258. void gen_FSWAP_R(RANDOMX_GEN_ARGS);
  259. void gen_FADD_R(RANDOMX_GEN_ARGS);
  260. void gen_FADD_M(RANDOMX_GEN_ARGS);
  261. void gen_FSUB_R(RANDOMX_GEN_ARGS);
  262. void gen_FSUB_M(RANDOMX_GEN_ARGS);
  263. void gen_FSCAL_R(RANDOMX_GEN_ARGS);
  264. void gen_FMUL_R(RANDOMX_GEN_ARGS);
  265. void gen_FDIV_M(RANDOMX_GEN_ARGS);
  266. void gen_FSQRT_R(RANDOMX_GEN_ARGS);
  267. void gen_CBRANCH(RANDOMX_GEN_ARGS);
  268. void gen_CFROUND(RANDOMX_GEN_ARGS);
  269. void gen_ISTORE(RANDOMX_GEN_ARGS);
  270. void gen_NOP(RANDOMX_GEN_ARGS);
  271. #endif
  272. };
  273. }