hash_float_x86.hpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2005-2009 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. // A non-portable hash function form non-zero floats on x86.
  5. //
  6. // Even if you're on an x86 platform, this might not work if their floating
  7. // point isn't set up as this expects. So this should only be used if it's
  8. // absolutely certain that it will work.
  9. #if !defined(BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_X86_HEADER)
  10. #define BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_X86_HEADER
  11. #include <boost/cstdint.hpp>
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  13. # pragma once
  14. #endif
  15. namespace boost
  16. {
  17. namespace hash_detail
  18. {
  19. inline void hash_float_combine(std::size_t& seed, std::size_t value)
  20. {
  21. seed ^= value + (seed<<6) + (seed>>2);
  22. }
  23. inline std::size_t float_hash_impl(float v)
  24. {
  25. boost::uint32_t* ptr = (boost::uint32_t*)&v;
  26. std::size_t seed = *ptr;
  27. return seed;
  28. }
  29. inline std::size_t float_hash_impl(double v)
  30. {
  31. boost::uint32_t* ptr = (boost::uint32_t*)&v;
  32. std::size_t seed = *ptr++;
  33. hash_float_combine(seed, *ptr);
  34. return seed;
  35. }
  36. inline std::size_t float_hash_impl(long double v)
  37. {
  38. boost::uint32_t* ptr = (boost::uint32_t*)&v;
  39. std::size_t seed = *ptr++;
  40. hash_float_combine(seed, *ptr++);
  41. hash_float_combine(seed, *(boost::uint16_t*)ptr);
  42. return seed;
  43. }
  44. }
  45. }
  46. #endif