intrin_portable.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 "blake2/endian.h"
  18. constexpr int32_t unsigned32ToSigned2sCompl(uint32_t x) {
  19. return (-1 == ~0) ? (int32_t)x : (x > INT32_MAX ? (-(int32_t)(UINT32_MAX - x) - 1) : (int32_t)x);
  20. }
  21. constexpr int64_t unsigned64ToSigned2sCompl(uint64_t x) {
  22. return (-1 == ~0) ? (int64_t)x : (x > INT64_MAX ? (-(int64_t)(UINT64_MAX - x) - 1) : (int64_t)x);
  23. }
  24. constexpr uint64_t signExtend2sCompl(uint32_t x) {
  25. return (-1 == ~0) ? (int64_t)(int32_t)(x) : (x > INT32_MAX ? (x | 0xffffffff00000000ULL) : (uint64_t)x);
  26. }
  27. constexpr int RoundToNearest = 0;
  28. constexpr int RoundDown = 1;
  29. constexpr int RoundUp = 2;
  30. constexpr int RoundToZero = 3;
  31. #if defined(_MSC_VER)
  32. #if defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP == 2)
  33. #define __SSE2__ 1
  34. #endif
  35. #endif
  36. #ifdef __SSE2__
  37. #ifdef __GNUC__
  38. #include <x86intrin.h>
  39. #else
  40. #include <intrin.h>
  41. #endif
  42. typedef __m128i rx_vec_i128;
  43. typedef __m128d rx_vec_f128;
  44. #define rx_aligned_alloc(a, b) _mm_malloc(a,b)
  45. #define rx_aligned_free(a) _mm_free(a)
  46. #define rx_prefetch_nta(x) _mm_prefetch((const char *)(x), _MM_HINT_NTA)
  47. #define rx_load_vec_f128 _mm_load_pd
  48. #define rx_store_vec_f128 _mm_store_pd
  49. #define rx_shuffle_vec_f128 _mm_shuffle_pd
  50. #define rx_add_vec_f128 _mm_add_pd
  51. #define rx_sub_vec_f128 _mm_sub_pd
  52. #define rx_mul_vec_f128 _mm_mul_pd
  53. #define rx_div_vec_f128 _mm_div_pd
  54. #define rx_sqrt_vec_f128 _mm_sqrt_pd
  55. #define rx_set1_long_vec_i128 _mm_set1_epi64x
  56. #define rx_vec_i128_vec_f128 _mm_castsi128_pd
  57. FORCE_INLINE rx_vec_f128 rx_set_vec_f128(uint64_t x1, uint64_t x0) {
  58. return _mm_castsi128_pd(_mm_set_epi64x(x1, x0));
  59. }
  60. FORCE_INLINE rx_vec_f128 rx_set1_vec_f128(uint64_t x) {
  61. return _mm_castsi128_pd(_mm_set1_epi64x(x));
  62. }
  63. #define rx_xor_vec_f128 _mm_xor_pd
  64. #define rx_and_vec_f128 _mm_and_pd
  65. #define rx_or_vec_f128 _mm_or_pd
  66. #define rx_aesenc_vec_i128 _mm_aesenc_si128
  67. #define rx_aesdec_vec_i128 _mm_aesdec_si128
  68. FORCE_INLINE int rx_vec_i128_x(rx_vec_i128 a) {
  69. return _mm_cvtsi128_si32(a);
  70. }
  71. FORCE_INLINE int rx_vec_i128_y(rx_vec_i128 a) {
  72. return _mm_cvtsi128_si32(_mm_shuffle_epi32(a, 0x55));
  73. }
  74. FORCE_INLINE int rx_vec_i128_z(rx_vec_i128 a) {
  75. return _mm_cvtsi128_si32(_mm_shuffle_epi32(a, 0xaa));
  76. }
  77. FORCE_INLINE int rx_vec_i128_w(rx_vec_i128 a) {
  78. return _mm_cvtsi128_si32(_mm_shuffle_epi32(a, 0xff));
  79. }
  80. #define rx_set_int_vec_i128 _mm_set_epi32
  81. #define rx_xor_vec_i128 _mm_xor_si128
  82. #define rx_load_vec_i128 _mm_load_si128
  83. #define rx_store_vec_i128 _mm_store_si128
  84. FORCE_INLINE rx_vec_f128 rx_cvt_packed_int_vec_f128(const void* addr) {
  85. __m128i ix = _mm_loadl_epi64((const __m128i*)addr);
  86. return _mm_cvtepi32_pd(ix);
  87. }
  88. constexpr uint32_t rx_mxcsr_default = 0x9FC0; //Flush to zero, denormals are zero, default rounding mode, all exceptions disabled
  89. FORCE_INLINE void rx_reset_float_state() {
  90. _mm_setcsr(rx_mxcsr_default);
  91. }
  92. FORCE_INLINE void rx_set_rounding_mode(uint32_t mode) {
  93. _mm_setcsr(rx_mxcsr_default | (mode << 13));
  94. }
  95. #else
  96. #include <cstdint>
  97. #include <stdexcept>
  98. #include <cstdlib>
  99. #include <cmath>
  100. typedef union {
  101. uint64_t u64[2];
  102. uint32_t u32[4];
  103. uint16_t u16[8];
  104. uint8_t u8[16];
  105. } rx_vec_i128;
  106. typedef union {
  107. struct {
  108. double lo;
  109. double hi;
  110. };
  111. rx_vec_i128 i;
  112. } rx_vec_f128;
  113. #define rx_aligned_alloc(a, b) malloc(a)
  114. #define rx_aligned_free(a) free(a)
  115. #define rx_prefetch_nta(x)
  116. FORCE_INLINE rx_vec_f128 rx_load_vec_f128(const double* pd) {
  117. rx_vec_f128 x;
  118. x.i.u64[0] = load64(pd + 0);
  119. x.i.u64[1] = load64(pd + 1);
  120. return x;
  121. }
  122. FORCE_INLINE void rx_store_vec_f128(double* mem_addr, rx_vec_f128 a) {
  123. store64(mem_addr + 0, a.i.u64[0]);
  124. store64(mem_addr + 1, a.i.u64[1]);
  125. }
  126. FORCE_INLINE rx_vec_f128 rx_shuffle_vec_f128(rx_vec_f128 a, rx_vec_f128 b, int imm8) {
  127. rx_vec_f128 x;
  128. x.lo = (imm8 & 1) ? a.hi : a.lo;
  129. x.hi = (imm8 & 2) ? b.hi : b.lo;
  130. return x;
  131. }
  132. FORCE_INLINE rx_vec_f128 rx_add_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  133. rx_vec_f128 x;
  134. x.lo = a.lo + b.lo;
  135. x.hi = a.hi + b.hi;
  136. return x;
  137. }
  138. FORCE_INLINE rx_vec_f128 rx_sub_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  139. rx_vec_f128 x;
  140. x.lo = a.lo - b.lo;
  141. x.hi = a.hi - b.hi;
  142. return x;
  143. }
  144. FORCE_INLINE rx_vec_f128 rx_mul_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  145. rx_vec_f128 x;
  146. x.lo = a.lo * b.lo;
  147. x.hi = a.hi * b.hi;
  148. return x;
  149. }
  150. FORCE_INLINE rx_vec_f128 rx_div_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  151. rx_vec_f128 x;
  152. x.lo = a.lo / b.lo;
  153. x.hi = a.hi / b.hi;
  154. return x;
  155. }
  156. FORCE_INLINE rx_vec_f128 rx_sqrt_vec_f128(rx_vec_f128 a) {
  157. rx_vec_f128 x;
  158. x.lo = sqrt(a.lo);
  159. x.hi = sqrt(a.hi);
  160. return x;
  161. }
  162. FORCE_INLINE rx_vec_i128 rx_set1_long_vec_i128(uint64_t a) {
  163. rx_vec_i128 x;
  164. x.u64[0] = a;
  165. x.u64[1] = a;
  166. return x;
  167. }
  168. FORCE_INLINE rx_vec_f128 rx_vec_i128_vec_f128(rx_vec_i128 a) {
  169. rx_vec_f128 x;
  170. x.i = a;
  171. return x;
  172. }
  173. FORCE_INLINE rx_vec_f128 rx_set_vec_f128(uint64_t x1, uint64_t x0) {
  174. rx_vec_f128 v;
  175. v.i.u64[0] = x0;
  176. v.i.u64[1] = x1;
  177. return v;
  178. }
  179. FORCE_INLINE rx_vec_f128 rx_set1_vec_f128(uint64_t x) {
  180. rx_vec_f128 v;
  181. v.i.u64[0] = x;
  182. v.i.u64[1] = x;
  183. return v;
  184. }
  185. FORCE_INLINE rx_vec_f128 rx_xor_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  186. rx_vec_f128 x;
  187. x.i.u64[0] = a.i.u64[0] ^ b.i.u64[0];
  188. x.i.u64[1] = a.i.u64[1] ^ b.i.u64[1];
  189. return x;
  190. }
  191. FORCE_INLINE rx_vec_f128 rx_and_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  192. rx_vec_f128 x;
  193. x.i.u64[0] = a.i.u64[0] & b.i.u64[0];
  194. x.i.u64[1] = a.i.u64[1] & b.i.u64[1];
  195. return x;
  196. }
  197. FORCE_INLINE rx_vec_f128 rx_or_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  198. rx_vec_f128 x;
  199. x.i.u64[0] = a.i.u64[0] | b.i.u64[0];
  200. x.i.u64[1] = a.i.u64[1] | b.i.u64[1];
  201. return x;
  202. }
  203. static const char* platformError = "Platform doesn't support hardware AES";
  204. FORCE_INLINE rx_vec_i128 rx_aesenc_vec_i128(rx_vec_i128 v, rx_vec_i128 rkey) {
  205. throw std::runtime_error(platformError);
  206. }
  207. FORCE_INLINE rx_vec_i128 rx_aesdec_vec_i128(rx_vec_i128 v, rx_vec_i128 rkey) {
  208. throw std::runtime_error(platformError);
  209. }
  210. FORCE_INLINE int rx_vec_i128_x(rx_vec_i128 a) {
  211. return a.u32[0];
  212. }
  213. FORCE_INLINE int rx_vec_i128_y(rx_vec_i128 a) {
  214. return a.u32[1];
  215. }
  216. FORCE_INLINE int rx_vec_i128_z(rx_vec_i128 a) {
  217. return a.u32[2];
  218. }
  219. FORCE_INLINE int rx_vec_i128_w(rx_vec_i128 a) {
  220. return a.u32[3];
  221. }
  222. FORCE_INLINE rx_vec_i128 rx_set_int_vec_i128(int _I3, int _I2, int _I1, int _I0) {
  223. rx_vec_i128 v;
  224. v.u32[0] = _I0;
  225. v.u32[1] = _I1;
  226. v.u32[2] = _I2;
  227. v.u32[3] = _I3;
  228. return v;
  229. };
  230. FORCE_INLINE rx_vec_i128 rx_xor_vec_i128(rx_vec_i128 _A, rx_vec_i128 _B) {
  231. rx_vec_i128 c;
  232. c.u32[0] = _A.u32[0] ^ _B.u32[0];
  233. c.u32[1] = _A.u32[1] ^ _B.u32[1];
  234. c.u32[2] = _A.u32[2] ^ _B.u32[2];
  235. c.u32[3] = _A.u32[3] ^ _B.u32[3];
  236. return c;
  237. }
  238. FORCE_INLINE rx_vec_i128 rx_load_vec_i128(rx_vec_i128 const*_P) {
  239. #if defined(NATIVE_LITTLE_ENDIAN)
  240. return *_P;
  241. #else
  242. uint32_t* ptr = (uint32_t*)_P;
  243. rx_vec_i128 c;
  244. c.u32[0] = load32(ptr + 0);
  245. c.u32[1] = load32(ptr + 1);
  246. c.u32[2] = load32(ptr + 2);
  247. c.u32[3] = load32(ptr + 3);
  248. return c;
  249. #endif
  250. }
  251. FORCE_INLINE void rx_store_vec_i128(rx_vec_i128 *_P, rx_vec_i128 _B) {
  252. #if defined(NATIVE_LITTLE_ENDIAN)
  253. *_P = _B;
  254. #else
  255. uint32_t* ptr = (uint32_t*)_P;
  256. store32(ptr + 0, _B.u32[0]);
  257. store32(ptr + 1, _B.u32[1]);
  258. store32(ptr + 2, _B.u32[2]);
  259. store32(ptr + 3, _B.u32[3]);
  260. #endif
  261. }
  262. FORCE_INLINE rx_vec_f128 rx_cvt_packed_int_vec_f128(const void* addr) {
  263. rx_vec_f128 x;
  264. x.lo = (double)unsigned32ToSigned2sCompl(load32((uint8_t*)addr + 0));
  265. x.hi = (double)unsigned32ToSigned2sCompl(load32((uint8_t*)addr + 4));
  266. return x;
  267. }
  268. #define RANDOMX_DEFAULT_FENV
  269. void rx_reset_float_state();
  270. void rx_set_rounding_mode(uint32_t mode);
  271. #endif
  272. double loadDoublePortable(const void* addr);
  273. uint64_t mulh(uint64_t, uint64_t);
  274. int64_t smulh(int64_t, int64_t);
  275. uint64_t rotl(uint64_t, int);
  276. uint64_t rotr(uint64_t, int);