instructionsPortable.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. //#define DEBUG
  16. //#define FTZ
  17. #include "instructions.hpp"
  18. #include "intrinPortable.h"
  19. #pragma STDC FENV_ACCESS on
  20. #include <cfenv>
  21. #include <cmath>
  22. #ifdef DEBUG
  23. #include <iostream>
  24. #endif
  25. #if defined(__SIZEOF_INT128__)
  26. typedef unsigned __int128 uint128_t;
  27. typedef __int128 int128_t;
  28. static inline uint64_t __umulhi64(uint64_t a, uint64_t b) {
  29. return ((uint128_t)a * b) >> 64;
  30. }
  31. static inline uint64_t __imulhi64(int64_t a, int64_t b) {
  32. return ((int128_t)a * b) >> 64;
  33. }
  34. #define umulhi64 __umulhi64
  35. #define imulhi64 __imulhi64
  36. #endif
  37. #if defined(_MSC_VER)
  38. #define HAS_VALUE(X) X ## 0
  39. #define EVAL_DEFINE(X) HAS_VALUE(X)
  40. #include <intrin.h>
  41. #include <stdlib.h>
  42. #define ror64 _rotr64
  43. #define rol64 _rotl64
  44. #if EVAL_DEFINE(__MACHINEARM64_X64(1))
  45. #define umulhi64 __umulh
  46. #endif
  47. #if EVAL_DEFINE(__MACHINEX64(1))
  48. static inline uint64_t __imulhi64(int64_t a, int64_t b) {
  49. int64_t hi;
  50. _mul128(a, b, &hi);
  51. return hi;
  52. }
  53. #define imulhi64 __imulhi64
  54. #endif
  55. static inline uint32_t _setRoundMode(uint32_t mode) {
  56. return _controlfp(mode, _MCW_RC);
  57. }
  58. #define setRoundMode _setRoundMode
  59. #endif
  60. #ifndef setRoundMode
  61. #define setRoundMode fesetround
  62. #endif
  63. #ifndef ror64
  64. static inline uint64_t __ror64(uint64_t a, int b) {
  65. return (a >> b) | (a << (64 - b));
  66. }
  67. #define ror64 __ror64
  68. #endif
  69. #ifndef rol64
  70. static inline uint64_t __rol64(uint64_t a, int b) {
  71. return (a << b) | (a >> (64 - b));
  72. }
  73. #define rol64 __rol64
  74. #endif
  75. #ifndef sar64
  76. #include <type_traits>
  77. constexpr int64_t builtintShr64(int64_t value, int shift) noexcept {
  78. return value >> shift;
  79. }
  80. struct UsesArithmeticShift : std::integral_constant<bool, builtintShr64(-1LL, 1) == -1LL> {
  81. };
  82. static inline int64_t __sar64(int64_t a, int b) {
  83. return UsesArithmeticShift::value ? builtintShr64(a, b) : (a < 0 ? ~(~a >> b) : a >> b);
  84. }
  85. #define sar64 __sar64
  86. #endif
  87. #ifndef umulhi64
  88. #define LO(x) ((x)&0xffffffff)
  89. #define HI(x) ((x)>>32)
  90. static inline uint64_t __umulhi64(uint64_t a, uint64_t b) {
  91. uint64_t ah = HI(a), al = LO(a);
  92. uint64_t bh = HI(b), bl = LO(b);
  93. uint64_t x00 = al * bl;
  94. uint64_t x01 = al * bh;
  95. uint64_t x10 = ah * bl;
  96. uint64_t x11 = ah * bh;
  97. uint64_t m1 = LO(x10) + LO(x01) + HI(x00);
  98. uint64_t m2 = HI(x10) + HI(x01) + LO(x11) + HI(m1);
  99. uint64_t m3 = HI(x11) + HI(m2);
  100. return (m3 << 32) + LO(m2);
  101. }
  102. #define umulhi64 __umulhi64
  103. #endif
  104. #ifndef imulhi64
  105. static inline int64_t __imulhi64(int64_t a, int64_t b) {
  106. int64_t hi = umulhi64(a, b);
  107. if (a < 0LL) hi -= b;
  108. if (b < 0LL) hi -= a;
  109. return hi;
  110. }
  111. #define imulhi64 __imulhi64
  112. #endif
  113. // avoid undefined behavior of signed overflow
  114. static inline int32_t safeSub(int32_t a, int32_t b) {
  115. return int32_t(uint32_t(a) - uint32_t(b));
  116. }
  117. #if __GNUC__ >= 5
  118. #undef __has_builtin
  119. #define __has_builtin(x) 1
  120. #endif
  121. #if defined(__has_builtin)
  122. #if __has_builtin(__builtin_sub_overflow)
  123. static inline bool __subOverflow(int32_t a, int32_t b) {
  124. int32_t temp;
  125. return __builtin_sub_overflow(a, b, &temp);
  126. }
  127. #define subOverflow __subOverflow
  128. #endif
  129. #endif
  130. #ifndef subOverflow
  131. static inline bool __subOverflow(int32_t a, int32_t b) {
  132. auto c = safeSub(a, b);
  133. return (c < a) != (b > 0);
  134. }
  135. #define subOverflow __subOverflow
  136. #endif
  137. static double FlushDenormal(double x) {
  138. if (std::fpclassify(x) == FP_SUBNORMAL) {
  139. return 0;
  140. }
  141. return x;
  142. }
  143. #ifdef FTZ
  144. #undef FTZ
  145. #define FTZ(x) FlushDenormal(x)
  146. #else
  147. #define FTZ(x) x
  148. #endif
  149. namespace RandomX {
  150. extern "C" {
  151. void ADD_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  152. c.u64 = a.u64 + b.u64;
  153. }
  154. void ADD_32(convertible_t& a, convertible_t& b, convertible_t& c) {
  155. c.u64 = a.u32 + b.u32;
  156. }
  157. void SUB_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  158. c.u64 = a.u64 - b.u64;
  159. }
  160. void SUB_32(convertible_t& a, convertible_t& b, convertible_t& c) {
  161. c.u64 = a.u32 - b.u32;
  162. }
  163. void MUL_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  164. c.u64 = a.u64 * b.u64;
  165. }
  166. void MULH_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  167. c.u64 = umulhi64(a.u64, b.u64);
  168. }
  169. void MUL_32(convertible_t& a, convertible_t& b, convertible_t& c) {
  170. c.u64 = (uint64_t)a.u32 * b.u32;
  171. }
  172. void IMUL_32(convertible_t& a, convertible_t& b, convertible_t& c) {
  173. c.i64 = (int64_t)a.i32 * b.i32;
  174. }
  175. void IMULH_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  176. c.i64 = imulhi64(a.i64, b.i64);
  177. }
  178. void DIV_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  179. c.u64 = a.u64 / (b.u32 != 0 ? b.u32 : 1U);
  180. }
  181. void IDIV_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  182. if (a.i64 == INT64_MIN && b.i32 == -1)
  183. c.i64 = INT64_MIN;
  184. else
  185. c.i64 = a.i64 / (b.i32 != 0 ? b.i32 : 1);
  186. }
  187. void AND_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  188. c.u64 = a.u64 & b.u64;
  189. }
  190. void AND_32(convertible_t& a, convertible_t& b, convertible_t& c) {
  191. c.u64 = a.u32 & b.u32;
  192. }
  193. void OR_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  194. c.u64 = a.u64 | b.u64;
  195. }
  196. void OR_32(convertible_t& a, convertible_t& b, convertible_t& c) {
  197. c.u64 = a.u32 | b.u32;
  198. }
  199. void XOR_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  200. c.u64 = a.u64 ^ b.u64;
  201. }
  202. void XOR_32(convertible_t& a, convertible_t& b, convertible_t& c) {
  203. c.u64 = a.u32 ^ b.u32;
  204. }
  205. void SHL_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  206. c.u64 = a.u64 << (b.u64 & 63);
  207. }
  208. void SHR_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  209. c.u64 = a.u64 >> (b.u64 & 63);
  210. }
  211. void SAR_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  212. c.u64 = sar64(a.i64, b.u64 & 63);
  213. }
  214. void ROL_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  215. c.u64 = rol64(a.u64, (b.u64 & 63));
  216. }
  217. void ROR_64(convertible_t& a, convertible_t& b, convertible_t& c) {
  218. c.u64 = ror64(a.u64, (b.u64 & 63));
  219. }
  220. bool JMP_COND(uint8_t type, convertible_t& regb, int32_t imm32) {
  221. switch (type & 7)
  222. {
  223. case 0:
  224. return regb.u32 <= (uint32_t)imm32;
  225. case 1:
  226. return regb.u32 > (uint32_t)imm32;
  227. case 2:
  228. return safeSub(regb.i32, imm32) < 0;
  229. case 3:
  230. return safeSub(regb.i32, imm32) >= 0;
  231. case 4:
  232. return subOverflow(regb.i32, imm32);
  233. case 5:
  234. return !subOverflow(regb.i32, imm32);
  235. case 6:
  236. return regb.i32 < imm32;
  237. case 7:
  238. return regb.i32 >= imm32;
  239. }
  240. }
  241. void FPINIT() {
  242. setRoundMode(FE_TONEAREST);
  243. }
  244. void FPADD(convertible_t& a, double b, convertible_t& c) {
  245. c.f64 = FTZ(convertToDouble(a.i64) + b);
  246. }
  247. void FPSUB(convertible_t& a, double b, convertible_t& c) {
  248. c.f64 = FTZ(convertToDouble(a.i64) - b);
  249. }
  250. void FPMUL(convertible_t& a, double b, convertible_t& c) {
  251. c.f64 = FTZ(convertToDoubleNonZero(a.i64) * b);
  252. }
  253. void FPDIV(convertible_t& a, double b, convertible_t& c) {
  254. c.f64 = FTZ(convertToDoubleNonZero(a.i64) / b);
  255. }
  256. void FPSQRT(convertible_t& a, convertible_t& b, convertible_t& c) {
  257. #ifdef __SSE2__
  258. double d = convertToDoubleNonNegative(a.i64);
  259. c.f64 = _mm_cvtsd_f64(_mm_sqrt_sd(_mm_setzero_pd(), _mm_load_pd(&d)));
  260. #else
  261. c.f64 = FTZ(sqrt(convertToDoubleNonNegative(a.i64)));
  262. #endif
  263. }
  264. void FPROUND(convertible_t& a, convertible_t& b, convertible_t& c) {
  265. c.f64 = convertToDouble(a.i64);
  266. switch (a.u64 & 3) {
  267. case RoundDown:
  268. #ifdef DEBUG
  269. std::cout << "Round FE_DOWNWARD (" << FE_DOWNWARD << ") = " <<
  270. #endif
  271. setRoundMode(FE_DOWNWARD);
  272. #ifdef DEBUG
  273. std::cout << std::endl;
  274. #endif
  275. break;
  276. case RoundUp:
  277. #ifdef DEBUG
  278. std::cout << "Round FE_UPWARD (" << FE_UPWARD << ") = " <<
  279. #endif
  280. setRoundMode(FE_UPWARD);
  281. #ifdef DEBUG
  282. std::cout << std::endl;
  283. #endif
  284. break;
  285. case RoundToZero:
  286. #ifdef DEBUG
  287. std::cout << "Round FE_TOWARDZERO (" << FE_TOWARDZERO << ") = " <<
  288. #endif
  289. setRoundMode(FE_TOWARDZERO);
  290. #ifdef DEBUG
  291. std::cout << std::endl;
  292. #endif
  293. break;
  294. default:
  295. #ifdef DEBUG
  296. std::cout << "Round FE_TONEAREST (" << FE_TONEAREST << ") = " <<
  297. #endif
  298. setRoundMode(FE_TONEAREST);
  299. #ifdef DEBUG
  300. std::cout << std::endl;
  301. #endif
  302. break;
  303. }
  304. }
  305. }
  306. }