xmx.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* 32b/64b xmx mix function.
  2. *
  3. * Copyright 2022 Peter Dimov.
  4. * Copyright 2022 Joaquin M Lopez Munoz.
  5. * Distributed under the Boost Software License, Version 1.0.
  6. * (See accompanying file LICENSE_1_0.txt or copy at
  7. * http://www.boost.org/LICENSE_1_0.txt)
  8. *
  9. * See https://www.boost.org/libs/unordered for library home page.
  10. */
  11. #ifndef BOOST_UNORDERED_DETAIL_XMX_HPP
  12. #define BOOST_UNORDERED_DETAIL_XMX_HPP
  13. #include <boost/cstdint.hpp>
  14. #include <climits>
  15. #include <cstddef>
  16. namespace boost{
  17. namespace unordered{
  18. namespace detail{
  19. /* Bit mixer for improvement of statistical properties of hash functions.
  20. * The implementation is different on 64bit and 32bit architectures:
  21. *
  22. * - 64bit: same as xmx function in
  23. * http://jonkagstrom.com/bit-mixer-construction/index.html
  24. * - 32bit: generated by Hash Function Prospector
  25. * (https://github.com/skeeto/hash-prospector) and selected as the
  26. * best overall performer in benchmarks of Boost.Unordered flat containers.
  27. * Score assigned by Hash Prospector: 333.7934929677524
  28. */
  29. #if defined(SIZE_MAX)
  30. #if ((((SIZE_MAX >> 16) >> 16) >> 16) >> 15) != 0
  31. #define BOOST_UNORDERED_64B_ARCHITECTURE /* >64 bits assumed as 64 bits */
  32. #endif
  33. #elif defined(UINTPTR_MAX) /* used as proxy for std::size_t */
  34. #if ((((UINTPTR_MAX >> 16) >> 16) >> 16) >> 15) != 0
  35. #define BOOST_UNORDERED_64B_ARCHITECTURE
  36. #endif
  37. #endif
  38. static inline std::size_t xmx(std::size_t x)noexcept
  39. {
  40. #if defined(BOOST_UNORDERED_64B_ARCHITECTURE)
  41. boost::uint64_t z=(boost::uint64_t)x;
  42. z^=z>>23;
  43. z*=0xff51afd7ed558ccdull;
  44. z^=z>>23;
  45. return (std::size_t)z;
  46. #else /* 32 bits assumed */
  47. x^=x>>18;
  48. x*=0x56b5aaadu;
  49. x^=x>>16;
  50. return x;
  51. #endif
  52. }
  53. #ifdef BOOST_UNORDERED_64B_ARCHITECTURE
  54. #undef BOOST_UNORDERED_64B_ARCHITECTURE
  55. #endif
  56. } /* namespace detail */
  57. } /* namespace unordered */
  58. } /* namespace boost */
  59. #endif