instructions_portable.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 STDC FENV_ACCESS ON
  26. #include <cfenv>
  27. #include <cmath>
  28. #include "common.hpp"
  29. #include "intrin_portable.h"
  30. #include "blake2/endian.h"
  31. #if defined(__SIZEOF_INT128__)
  32. typedef unsigned __int128 uint128_t;
  33. typedef __int128 int128_t;
  34. uint64_t mulh(uint64_t a, uint64_t b) {
  35. return ((uint128_t)a * b) >> 64;
  36. }
  37. int64_t smulh(int64_t a, int64_t b) {
  38. return ((int128_t)a * b) >> 64;
  39. }
  40. #define HAVE_MULH
  41. #define HAVE_SMULH
  42. #endif
  43. #if defined(_MSC_VER)
  44. #define HAS_VALUE(X) X ## 0
  45. #define EVAL_DEFINE(X) HAS_VALUE(X)
  46. #include <intrin.h>
  47. #include <stdlib.h>
  48. uint64_t rotl(uint64_t x, int c) {
  49. return _rotl64(x, c);
  50. }
  51. uint64_t rotr(uint64_t x , int c) {
  52. return _rotr64(x, c);
  53. }
  54. #define HAVE_ROTL
  55. #define HAVE_ROTR
  56. #if EVAL_DEFINE(__MACHINEARM64_X64(1))
  57. uint64_t mulh(uint64_t a, uint64_t b) {
  58. return __umulh(a, b);
  59. }
  60. #define HAVE_MULH
  61. #endif
  62. #if EVAL_DEFINE(__MACHINEX64(1))
  63. int64_t smulh(int64_t a, int64_t b) {
  64. int64_t hi;
  65. _mul128(a, b, &hi);
  66. return hi;
  67. }
  68. #define HAVE_SMULH
  69. #endif
  70. static void setRoundMode_(uint32_t mode) {
  71. _controlfp(mode, _MCW_RC);
  72. }
  73. #define HAVE_SETROUNDMODE_IMPL
  74. #endif
  75. #ifndef HAVE_SETROUNDMODE_IMPL
  76. static void setRoundMode_(uint32_t mode) {
  77. fesetround(mode);
  78. }
  79. #endif
  80. #ifndef HAVE_ROTR
  81. uint64_t rotr(uint64_t a, int b) {
  82. return (a >> b) | (a << (64 - b));
  83. }
  84. #define HAVE_ROTR
  85. #endif
  86. #ifndef HAVE_ROTL
  87. uint64_t rotl(uint64_t a, int b) {
  88. return (a << b) | (a >> (64 - b));
  89. }
  90. #define HAVE_ROTL
  91. #endif
  92. #ifndef HAVE_MULH
  93. #define LO(x) ((x)&0xffffffff)
  94. #define HI(x) ((x)>>32)
  95. uint64_t mulh(uint64_t a, uint64_t b) {
  96. uint64_t ah = HI(a), al = LO(a);
  97. uint64_t bh = HI(b), bl = LO(b);
  98. uint64_t x00 = al * bl;
  99. uint64_t x01 = al * bh;
  100. uint64_t x10 = ah * bl;
  101. uint64_t x11 = ah * bh;
  102. uint64_t m1 = LO(x10) + LO(x01) + HI(x00);
  103. uint64_t m2 = HI(x10) + HI(x01) + LO(x11) + HI(m1);
  104. uint64_t m3 = HI(x11) + HI(m2);
  105. return (m3 << 32) + LO(m2);
  106. }
  107. #define HAVE_MULH
  108. #endif
  109. #ifndef HAVE_SMULH
  110. int64_t smulh(int64_t a, int64_t b) {
  111. int64_t hi = mulh(a, b);
  112. if (a < 0LL) hi -= b;
  113. if (b < 0LL) hi -= a;
  114. return hi;
  115. }
  116. #define HAVE_SMULH
  117. #endif
  118. #ifdef RANDOMX_DEFAULT_FENV
  119. void rx_reset_float_state() {
  120. setRoundMode_(FE_TONEAREST);
  121. rx_set_double_precision(); //set precision to 53 bits if needed by the platform
  122. }
  123. void rx_set_rounding_mode(uint32_t mode) {
  124. switch (mode & 3) {
  125. case RoundDown:
  126. setRoundMode_(FE_DOWNWARD);
  127. break;
  128. case RoundUp:
  129. setRoundMode_(FE_UPWARD);
  130. break;
  131. case RoundToZero:
  132. setRoundMode_(FE_TOWARDZERO);
  133. break;
  134. case RoundToNearest:
  135. setRoundMode_(FE_TONEAREST);
  136. break;
  137. default:
  138. UNREACHABLE;
  139. }
  140. }
  141. #endif
  142. #ifdef RANDOMX_USE_X87
  143. #ifdef _M_IX86
  144. void rx_set_double_precision() {
  145. _control87(_PC_53, _MCW_PC);
  146. }
  147. #elif defined(__i386)
  148. void rx_set_double_precision() {
  149. uint16_t volatile x87cw;
  150. asm volatile("fstcw %0" : "=m" (x87cw));
  151. x87cw &= ~0x300;
  152. x87cw |= 0x200;
  153. asm volatile("fldcw %0" : : "m" (x87cw));
  154. }
  155. #endif
  156. #endif //RANDOMX_USE_X87
  157. union double_ser_t {
  158. double f;
  159. uint64_t i;
  160. };
  161. double loadDoublePortable(const void* addr) {
  162. double_ser_t ds;
  163. ds.i = load64(addr);
  164. return ds.f;
  165. }