3 #ifndef BOOST_MATH_FP_TRAITS_HPP
4 #define BOOST_MATH_FP_TRAITS_HPP
6 // Copyright (c) 2006 Johan Rade
8 // Distributed under the Boost Software License, Version 1.0.
9 // (See accompanying file LICENSE_1_0.txt
10 // or copy at http://www.boost.org/LICENSE_1_0.txt)
13 To support old compilers, care has been taken to avoid partial template
14 specialization and meta function forwarding.
15 With these techniques, the code could be simplified.
18 #if defined(__vms) && defined(__DECCXX) && !__IEEE_FLOAT
19 // The VAX floating point formats are used (for float and double)
20 # define BOOST_FPCLASSIFY_VAX_FORMAT
25 #include <boost/assert.hpp>
26 #include <boost/cstdint.hpp>
27 #include <boost/detail/endian.hpp>
28 #include <boost/static_assert.hpp>
29 #include <boost/type_traits/is_floating_point.hpp>
31 #ifdef BOOST_NO_STDC_NAMESPACE
32 namespace std{ using ::memcpy; }
41 #define FP_SUBNORMAL 4
45 #define BOOST_HAS_FPCLASSIFY
48 # if (defined(__GLIBCPP__) || defined(__GLIBCXX__)) \
49 && defined(_GLIBCXX_USE_C99_MATH) \
50 && !(defined(_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) \
51 && (_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC != 0))
52 # ifdef _STLP_VENDOR_CSTD
53 # if _STLPORT_VERSION >= 0x520
54 # define BOOST_FPCLASSIFY_PREFIX ::__std_alias::
56 # define BOOST_FPCLASSIFY_PREFIX ::_STLP_VENDOR_CSTD::
59 # define BOOST_FPCLASSIFY_PREFIX ::std::
62 # undef BOOST_HAS_FPCLASSIFY
63 # define BOOST_FPCLASSIFY_PREFIX
65 #elif (defined(__HP_aCC) && !defined(__hppa))
66 // aCC 6 appears to do "#define fpclassify fpclassify" which messes us up a bit!
67 # define BOOST_FPCLASSIFY_PREFIX ::
69 # define BOOST_FPCLASSIFY_PREFIX
73 # undef BOOST_HAS_FPCLASSIFY
79 //------------------------------------------------------------------------------
85 //------------------------------------------------------------------------------
88 The following classes are used to tag the different methods that are used
89 for floating point classification
93 template <bool has_limits>
94 struct generic_tag {};
96 struct ieee_copy_all_bits_tag : public ieee_tag {};
97 struct ieee_copy_leading_bits_tag : public ieee_tag {};
99 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
101 // These helper functions are used only when numeric_limits<>
102 // members are not compile time constants:
104 inline bool is_generic_tag_false(const generic_tag<false>&)
108 inline bool is_generic_tag_false(...)
114 //------------------------------------------------------------------------------
117 Most processors support three different floating point precisions:
118 single precision (32 bits), double precision (64 bits)
119 and extended double precision (80 - 128 bits, depending on the processor)
121 Note that the C++ type long double can be implemented
122 both as double precision and extended double precision.
125 struct unknown_precision{};
126 struct single_precision {};
127 struct double_precision {};
128 struct extended_double_precision {};
130 // native_tag version --------------------------------------------------------------
132 template<class T> struct fp_traits_native
134 typedef native_tag method;
137 // generic_tag version -------------------------------------------------------------
139 template<class T, class U> struct fp_traits_non_native
141 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
142 typedef generic_tag<std::numeric_limits<T>::is_specialized> method;
144 typedef generic_tag<false> method;
148 // ieee_tag versions ---------------------------------------------------------------
151 These specializations of fp_traits_non_native contain information needed
152 to "parse" the binary representation of a floating point number.
156 bits -- the target type when copying the leading bytes of a floating
157 point number. It is a typedef for uint32_t or uint64_t.
159 method -- tells us whether all bytes are copied or not.
160 It is a typedef for ieee_copy_all_bits_tag or ieee_copy_leading_bits_tag.
164 sign, exponent, flag, significand -- bit masks that give the meaning of the
165 bits in the leading bytes.
167 Static function members:
169 get_bits(), set_bits() -- provide access to the leading bytes.
173 // ieee_tag version, float (32 bits) -----------------------------------------------
175 #ifndef BOOST_FPCLASSIFY_VAX_FORMAT
177 template<> struct fp_traits_non_native<float, single_precision>
179 typedef ieee_copy_all_bits_tag method;
181 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
182 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7f800000);
183 BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00000000);
184 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x007fffff);
186 typedef uint32_t bits;
187 static void get_bits(float x, uint32_t& a) { std::memcpy(&a, &x, 4); }
188 static void set_bits(float& x, uint32_t a) { std::memcpy(&x, &a, 4); }
191 // ieee_tag version, double (64 bits) ----------------------------------------------
193 #if defined(BOOST_NO_INT64_T) || defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION) \
194 || defined(__BORLANDC__) || defined(__CODEGEAR__)
196 template<> struct fp_traits_non_native<double, double_precision>
198 typedef ieee_copy_leading_bits_tag method;
200 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
201 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7ff00000);
202 BOOST_STATIC_CONSTANT(uint32_t, flag = 0);
203 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x000fffff);
205 typedef uint32_t bits;
207 static void get_bits(double x, uint32_t& a)
209 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
212 static void set_bits(double& x, uint32_t a)
214 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
219 #if defined(BOOST_BIG_ENDIAN)
220 BOOST_STATIC_CONSTANT(int, offset_ = 0);
221 #elif defined(BOOST_LITTLE_ENDIAN)
222 BOOST_STATIC_CONSTANT(int, offset_ = 4);
224 BOOST_STATIC_ASSERT(false);
228 //..............................................................................
232 template<> struct fp_traits_non_native<double, double_precision>
234 typedef ieee_copy_all_bits_tag method;
236 static const uint64_t sign = ((uint64_t)0x80000000u) << 32;
237 static const uint64_t exponent = ((uint64_t)0x7ff00000) << 32;
238 static const uint64_t flag = 0;
239 static const uint64_t significand
240 = (((uint64_t)0x000fffff) << 32) + ((uint64_t)0xffffffffu);
242 typedef uint64_t bits;
243 static void get_bits(double x, uint64_t& a) { std::memcpy(&a, &x, 8); }
244 static void set_bits(double& x, uint64_t a) { std::memcpy(&x, &a, 8); }
249 #endif // #ifndef BOOST_FPCLASSIFY_VAX_FORMAT
251 // long double (64 bits) -------------------------------------------------------
253 #if defined(BOOST_NO_INT64_T) || defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)\
254 || defined(__BORLANDC__) || defined(__CODEGEAR__)
256 template<> struct fp_traits_non_native<long double, double_precision>
258 typedef ieee_copy_leading_bits_tag method;
260 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
261 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7ff00000);
262 BOOST_STATIC_CONSTANT(uint32_t, flag = 0);
263 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x000fffff);
265 typedef uint32_t bits;
267 static void get_bits(long double x, uint32_t& a)
269 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
272 static void set_bits(long double& x, uint32_t a)
274 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
279 #if defined(BOOST_BIG_ENDIAN)
280 BOOST_STATIC_CONSTANT(int, offset_ = 0);
281 #elif defined(BOOST_LITTLE_ENDIAN)
282 BOOST_STATIC_CONSTANT(int, offset_ = 4);
284 BOOST_STATIC_ASSERT(false);
288 //..............................................................................
292 template<> struct fp_traits_non_native<long double, double_precision>
294 typedef ieee_copy_all_bits_tag method;
296 static const uint64_t sign = (uint64_t)0x80000000u << 32;
297 static const uint64_t exponent = (uint64_t)0x7ff00000 << 32;
298 static const uint64_t flag = 0;
299 static const uint64_t significand
300 = ((uint64_t)0x000fffff << 32) + (uint64_t)0xffffffffu;
302 typedef uint64_t bits;
303 static void get_bits(long double x, uint64_t& a) { std::memcpy(&a, &x, 8); }
304 static void set_bits(long double& x, uint64_t a) { std::memcpy(&x, &a, 8); }
310 // long double (>64 bits), x86 and x64 -----------------------------------------
312 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) \
313 || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) \
314 || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)
316 // Intel extended double precision format (80 bits)
319 struct fp_traits_non_native<long double, extended_double_precision>
321 typedef ieee_copy_leading_bits_tag method;
323 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
324 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7fff0000);
325 BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00008000);
326 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x00007fff);
328 typedef uint32_t bits;
330 static void get_bits(long double x, uint32_t& a)
332 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + 6, 4);
335 static void set_bits(long double& x, uint32_t a)
337 std::memcpy(reinterpret_cast<unsigned char*>(&x) + 6, &a, 4);
342 // long double (>64 bits), Itanium ---------------------------------------------
344 #elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
346 // The floating point format is unknown at compile time
347 // No template specialization is provided.
348 // The generic_tag definition is used.
350 // The Itanium supports both
351 // the Intel extended double precision format (80 bits) and
352 // the IEEE extended double precision format with 15 exponent bits (128 bits).
355 // long double (>64 bits), PowerPC ---------------------------------------------
357 #elif defined(__powerpc) || defined(__powerpc__) || defined(__POWERPC__) \
358 || defined(__ppc) || defined(__ppc__) || defined(__PPC__)
360 // PowerPC extended double precision format (128 bits)
363 struct fp_traits_non_native<long double, extended_double_precision>
365 typedef ieee_copy_leading_bits_tag method;
367 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
368 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7ff00000);
369 BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00000000);
370 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x000fffff);
372 typedef uint32_t bits;
374 static void get_bits(long double x, uint32_t& a)
376 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
379 static void set_bits(long double& x, uint32_t a)
381 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
386 #if defined(BOOST_BIG_ENDIAN)
387 BOOST_STATIC_CONSTANT(int, offset_ = 0);
388 #elif defined(BOOST_LITTLE_ENDIAN)
389 BOOST_STATIC_CONSTANT(int, offset_ = 12);
391 BOOST_STATIC_ASSERT(false);
396 // long double (>64 bits), Motorola 68K ----------------------------------------
398 #elif defined(__m68k) || defined(__m68k__) \
399 || defined(__mc68000) || defined(__mc68000__) \
401 // Motorola extended double precision format (96 bits)
403 // It is the same format as the Intel extended double precision format,
404 // except that 1) it is big-endian, 2) the 3rd and 4th byte are padding, and
405 // 3) the flag bit is not set for infinity
408 struct fp_traits_non_native<long double, extended_double_precision>
410 typedef ieee_copy_leading_bits_tag method;
412 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
413 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7fff0000);
414 BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00008000);
415 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x00007fff);
417 // copy 1st, 2nd, 5th and 6th byte. 3rd and 4th byte are padding.
419 typedef uint32_t bits;
421 static void get_bits(long double x, uint32_t& a)
423 std::memcpy(&a, &x, 2);
424 std::memcpy(reinterpret_cast<unsigned char*>(&a) + 2,
425 reinterpret_cast<const unsigned char*>(&x) + 4, 2);
428 static void set_bits(long double& x, uint32_t a)
430 std::memcpy(&x, &a, 2);
431 std::memcpy(reinterpret_cast<unsigned char*>(&x) + 4,
432 reinterpret_cast<const unsigned char*>(&a) + 2, 2);
437 // long double (>64 bits), All other processors --------------------------------
441 // IEEE extended double precision format with 15 exponent bits (128 bits)
444 struct fp_traits_non_native<long double, extended_double_precision>
446 typedef ieee_copy_leading_bits_tag method;
448 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
449 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7fff0000);
450 BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00000000);
451 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x0000ffff);
453 typedef uint32_t bits;
455 static void get_bits(long double x, uint32_t& a)
457 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
460 static void set_bits(long double& x, uint32_t a)
462 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
467 #if defined(BOOST_BIG_ENDIAN)
468 BOOST_STATIC_CONSTANT(int, offset_ = 0);
469 #elif defined(BOOST_LITTLE_ENDIAN)
470 BOOST_STATIC_CONSTANT(int, offset_ = 12);
472 BOOST_STATIC_ASSERT(false);
478 //------------------------------------------------------------------------------
480 // size_to_precision is a type switch for converting a C++ floating point type
481 // to the corresponding precision type.
483 template<int n, bool fp> struct size_to_precision
485 typedef unknown_precision type;
488 template<> struct size_to_precision<4, true>
490 typedef single_precision type;
493 template<> struct size_to_precision<8, true>
495 typedef double_precision type;
498 template<> struct size_to_precision<10, true>
500 typedef extended_double_precision type;
503 template<> struct size_to_precision<12, true>
505 typedef extended_double_precision type;
508 template<> struct size_to_precision<16, true>
510 typedef extended_double_precision type;
513 //------------------------------------------------------------------------------
515 // Figure out whether to use native classification functions based on
516 // whether T is a built in floating point type or not:
521 typedef BOOST_DEDUCED_TYPENAME size_to_precision<sizeof(T), ::boost::is_floating_point<T>::value>::type precision;
522 typedef fp_traits_non_native<T, precision> type;
525 struct select_native<float>
527 typedef fp_traits_native<float> type;
530 struct select_native<double>
532 typedef fp_traits_native<double> type;
535 struct select_native<long double>
537 typedef fp_traits_native<long double> type;
540 //------------------------------------------------------------------------------
542 // fp_traits is a type switch that selects the right fp_traits_non_native
544 #if (defined(BOOST_MATH_USE_C99) && !(defined(__GNUC__) && (__GNUC__ < 4))) \
545 && !defined(__hpux) \
546 && !defined(__DECCXX)\
547 && !defined(__osf__) \
548 && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)
549 # define BOOST_MATH_USE_STD_FPCLASSIFY
552 template<class T> struct fp_traits
554 #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && !defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY)
555 typedef typename select_native<T>::type type;
557 typedef BOOST_DEDUCED_TYPENAME size_to_precision<sizeof(T), ::boost::is_floating_point<T>::value>::type precision;
558 typedef fp_traits_non_native<T, precision> type;
562 //------------------------------------------------------------------------------
564 } // namespace detail