instructions_portable.cpp 4.9 KB

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