float_functions.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_FLOAT_FUNCTIONS_HPP)
  5. #define BOOST_FUNCTIONAL_HASH_DETAIL_FLOAT_FUNCTIONS_HPP
  6. #include <boost/config.hpp>
  7. #if defined(BOOST_HAS_PRAGMA_ONCE)
  8. #pragma once
  9. #endif
  10. #include <boost/config/no_tr1/cmath.hpp>
  11. // Set BOOST_HASH_CONFORMANT_FLOATS to 1 for libraries known to have
  12. // sufficiently good floating point support to not require any
  13. // workarounds.
  14. //
  15. // When set to 0, the library tries to automatically
  16. // use the best available implementation. This normally works well, but
  17. // breaks when ambiguities are created by odd namespacing of the functions.
  18. //
  19. // Note that if this is set to 0, the library should still take full
  20. // advantage of the platform's floating point support.
  21. #if defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION)
  22. # define BOOST_HASH_CONFORMANT_FLOATS 0
  23. #elif defined(__LIBCOMO__)
  24. # define BOOST_HASH_CONFORMANT_FLOATS 0
  25. #elif defined(__STD_RWCOMPILER_H__) || defined(_RWSTD_VER)
  26. // Rogue Wave library:
  27. # define BOOST_HASH_CONFORMANT_FLOATS 0
  28. #elif defined(_LIBCPP_VERSION)
  29. // libc++
  30. # define BOOST_HASH_CONFORMANT_FLOATS 1
  31. #elif defined(__GLIBCPP__) || defined(__GLIBCXX__)
  32. // GNU libstdc++ 3
  33. # if defined(__GNUC__) && __GNUC__ >= 4
  34. # define BOOST_HASH_CONFORMANT_FLOATS 1
  35. # else
  36. # define BOOST_HASH_CONFORMANT_FLOATS 0
  37. # endif
  38. #elif defined(__STL_CONFIG_H)
  39. // generic SGI STL
  40. # define BOOST_HASH_CONFORMANT_FLOATS 0
  41. #elif defined(__MSL_CPP__)
  42. // MSL standard lib:
  43. # define BOOST_HASH_CONFORMANT_FLOATS 0
  44. #elif defined(__IBMCPP__)
  45. // VACPP std lib (probably conformant for much earlier version).
  46. # if __IBMCPP__ >= 1210
  47. # define BOOST_HASH_CONFORMANT_FLOATS 1
  48. # else
  49. # define BOOST_HASH_CONFORMANT_FLOATS 0
  50. # endif
  51. #elif defined(MSIPL_COMPILE_H)
  52. // Modena C++ standard library
  53. # define BOOST_HASH_CONFORMANT_FLOATS 0
  54. #elif (defined(_YVALS) && !defined(__IBMCPP__)) || defined(_CPPLIB_VER)
  55. // Dinkumware Library (this has to appear after any possible replacement libraries):
  56. # if _CPPLIB_VER >= 405
  57. # define BOOST_HASH_CONFORMANT_FLOATS 1
  58. # else
  59. # define BOOST_HASH_CONFORMANT_FLOATS 0
  60. # endif
  61. #else
  62. # define BOOST_HASH_CONFORMANT_FLOATS 0
  63. #endif
  64. #if BOOST_HASH_CONFORMANT_FLOATS
  65. // The standard library is known to be compliant, so don't use the
  66. // configuration mechanism.
  67. namespace boost {
  68. namespace hash_detail {
  69. template <typename Float>
  70. struct call_ldexp {
  71. typedef Float float_type;
  72. inline Float operator()(Float x, int y) const {
  73. return std::ldexp(x, y);
  74. }
  75. };
  76. template <typename Float>
  77. struct call_frexp {
  78. typedef Float float_type;
  79. inline Float operator()(Float x, int* y) const {
  80. return std::frexp(x, y);
  81. }
  82. };
  83. template <typename Float>
  84. struct select_hash_type
  85. {
  86. typedef Float type;
  87. };
  88. }
  89. }
  90. #else // BOOST_HASH_CONFORMANT_FLOATS == 0
  91. // The C++ standard requires that the C float functions are overloarded
  92. // for float, double and long double in the std namespace, but some of the older
  93. // library implementations don't support this. On some that don't, the C99
  94. // float functions (frexpf, frexpl, etc.) are available.
  95. //
  96. // The following tries to automatically detect which are available.
  97. namespace boost {
  98. namespace hash_detail {
  99. // Returned by dummy versions of the float functions.
  100. struct not_found {
  101. // Implicitly convertible to float and long double in order to avoid
  102. // a compile error when the dummy float functions are used.
  103. inline operator float() const { return 0; }
  104. inline operator long double() const { return 0; }
  105. };
  106. // A type for detecting the return type of functions.
  107. template <typename T> struct is;
  108. template <> struct is<float> { char x[10]; };
  109. template <> struct is<double> { char x[20]; };
  110. template <> struct is<long double> { char x[30]; };
  111. template <> struct is<boost::hash_detail::not_found> { char x[40]; };
  112. // Used to convert the return type of a function to a type for sizeof.
  113. template <typename T> is<T> float_type(T);
  114. // call_ldexp
  115. //
  116. // This will get specialized for float and long double
  117. template <typename Float> struct call_ldexp
  118. {
  119. typedef double float_type;
  120. inline double operator()(double a, int b) const
  121. {
  122. using namespace std;
  123. return ldexp(a, b);
  124. }
  125. };
  126. // call_frexp
  127. //
  128. // This will get specialized for float and long double
  129. template <typename Float> struct call_frexp
  130. {
  131. typedef double float_type;
  132. inline double operator()(double a, int* b) const
  133. {
  134. using namespace std;
  135. return frexp(a, b);
  136. }
  137. };
  138. }
  139. }
  140. // A namespace for dummy functions to detect when the actual function we want
  141. // isn't available. ldexpl, ldexpf etc. might be added tby the macros below.
  142. //
  143. // AFAICT these have to be outside of the boost namespace, as if they're in
  144. // the boost namespace they'll always be preferable to any other function
  145. // (since the arguments are built in types, ADL can't be used).
  146. namespace boost_hash_detect_float_functions {
  147. template <class Float> boost::hash_detail::not_found ldexp(Float, int);
  148. template <class Float> boost::hash_detail::not_found frexp(Float, int*);
  149. }
  150. // Macros for generating specializations of call_ldexp and call_frexp.
  151. //
  152. // check_cpp and check_c99 check if the C++ or C99 functions are available.
  153. //
  154. // Then the call_* functions select an appropriate implementation.
  155. //
  156. // I used c99_func in a few places just to get a unique name.
  157. //
  158. // Important: when using 'using namespace' at namespace level, include as
  159. // little as possible in that namespace, as Visual C++ has an odd bug which
  160. // can cause the namespace to be imported at the global level. This seems to
  161. // happen mainly when there's a template in the same namesapce.
  162. #define BOOST_HASH_CALL_FLOAT_FUNC(cpp_func, c99_func, type1, type2) \
  163. namespace boost_hash_detect_float_functions { \
  164. template <class Float> \
  165. boost::hash_detail::not_found c99_func(Float, type2); \
  166. } \
  167. \
  168. namespace boost { \
  169. namespace hash_detail { \
  170. namespace c99_func##_detect { \
  171. using namespace std; \
  172. using namespace boost_hash_detect_float_functions; \
  173. \
  174. struct check { \
  175. static type1 x; \
  176. static type2 y; \
  177. BOOST_STATIC_CONSTANT(bool, cpp = \
  178. sizeof(float_type(cpp_func(x,y))) \
  179. == sizeof(is<type1>)); \
  180. BOOST_STATIC_CONSTANT(bool, c99 = \
  181. sizeof(float_type(c99_func(x,y))) \
  182. == sizeof(is<type1>)); \
  183. }; \
  184. } \
  185. \
  186. template <bool x> \
  187. struct call_c99_##c99_func : \
  188. boost::hash_detail::call_##cpp_func<double> {}; \
  189. \
  190. template <> \
  191. struct call_c99_##c99_func<true> { \
  192. typedef type1 float_type; \
  193. \
  194. template <typename T> \
  195. inline type1 operator()(type1 a, T b) const \
  196. { \
  197. using namespace std; \
  198. return c99_func(a, b); \
  199. } \
  200. }; \
  201. \
  202. template <bool x> \
  203. struct call_cpp_##c99_func : \
  204. call_c99_##c99_func< \
  205. ::boost::hash_detail::c99_func##_detect::check::c99 \
  206. > {}; \
  207. \
  208. template <> \
  209. struct call_cpp_##c99_func<true> { \
  210. typedef type1 float_type; \
  211. \
  212. template <typename T> \
  213. inline type1 operator()(type1 a, T b) const \
  214. { \
  215. using namespace std; \
  216. return cpp_func(a, b); \
  217. } \
  218. }; \
  219. \
  220. template <> \
  221. struct call_##cpp_func<type1> : \
  222. call_cpp_##c99_func< \
  223. ::boost::hash_detail::c99_func##_detect::check::cpp \
  224. > {}; \
  225. } \
  226. }
  227. #define BOOST_HASH_CALL_FLOAT_MACRO(cpp_func, c99_func, type1, type2) \
  228. namespace boost { \
  229. namespace hash_detail { \
  230. \
  231. template <> \
  232. struct call_##cpp_func<type1> { \
  233. typedef type1 float_type; \
  234. inline type1 operator()(type1 x, type2 y) const { \
  235. return c99_func(x, y); \
  236. } \
  237. }; \
  238. } \
  239. }
  240. #if defined(ldexpf)
  241. BOOST_HASH_CALL_FLOAT_MACRO(ldexp, ldexpf, float, int)
  242. #else
  243. BOOST_HASH_CALL_FLOAT_FUNC(ldexp, ldexpf, float, int)
  244. #endif
  245. #if defined(ldexpl)
  246. BOOST_HASH_CALL_FLOAT_MACRO(ldexp, ldexpl, long double, int)
  247. #else
  248. BOOST_HASH_CALL_FLOAT_FUNC(ldexp, ldexpl, long double, int)
  249. #endif
  250. #if defined(frexpf)
  251. BOOST_HASH_CALL_FLOAT_MACRO(frexp, frexpf, float, int*)
  252. #else
  253. BOOST_HASH_CALL_FLOAT_FUNC(frexp, frexpf, float, int*)
  254. #endif
  255. #if defined(frexpl)
  256. BOOST_HASH_CALL_FLOAT_MACRO(frexp, frexpl, long double, int*)
  257. #else
  258. BOOST_HASH_CALL_FLOAT_FUNC(frexp, frexpl, long double, int*)
  259. #endif
  260. #undef BOOST_HASH_CALL_FLOAT_MACRO
  261. #undef BOOST_HASH_CALL_FLOAT_FUNC
  262. namespace boost
  263. {
  264. namespace hash_detail
  265. {
  266. template <typename Float1, typename Float2>
  267. struct select_hash_type_impl {
  268. typedef double type;
  269. };
  270. template <>
  271. struct select_hash_type_impl<float, float> {
  272. typedef float type;
  273. };
  274. template <>
  275. struct select_hash_type_impl<long double, long double> {
  276. typedef long double type;
  277. };
  278. // select_hash_type
  279. //
  280. // If there is support for a particular floating point type, use that
  281. // otherwise use double (there's always support for double).
  282. template <typename Float>
  283. struct select_hash_type : select_hash_type_impl<
  284. BOOST_DEDUCED_TYPENAME call_ldexp<Float>::float_type,
  285. BOOST_DEDUCED_TYPENAME call_frexp<Float>::float_type
  286. > {};
  287. }
  288. }
  289. #endif // BOOST_HASH_CONFORMANT_FLOATS
  290. #endif