]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/math/special_functions/detail/fp_traits.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / math / special_functions / detail / fp_traits.hpp
1 // fp_traits.hpp
2
3 #ifndef BOOST_MATH_FP_TRAITS_HPP
4 #define BOOST_MATH_FP_TRAITS_HPP
5
6 // Copyright (c) 2006 Johan Rade
7
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)
11
12 /*
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.
16 */
17
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
21 #endif
22
23 #include <cstring>
24 #include <limits>
25
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>
31
32 #ifdef BOOST_NO_STDC_NAMESPACE
33 namespace std{ using ::memcpy; }
34 #endif
35
36 #ifndef FP_NORMAL
37
38 #define FP_ZERO 0
39 #define FP_NORMAL 1
40 #define FP_INFINITE 2
41 #define FP_NAN 3
42 #define FP_SUBNORMAL 4
43
44 #else
45
46 #define BOOST_HAS_FPCLASSIFY
47
48 #ifndef 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::
56 # else
57 # define BOOST_FPCLASSIFY_PREFIX ::_STLP_VENDOR_CSTD::
58 # endif
59 # else
60 # define BOOST_FPCLASSIFY_PREFIX ::std::
61 # endif
62 # else
63 # undef BOOST_HAS_FPCLASSIFY
64 # define BOOST_FPCLASSIFY_PREFIX
65 # endif
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 ::
69 #else
70 # define BOOST_FPCLASSIFY_PREFIX
71 #endif
72
73 #ifdef __MINGW32__
74 # undef BOOST_HAS_FPCLASSIFY
75 #endif
76
77 #endif
78
79
80 //------------------------------------------------------------------------------
81
82 namespace boost {
83 namespace math {
84 namespace detail {
85
86 //------------------------------------------------------------------------------
87
88 /*
89 The following classes are used to tag the different methods that are used
90 for floating point classification
91 */
92
93 struct native_tag {};
94 template <bool has_limits>
95 struct generic_tag {};
96 struct ieee_tag {};
97 struct ieee_copy_all_bits_tag : public ieee_tag {};
98 struct ieee_copy_leading_bits_tag : public ieee_tag {};
99
100 #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
101 //
102 // These helper functions are used only when numeric_limits<>
103 // members are not compile time constants:
104 //
105 inline bool is_generic_tag_false(const generic_tag<false>*)
106 {
107 return true;
108 }
109 inline bool is_generic_tag_false(const void*)
110 {
111 return false;
112 }
113 #endif
114
115 //------------------------------------------------------------------------------
116
117 /*
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)
121
122 Note that the C++ type long double can be implemented
123 both as double precision and extended double precision.
124 */
125
126 struct unknown_precision{};
127 struct single_precision {};
128 struct double_precision {};
129 struct extended_double_precision {};
130
131 // native_tag version --------------------------------------------------------------
132
133 template<class T> struct fp_traits_native
134 {
135 typedef native_tag method;
136 };
137
138 // generic_tag version -------------------------------------------------------------
139
140 template<class T, class U> struct fp_traits_non_native
141 {
142 #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS
143 typedef generic_tag<std::numeric_limits<T>::is_specialized> method;
144 #else
145 typedef generic_tag<false> method;
146 #endif
147 };
148
149 // ieee_tag versions ---------------------------------------------------------------
150
151 /*
152 These specializations of fp_traits_non_native contain information needed
153 to "parse" the binary representation of a floating point number.
154
155 Typedef members:
156
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.
159
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.
162
163 Static data members:
164
165 sign, exponent, flag, significand -- bit masks that give the meaning of the
166 bits in the leading bytes.
167
168 Static function members:
169
170 get_bits(), set_bits() -- provide access to the leading bytes.
171
172 */
173
174 // ieee_tag version, float (32 bits) -----------------------------------------------
175
176 #ifndef BOOST_FPCLASSIFY_VAX_FORMAT
177
178 template<> struct fp_traits_non_native<float, single_precision>
179 {
180 typedef ieee_copy_all_bits_tag method;
181
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);
186
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); }
190 };
191
192 // ieee_tag version, double (64 bits) ----------------------------------------------
193
194 #if defined(BOOST_NO_INT64_T) || defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION) \
195 || defined(BOOST_BORLANDC) || defined(__CODEGEAR__)
196
197 template<> struct fp_traits_non_native<double, double_precision>
198 {
199 typedef ieee_copy_leading_bits_tag method;
200
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);
205
206 typedef uint32_t bits;
207
208 static void get_bits(double x, uint32_t& a)
209 {
210 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
211 }
212
213 static void set_bits(double& x, uint32_t a)
214 {
215 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
216 }
217
218 private:
219
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);
224 #else
225 BOOST_STATIC_ASSERT(false);
226 #endif
227 };
228
229 //..............................................................................
230
231 #else
232
233 template<> struct fp_traits_non_native<double, double_precision>
234 {
235 typedef ieee_copy_all_bits_tag method;
236
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);
242
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); }
246 };
247
248 #endif
249
250 #endif // #ifndef BOOST_FPCLASSIFY_VAX_FORMAT
251
252 // long double (64 bits) -------------------------------------------------------
253
254 #if defined(BOOST_NO_INT64_T) || defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)\
255 || defined(BOOST_BORLANDC) || defined(__CODEGEAR__)
256
257 template<> struct fp_traits_non_native<long double, double_precision>
258 {
259 typedef ieee_copy_leading_bits_tag method;
260
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);
265
266 typedef uint32_t bits;
267
268 static void get_bits(long double x, uint32_t& a)
269 {
270 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
271 }
272
273 static void set_bits(long double& x, uint32_t a)
274 {
275 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
276 }
277
278 private:
279
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);
284 #else
285 BOOST_STATIC_ASSERT(false);
286 #endif
287 };
288
289 //..............................................................................
290
291 #else
292
293 template<> struct fp_traits_non_native<long double, double_precision>
294 {
295 typedef ieee_copy_all_bits_tag method;
296
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;
302
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); }
306 };
307
308 #endif
309
310
311 // long double (>64 bits), x86 and x64 -----------------------------------------
312
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)
316
317 // Intel extended double precision format (80 bits)
318
319 template<>
320 struct fp_traits_non_native<long double, extended_double_precision>
321 {
322 typedef ieee_copy_leading_bits_tag method;
323
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);
328
329 typedef uint32_t bits;
330
331 static void get_bits(long double x, uint32_t& a)
332 {
333 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + 6, 4);
334 }
335
336 static void set_bits(long double& x, uint32_t a)
337 {
338 std::memcpy(reinterpret_cast<unsigned char*>(&x) + 6, &a, 4);
339 }
340 };
341
342
343 // long double (>64 bits), Itanium ---------------------------------------------
344
345 #elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64)
346
347 // The floating point format is unknown at compile time
348 // No template specialization is provided.
349 // The generic_tag definition is used.
350
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).
354
355 #elif defined(__GNUC__) && (LDBL_MANT_DIG == 106)
356
357 //
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...
361 //
362
363 // long double (>64 bits), PowerPC ---------------------------------------------
364
365 #elif defined(__powerpc) || defined(__powerpc__) || defined(__POWERPC__) \
366 || defined(__ppc) || defined(__ppc__) || defined(__PPC__)
367
368 // PowerPC extended double precision format (128 bits)
369
370 template<>
371 struct fp_traits_non_native<long double, extended_double_precision>
372 {
373 typedef ieee_copy_leading_bits_tag method;
374
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);
379
380 typedef uint32_t bits;
381
382 static void get_bits(long double x, uint32_t& a)
383 {
384 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
385 }
386
387 static void set_bits(long double& x, uint32_t a)
388 {
389 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
390 }
391
392 private:
393
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);
398 #else
399 BOOST_STATIC_ASSERT(false);
400 #endif
401 };
402
403
404 // long double (>64 bits), Motorola 68K ----------------------------------------
405
406 #elif defined(__m68k) || defined(__m68k__) \
407 || defined(__mc68000) || defined(__mc68000__) \
408
409 // Motorola extended double precision format (96 bits)
410
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
414
415 template<>
416 struct fp_traits_non_native<long double, extended_double_precision>
417 {
418 typedef ieee_copy_leading_bits_tag method;
419
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);
424
425 // copy 1st, 2nd, 5th and 6th byte. 3rd and 4th byte are padding.
426
427 typedef uint32_t bits;
428
429 static void get_bits(long double x, uint32_t& a)
430 {
431 std::memcpy(&a, &x, 2);
432 std::memcpy(reinterpret_cast<unsigned char*>(&a) + 2,
433 reinterpret_cast<const unsigned char*>(&x) + 4, 2);
434 }
435
436 static void set_bits(long double& x, uint32_t a)
437 {
438 std::memcpy(&x, &a, 2);
439 std::memcpy(reinterpret_cast<unsigned char*>(&x) + 4,
440 reinterpret_cast<const unsigned char*>(&a) + 2, 2);
441 }
442 };
443
444
445 // long double (>64 bits), All other processors --------------------------------
446
447 #else
448
449 // IEEE extended double precision format with 15 exponent bits (128 bits)
450
451 template<>
452 struct fp_traits_non_native<long double, extended_double_precision>
453 {
454 typedef ieee_copy_leading_bits_tag method;
455
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);
460
461 typedef uint32_t bits;
462
463 static void get_bits(long double x, uint32_t& a)
464 {
465 std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4);
466 }
467
468 static void set_bits(long double& x, uint32_t a)
469 {
470 std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4);
471 }
472
473 private:
474
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);
479 #else
480 BOOST_STATIC_ASSERT(false);
481 #endif
482 };
483
484 #endif
485
486 //------------------------------------------------------------------------------
487
488 // size_to_precision is a type switch for converting a C++ floating point type
489 // to the corresponding precision type.
490
491 template<int n, bool fp> struct size_to_precision
492 {
493 typedef unknown_precision type;
494 };
495
496 template<> struct size_to_precision<4, true>
497 {
498 typedef single_precision type;
499 };
500
501 template<> struct size_to_precision<8, true>
502 {
503 typedef double_precision type;
504 };
505
506 template<> struct size_to_precision<10, true>
507 {
508 typedef extended_double_precision type;
509 };
510
511 template<> struct size_to_precision<12, true>
512 {
513 typedef extended_double_precision type;
514 };
515
516 template<> struct size_to_precision<16, true>
517 {
518 typedef extended_double_precision type;
519 };
520
521 //------------------------------------------------------------------------------
522 //
523 // Figure out whether to use native classification functions based on
524 // whether T is a built in floating point type or not:
525 //
526 template <class T>
527 struct select_native
528 {
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;
531 };
532 template<>
533 struct select_native<float>
534 {
535 typedef fp_traits_native<float> type;
536 };
537 template<>
538 struct select_native<double>
539 {
540 typedef fp_traits_native<double> type;
541 };
542 template<>
543 struct select_native<long double>
544 {
545 typedef fp_traits_native<long double> type;
546 };
547
548 //------------------------------------------------------------------------------
549
550 // fp_traits is a type switch that selects the right fp_traits_non_native
551
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)\
560 && !defined(sun)\
561 && !defined(__VXWORKS__)
562 # define BOOST_MATH_USE_STD_FPCLASSIFY
563 #endif
564
565 template<class T> struct fp_traits
566 {
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;
570 #else
571 typedef fp_traits_non_native<T, precision> type;
572 #endif
573 typedef fp_traits_non_native<T, precision> sign_change_type;
574 };
575
576 //------------------------------------------------------------------------------
577
578 } // namespace detail
579 } // namespace math
580 } // namespace boost
581
582 #endif