intrin_portable.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. #pragma once
  26. #include <cstdint>
  27. #include "blake2/endian.h"
  28. constexpr int32_t unsigned32ToSigned2sCompl(uint32_t x) {
  29. return (-1 == ~0) ? (int32_t)x : (x > INT32_MAX ? (-(int32_t)(UINT32_MAX - x) - 1) : (int32_t)x);
  30. }
  31. constexpr int64_t unsigned64ToSigned2sCompl(uint64_t x) {
  32. return (-1 == ~0) ? (int64_t)x : (x > INT64_MAX ? (-(int64_t)(UINT64_MAX - x) - 1) : (int64_t)x);
  33. }
  34. constexpr uint64_t signExtend2sCompl(uint32_t x) {
  35. return (-1 == ~0) ? (int64_t)(int32_t)(x) : (x > INT32_MAX ? (x | 0xffffffff00000000ULL) : (uint64_t)x);
  36. }
  37. constexpr int RoundToNearest = 0;
  38. constexpr int RoundDown = 1;
  39. constexpr int RoundUp = 2;
  40. constexpr int RoundToZero = 3;
  41. //MSVC doesn't define __SSE2__, so we have to define it manually if SSE2 is available
  42. #if !defined(__SSE2__) && (defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP == 2))
  43. #define __SSE2__ 1
  44. #endif
  45. //MSVC doesn't define __AES__
  46. #if defined(_MSC_VER) && defined(__SSE2__)
  47. #define __AES__
  48. #endif
  49. //the library "sqrt" function provided by MSVC for x86 targets doesn't give
  50. //the correct results, so we have to use inline assembly to call x87 fsqrt directly
  51. #if !defined(__SSE2__)
  52. #if defined(_MSC_VER) && defined(_M_IX86)
  53. inline double __cdecl rx_sqrt(double x) {
  54. __asm {
  55. fld x
  56. fsqrt
  57. }
  58. }
  59. #define rx_sqrt rx_sqrt
  60. void rx_set_double_precision();
  61. #define RANDOMX_USE_X87
  62. #elif defined(__i386)
  63. void rx_set_double_precision();
  64. #define RANDOMX_USE_X87
  65. #endif
  66. #endif //__SSE2__
  67. #if !defined(rx_sqrt)
  68. #define rx_sqrt sqrt
  69. #endif
  70. #if !defined(RANDOMX_USE_X87)
  71. #define rx_set_double_precision(x)
  72. #endif
  73. #ifdef __SSE2__
  74. #ifdef __GNUC__
  75. #include <x86intrin.h>
  76. #else
  77. #include <intrin.h>
  78. #endif
  79. typedef __m128i rx_vec_i128;
  80. typedef __m128d rx_vec_f128;
  81. #define rx_aligned_alloc(a, b) _mm_malloc(a,b)
  82. #define rx_aligned_free(a) _mm_free(a)
  83. #define rx_prefetch_nta(x) _mm_prefetch((const char *)(x), _MM_HINT_NTA)
  84. #define rx_prefetch_t0(x) _mm_prefetch((const char *)(x), _MM_HINT_T0)
  85. #define rx_load_vec_f128 _mm_load_pd
  86. #define rx_store_vec_f128 _mm_store_pd
  87. #define rx_add_vec_f128 _mm_add_pd
  88. #define rx_sub_vec_f128 _mm_sub_pd
  89. #define rx_mul_vec_f128 _mm_mul_pd
  90. #define rx_div_vec_f128 _mm_div_pd
  91. #define rx_sqrt_vec_f128 _mm_sqrt_pd
  92. FORCE_INLINE rx_vec_f128 rx_swap_vec_f128(rx_vec_f128 a) {
  93. return _mm_shuffle_pd(a, a, 1);
  94. }
  95. FORCE_INLINE rx_vec_f128 rx_set_vec_f128(uint64_t x1, uint64_t x0) {
  96. return _mm_castsi128_pd(_mm_set_epi64x(x1, x0));
  97. }
  98. FORCE_INLINE rx_vec_f128 rx_set1_vec_f128(uint64_t x) {
  99. return _mm_castsi128_pd(_mm_set1_epi64x(x));
  100. }
  101. #define rx_xor_vec_f128 _mm_xor_pd
  102. #define rx_and_vec_f128 _mm_and_pd
  103. #define rx_or_vec_f128 _mm_or_pd
  104. #ifdef __AES__
  105. #define rx_aesenc_vec_i128 _mm_aesenc_si128
  106. #define rx_aesdec_vec_i128 _mm_aesdec_si128
  107. #define HAVE_AES 1
  108. #endif //__AES__
  109. FORCE_INLINE int rx_vec_i128_x(rx_vec_i128 a) {
  110. return _mm_cvtsi128_si32(a);
  111. }
  112. FORCE_INLINE int rx_vec_i128_y(rx_vec_i128 a) {
  113. return _mm_cvtsi128_si32(_mm_shuffle_epi32(a, 0x55));
  114. }
  115. FORCE_INLINE int rx_vec_i128_z(rx_vec_i128 a) {
  116. return _mm_cvtsi128_si32(_mm_shuffle_epi32(a, 0xaa));
  117. }
  118. FORCE_INLINE int rx_vec_i128_w(rx_vec_i128 a) {
  119. return _mm_cvtsi128_si32(_mm_shuffle_epi32(a, 0xff));
  120. }
  121. #define rx_set_int_vec_i128 _mm_set_epi32
  122. #define rx_xor_vec_i128 _mm_xor_si128
  123. #define rx_load_vec_i128 _mm_load_si128
  124. #define rx_store_vec_i128 _mm_store_si128
  125. FORCE_INLINE rx_vec_f128 rx_cvt_packed_int_vec_f128(const void* addr) {
  126. __m128i ix = _mm_loadl_epi64((const __m128i*)addr);
  127. return _mm_cvtepi32_pd(ix);
  128. }
  129. constexpr uint32_t rx_mxcsr_default = 0x9FC0; //Flush to zero, denormals are zero, default rounding mode, all exceptions disabled
  130. FORCE_INLINE void rx_reset_float_state() {
  131. _mm_setcsr(rx_mxcsr_default);
  132. }
  133. FORCE_INLINE void rx_set_rounding_mode(uint32_t mode) {
  134. _mm_setcsr(rx_mxcsr_default | (mode << 13));
  135. }
  136. FORCE_INLINE uint32_t rx_get_rounding_mode() {
  137. return (_mm_getcsr() >> 13) & 3;
  138. }
  139. #elif defined(__PPC64__) && defined(__ALTIVEC__) && defined(__VSX__) //sadly only POWER7 and newer will be able to use SIMD acceleration. Earlier processors cant use doubles or 64 bit integers with SIMD
  140. #include <cstdint>
  141. #include <stdexcept>
  142. #include <cstdlib>
  143. #include <altivec.h>
  144. #undef vector
  145. #undef pixel
  146. #undef bool
  147. typedef __vector uint8_t __m128i;
  148. typedef __vector uint32_t __m128l;
  149. typedef __vector int __m128li;
  150. typedef __vector uint64_t __m128ll;
  151. typedef __vector double __m128d;
  152. typedef __m128i rx_vec_i128;
  153. typedef __m128d rx_vec_f128;
  154. typedef union{
  155. rx_vec_i128 i;
  156. rx_vec_f128 d;
  157. uint64_t u64[2];
  158. double d64[2];
  159. uint32_t u32[4];
  160. int i32[4];
  161. } vec_u;
  162. #define rx_aligned_alloc(a, b) malloc(a)
  163. #define rx_aligned_free(a) free(a)
  164. #define rx_prefetch_nta(x)
  165. #define rx_prefetch_t0(x)
  166. /* Splat 64-bit long long to 2 64-bit long longs */
  167. FORCE_INLINE __m128i vec_splat2sd (int64_t scalar)
  168. { return (__m128i) vec_splats (scalar); }
  169. FORCE_INLINE rx_vec_f128 rx_load_vec_f128(const double* pd) {
  170. #if defined(NATIVE_LITTLE_ENDIAN)
  171. return (rx_vec_f128)vec_vsx_ld(0,pd);
  172. #else
  173. vec_u t;
  174. t.u64[0] = load64(pd + 0);
  175. t.u64[1] = load64(pd + 1);
  176. return (rx_vec_f128)t.d;
  177. #endif
  178. }
  179. FORCE_INLINE void rx_store_vec_f128(double* mem_addr, rx_vec_f128 a) {
  180. #if defined(NATIVE_LITTLE_ENDIAN)
  181. vec_vsx_st(a,0,(rx_vec_f128*)mem_addr);
  182. #else
  183. vec_u _a;
  184. _a.d = a;
  185. store64(mem_addr + 0, _a.u64[0]);
  186. store64(mem_addr + 1, _a.u64[1]);
  187. #endif
  188. }
  189. FORCE_INLINE rx_vec_f128 rx_swap_vec_f128(rx_vec_f128 a) {
  190. return (rx_vec_f128)vec_perm((__m128i)a,(__m128i)a,(__m128i){8,9,10,11,12,13,14,15,0,1,2,3,4,5,6,7});
  191. }
  192. FORCE_INLINE rx_vec_f128 rx_add_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  193. return (rx_vec_f128)vec_add(a,b);
  194. }
  195. FORCE_INLINE rx_vec_f128 rx_sub_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  196. return (rx_vec_f128)vec_sub(a,b);
  197. }
  198. FORCE_INLINE rx_vec_f128 rx_mul_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  199. return (rx_vec_f128)vec_mul(a,b);
  200. }
  201. FORCE_INLINE rx_vec_f128 rx_div_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  202. return (rx_vec_f128)vec_div(a,b);
  203. }
  204. FORCE_INLINE rx_vec_f128 rx_sqrt_vec_f128(rx_vec_f128 a) {
  205. return (rx_vec_f128)vec_sqrt(a);
  206. }
  207. FORCE_INLINE rx_vec_i128 rx_set1_long_vec_i128(uint64_t a) {
  208. return (rx_vec_i128)vec_splat2sd(a);
  209. }
  210. FORCE_INLINE rx_vec_f128 rx_vec_i128_vec_f128(rx_vec_i128 a) {
  211. return (rx_vec_f128)a;
  212. }
  213. FORCE_INLINE rx_vec_f128 rx_set_vec_f128(uint64_t x1, uint64_t x0) {
  214. return (rx_vec_f128)(__m128ll){x0,x1};
  215. }
  216. FORCE_INLINE rx_vec_f128 rx_set1_vec_f128(uint64_t x) {
  217. return (rx_vec_f128)vec_splat2sd(x);
  218. }
  219. FORCE_INLINE rx_vec_f128 rx_xor_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  220. return (rx_vec_f128)vec_xor(a,b);
  221. }
  222. FORCE_INLINE rx_vec_f128 rx_and_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  223. return (rx_vec_f128)vec_and(a,b);
  224. }
  225. FORCE_INLINE rx_vec_f128 rx_or_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  226. return (rx_vec_f128)vec_or(a,b);
  227. }
  228. #if defined(__CRYPTO__)
  229. FORCE_INLINE __m128ll vrev(__m128i v){
  230. #if defined(NATIVE_LITTLE_ENDIAN)
  231. return (__m128ll)vec_perm((__m128i)v,(__m128i){0},(__m128i){15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0});
  232. #else
  233. return (__m128ll)vec_perm((__m128i)v,(__m128i){0},(__m128i){3,2,1,0, 7,6,5,4, 11,10,9,8, 15,14,13,12});
  234. #endif
  235. }
  236. FORCE_INLINE rx_vec_i128 rx_aesenc_vec_i128(rx_vec_i128 v, rx_vec_i128 rkey) {
  237. __m128ll _v = vrev(v);
  238. __m128ll _rkey = vrev(rkey);
  239. __m128ll result = vrev((__m128i)__builtin_crypto_vcipher(_v,_rkey));
  240. return (rx_vec_i128)result;
  241. }
  242. FORCE_INLINE rx_vec_i128 rx_aesdec_vec_i128(rx_vec_i128 v, rx_vec_i128 rkey) {
  243. __m128ll _v = vrev(v);
  244. __m128ll zero = (__m128ll){0};
  245. __m128ll out = vrev((__m128i)__builtin_crypto_vncipher(_v,zero));
  246. return (rx_vec_i128)vec_xor((__m128i)out,rkey);
  247. }
  248. #define HAVE_AES 1
  249. #endif //__CRYPTO__
  250. FORCE_INLINE int rx_vec_i128_x(rx_vec_i128 a) {
  251. vec_u _a;
  252. _a.i = a;
  253. return _a.i32[0];
  254. }
  255. FORCE_INLINE int rx_vec_i128_y(rx_vec_i128 a) {
  256. vec_u _a;
  257. _a.i = a;
  258. return _a.i32[1];
  259. }
  260. FORCE_INLINE int rx_vec_i128_z(rx_vec_i128 a) {
  261. vec_u _a;
  262. _a.i = a;
  263. return _a.i32[2];
  264. }
  265. FORCE_INLINE int rx_vec_i128_w(rx_vec_i128 a) {
  266. vec_u _a;
  267. _a.i = a;
  268. return _a.i32[3];
  269. }
  270. FORCE_INLINE rx_vec_i128 rx_set_int_vec_i128(int i3, int i2, int i1, int i0) {
  271. return (rx_vec_i128)((__m128li){i0,i1,i2,i3});
  272. };
  273. FORCE_INLINE rx_vec_i128 rx_xor_vec_i128(rx_vec_i128 a, rx_vec_i128 b) {
  274. return (rx_vec_i128)vec_xor(a,b);
  275. }
  276. FORCE_INLINE rx_vec_i128 rx_load_vec_i128(rx_vec_i128 const *p) {
  277. #if defined(NATIVE_LITTLE_ENDIAN)
  278. return *p;
  279. #else
  280. const uint32_t* ptr = (const uint32_t*)p;
  281. vec_u c;
  282. c.u32[0] = load32(ptr + 0);
  283. c.u32[1] = load32(ptr + 1);
  284. c.u32[2] = load32(ptr + 2);
  285. c.u32[3] = load32(ptr + 3);
  286. return (rx_vec_i128)c.i;
  287. #endif
  288. }
  289. FORCE_INLINE void rx_store_vec_i128(rx_vec_i128 *p, rx_vec_i128 b) {
  290. #if defined(NATIVE_LITTLE_ENDIAN)
  291. *p = b;
  292. #else
  293. uint32_t* ptr = (uint32_t*)p;
  294. vec_u B;
  295. B.i = b;
  296. store32(ptr + 0, B.u32[0]);
  297. store32(ptr + 1, B.u32[1]);
  298. store32(ptr + 2, B.u32[2]);
  299. store32(ptr + 3, B.u32[3]);
  300. #endif
  301. }
  302. FORCE_INLINE rx_vec_f128 rx_cvt_packed_int_vec_f128(const void* addr) {
  303. vec_u x;
  304. x.d64[0] = (double)unsigned32ToSigned2sCompl(load32((const uint8_t*)addr + 0));
  305. x.d64[1] = (double)unsigned32ToSigned2sCompl(load32((const uint8_t*)addr + 4));
  306. return (rx_vec_f128)x.d;
  307. }
  308. #define RANDOMX_DEFAULT_FENV
  309. #elif defined(__aarch64__)
  310. #include <stdlib.h>
  311. #include <arm_neon.h>
  312. #include <arm_acle.h>
  313. typedef uint8x16_t rx_vec_i128;
  314. typedef float64x2_t rx_vec_f128;
  315. inline void* rx_aligned_alloc(size_t size, size_t align) {
  316. void* p;
  317. if (posix_memalign(&p, align, size) == 0)
  318. return p;
  319. return 0;
  320. };
  321. #define rx_aligned_free(a) free(a)
  322. inline void rx_prefetch_nta(void* ptr) {
  323. asm volatile ("prfm pldl1strm, [%0]\n" : : "r" (ptr));
  324. }
  325. inline void rx_prefetch_t0(const void* ptr) {
  326. asm volatile ("prfm pldl1strm, [%0]\n" : : "r" (ptr));
  327. }
  328. FORCE_INLINE rx_vec_f128 rx_load_vec_f128(const double* pd) {
  329. return vld1q_f64((const float64_t*)pd);
  330. }
  331. FORCE_INLINE void rx_store_vec_f128(double* mem_addr, rx_vec_f128 val) {
  332. vst1q_f64((float64_t*)mem_addr, val);
  333. }
  334. FORCE_INLINE rx_vec_f128 rx_swap_vec_f128(rx_vec_f128 a) {
  335. float64x2_t temp;
  336. temp = vcopyq_laneq_f64(temp, 1, a, 1);
  337. a = vcopyq_laneq_f64(a, 1, a, 0);
  338. return vcopyq_laneq_f64(a, 0, temp, 1);
  339. }
  340. FORCE_INLINE rx_vec_f128 rx_set_vec_f128(uint64_t x1, uint64_t x0) {
  341. uint64x2_t temp0 = vdupq_n_u64(x0);
  342. uint64x2_t temp1 = vdupq_n_u64(x1);
  343. return vreinterpretq_f64_u64(vcopyq_laneq_u64(temp0, 1, temp1, 0));
  344. }
  345. FORCE_INLINE rx_vec_f128 rx_set1_vec_f128(uint64_t x) {
  346. return vreinterpretq_f64_u64(vdupq_n_u64(x));
  347. }
  348. #define rx_add_vec_f128 vaddq_f64
  349. #define rx_sub_vec_f128 vsubq_f64
  350. #define rx_mul_vec_f128 vmulq_f64
  351. #define rx_div_vec_f128 vdivq_f64
  352. #define rx_sqrt_vec_f128 vsqrtq_f64
  353. FORCE_INLINE rx_vec_f128 rx_xor_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  354. return vreinterpretq_f64_u8(veorq_u8(vreinterpretq_u8_f64(a), vreinterpretq_u8_f64(b)));
  355. }
  356. FORCE_INLINE rx_vec_f128 rx_and_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  357. return vreinterpretq_f64_u8(vandq_u8(vreinterpretq_u8_f64(a), vreinterpretq_u8_f64(b)));
  358. }
  359. FORCE_INLINE rx_vec_f128 rx_or_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  360. return vreinterpretq_f64_u8(vorrq_u8(vreinterpretq_u8_f64(a), vreinterpretq_u8_f64(b)));
  361. }
  362. #ifdef __ARM_FEATURE_CRYPTO
  363. FORCE_INLINE rx_vec_i128 rx_aesenc_vec_i128(rx_vec_i128 a, rx_vec_i128 key) {
  364. const uint8x16_t zero = { 0 };
  365. return vaesmcq_u8(vaeseq_u8(a, zero)) ^ key;
  366. }
  367. FORCE_INLINE rx_vec_i128 rx_aesdec_vec_i128(rx_vec_i128 a, rx_vec_i128 key) {
  368. const uint8x16_t zero = { 0 };
  369. return vaesimcq_u8(vaesdq_u8(a, zero)) ^ key;
  370. }
  371. #define HAVE_AES 1
  372. #endif
  373. #define rx_xor_vec_i128 veorq_u8
  374. FORCE_INLINE int rx_vec_i128_x(rx_vec_i128 a) {
  375. return vgetq_lane_s32(vreinterpretq_s32_u8(a), 0);
  376. }
  377. FORCE_INLINE int rx_vec_i128_y(rx_vec_i128 a) {
  378. return vgetq_lane_s32(vreinterpretq_s32_u8(a), 1);
  379. }
  380. FORCE_INLINE int rx_vec_i128_z(rx_vec_i128 a) {
  381. return vgetq_lane_s32(vreinterpretq_s32_u8(a), 2);
  382. }
  383. FORCE_INLINE int rx_vec_i128_w(rx_vec_i128 a) {
  384. return vgetq_lane_s32(vreinterpretq_s32_u8(a), 3);
  385. }
  386. FORCE_INLINE rx_vec_i128 rx_set_int_vec_i128(int i3, int i2, int i1, int i0) {
  387. int32_t data[4];
  388. data[0] = i0;
  389. data[1] = i1;
  390. data[2] = i2;
  391. data[3] = i3;
  392. return vreinterpretq_u8_s32(vld1q_s32(data));
  393. };
  394. #define rx_xor_vec_i128 veorq_u8
  395. FORCE_INLINE rx_vec_i128 rx_load_vec_i128(const rx_vec_i128* mem_addr) {
  396. return vld1q_u8((const uint8_t*)mem_addr);
  397. }
  398. FORCE_INLINE void rx_store_vec_i128(rx_vec_i128* mem_addr, rx_vec_i128 val) {
  399. vst1q_u8((uint8_t*)mem_addr, val);
  400. }
  401. FORCE_INLINE rx_vec_f128 rx_cvt_packed_int_vec_f128(const void* addr) {
  402. double lo = unsigned32ToSigned2sCompl(load32((uint8_t*)addr + 0));
  403. double hi = unsigned32ToSigned2sCompl(load32((uint8_t*)addr + 4));
  404. rx_vec_f128 x;
  405. x = vsetq_lane_f64(lo, x, 0);
  406. x = vsetq_lane_f64(hi, x, 1);
  407. return x;
  408. }
  409. #define RANDOMX_DEFAULT_FENV
  410. #else //portable fallback
  411. #include <cstdint>
  412. #include <stdexcept>
  413. #include <cstdlib>
  414. #include <cmath>
  415. typedef union {
  416. uint64_t u64[2];
  417. uint32_t u32[4];
  418. uint16_t u16[8];
  419. uint8_t u8[16];
  420. } rx_vec_i128;
  421. typedef union {
  422. struct {
  423. double lo;
  424. double hi;
  425. };
  426. rx_vec_i128 i;
  427. } rx_vec_f128;
  428. #define rx_aligned_alloc(a, b) malloc(a)
  429. #define rx_aligned_free(a) free(a)
  430. #define rx_prefetch_nta(x)
  431. #define rx_prefetch_t0(x)
  432. FORCE_INLINE rx_vec_f128 rx_load_vec_f128(const double* pd) {
  433. rx_vec_f128 x;
  434. x.i.u64[0] = load64(pd + 0);
  435. x.i.u64[1] = load64(pd + 1);
  436. return x;
  437. }
  438. FORCE_INLINE void rx_store_vec_f128(double* mem_addr, rx_vec_f128 a) {
  439. store64(mem_addr + 0, a.i.u64[0]);
  440. store64(mem_addr + 1, a.i.u64[1]);
  441. }
  442. FORCE_INLINE rx_vec_f128 rx_swap_vec_f128(rx_vec_f128 a) {
  443. double temp = a.hi;
  444. a.hi = a.lo;
  445. a.lo = temp;
  446. return a;
  447. }
  448. FORCE_INLINE rx_vec_f128 rx_add_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  449. rx_vec_f128 x;
  450. x.lo = a.lo + b.lo;
  451. x.hi = a.hi + b.hi;
  452. return x;
  453. }
  454. FORCE_INLINE rx_vec_f128 rx_sub_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  455. rx_vec_f128 x;
  456. x.lo = a.lo - b.lo;
  457. x.hi = a.hi - b.hi;
  458. return x;
  459. }
  460. FORCE_INLINE rx_vec_f128 rx_mul_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  461. rx_vec_f128 x;
  462. x.lo = a.lo * b.lo;
  463. x.hi = a.hi * b.hi;
  464. return x;
  465. }
  466. FORCE_INLINE rx_vec_f128 rx_div_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  467. rx_vec_f128 x;
  468. x.lo = a.lo / b.lo;
  469. x.hi = a.hi / b.hi;
  470. return x;
  471. }
  472. FORCE_INLINE rx_vec_f128 rx_sqrt_vec_f128(rx_vec_f128 a) {
  473. rx_vec_f128 x;
  474. x.lo = rx_sqrt(a.lo);
  475. x.hi = rx_sqrt(a.hi);
  476. return x;
  477. }
  478. FORCE_INLINE rx_vec_i128 rx_set1_long_vec_i128(uint64_t a) {
  479. rx_vec_i128 x;
  480. x.u64[0] = a;
  481. x.u64[1] = a;
  482. return x;
  483. }
  484. FORCE_INLINE rx_vec_f128 rx_vec_i128_vec_f128(rx_vec_i128 a) {
  485. rx_vec_f128 x;
  486. x.i = a;
  487. return x;
  488. }
  489. FORCE_INLINE rx_vec_f128 rx_set_vec_f128(uint64_t x1, uint64_t x0) {
  490. rx_vec_f128 v;
  491. v.i.u64[0] = x0;
  492. v.i.u64[1] = x1;
  493. return v;
  494. }
  495. FORCE_INLINE rx_vec_f128 rx_set1_vec_f128(uint64_t x) {
  496. rx_vec_f128 v;
  497. v.i.u64[0] = x;
  498. v.i.u64[1] = x;
  499. return v;
  500. }
  501. FORCE_INLINE rx_vec_f128 rx_xor_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  502. rx_vec_f128 x;
  503. x.i.u64[0] = a.i.u64[0] ^ b.i.u64[0];
  504. x.i.u64[1] = a.i.u64[1] ^ b.i.u64[1];
  505. return x;
  506. }
  507. FORCE_INLINE rx_vec_f128 rx_and_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  508. rx_vec_f128 x;
  509. x.i.u64[0] = a.i.u64[0] & b.i.u64[0];
  510. x.i.u64[1] = a.i.u64[1] & b.i.u64[1];
  511. return x;
  512. }
  513. FORCE_INLINE rx_vec_f128 rx_or_vec_f128(rx_vec_f128 a, rx_vec_f128 b) {
  514. rx_vec_f128 x;
  515. x.i.u64[0] = a.i.u64[0] | b.i.u64[0];
  516. x.i.u64[1] = a.i.u64[1] | b.i.u64[1];
  517. return x;
  518. }
  519. FORCE_INLINE int rx_vec_i128_x(rx_vec_i128 a) {
  520. return a.u32[0];
  521. }
  522. FORCE_INLINE int rx_vec_i128_y(rx_vec_i128 a) {
  523. return a.u32[1];
  524. }
  525. FORCE_INLINE int rx_vec_i128_z(rx_vec_i128 a) {
  526. return a.u32[2];
  527. }
  528. FORCE_INLINE int rx_vec_i128_w(rx_vec_i128 a) {
  529. return a.u32[3];
  530. }
  531. FORCE_INLINE rx_vec_i128 rx_set_int_vec_i128(int i3, int i2, int i1, int i0) {
  532. rx_vec_i128 v;
  533. v.u32[0] = i0;
  534. v.u32[1] = i1;
  535. v.u32[2] = i2;
  536. v.u32[3] = i3;
  537. return v;
  538. };
  539. FORCE_INLINE rx_vec_i128 rx_xor_vec_i128(rx_vec_i128 a, rx_vec_i128 b) {
  540. rx_vec_i128 c;
  541. c.u32[0] = a.u32[0] ^ b.u32[0];
  542. c.u32[1] = a.u32[1] ^ b.u32[1];
  543. c.u32[2] = a.u32[2] ^ b.u32[2];
  544. c.u32[3] = a.u32[3] ^ b.u32[3];
  545. return c;
  546. }
  547. FORCE_INLINE rx_vec_i128 rx_load_vec_i128(rx_vec_i128 const* p) {
  548. #if defined(NATIVE_LITTLE_ENDIAN)
  549. return *p;
  550. #else
  551. const uint32_t* ptr = (const uint32_t*)p;
  552. rx_vec_i128 c;
  553. c.u32[0] = load32(ptr + 0);
  554. c.u32[1] = load32(ptr + 1);
  555. c.u32[2] = load32(ptr + 2);
  556. c.u32[3] = load32(ptr + 3);
  557. return c;
  558. #endif
  559. }
  560. FORCE_INLINE void rx_store_vec_i128(rx_vec_i128 *p, rx_vec_i128 b) {
  561. #if defined(NATIVE_LITTLE_ENDIAN)
  562. *p = b;
  563. #else
  564. uint32_t* ptr = (uint32_t*)p;
  565. store32(ptr + 0, b.u32[0]);
  566. store32(ptr + 1, b.u32[1]);
  567. store32(ptr + 2, b.u32[2]);
  568. store32(ptr + 3, b.u32[3]);
  569. #endif
  570. }
  571. FORCE_INLINE rx_vec_f128 rx_cvt_packed_int_vec_f128(const void* addr) {
  572. rx_vec_f128 x;
  573. x.lo = (double)unsigned32ToSigned2sCompl(load32((const uint8_t*)addr + 0));
  574. x.hi = (double)unsigned32ToSigned2sCompl(load32((const uint8_t*)addr + 4));
  575. return x;
  576. }
  577. #define RANDOMX_DEFAULT_FENV
  578. #endif
  579. #ifndef HAVE_AES
  580. static const char* platformError = "Platform doesn't support hardware AES";
  581. #include <stdexcept>
  582. FORCE_INLINE rx_vec_i128 rx_aesenc_vec_i128(rx_vec_i128 v, rx_vec_i128 rkey) {
  583. throw std::runtime_error(platformError);
  584. }
  585. FORCE_INLINE rx_vec_i128 rx_aesdec_vec_i128(rx_vec_i128 v, rx_vec_i128 rkey) {
  586. throw std::runtime_error(platformError);
  587. }
  588. #define HAVE_AES 0
  589. #endif
  590. #ifdef RANDOMX_DEFAULT_FENV
  591. void rx_reset_float_state();
  592. void rx_set_rounding_mode(uint32_t mode);
  593. uint32_t rx_get_rounding_mode();
  594. #endif
  595. double loadDoublePortable(const void* addr);
  596. uint64_t mulh(uint64_t, uint64_t);
  597. int64_t smulh(int64_t, int64_t);
  598. uint64_t rotl(uint64_t, unsigned int);
  599. uint64_t rotr(uint64_t, unsigned int);