intrinPortable.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #if defined(_MSC_VER)
  18. #if defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP == 2)
  19. #define __SSE2__ 1
  20. #endif
  21. #endif
  22. #ifdef __SSE2__
  23. #ifdef __GNUC__
  24. #include <x86intrin.h>
  25. #else
  26. #include <intrin.h>
  27. #endif
  28. inline __m128d _mm_abs(__m128d xd) {
  29. const __m128d absmask = _mm_castsi128_pd(_mm_set1_epi64x(~(1LL << 63)));
  30. return _mm_and_pd(xd, absmask);
  31. }
  32. #define PREFETCHNTA(x) _mm_prefetch((const char *)(x), _MM_HINT_NTA)
  33. #else
  34. #include <cstdint>
  35. #include <stdexcept>
  36. #define _mm_malloc(a,b) malloc(a)
  37. #define _mm_free(a) free(a)
  38. #define PREFETCHNTA(x)
  39. typedef union {
  40. uint64_t u64[2];
  41. uint32_t u32[4];
  42. uint16_t u16[8];
  43. uint8_t u8[16];
  44. } __m128i;
  45. typedef struct {
  46. double lo;
  47. double hi;
  48. } __m128d;
  49. inline __m128d _mm_load_pd(const double* pd) {
  50. __m128d x;
  51. x.lo = *(pd + 0);
  52. x.hi = *(pd + 1);
  53. return x;
  54. }
  55. static const char* platformError = "Platform doesn't support hardware AES";
  56. inline __m128i _mm_aeskeygenassist_si128(__m128i key, uint8_t rcon) {
  57. throw std::runtime_error(platformError);
  58. }
  59. inline __m128i _mm_aesenc_si128(__m128i v, __m128i rkey) {
  60. throw std::runtime_error(platformError);
  61. }
  62. inline __m128i _mm_aesdec_si128(__m128i v, __m128i rkey) {
  63. throw std::runtime_error(platformError);
  64. }
  65. inline int _mm_cvtsi128_si32(__m128i v) {
  66. return v.u32[0];
  67. }
  68. inline __m128i _mm_cvtsi32_si128(int si32) {
  69. __m128i v;
  70. v.u32[0] = si32;
  71. v.u32[1] = 0;
  72. v.u32[2] = 0;
  73. v.u32[3] = 0;
  74. return v;
  75. }
  76. inline __m128i _mm_set_epi64x(int64_t _I1, int64_t _I0) {
  77. __m128i v;
  78. v.u64[0] = _I0;
  79. v.u64[1] = _I1;
  80. return v;
  81. }
  82. inline __m128i _mm_set_epi32(int _I3, int _I2, int _I1, int _I0) {
  83. __m128i v;
  84. v.u32[0] = _I0;
  85. v.u32[1] = _I1;
  86. v.u32[2] = _I2;
  87. v.u32[3] = _I3;
  88. return v;
  89. };
  90. inline __m128i _mm_xor_si128(__m128i _A, __m128i _B) {
  91. __m128i c;
  92. c.u32[0] = _A.u32[0] ^ _B.u32[0];
  93. c.u32[1] = _A.u32[1] ^ _B.u32[1];
  94. c.u32[2] = _A.u32[2] ^ _B.u32[2];
  95. c.u32[3] = _A.u32[3] ^ _B.u32[3];
  96. return c;
  97. }
  98. inline __m128i _mm_shuffle_epi32(__m128i _A, int _Imm) {
  99. __m128i c;
  100. c.u32[0] = _A.u32[_Imm & 3];
  101. c.u32[1] = _A.u32[(_Imm >> 2) & 3];
  102. c.u32[2] = _A.u32[(_Imm >> 4) & 3];
  103. c.u32[3] = _A.u32[(_Imm >> 6) & 3];
  104. return c;
  105. }
  106. inline __m128i _mm_load_si128(__m128i const*_P) {
  107. return *_P;
  108. }
  109. inline void _mm_store_si128(__m128i *_P, __m128i _B) {
  110. *_P = _B;
  111. }
  112. inline __m128i _mm_slli_si128(__m128i _A, int _Imm) {
  113. _Imm &= 255;
  114. if (_Imm > 15) {
  115. _A.u64[0] = 0;
  116. _A.u64[1] = 0;
  117. }
  118. else {
  119. for (int i = 15; i >= _Imm; --i) {
  120. _A.u8[i] = _A.u8[i - _Imm];
  121. }
  122. for (int i = 0; i < _Imm; ++i) {
  123. _A.u8[i] = 0;
  124. }
  125. }
  126. return _A;
  127. }
  128. #endif
  129. constexpr int RoundToNearest = 0;
  130. constexpr int RoundDown = 1;
  131. constexpr int RoundUp = 2;
  132. constexpr int RoundToZero = 3;
  133. constexpr int32_t unsigned32ToSigned2sCompl(uint32_t x) {
  134. return (-1 == ~0) ? (int32_t)x : (x > INT32_MAX ? (-(int32_t)(UINT32_MAX - x) - 1) : (int32_t)x);
  135. }
  136. constexpr int64_t unsigned64ToSigned2sCompl(uint64_t x) {
  137. return (-1 == ~0) ? (int64_t)x : (x > INT64_MAX ? (-(int64_t)(UINT64_MAX - x) - 1) : (int64_t)x);
  138. }
  139. constexpr uint64_t signExtend2sCompl(uint32_t x) {
  140. return (-1 == ~0) ? (int64_t)(int32_t)(x) : (x > INT32_MAX ? (x | 0xffffffff00000000ULL) : (uint64_t)x);
  141. }
  142. inline __m128d load_cvt_i32x2(const void* addr) {
  143. __m128i ix = _mm_load_si128((const __m128i*)addr);
  144. return _mm_cvtepi32_pd(ix);
  145. }
  146. double loadDoublePortable(const void* addr);
  147. uint64_t mulh(uint64_t, uint64_t);
  148. int64_t smulh(int64_t, int64_t);
  149. uint64_t rotl(uint64_t, int);
  150. uint64_t rotr(uint64_t, int);
  151. void initFpu();
  152. void setRoundMode(uint32_t);
  153. bool condition(uint32_t, uint32_t, uint32_t);