hash_float.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #if !defined(BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_HEADER)
  5. #define BOOST_FUNCTIONAL_HASH_DETAIL_HASH_FLOAT_HEADER
  6. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  7. # pragma once
  8. #endif
  9. #include <boost/config.hpp>
  10. #include <boost/functional/hash/detail/float_functions.hpp>
  11. #include <boost/functional/hash/detail/limits.hpp>
  12. #include <boost/integer/static_log2.hpp>
  13. #include <boost/cstdint.hpp>
  14. #include <boost/assert.hpp>
  15. // Include hash implementation for the current platform.
  16. // Cygwn
  17. #if defined(__CYGWIN__)
  18. # if defined(__i386__) || defined(_M_IX86)
  19. # include <boost/functional/hash/detail/hash_float_x86.hpp>
  20. # else
  21. # include <boost/functional/hash/detail/hash_float_generic.hpp>
  22. # endif
  23. #else
  24. # include <boost/functional/hash/detail/hash_float_generic.hpp>
  25. #endif
  26. // Can we use fpclassify?
  27. // STLport
  28. #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
  29. #define BOOST_HASH_USE_FPCLASSIFY 0
  30. // GNU libstdc++ 3
  31. #elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
  32. # if (defined(__USE_ISOC99) || defined(_GLIBCXX_USE_C99_MATH)) && \
  33. !(defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__))
  34. # define BOOST_HASH_USE_FPCLASSIFY 1
  35. # else
  36. # define BOOST_HASH_USE_FPCLASSIFY 0
  37. # endif
  38. // Everything else
  39. #else
  40. # define BOOST_HASH_USE_FPCLASSIFY 0
  41. #endif
  42. #if BOOST_HASH_USE_FPCLASSIFY
  43. #include <boost/config/no_tr1/cmath.hpp>
  44. namespace boost
  45. {
  46. namespace hash_detail
  47. {
  48. template <class T>
  49. inline std::size_t float_hash_value(T v)
  50. {
  51. using namespace std;
  52. switch (fpclassify(v)) {
  53. case FP_ZERO:
  54. return 0;
  55. case FP_INFINITE:
  56. return (std::size_t)(v > 0 ? -1 : -2);
  57. case FP_NAN:
  58. return (std::size_t)(-3);
  59. case FP_NORMAL:
  60. case FP_SUBNORMAL:
  61. return float_hash_impl(v);
  62. default:
  63. BOOST_ASSERT(0);
  64. return 0;
  65. }
  66. }
  67. }
  68. }
  69. #else // !BOOST_HASH_USE_FPCLASSIFY
  70. namespace boost
  71. {
  72. namespace hash_detail
  73. {
  74. template <class T>
  75. inline std::size_t float_hash_value(T v)
  76. {
  77. return v == 0 ? 0 : float_hash_impl(v);
  78. }
  79. }
  80. }
  81. #endif // BOOST_HASH_USE_FPCLASSIFY
  82. #undef BOOST_HASH_USE_FPCLASSIFY
  83. #endif