perf-simulation.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /*
  2. Copyright (c) 2018-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. #include "utility.hpp"
  26. #include "../common.hpp"
  27. #include "../aes_hash.hpp"
  28. #include "../program.hpp"
  29. #include "../blake2/blake2.h"
  30. #include <algorithm>
  31. #include <iomanip>
  32. int analyze(randomx::Program& p);
  33. int executeInOrder(randomx::Program& p, randomx::Program& original, bool print, int executionPorts, int memoryPorts, bool speculate, int pipeline);
  34. int executeOutOfOrder(randomx::Program& p, randomx::Program& original, bool print, int executionPorts, int memoryPorts, bool speculate, int pipeline);
  35. constexpr uint32_t DST_NOP = 0;
  36. constexpr uint32_t DST_INT = 1;
  37. constexpr uint32_t DST_FLT = 2;
  38. constexpr uint32_t DST_MEM = 3;
  39. constexpr uint32_t MASK_DST = 3;
  40. constexpr uint32_t SRC_NOP = 0;
  41. constexpr uint32_t SRC_INT = 4;
  42. constexpr uint32_t SRC_FLT = 8;
  43. constexpr uint32_t SRC_MEM = 12;
  44. constexpr uint32_t MASK_SRC = 12;
  45. constexpr uint32_t OP_CFROUND = 16;
  46. constexpr uint32_t OP_SWAP = 32;
  47. constexpr uint32_t OP_BRANCH = 48;
  48. constexpr uint32_t MASK_EXT = 48;
  49. constexpr uint32_t OP_FLOAT = 64;
  50. constexpr uint32_t BRANCH_TARGET = 128;
  51. //template<bool softAes>
  52. void generate(randomx::Program& p, uint32_t nonce) {
  53. alignas(16) uint64_t hash[8];
  54. blake2b(hash, sizeof(hash), &nonce, sizeof(nonce), nullptr, 0);
  55. fillAes1Rx4<false>((void*)hash, sizeof(p), &p);
  56. }
  57. bool has(randomx::Instruction& instr, uint32_t mask, uint32_t prop) {
  58. return (instr.opcode & mask) == prop;
  59. }
  60. bool has(randomx::Instruction& instr, uint32_t prop) {
  61. return (instr.opcode & prop) != 0;
  62. }
  63. int main(int argc, char** argv) {
  64. int nonces, seed, executionPorts, memoryPorts, pipeline;
  65. bool print, reorder, speculate;
  66. readOption("--print", argc, argv, print);
  67. readOption("--reorder", argc, argv, reorder);
  68. readOption("--speculate", argc, argv, speculate);
  69. readIntOption("--nonces", argc, argv, nonces, 1);
  70. readIntOption("--seed", argc, argv, seed, 0);
  71. readIntOption("--executionPorts", argc, argv, executionPorts, 4);
  72. readIntOption("--memoryPorts", argc, argv, memoryPorts, 2);
  73. readIntOption("--pipeline", argc, argv, pipeline, 3);
  74. randomx::Program p, original;
  75. double totalCycles = 0.0;
  76. double jumpCount = 0;
  77. for (int i = 0; i < nonces; ++i) {
  78. generate(original, i ^ seed);
  79. memcpy(&p, &original, sizeof(p));
  80. jumpCount += analyze(p);
  81. totalCycles +=
  82. reorder
  83. ?
  84. executeOutOfOrder(p, original, print, executionPorts, memoryPorts, speculate, pipeline)
  85. :
  86. executeInOrder(p, original, print, executionPorts, memoryPorts, speculate, pipeline);
  87. }
  88. totalCycles /= nonces;
  89. jumpCount /= nonces;
  90. std::cout << "Execution took " << totalCycles << " cycles per program" << std::endl;
  91. //std::cout << "Jump count: " << jumpCount << std::endl;
  92. return 0;
  93. }
  94. int executeInOrder(randomx::Program& p, randomx::Program& original, bool print, int executionPorts, int memoryPorts, bool speculate, int pipeline) {
  95. int cycle = pipeline - 1;
  96. int index = 0;
  97. int branchCount = 0;
  98. int int_reg_ready[randomx::RegistersCount] = { 0 };
  99. int flt_reg_ready[randomx::RegistersCount] = { 0 };
  100. //each workgroup takes 1 or 2 cycles (2 cycles if any instruction has a memory operand)
  101. while (index < RANDOMX_PROGRAM_SIZE) {
  102. int memoryAccesses = 0;
  103. bool hasRound = false;
  104. int workers = 0;
  105. //std::cout << "-----------" << std::endl;
  106. for (; workers < executionPorts && memoryAccesses < memoryPorts && index < RANDOMX_PROGRAM_SIZE; ++workers) {
  107. auto& instr = p(index);
  108. auto& origi = original(index);
  109. origi.dst %= randomx::RegistersCount;
  110. origi.src %= randomx::RegistersCount;
  111. //check dependencies
  112. if (has(instr, MASK_SRC, SRC_INT) && int_reg_ready[instr.src] > cycle)
  113. break;
  114. if (has(instr, MASK_SRC, SRC_MEM) && int_reg_ready[instr.src] > cycle - 1)
  115. break;
  116. if (has(instr, MASK_DST, DST_MEM) && int_reg_ready[instr.dst] > cycle - 1)
  117. break;
  118. if (has(instr, MASK_DST, DST_FLT) && flt_reg_ready[instr.dst] > cycle)
  119. break;
  120. if (has(instr, MASK_DST, DST_INT) && int_reg_ready[instr.dst] > cycle)
  121. break;
  122. if (hasRound && has(instr, OP_FLOAT))
  123. break;
  124. //execute
  125. index++;
  126. if (has(instr, MASK_EXT, OP_BRANCH)) {
  127. branchCount++;
  128. }
  129. if (has(instr, MASK_DST, DST_FLT))
  130. flt_reg_ready[instr.dst] = cycle + 1;
  131. if (has(instr, MASK_DST, DST_INT))
  132. int_reg_ready[instr.dst] = cycle + 1;
  133. if (has(instr, MASK_EXT, OP_SWAP)) {
  134. int_reg_ready[instr.src] = cycle + 1;
  135. }
  136. if (has(instr, MASK_EXT, OP_CFROUND))
  137. hasRound = true;
  138. if (has(instr, MASK_SRC, SRC_MEM) || has(instr, MASK_DST, DST_MEM)) {
  139. memoryAccesses++;
  140. }
  141. if (print)
  142. std::cout << std::setw(2) << (cycle + 1) << ": " << origi;
  143. //non-speculative execution must stall after branch
  144. if (!speculate && has(instr, MASK_EXT, OP_BRANCH)) {
  145. cycle += pipeline - 1;
  146. break;
  147. }
  148. }
  149. //std::cout << " workers: " << workers << std::endl;
  150. cycle++;
  151. }
  152. if (speculate) {
  153. //account for mispredicted branches
  154. int i = 0;
  155. while (branchCount--) {
  156. auto entropy = p.getEntropy(i / 8);
  157. entropy >> (i % 8) * 8;
  158. if ((entropy & 0xff) == 0) // 1/256 chance to flush the pipeline
  159. cycle += pipeline - 1;
  160. }
  161. }
  162. return cycle;
  163. }
  164. int executeOutOfOrder(randomx::Program& p, randomx::Program& original, bool print, int executionPorts, int memoryPorts, bool speculate, int pipeline) {
  165. int index = 0;
  166. int busyExecutionPorts[2 * RANDOMX_PROGRAM_SIZE] = { 0 };
  167. int busyMemoryPorts[2 * RANDOMX_PROGRAM_SIZE] = { 0 };
  168. int int_reg_ready[randomx::RegistersCount] = { 0 };
  169. int flt_reg_ready[randomx::RegistersCount] = { 0 };
  170. int fprcReady = 0;
  171. int lastBranch = 0;
  172. int branchCount = 0;
  173. for (; index < RANDOMX_PROGRAM_SIZE; ++index) {
  174. auto& instr = p(index);
  175. int retireCycle = pipeline - 1;
  176. //non-speculative execution cannot reorder across branches
  177. if (!speculate && !has(instr, MASK_EXT, OP_BRANCH))
  178. retireCycle = std::max(lastBranch + pipeline - 1, retireCycle);
  179. //check dependencies
  180. if (has(instr, MASK_SRC, SRC_INT)) {
  181. retireCycle = std::max(retireCycle, int_reg_ready[instr.src]);
  182. int_reg_ready[instr.src] = retireCycle;
  183. }
  184. if (has(instr, MASK_SRC, SRC_MEM)) {
  185. retireCycle = std::max(retireCycle, int_reg_ready[instr.src] + 1);
  186. //find free memory port
  187. while (busyMemoryPorts[retireCycle - 1] >= memoryPorts) {
  188. retireCycle++;
  189. }
  190. busyMemoryPorts[retireCycle - 1]++;
  191. }
  192. if (has(instr, MASK_DST, DST_FLT)) {
  193. retireCycle = std::max(retireCycle, flt_reg_ready[instr.dst]);
  194. }
  195. if (has(instr, MASK_DST, DST_INT)) {
  196. retireCycle = std::max(retireCycle, int_reg_ready[instr.dst]);
  197. }
  198. //floating point operations depend on the fprc register
  199. if (has(instr, OP_FLOAT))
  200. retireCycle = std::max(retireCycle, fprcReady);
  201. //execute
  202. if (has(instr, MASK_DST, DST_MEM)) {
  203. retireCycle = std::max(retireCycle, int_reg_ready[instr.dst] + 1);
  204. //find free memory port
  205. while (busyMemoryPorts[retireCycle - 1] >= memoryPorts) {
  206. retireCycle++;
  207. }
  208. busyMemoryPorts[retireCycle - 1]++;
  209. retireCycle++;
  210. }
  211. if (has(instr, MASK_DST, DST_FLT)) {
  212. //find free execution port
  213. do {
  214. retireCycle++;
  215. } while (busyExecutionPorts[retireCycle - 1] >= executionPorts);
  216. busyExecutionPorts[retireCycle - 1]++;
  217. flt_reg_ready[instr.dst] = retireCycle;
  218. }
  219. if (has(instr, MASK_DST, DST_INT)) {
  220. //find free execution port
  221. do {
  222. retireCycle++;
  223. } while (busyExecutionPorts[retireCycle - 1] >= executionPorts);
  224. busyExecutionPorts[retireCycle - 1]++;
  225. int_reg_ready[instr.dst] = retireCycle;
  226. }
  227. if (has(instr, MASK_EXT, OP_SWAP)) {
  228. int_reg_ready[instr.src] = retireCycle;
  229. }
  230. if (has(instr, MASK_EXT, OP_CFROUND)) {
  231. do {
  232. retireCycle++;
  233. } while (busyExecutionPorts[retireCycle - 1] >= executionPorts);
  234. busyExecutionPorts[retireCycle - 1]++;
  235. fprcReady = retireCycle;
  236. }
  237. if (has(instr, MASK_EXT, OP_BRANCH)) {
  238. /*if (!speculate && instr.mod == 1) { //simulated predication
  239. do {
  240. retireCycle++;
  241. } while (busyExecutionPorts[retireCycle - 1] >= executionPorts);
  242. busyExecutionPorts[retireCycle - 1]++;
  243. int_reg_ready[instr.dst] = retireCycle;
  244. }*/
  245. //else {
  246. lastBranch = std::max(lastBranch, retireCycle);
  247. branchCount++;
  248. //}
  249. }
  250. //print
  251. auto& origi = original(index);
  252. origi.dst %= randomx::RegistersCount;
  253. origi.src %= randomx::RegistersCount;
  254. if (print) {
  255. std::cout << std::setw(2) << retireCycle << ": " << origi;
  256. if (has(instr, MASK_EXT, OP_BRANCH)) {
  257. std::cout << " jump: " << (int)instr.mod << std::endl;
  258. }
  259. }
  260. }
  261. int cycle = 0;
  262. for (int i = 0; i < randomx::RegistersCount; ++i) {
  263. cycle = std::max(cycle, int_reg_ready[i]);
  264. }
  265. for (int i = 0; i < randomx::RegistersCount; ++i) {
  266. cycle = std::max(cycle, flt_reg_ready[i]);
  267. }
  268. if (speculate) {
  269. //account for mispredicted branches
  270. int i = 0;
  271. while (branchCount--) {
  272. auto entropy = p.getEntropy(i / 8);
  273. entropy >> (i % 8) * 8;
  274. if ((entropy & 0xff) == 0) // 1/256 chance to flush the pipeline
  275. cycle += pipeline - 1;
  276. }
  277. }
  278. return cycle;
  279. }
  280. #include "../bytecode_machine.hpp"
  281. //old register selection
  282. struct RegisterUsage {
  283. int32_t lastUsed;
  284. int32_t count;
  285. };
  286. inline int getConditionRegister(RegisterUsage(&registerUsage)[randomx::RegistersCount]) {
  287. int min = INT_MAX;
  288. int minCount = 0;
  289. int minIndex;
  290. //prefer registers that have been used as a condition register fewer times
  291. for (unsigned i = 0; i < randomx::RegistersCount; ++i) {
  292. if (registerUsage[i].lastUsed < min || (registerUsage[i].lastUsed == min && registerUsage[i].count < minCount)) {
  293. min = registerUsage[i].lastUsed;
  294. minCount = registerUsage[i].count;
  295. minIndex = i;
  296. }
  297. }
  298. return minIndex;
  299. }
  300. int analyze(randomx::Program& p) {
  301. int jumpCount = 0;
  302. RegisterUsage registerUsage[randomx::RegistersCount];
  303. for (unsigned i = 0; i < randomx::RegistersCount; ++i) {
  304. registerUsage[i].lastUsed = -1;
  305. registerUsage[i].count = 0;
  306. }
  307. for (unsigned i = 0; i < RANDOMX_PROGRAM_SIZE; ++i) {
  308. auto& instr = p(i);
  309. int opcode = instr.opcode;
  310. instr.opcode = 0;
  311. if (opcode < randomx::ceil_IADD_RS) {
  312. instr.dst = instr.dst % randomx::RegistersCount;
  313. instr.src = instr.src % randomx::RegistersCount;
  314. instr.opcode |= SRC_INT;
  315. instr.opcode |= DST_INT;
  316. registerUsage[instr.dst].lastUsed = i;
  317. continue;
  318. }
  319. if (opcode < randomx::ceil_IADD_M) {
  320. instr.dst = instr.dst % randomx::RegistersCount;
  321. instr.src = instr.src % randomx::RegistersCount;
  322. instr.opcode |= SRC_MEM;
  323. instr.opcode |= DST_INT;
  324. if (instr.src != instr.dst) {
  325. instr.imm32 = (instr.getModMem() ? randomx::ScratchpadL1Mask : randomx::ScratchpadL2Mask);
  326. }
  327. else {
  328. instr.imm32 &= randomx::ScratchpadL3Mask;
  329. }
  330. registerUsage[instr.dst].lastUsed = i;
  331. continue;
  332. }
  333. if (opcode < randomx::ceil_ISUB_R) {
  334. instr.dst = instr.dst % randomx::RegistersCount;
  335. instr.src = instr.src % randomx::RegistersCount;
  336. instr.opcode |= DST_INT;
  337. instr.opcode |= SRC_INT;
  338. registerUsage[instr.dst].lastUsed = i;
  339. continue;
  340. }
  341. if (opcode < randomx::ceil_ISUB_M) {
  342. instr.dst = instr.dst % randomx::RegistersCount;
  343. instr.src = instr.src % randomx::RegistersCount;
  344. instr.opcode |= SRC_MEM;
  345. instr.opcode |= DST_INT;
  346. if (instr.src != instr.dst) {
  347. instr.imm32 = (instr.getModMem() ? randomx::ScratchpadL1Mask : randomx::ScratchpadL2Mask);
  348. }
  349. else {
  350. instr.imm32 &= randomx::ScratchpadL3Mask;
  351. }
  352. registerUsage[instr.dst].lastUsed = i;
  353. continue;
  354. }
  355. if (opcode < randomx::ceil_IMUL_R) {
  356. instr.dst = instr.dst % randomx::RegistersCount;
  357. instr.src = instr.src % randomx::RegistersCount;
  358. instr.opcode |= DST_INT;
  359. instr.opcode |= SRC_INT;
  360. registerUsage[instr.dst].lastUsed = i;
  361. continue;
  362. }
  363. if (opcode < randomx::ceil_IMUL_M) {
  364. instr.dst = instr.dst % randomx::RegistersCount;
  365. instr.src = instr.src % randomx::RegistersCount;
  366. instr.opcode |= SRC_MEM;
  367. instr.opcode |= DST_INT;
  368. if (instr.src != instr.dst) {
  369. instr.imm32 = (instr.getModMem() ? randomx::ScratchpadL1Mask : randomx::ScratchpadL2Mask);
  370. }
  371. else {
  372. instr.imm32 &= randomx::ScratchpadL3Mask;
  373. }
  374. registerUsage[instr.dst].lastUsed = i;
  375. continue;
  376. }
  377. if (opcode < randomx::ceil_IMULH_R) {
  378. instr.dst = instr.dst % randomx::RegistersCount;
  379. instr.src = instr.src % randomx::RegistersCount;
  380. instr.opcode |= DST_INT;
  381. instr.opcode |= SRC_INT;
  382. registerUsage[instr.dst].lastUsed = i;
  383. continue;
  384. }
  385. if (opcode < randomx::ceil_IMULH_M) {
  386. instr.dst = instr.dst % randomx::RegistersCount;
  387. instr.src = instr.src % randomx::RegistersCount;
  388. instr.opcode |= SRC_MEM;
  389. instr.opcode |= DST_INT;
  390. if (instr.src != instr.dst) {
  391. instr.imm32 = (instr.getModMem() ? randomx::ScratchpadL1Mask : randomx::ScratchpadL2Mask);
  392. }
  393. else {
  394. instr.imm32 &= randomx::ScratchpadL3Mask;
  395. }
  396. registerUsage[instr.dst].lastUsed = i;
  397. continue;
  398. }
  399. if (opcode < randomx::ceil_ISMULH_R) {
  400. instr.dst = instr.dst % randomx::RegistersCount;
  401. instr.src = instr.src % randomx::RegistersCount;
  402. instr.opcode |= DST_INT;
  403. instr.opcode |= SRC_INT;
  404. registerUsage[instr.dst].lastUsed = i;
  405. continue;
  406. }
  407. if (opcode < randomx::ceil_ISMULH_M) {
  408. instr.dst = instr.dst % randomx::RegistersCount;
  409. instr.src = instr.src % randomx::RegistersCount;
  410. instr.opcode |= SRC_MEM;
  411. instr.opcode |= DST_INT;
  412. if (instr.src != instr.dst) {
  413. instr.imm32 = (instr.getModMem() ? randomx::ScratchpadL1Mask : randomx::ScratchpadL2Mask);
  414. }
  415. else {
  416. instr.imm32 &= randomx::ScratchpadL3Mask;
  417. }
  418. registerUsage[instr.dst].lastUsed = i;
  419. continue;
  420. }
  421. if (opcode < randomx::ceil_IMUL_RCP) {
  422. uint64_t divisor = instr.getImm32();
  423. if (!randomx::isZeroOrPowerOf2(divisor)) {
  424. instr.dst = instr.dst % randomx::RegistersCount;
  425. instr.opcode |= DST_INT;
  426. registerUsage[instr.dst].lastUsed = i;
  427. }
  428. continue;
  429. }
  430. if (opcode < randomx::ceil_INEG_R) {
  431. instr.dst = instr.dst % randomx::RegistersCount;
  432. instr.opcode |= DST_INT;
  433. registerUsage[instr.dst].lastUsed = i;
  434. continue;
  435. }
  436. if (opcode < randomx::ceil_IXOR_R) {
  437. instr.dst = instr.dst % randomx::RegistersCount;
  438. instr.src = instr.src % randomx::RegistersCount;
  439. instr.opcode |= DST_INT;
  440. instr.opcode |= SRC_INT;
  441. registerUsage[instr.dst].lastUsed = i;
  442. continue;
  443. }
  444. if (opcode < randomx::ceil_IXOR_M) {
  445. instr.dst = instr.dst % randomx::RegistersCount;
  446. instr.src = instr.src % randomx::RegistersCount;
  447. instr.opcode |= SRC_MEM;
  448. instr.opcode |= DST_INT;
  449. if (instr.src != instr.dst) {
  450. instr.imm32 = (instr.getModMem() ? randomx::ScratchpadL1Mask : randomx::ScratchpadL2Mask);
  451. }
  452. else {
  453. instr.imm32 &= randomx::ScratchpadL3Mask;
  454. }
  455. registerUsage[instr.dst].lastUsed = i;
  456. continue;
  457. }
  458. if (opcode < randomx::ceil_IROR_R) {
  459. instr.dst = instr.dst % randomx::RegistersCount;
  460. instr.src = instr.src % randomx::RegistersCount;
  461. instr.opcode |= DST_INT;
  462. instr.opcode |= SRC_INT;
  463. registerUsage[instr.dst].lastUsed = i;
  464. continue;
  465. }
  466. if (opcode < randomx::ceil_IROL_R) {
  467. instr.dst = instr.dst % randomx::RegistersCount;
  468. instr.src = instr.src % randomx::RegistersCount;
  469. instr.opcode |= DST_INT;
  470. instr.opcode |= SRC_INT;
  471. registerUsage[instr.dst].lastUsed = i;
  472. continue;
  473. }
  474. if (opcode < randomx::ceil_ISWAP_R) {
  475. instr.dst = instr.dst % randomx::RegistersCount;
  476. instr.src = instr.src % randomx::RegistersCount;
  477. if (instr.src != instr.dst) {
  478. instr.opcode |= DST_INT;
  479. instr.opcode |= SRC_INT;
  480. instr.opcode |= OP_SWAP;
  481. registerUsage[instr.dst].lastUsed = i;
  482. registerUsage[instr.src].lastUsed = i;
  483. }
  484. continue;
  485. }
  486. if (opcode < randomx::ceil_FSWAP_R) {
  487. instr.dst = instr.dst % randomx::RegistersCount;
  488. instr.opcode |= DST_FLT;
  489. continue;
  490. }
  491. if (opcode < randomx::ceil_FADD_R) {
  492. instr.dst = instr.dst % randomx::RegisterCountFlt;
  493. instr.opcode |= DST_FLT;
  494. instr.opcode |= OP_FLOAT;
  495. continue;
  496. }
  497. if (opcode < randomx::ceil_FADD_M) {
  498. instr.dst = instr.dst % randomx::RegisterCountFlt;
  499. instr.src = instr.src % randomx::RegistersCount;
  500. instr.opcode |= DST_FLT;
  501. instr.opcode |= SRC_MEM;
  502. instr.opcode |= OP_FLOAT;
  503. instr.imm32 = (instr.getModMem() ? randomx::ScratchpadL1Mask : randomx::ScratchpadL2Mask);
  504. continue;
  505. }
  506. if (opcode < randomx::ceil_FSUB_R) {
  507. instr.dst = instr.dst % randomx::RegisterCountFlt;
  508. instr.opcode |= DST_FLT;
  509. instr.opcode |= OP_FLOAT;
  510. continue;
  511. }
  512. if (opcode < randomx::ceil_FSUB_M) {
  513. instr.dst = instr.dst % randomx::RegisterCountFlt;
  514. instr.src = instr.src % randomx::RegistersCount;
  515. instr.opcode |= DST_FLT;
  516. instr.opcode |= SRC_MEM;
  517. instr.opcode |= OP_FLOAT;
  518. instr.imm32 = (instr.getModMem() ? randomx::ScratchpadL1Mask : randomx::ScratchpadL2Mask);
  519. continue;
  520. }
  521. if (opcode < randomx::ceil_FSCAL_R) {
  522. instr.dst = instr.dst % randomx::RegisterCountFlt;
  523. instr.opcode |= DST_FLT;
  524. continue;
  525. }
  526. if (opcode < randomx::ceil_FMUL_R) {
  527. instr.dst = 4 + instr.dst % randomx::RegisterCountFlt;
  528. instr.opcode |= DST_FLT;
  529. instr.opcode |= OP_FLOAT;
  530. continue;
  531. }
  532. if (opcode < randomx::ceil_FDIV_M) {
  533. instr.dst = 4 + instr.dst % randomx::RegisterCountFlt;
  534. instr.src = instr.src % randomx::RegistersCount;
  535. instr.opcode |= DST_FLT;
  536. instr.opcode |= SRC_MEM;
  537. instr.opcode |= OP_FLOAT;
  538. instr.imm32 = (instr.getModMem() ? randomx::ScratchpadL1Mask : randomx::ScratchpadL2Mask);
  539. continue;
  540. }
  541. if (opcode < randomx::ceil_FSQRT_R) {
  542. instr.dst = 4 + instr.dst % randomx::RegisterCountFlt;
  543. instr.opcode |= DST_FLT;
  544. instr.opcode |= OP_FLOAT;
  545. continue;
  546. }
  547. if (opcode < randomx::ceil_CBRANCH) {
  548. instr.opcode |= OP_BRANCH;
  549. instr.opcode |= DST_INT;
  550. int reg = instr.dst % randomx::RegistersCount;
  551. int target = registerUsage[reg].lastUsed;
  552. int offset = (i - target);
  553. instr.mod = offset;
  554. jumpCount += offset;
  555. p(target + 1).opcode |= BRANCH_TARGET;
  556. registerUsage[reg].count++;
  557. instr.dst = reg;
  558. //mark all registers as used
  559. for (unsigned j = 0; j < randomx::RegistersCount; ++j) {
  560. registerUsage[j].lastUsed = i;
  561. }
  562. continue;
  563. }
  564. if (opcode < randomx::ceil_CFROUND) {
  565. instr.src = instr.src % randomx::RegistersCount;
  566. instr.opcode |= SRC_INT;
  567. instr.opcode |= OP_CFROUND;
  568. continue;
  569. }
  570. if (opcode < randomx::ceil_ISTORE) {
  571. instr.dst = instr.dst % randomx::RegistersCount;
  572. instr.src = instr.src % randomx::RegistersCount;
  573. instr.opcode |= DST_MEM;
  574. if (instr.getModCond() < randomx::StoreL3Condition)
  575. instr.imm32 = (instr.getModMem() ? randomx::ScratchpadL1Mask : randomx::ScratchpadL2Mask);
  576. else
  577. instr.imm32 &= randomx::ScratchpadL3Mask;
  578. continue;
  579. }
  580. if (opcode < randomx::ceil_NOP) {
  581. }
  582. }
  583. return jumpCount;
  584. }