Chris@16: Chris@16: // Copyright 2005-2009 Daniel James. Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #if !defined(BOOST_FUNCTIONAL_HASH_DETAIL_FLOAT_FUNCTIONS_HPP) Chris@16: #define BOOST_FUNCTIONAL_HASH_DETAIL_FLOAT_FUNCTIONS_HPP Chris@16: Chris@16: #include Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: #pragma once Chris@101: #endif Chris@101: Chris@16: #include Chris@16: Chris@16: // Set BOOST_HASH_CONFORMANT_FLOATS to 1 for libraries known to have Chris@16: // sufficiently good floating point support to not require any Chris@16: // workarounds. Chris@16: // Chris@16: // When set to 0, the library tries to automatically Chris@16: // use the best available implementation. This normally works well, but Chris@16: // breaks when ambiguities are created by odd namespacing of the functions. Chris@16: // Chris@16: // Note that if this is set to 0, the library should still take full Chris@16: // advantage of the platform's floating point support. Chris@16: Chris@16: #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION) Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 0 Chris@16: #elif defined(__LIBCOMO__) Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 0 Chris@16: #elif defined(__STD_RWCOMPILER_H__) || defined(_RWSTD_VER) Chris@16: // Rogue Wave library: Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 0 Chris@16: #elif defined(_LIBCPP_VERSION) Chris@16: // libc++ Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 1 Chris@16: #elif defined(__GLIBCPP__) || defined(__GLIBCXX__) Chris@16: // GNU libstdc++ 3 Chris@16: # if defined(__GNUC__) && __GNUC__ >= 4 Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 1 Chris@16: # else Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 0 Chris@16: # endif Chris@16: #elif defined(__STL_CONFIG_H) Chris@16: // generic SGI STL Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 0 Chris@16: #elif defined(__MSL_CPP__) Chris@16: // MSL standard lib: Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 0 Chris@16: #elif defined(__IBMCPP__) Chris@16: // VACPP std lib (probably conformant for much earlier version). Chris@16: # if __IBMCPP__ >= 1210 Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 1 Chris@16: # else Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 0 Chris@16: # endif Chris@16: #elif defined(MSIPL_COMPILE_H) Chris@16: // Modena C++ standard library Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 0 Chris@16: #elif (defined(_YVALS) && !defined(__IBMCPP__)) || defined(_CPPLIB_VER) Chris@16: // Dinkumware Library (this has to appear after any possible replacement libraries): Chris@16: # if _CPPLIB_VER >= 405 Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 1 Chris@16: # else Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 0 Chris@16: # endif Chris@16: #else Chris@16: # define BOOST_HASH_CONFORMANT_FLOATS 0 Chris@16: #endif Chris@16: Chris@16: #if BOOST_HASH_CONFORMANT_FLOATS Chris@16: Chris@16: // The standard library is known to be compliant, so don't use the Chris@16: // configuration mechanism. Chris@16: Chris@16: namespace boost { Chris@16: namespace hash_detail { Chris@16: template Chris@16: struct call_ldexp { Chris@16: typedef Float float_type; Chris@16: inline Float operator()(Float x, int y) const { Chris@16: return std::ldexp(x, y); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct call_frexp { Chris@16: typedef Float float_type; Chris@16: inline Float operator()(Float x, int* y) const { Chris@16: return std::frexp(x, y); Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct select_hash_type Chris@16: { Chris@16: typedef Float type; Chris@16: }; Chris@16: } Chris@16: } Chris@16: Chris@16: #else // BOOST_HASH_CONFORMANT_FLOATS == 0 Chris@16: Chris@16: // The C++ standard requires that the C float functions are overloarded Chris@16: // for float, double and long double in the std namespace, but some of the older Chris@16: // library implementations don't support this. On some that don't, the C99 Chris@16: // float functions (frexpf, frexpl, etc.) are available. Chris@16: // Chris@16: // The following tries to automatically detect which are available. Chris@16: Chris@16: namespace boost { Chris@16: namespace hash_detail { Chris@16: Chris@16: // Returned by dummy versions of the float functions. Chris@16: Chris@16: struct not_found { Chris@16: // Implicitly convertible to float and long double in order to avoid Chris@16: // a compile error when the dummy float functions are used. Chris@16: Chris@16: inline operator float() const { return 0; } Chris@16: inline operator long double() const { return 0; } Chris@16: }; Chris@16: Chris@16: // A type for detecting the return type of functions. Chris@16: Chris@16: template struct is; Chris@16: template <> struct is { char x[10]; }; Chris@16: template <> struct is { char x[20]; }; Chris@16: template <> struct is { char x[30]; }; Chris@16: template <> struct is { char x[40]; }; Chris@16: Chris@16: // Used to convert the return type of a function to a type for sizeof. Chris@16: Chris@16: template is float_type(T); Chris@16: Chris@16: // call_ldexp Chris@16: // Chris@16: // This will get specialized for float and long double Chris@16: Chris@16: template struct call_ldexp Chris@16: { Chris@16: typedef double float_type; Chris@16: Chris@16: inline double operator()(double a, int b) const Chris@16: { Chris@16: using namespace std; Chris@16: return ldexp(a, b); Chris@16: } Chris@16: }; Chris@16: Chris@16: // call_frexp Chris@16: // Chris@16: // This will get specialized for float and long double Chris@16: Chris@16: template struct call_frexp Chris@16: { Chris@16: typedef double float_type; Chris@16: Chris@16: inline double operator()(double a, int* b) const Chris@16: { Chris@16: using namespace std; Chris@16: return frexp(a, b); Chris@16: } Chris@16: }; Chris@16: } Chris@16: } Chris@16: Chris@16: // A namespace for dummy functions to detect when the actual function we want Chris@16: // isn't available. ldexpl, ldexpf etc. might be added tby the macros below. Chris@16: // Chris@16: // AFAICT these have to be outside of the boost namespace, as if they're in Chris@16: // the boost namespace they'll always be preferable to any other function Chris@16: // (since the arguments are built in types, ADL can't be used). Chris@16: Chris@16: namespace boost_hash_detect_float_functions { Chris@16: template boost::hash_detail::not_found ldexp(Float, int); Chris@16: template boost::hash_detail::not_found frexp(Float, int*); Chris@16: } Chris@16: Chris@16: // Macros for generating specializations of call_ldexp and call_frexp. Chris@16: // Chris@16: // check_cpp and check_c99 check if the C++ or C99 functions are available. Chris@16: // Chris@16: // Then the call_* functions select an appropriate implementation. Chris@16: // Chris@16: // I used c99_func in a few places just to get a unique name. Chris@16: // Chris@16: // Important: when using 'using namespace' at namespace level, include as Chris@16: // little as possible in that namespace, as Visual C++ has an odd bug which Chris@16: // can cause the namespace to be imported at the global level. This seems to Chris@16: // happen mainly when there's a template in the same namesapce. Chris@16: Chris@16: #define BOOST_HASH_CALL_FLOAT_FUNC(cpp_func, c99_func, type1, type2) \ Chris@16: namespace boost_hash_detect_float_functions { \ Chris@16: template \ Chris@16: boost::hash_detail::not_found c99_func(Float, type2); \ Chris@16: } \ Chris@16: \ Chris@16: namespace boost { \ Chris@16: namespace hash_detail { \ Chris@16: namespace c99_func##_detect { \ Chris@16: using namespace std; \ Chris@16: using namespace boost_hash_detect_float_functions; \ Chris@16: \ Chris@16: struct check { \ Chris@16: static type1 x; \ Chris@16: static type2 y; \ Chris@16: BOOST_STATIC_CONSTANT(bool, cpp = \ Chris@16: sizeof(float_type(cpp_func(x,y))) \ Chris@16: == sizeof(is)); \ Chris@16: BOOST_STATIC_CONSTANT(bool, c99 = \ Chris@16: sizeof(float_type(c99_func(x,y))) \ Chris@16: == sizeof(is)); \ Chris@16: }; \ Chris@16: } \ Chris@16: \ Chris@16: template \ Chris@16: struct call_c99_##c99_func : \ Chris@16: boost::hash_detail::call_##cpp_func {}; \ Chris@16: \ Chris@16: template <> \ Chris@16: struct call_c99_##c99_func { \ Chris@16: typedef type1 float_type; \ Chris@16: \ Chris@16: template \ Chris@16: inline type1 operator()(type1 a, T b) const \ Chris@16: { \ Chris@16: using namespace std; \ Chris@16: return c99_func(a, b); \ Chris@16: } \ Chris@16: }; \ Chris@16: \ Chris@16: template \ Chris@16: struct call_cpp_##c99_func : \ Chris@16: call_c99_##c99_func< \ Chris@16: ::boost::hash_detail::c99_func##_detect::check::c99 \ Chris@16: > {}; \ Chris@16: \ Chris@16: template <> \ Chris@16: struct call_cpp_##c99_func { \ Chris@16: typedef type1 float_type; \ Chris@16: \ Chris@16: template \ Chris@16: inline type1 operator()(type1 a, T b) const \ Chris@16: { \ Chris@16: using namespace std; \ Chris@16: return cpp_func(a, b); \ Chris@16: } \ Chris@16: }; \ Chris@16: \ Chris@16: template <> \ Chris@16: struct call_##cpp_func : \ Chris@16: call_cpp_##c99_func< \ Chris@16: ::boost::hash_detail::c99_func##_detect::check::cpp \ Chris@16: > {}; \ Chris@16: } \ Chris@16: } Chris@16: Chris@16: #define BOOST_HASH_CALL_FLOAT_MACRO(cpp_func, c99_func, type1, type2) \ Chris@16: namespace boost { \ Chris@16: namespace hash_detail { \ Chris@16: \ Chris@16: template <> \ Chris@16: struct call_##cpp_func { \ Chris@16: typedef type1 float_type; \ Chris@16: inline type1 operator()(type1 x, type2 y) const { \ Chris@16: return c99_func(x, y); \ Chris@16: } \ Chris@16: }; \ Chris@16: } \ Chris@16: } Chris@16: Chris@16: #if defined(ldexpf) Chris@16: BOOST_HASH_CALL_FLOAT_MACRO(ldexp, ldexpf, float, int) Chris@16: #else Chris@16: BOOST_HASH_CALL_FLOAT_FUNC(ldexp, ldexpf, float, int) Chris@16: #endif Chris@16: Chris@16: #if defined(ldexpl) Chris@16: BOOST_HASH_CALL_FLOAT_MACRO(ldexp, ldexpl, long double, int) Chris@16: #else Chris@16: BOOST_HASH_CALL_FLOAT_FUNC(ldexp, ldexpl, long double, int) Chris@16: #endif Chris@16: Chris@16: #if defined(frexpf) Chris@16: BOOST_HASH_CALL_FLOAT_MACRO(frexp, frexpf, float, int*) Chris@16: #else Chris@16: BOOST_HASH_CALL_FLOAT_FUNC(frexp, frexpf, float, int*) Chris@16: #endif Chris@16: Chris@16: #if defined(frexpl) Chris@16: BOOST_HASH_CALL_FLOAT_MACRO(frexp, frexpl, long double, int*) Chris@16: #else Chris@16: BOOST_HASH_CALL_FLOAT_FUNC(frexp, frexpl, long double, int*) Chris@16: #endif Chris@16: Chris@16: #undef BOOST_HASH_CALL_FLOAT_MACRO Chris@16: #undef BOOST_HASH_CALL_FLOAT_FUNC Chris@16: Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: namespace hash_detail Chris@16: { Chris@16: template Chris@16: struct select_hash_type_impl { Chris@16: typedef double type; Chris@16: }; Chris@16: Chris@16: template <> Chris@16: struct select_hash_type_impl { Chris@16: typedef float type; Chris@16: }; Chris@16: Chris@16: template <> Chris@16: struct select_hash_type_impl { Chris@16: typedef long double type; Chris@16: }; Chris@16: Chris@16: Chris@16: // select_hash_type Chris@16: // Chris@16: // If there is support for a particular floating point type, use that Chris@16: // otherwise use double (there's always support for double). Chris@16: Chris@16: template Chris@16: struct select_hash_type : select_hash_type_impl< Chris@16: BOOST_DEDUCED_TYPENAME call_ldexp::float_type, Chris@16: BOOST_DEDUCED_TYPENAME call_frexp::float_type Chris@16: > {}; Chris@16: } Chris@16: } Chris@16: Chris@16: #endif // BOOST_HASH_CONFORMANT_FLOATS Chris@16: Chris@16: #endif