mulx.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef BOOST_UNORDERED_DETAIL_MULX_HPP
  2. #define BOOST_UNORDERED_DETAIL_MULX_HPP
  3. // Copyright 2022 Peter Dimov.
  4. // Copyright 2022 Joaquin M Lopez Munoz.
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // https://www.boost.org/LICENSE_1_0.txt)
  7. #include <boost/cstdint.hpp>
  8. #include <climits>
  9. #include <cstddef>
  10. #if defined(_MSC_VER) && !defined(__clang__)
  11. # include <intrin.h>
  12. #endif
  13. namespace boost {
  14. namespace unordered {
  15. namespace detail {
  16. // Bit mixer based on the mulx primitive
  17. #if defined(_MSC_VER) && defined(_M_X64) && !defined(__clang__)
  18. __forceinline boost::uint64_t mulx64( boost::uint64_t x, boost::uint64_t y )
  19. {
  20. boost::uint64_t r2;
  21. boost::uint64_t r = _umul128( x, y, &r2 );
  22. return r ^ r2;
  23. }
  24. #elif defined(_MSC_VER) && defined(_M_ARM64) && !defined(__clang__)
  25. __forceinline boost::uint64_t mulx64( boost::uint64_t x, boost::uint64_t y )
  26. {
  27. boost::uint64_t r = x * y;
  28. boost::uint64_t r2 = __umulh( x, y );
  29. return r ^ r2;
  30. }
  31. #elif defined(__SIZEOF_INT128__)
  32. inline boost::uint64_t mulx64( boost::uint64_t x, boost::uint64_t y )
  33. {
  34. __uint128_t r = (__uint128_t)x * y;
  35. return (boost::uint64_t)r ^ (boost::uint64_t)( r >> 64 );
  36. }
  37. #else
  38. inline boost::uint64_t mulx64( boost::uint64_t x, boost::uint64_t y )
  39. {
  40. boost::uint64_t x1 = (boost::uint32_t)x;
  41. boost::uint64_t x2 = x >> 32;
  42. boost::uint64_t y1 = (boost::uint32_t)y;
  43. boost::uint64_t y2 = y >> 32;
  44. boost::uint64_t r3 = x2 * y2;
  45. boost::uint64_t r2a = x1 * y2;
  46. r3 += r2a >> 32;
  47. boost::uint64_t r2b = x2 * y1;
  48. r3 += r2b >> 32;
  49. boost::uint64_t r1 = x1 * y1;
  50. boost::uint64_t r2 = (r1 >> 32) + (boost::uint32_t)r2a + (boost::uint32_t)r2b;
  51. r1 = (r2 << 32) + (boost::uint32_t)r1;
  52. r3 += r2 >> 32;
  53. return r1 ^ r3;
  54. }
  55. #endif
  56. inline boost::uint32_t mulx32( boost::uint32_t x, boost::uint32_t y )
  57. {
  58. boost::uint64_t r = (boost::uint64_t)x * y;
  59. #if defined(__MSVC_RUNTIME_CHECKS)
  60. return (boost::uint32_t)(r & UINT32_MAX) ^ (boost::uint32_t)(r >> 32);
  61. #else
  62. return (boost::uint32_t)r ^ (boost::uint32_t)(r >> 32);
  63. #endif
  64. }
  65. #if defined(SIZE_MAX)
  66. #if ((((SIZE_MAX >> 16) >> 16) >> 16) >> 15) != 0
  67. #define BOOST_UNORDERED_64B_ARCHITECTURE /* >64 bits assumed as 64 bits */
  68. #endif
  69. #elif defined(UINTPTR_MAX) /* used as proxy for std::size_t */
  70. #if ((((UINTPTR_MAX >> 16) >> 16) >> 16) >> 15) != 0
  71. #define BOOST_UNORDERED_64B_ARCHITECTURE
  72. #endif
  73. #endif
  74. inline std::size_t mulx( std::size_t x ) noexcept
  75. {
  76. #if defined(BOOST_UNORDERED_64B_ARCHITECTURE)
  77. // multiplier is phi
  78. return (std::size_t)mulx64( (boost::uint64_t)x, 0x9E3779B97F4A7C15ull );
  79. #else /* 32 bits assumed */
  80. // multiplier from https://arxiv.org/abs/2001.05304
  81. return mulx32( x, 0xE817FB2Du );
  82. #endif
  83. }
  84. #ifdef BOOST_UNORDERED_64B_ARCHITECTURE
  85. #undef BOOST_UNORDERED_64B_ARCHITECTURE
  86. #endif
  87. } // namespace detail
  88. } // namespace unordered
  89. } // namespace boost
  90. #endif // #ifndef BOOST_UNORDERED_DETAIL_MULX_HPP