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
26 #include <boost/assert.hpp>
27 #include <boost/cstdint.hpp>
28 #include <boost/predef/other/endian.h>
29 #include <boost/static_assert.hpp>
30 #include <boost/type_traits/is_floating_point.hpp>
32 #ifdef BOOST_NO_STDC_NAMESPACE
33 namespace std{ using ::memcpy; }
42 #define FP_SUBNORMAL 4
46 #define BOOST_HAS_FPCLASSIFY
49 # if (defined(__GLIBCPP__) || defined(__GLIBCXX__)) \
50 && defined(_GLIBCXX_USE_C99_MATH) \
51 && !(defined(_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) \
52 && (_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC != 0))
53 # ifdef _STLP_VENDOR_CSTD
54 # if _STLPORT_VERSION >= 0x520
55 # define BOOST_FPCLASSIFY_PREFIX ::__std_alias::
57 # define BOOST_FPCLASSIFY_PREFIX ::_STLP_VENDOR_CSTD::
60 # define BOOST_FPCLASSIFY_PREFIX ::std::
63 # undef BOOST_HAS_FPCLASSIFY
64 # define BOOST_FPCLASSIFY_PREFIX
66 #elif (defined(__HP_aCC) && !defined(__hppa))
67 // aCC 6 appears to do "#define fpclassify fpclassify" which messes us up a bit!
68 # define BOOST_FPCLASSIFY_PREFIX ::
70 # define BOOST_FPCLASSIFY_PREFIX
74 # undef BOOST_HAS_FPCLASSIFY
80 //------------------------------------------------------------------------------
86 //------------------------------------------------------------------------------
89 The following classes are used to tag the different methods that are used
90 for floating point classification
94 template <bool has_limits>
95 struct generic_tag {};
97 struct ieee_copy_all_bits_tag : public ieee_tag {};
98 struct ieee_copy_leading_bits_tag : public ieee_tag {};
100 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
102 // These helper functions are used only when numeric_limits<>
103 // members are not compile time constants:
105 inline bool is_generic_tag_false(const generic_tag<false>*)
109 inline bool is_generic_tag_false(const void*)
115 //------------------------------------------------------------------------------
118 Most processors support three different floating point precisions:
119 single precision (32 bits), double precision (64 bits)
120 and extended double precision (80 - 128 bits, depending on the processor)
122 Note that the C++ type long double can be implemented
123 both as double precision and extended double precision.
126 struct unknown_precision{};
127 struct single_precision {};
128 struct double_precision {};
129 struct extended_double_precision {};
131 // native_tag version --------------------------------------------------------------
133 template<class T> struct fp_traits_native
135 typedef native_tag method;
138 // generic_tag version -------------------------------------------------------------
140 template<class T, class U> struct fp_traits_non_native
142 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
143 typedef generic_tag<std::numeric_limits<T>::is_specialized> method;
145 typedef generic_tag<false> method;
149 // ieee_tag versions ---------------------------------------------------------------
152 These specializations of fp_traits_non_native contain information needed
153 to "parse" the binary representation of a floating point number.
157 bits -- the target type when copying the leading bytes of a floating
158 point number. It is a typedef for uint32_t or uint64_t.
160 method -- tells us whether all bytes are copied or not.
161 It is a typedef for ieee_copy_all_bits_tag or ieee_copy_leading_bits_tag.
165 sign, exponent, flag, significand -- bit masks that give the meaning of the
166 bits in the leading bytes.
168 Static function members:
170 get_bits(), set_bits() -- provide access to the leading bytes.
174 // ieee_tag version, float (32 bits) -----------------------------------------------
176 #ifndef BOOST_FPCLASSIFY_VAX_FORMAT
178 template<> struct fp_traits_non_native<float, single_precision>
180 typedef ieee_copy_all_bits_tag method;
182 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
183 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7f800000);
184 BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00000000);
185 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x007fffff);
187 typedef uint32_t bits;
188 static void get_bits(float x, uint32_t& a) { std::memcpy(&a, &x, 4); }
189 static void set_bits(float& x, uint32_t a) { std::memcpy(&x, &a, 4); }
192 // ieee_tag version, double (64 bits) ----------------------------------------------
194 #if defined(BOOST_NO_INT64_T) || defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION) \
195 || defined(BOOST_BORLANDC) || defined(__CODEGEAR__)
197 template<> struct fp_traits_non_native<double, double_precision>
199 typedef ieee_copy_leading_bits_tag method;
201 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
202 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7ff00000);
203 BOOST_STATIC_CONSTANT(uint32_t, flag = 0);
204 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x000fffff);
206 typedef uint32_t bits;
208 static void get_bits(double x, uint32_t& a)
210 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
213 static void set_bits(double& x, uint32_t a)
215 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
220 #if BOOST_ENDIAN_BIG_BYTE
221 BOOST_STATIC_CONSTANT(int, offset_ = 0);
222 #elif BOOST_ENDIAN_LITTLE_BYTE
223 BOOST_STATIC_CONSTANT(int, offset_ = 4);
225 BOOST_STATIC_ASSERT(false);
229 //..............................................................................
233 template<> struct fp_traits_non_native<double, double_precision>
235 typedef ieee_copy_all_bits_tag method;
237 static const uint64_t sign = ((uint64_t)0x80000000u) << 32;
238 static const uint64_t exponent = ((uint64_t)0x7ff00000) << 32;
239 static const uint64_t flag = 0;
240 static const uint64_t significand
241 = (((uint64_t)0x000fffff) << 32) + ((uint64_t)0xffffffffu);
243 typedef uint64_t bits;
244 static void get_bits(double x, uint64_t& a) { std::memcpy(&a, &x, 8); }
245 static void set_bits(double& x, uint64_t a) { std::memcpy(&x, &a, 8); }
250 #endif // #ifndef BOOST_FPCLASSIFY_VAX_FORMAT
252 // long double (64 bits) -------------------------------------------------------
254 #if defined(BOOST_NO_INT64_T) || defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)\
255 || defined(BOOST_BORLANDC) || defined(__CODEGEAR__)
257 template<> struct fp_traits_non_native<long double, double_precision>
259 typedef ieee_copy_leading_bits_tag method;
261 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
262 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7ff00000);
263 BOOST_STATIC_CONSTANT(uint32_t, flag = 0);
264 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x000fffff);
266 typedef uint32_t bits;
268 static void get_bits(long double x, uint32_t& a)
270 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
273 static void set_bits(long double& x, uint32_t a)
275 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
280 #if BOOST_ENDIAN_BIG_BYTE
281 BOOST_STATIC_CONSTANT(int, offset_ = 0);
282 #elif BOOST_ENDIAN_LITTLE_BYTE
283 BOOST_STATIC_CONSTANT(int, offset_ = 4);
285 BOOST_STATIC_ASSERT(false);
289 //..............................................................................
293 template<> struct fp_traits_non_native<long double, double_precision>
295 typedef ieee_copy_all_bits_tag method;
297 static const uint64_t sign = (uint64_t)0x80000000u << 32;
298 static const uint64_t exponent = (uint64_t)0x7ff00000 << 32;
299 static const uint64_t flag = 0;
300 static const uint64_t significand
301 = ((uint64_t)0x000fffff << 32) + (uint64_t)0xffffffffu;
303 typedef uint64_t bits;
304 static void get_bits(long double x, uint64_t& a) { std::memcpy(&a, &x, 8); }
305 static void set_bits(long double& x, uint64_t a) { std::memcpy(&x, &a, 8); }
311 // long double (>64 bits), x86 and x64 -----------------------------------------
313 #if defined(__i386) || defined(__i386__) || defined(_M_IX86) \
314 || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) \
315 || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64)
317 // Intel extended double precision format (80 bits)
320 struct fp_traits_non_native<long double, extended_double_precision>
322 typedef ieee_copy_leading_bits_tag method;
324 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
325 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7fff0000);
326 BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00008000);
327 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x00007fff);
329 typedef uint32_t bits;
331 static void get_bits(long double x, uint32_t& a)
333 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + 6, 4);
336 static void set_bits(long double& x, uint32_t a)
338 std::memcpy(reinterpret_cast<unsigned char*>(&x) + 6, &a, 4);
343 // long double (>64 bits), Itanium ---------------------------------------------
345 #elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
347 // The floating point format is unknown at compile time
348 // No template specialization is provided.
349 // The generic_tag definition is used.
351 // The Itanium supports both
352 // the Intel extended double precision format (80 bits) and
353 // the IEEE extended double precision format with 15 exponent bits (128 bits).
355 #elif defined(__GNUC__) && (LDBL_MANT_DIG == 106)
358 // Define nothing here and fall though to generic_tag:
359 // We have GCC's "double double" in effect, and any attempt
360 // to handle it via bit-fiddling is pretty much doomed to fail...
363 // long double (>64 bits), PowerPC ---------------------------------------------
365 #elif defined(__powerpc) || defined(__powerpc__) || defined(__POWERPC__) \
366 || defined(__ppc) || defined(__ppc__) || defined(__PPC__)
368 // PowerPC extended double precision format (128 bits)
371 struct fp_traits_non_native<long double, extended_double_precision>
373 typedef ieee_copy_leading_bits_tag method;
375 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
376 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7ff00000);
377 BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00000000);
378 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x000fffff);
380 typedef uint32_t bits;
382 static void get_bits(long double x, uint32_t& a)
384 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
387 static void set_bits(long double& x, uint32_t a)
389 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
394 #if BOOST_ENDIAN_BIG_BYTE
395 BOOST_STATIC_CONSTANT(int, offset_ = 0);
396 #elif BOOST_ENDIAN_LITTLE_BYTE
397 BOOST_STATIC_CONSTANT(int, offset_ = 12);
399 BOOST_STATIC_ASSERT(false);
404 // long double (>64 bits), Motorola 68K ----------------------------------------
406 #elif defined(__m68k) || defined(__m68k__) \
407 || defined(__mc68000) || defined(__mc68000__) \
409 // Motorola extended double precision format (96 bits)
411 // It is the same format as the Intel extended double precision format,
412 // except that 1) it is big-endian, 2) the 3rd and 4th byte are padding, and
413 // 3) the flag bit is not set for infinity
416 struct fp_traits_non_native<long double, extended_double_precision>
418 typedef ieee_copy_leading_bits_tag method;
420 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
421 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7fff0000);
422 BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00008000);
423 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x00007fff);
425 // copy 1st, 2nd, 5th and 6th byte. 3rd and 4th byte are padding.
427 typedef uint32_t bits;
429 static void get_bits(long double x, uint32_t& a)
431 std::memcpy(&a, &x, 2);
432 std::memcpy(reinterpret_cast<unsigned char*>(&a) + 2,
433 reinterpret_cast<const unsigned char*>(&x) + 4, 2);
436 static void set_bits(long double& x, uint32_t a)
438 std::memcpy(&x, &a, 2);
439 std::memcpy(reinterpret_cast<unsigned char*>(&x) + 4,
440 reinterpret_cast<const unsigned char*>(&a) + 2, 2);
445 // long double (>64 bits), All other processors --------------------------------
449 // IEEE extended double precision format with 15 exponent bits (128 bits)
452 struct fp_traits_non_native<long double, extended_double_precision>
454 typedef ieee_copy_leading_bits_tag method;
456 BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u);
457 BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7fff0000);
458 BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00000000);
459 BOOST_STATIC_CONSTANT(uint32_t, significand = 0x0000ffff);
461 typedef uint32_t bits;
463 static void get_bits(long double x, uint32_t& a)
465 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
468 static void set_bits(long double& x, uint32_t a)
470 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
475 #if BOOST_ENDIAN_BIG_BYTE
476 BOOST_STATIC_CONSTANT(int, offset_ = 0);
477 #elif BOOST_ENDIAN_LITTLE_BYTE
478 BOOST_STATIC_CONSTANT(int, offset_ = 12);
480 BOOST_STATIC_ASSERT(false);
486 //------------------------------------------------------------------------------
488 // size_to_precision is a type switch for converting a C++ floating point type
489 // to the corresponding precision type.
491 template<int n, bool fp> struct size_to_precision
493 typedef unknown_precision type;
496 template<> struct size_to_precision<4, true>
498 typedef single_precision type;
501 template<> struct size_to_precision<8, true>
503 typedef double_precision type;
506 template<> struct size_to_precision<10, true>
508 typedef extended_double_precision type;
511 template<> struct size_to_precision<12, true>
513 typedef extended_double_precision type;
516 template<> struct size_to_precision<16, true>
518 typedef extended_double_precision type;
521 //------------------------------------------------------------------------------
523 // Figure out whether to use native classification functions based on
524 // whether T is a built in floating point type or not:
529 typedef BOOST_DEDUCED_TYPENAME size_to_precision<sizeof(T), ::boost::is_floating_point<T>::value>::type precision;
530 typedef fp_traits_non_native<T, precision> type;
533 struct select_native<float>
535 typedef fp_traits_native<float> type;
538 struct select_native<double>
540 typedef fp_traits_native<double> type;
543 struct select_native<long double>
545 typedef fp_traits_native<long double> type;
548 //------------------------------------------------------------------------------
550 // fp_traits is a type switch that selects the right fp_traits_non_native
552 #if (defined(BOOST_MATH_USE_C99) && !(defined(__GNUC__) && (__GNUC__ < 4))) \
553 && !defined(__hpux) \
554 && !defined(__DECCXX)\
555 && !defined(__osf__) \
556 && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)\
557 && !defined(__FAST_MATH__)\
558 && !defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY)\
559 && !defined(BOOST_INTEL)\
561 && !defined(__VXWORKS__)
562 # define BOOST_MATH_USE_STD_FPCLASSIFY
565 template<class T> struct fp_traits
567 typedef BOOST_DEDUCED_TYPENAME size_to_precision<sizeof(T), ::boost::is_floating_point<T>::value>::type precision;
568 #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && !defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY)
569 typedef typename select_native<T>::type type;
571 typedef fp_traits_non_native<T, precision> type;
573 typedef fp_traits_non_native<T, precision> sign_change_type;
576 //------------------------------------------------------------------------------
578 } // namespace detail