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 | |
25 | #include <boost/assert.hpp> |
26 | #include <boost/cstdint.hpp> |
27 | #include <boost/predef/other/endian.h> |
28 | #include <boost/static_assert.hpp> |
29 | #include <boost/type_traits/is_floating_point.hpp> |
30 | |
31 | #ifdef BOOST_NO_STDC_NAMESPACE |
32 | namespace std{ using ::memcpy; } |
33 | #endif |
34 | |
35 | #ifndef FP_NORMAL |
36 | |
37 | #define FP_ZERO 0 |
38 | #define FP_NORMAL 1 |
39 | #define FP_INFINITE 2 |
40 | #define FP_NAN 3 |
41 | #define FP_SUBNORMAL 4 |
42 | |
43 | #else |
44 | |
45 | #define BOOST_HAS_FPCLASSIFY |
46 | |
47 | #ifndef 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:: |
55 | # else |
56 | # define BOOST_FPCLASSIFY_PREFIX ::_STLP_VENDOR_CSTD:: |
57 | # endif |
58 | # else |
59 | # define BOOST_FPCLASSIFY_PREFIX ::std:: |
60 | # endif |
61 | # else |
62 | # undef BOOST_HAS_FPCLASSIFY |
63 | # define BOOST_FPCLASSIFY_PREFIX |
64 | # endif |
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 :: |
68 | #else |
69 | # define BOOST_FPCLASSIFY_PREFIX |
70 | #endif |
71 | |
72 | #ifdef __MINGW32__ |
73 | # undef BOOST_HAS_FPCLASSIFY |
74 | #endif |
75 | |
76 | #endif |
77 | |
78 | |
79 | //------------------------------------------------------------------------------ |
80 | |
81 | namespace boost { |
82 | namespace math { |
83 | namespace detail { |
84 | |
85 | //------------------------------------------------------------------------------ |
86 | |
87 | /* |
88 | The following classes are used to tag the different methods that are used |
89 | for floating point classification |
90 | */ |
91 | |
92 | struct native_tag {}; |
93 | template <bool has_limits> |
94 | struct generic_tag {}; |
95 | struct ieee_tag {}; |
96 | struct ieee_copy_all_bits_tag : public ieee_tag {}; |
97 | struct ieee_copy_leading_bits_tag : public ieee_tag {}; |
98 | |
99 | #ifdef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS |
100 | // |
101 | // These helper functions are used only when numeric_limits<> |
102 | // members are not compile time constants: |
103 | // |
104 | inline bool is_generic_tag_false(const generic_tag<false>*) |
105 | { |
106 | return true; |
107 | } |
108 | inline bool is_generic_tag_false(const void*) |
109 | { |
110 | return false; |
111 | } |
112 | #endif |
113 | |
114 | //------------------------------------------------------------------------------ |
115 | |
116 | /* |
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) |
120 | |
121 | Note that the C++ type long double can be implemented |
122 | both as double precision and extended double precision. |
123 | */ |
124 | |
125 | struct unknown_precision{}; |
126 | struct single_precision {}; |
127 | struct double_precision {}; |
128 | struct extended_double_precision {}; |
129 | |
130 | // native_tag version -------------------------------------------------------------- |
131 | |
132 | template<class T> struct fp_traits_native |
133 | { |
134 | typedef native_tag method; |
135 | }; |
136 | |
137 | // generic_tag version ------------------------------------------------------------- |
138 | |
139 | template<class T, class U> struct fp_traits_non_native |
140 | { |
141 | #ifndef BOOST_NO_LIMITS_COMPILE_TIME_CONSTANTS |
142 | typedef generic_tag<std::numeric_limits<T>::is_specialized> method; |
143 | #else |
144 | typedef generic_tag<false> method; |
145 | #endif |
146 | }; |
147 | |
148 | // ieee_tag versions --------------------------------------------------------------- |
149 | |
150 | /* |
151 | These specializations of fp_traits_non_native contain information needed |
152 | to "parse" the binary representation of a floating point number. |
153 | |
154 | Typedef members: |
155 | |
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. |
158 | |
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. |
161 | |
162 | Static data members: |
163 | |
164 | sign, exponent, flag, significand -- bit masks that give the meaning of the |
165 | bits in the leading bytes. |
166 | |
167 | Static function members: |
168 | |
169 | get_bits(), set_bits() -- provide access to the leading bytes. |
170 | |
171 | */ |
172 | |
173 | // ieee_tag version, float (32 bits) ----------------------------------------------- |
174 | |
175 | #ifndef BOOST_FPCLASSIFY_VAX_FORMAT |
176 | |
177 | template<> struct fp_traits_non_native<float, single_precision> |
178 | { |
179 | typedef ieee_copy_all_bits_tag method; |
180 | |
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); |
185 | |
186 | typedef uint32_t bits; |
187 | static void get_bits(float x, uint32_t& a) { std::memcpy(dest: &a, src: &x, n: 4); } |
188 | static void set_bits(float& x, uint32_t a) { std::memcpy(dest: &x, src: &a, n: 4); } |
189 | }; |
190 | |
191 | // ieee_tag version, double (64 bits) ---------------------------------------------- |
192 | |
193 | #if defined(BOOST_NO_INT64_T) || defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION) \ |
194 | || defined(__BORLANDC__) || defined(__CODEGEAR__) |
195 | |
196 | template<> struct fp_traits_non_native<double, double_precision> |
197 | { |
198 | typedef ieee_copy_leading_bits_tag method; |
199 | |
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); |
204 | |
205 | typedef uint32_t bits; |
206 | |
207 | static void get_bits(double x, uint32_t& a) |
208 | { |
209 | std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4); |
210 | } |
211 | |
212 | static void set_bits(double& x, uint32_t a) |
213 | { |
214 | std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4); |
215 | } |
216 | |
217 | private: |
218 | |
219 | #if BOOST_ENDIAN_BIG_BYTE |
220 | BOOST_STATIC_CONSTANT(int, offset_ = 0); |
221 | #elif BOOST_ENDIAN_LITTLE_BYTE |
222 | BOOST_STATIC_CONSTANT(int, offset_ = 4); |
223 | #else |
224 | BOOST_STATIC_ASSERT(false); |
225 | #endif |
226 | }; |
227 | |
228 | //.............................................................................. |
229 | |
230 | #else |
231 | |
232 | template<> struct fp_traits_non_native<double, double_precision> |
233 | { |
234 | typedef ieee_copy_all_bits_tag method; |
235 | |
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); |
241 | |
242 | typedef uint64_t bits; |
243 | static void get_bits(double x, uint64_t& a) { std::memcpy(dest: &a, src: &x, n: 8); } |
244 | static void set_bits(double& x, uint64_t a) { std::memcpy(dest: &x, src: &a, n: 8); } |
245 | }; |
246 | |
247 | #endif |
248 | |
249 | #endif // #ifndef BOOST_FPCLASSIFY_VAX_FORMAT |
250 | |
251 | // long double (64 bits) ------------------------------------------------------- |
252 | |
253 | #if defined(BOOST_NO_INT64_T) || defined(BOOST_NO_INCLASS_MEMBER_INITIALIZATION)\ |
254 | || defined(__BORLANDC__) || defined(__CODEGEAR__) |
255 | |
256 | template<> struct fp_traits_non_native<long double, double_precision> |
257 | { |
258 | typedef ieee_copy_leading_bits_tag method; |
259 | |
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); |
264 | |
265 | typedef uint32_t bits; |
266 | |
267 | static void get_bits(long double x, uint32_t& a) |
268 | { |
269 | std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4); |
270 | } |
271 | |
272 | static void set_bits(long double& x, uint32_t a) |
273 | { |
274 | std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4); |
275 | } |
276 | |
277 | private: |
278 | |
279 | #if BOOST_ENDIAN_BIG_BYTE |
280 | BOOST_STATIC_CONSTANT(int, offset_ = 0); |
281 | #elif BOOST_ENDIAN_LITTLE_BYTE |
282 | BOOST_STATIC_CONSTANT(int, offset_ = 4); |
283 | #else |
284 | BOOST_STATIC_ASSERT(false); |
285 | #endif |
286 | }; |
287 | |
288 | //.............................................................................. |
289 | |
290 | #else |
291 | |
292 | template<> struct fp_traits_non_native<long double, double_precision> |
293 | { |
294 | typedef ieee_copy_all_bits_tag method; |
295 | |
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; |
301 | |
302 | typedef uint64_t bits; |
303 | static void get_bits(long double x, uint64_t& a) { std::memcpy(dest: &a, src: &x, n: 8); } |
304 | static void set_bits(long double& x, uint64_t a) { std::memcpy(dest: &x, src: &a, n: 8); } |
305 | }; |
306 | |
307 | #endif |
308 | |
309 | |
310 | // long double (>64 bits), x86 and x64 ----------------------------------------- |
311 | |
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) |
315 | |
316 | // Intel extended double precision format (80 bits) |
317 | |
318 | template<> |
319 | struct fp_traits_non_native<long double, extended_double_precision> |
320 | { |
321 | typedef ieee_copy_leading_bits_tag method; |
322 | |
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); |
327 | |
328 | typedef uint32_t bits; |
329 | |
330 | static void get_bits(long double x, uint32_t& a) |
331 | { |
332 | std::memcpy(dest: &a, src: reinterpret_cast<const unsigned char*>(&x) + 6, n: 4); |
333 | } |
334 | |
335 | static void set_bits(long double& x, uint32_t a) |
336 | { |
337 | std::memcpy(dest: reinterpret_cast<unsigned char*>(&x) + 6, src: &a, n: 4); |
338 | } |
339 | }; |
340 | |
341 | |
342 | // long double (>64 bits), Itanium --------------------------------------------- |
343 | |
344 | #elif defined(__ia64) || defined(__ia64__) || defined(_M_IA64) |
345 | |
346 | // The floating point format is unknown at compile time |
347 | // No template specialization is provided. |
348 | // The generic_tag definition is used. |
349 | |
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). |
353 | |
354 | #elif defined(__GNUC__) && (LDBL_MANT_DIG == 106) |
355 | |
356 | // |
357 | // Define nothing here and fall though to generic_tag: |
358 | // We have GCC's "double double" in effect, and any attempt |
359 | // to handle it via bit-fiddling is pretty much doomed to fail... |
360 | // |
361 | |
362 | // long double (>64 bits), PowerPC --------------------------------------------- |
363 | |
364 | #elif defined(__powerpc) || defined(__powerpc__) || defined(__POWERPC__) \ |
365 | || defined(__ppc) || defined(__ppc__) || defined(__PPC__) |
366 | |
367 | // PowerPC extended double precision format (128 bits) |
368 | |
369 | // Current 'fp_traits_non_native' does not work correctly with IBM long double |
370 | // due the fact that for some operations, like sign manipulation, the algorithm |
371 | // should manipulate both 'double' value. For algorithms that only depend on |
372 | // the most significant 32 bits (for instance, isinf or isnan), using the |
373 | // template double especialization is suffient. |
374 | |
375 | #if 0 |
376 | template<> |
377 | struct fp_traits_non_native<long double, extended_double_precision> |
378 | { |
379 | typedef ieee_copy_leading_bits_tag method; |
380 | |
381 | BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u); |
382 | BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7ff00000); |
383 | BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00000000); |
384 | BOOST_STATIC_CONSTANT(uint32_t, significand = 0x000fffff); |
385 | |
386 | typedef uint32_t bits; |
387 | |
388 | static void get_bits(long double x, uint32_t& a) |
389 | { |
390 | std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4); |
391 | } |
392 | |
393 | static void set_bits(long double& x, uint32_t a) |
394 | { |
395 | std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4); |
396 | } |
397 | |
398 | private: |
399 | |
400 | #if BOOST_ENDIAN_BIG_BYTE |
401 | BOOST_STATIC_CONSTANT(int, offset_ = 0); |
402 | #elif BOOST_ENDIAN_LITTLE_BYTE |
403 | BOOST_STATIC_CONSTANT(int, offset_ = 12); |
404 | #else |
405 | BOOST_STATIC_ASSERT(false); |
406 | #endif |
407 | }; |
408 | #endif |
409 | |
410 | // long double (>64 bits), Motorola 68K ---------------------------------------- |
411 | |
412 | #elif defined(__m68k) || defined(__m68k__) \ |
413 | || defined(__mc68000) || defined(__mc68000__) \ |
414 | |
415 | // Motorola extended double precision format (96 bits) |
416 | |
417 | // It is the same format as the Intel extended double precision format, |
418 | // except that 1) it is big-endian, 2) the 3rd and 4th byte are padding, and |
419 | // 3) the flag bit is not set for infinity |
420 | |
421 | template<> |
422 | struct fp_traits_non_native<long double, extended_double_precision> |
423 | { |
424 | typedef ieee_copy_leading_bits_tag method; |
425 | |
426 | BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u); |
427 | BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7fff0000); |
428 | BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00008000); |
429 | BOOST_STATIC_CONSTANT(uint32_t, significand = 0x00007fff); |
430 | |
431 | // copy 1st, 2nd, 5th and 6th byte. 3rd and 4th byte are padding. |
432 | |
433 | typedef uint32_t bits; |
434 | |
435 | static void get_bits(long double x, uint32_t& a) |
436 | { |
437 | std::memcpy(&a, &x, 2); |
438 | std::memcpy(reinterpret_cast<unsigned char*>(&a) + 2, |
439 | reinterpret_cast<const unsigned char*>(&x) + 4, 2); |
440 | } |
441 | |
442 | static void set_bits(long double& x, uint32_t a) |
443 | { |
444 | std::memcpy(&x, &a, 2); |
445 | std::memcpy(reinterpret_cast<unsigned char*>(&x) + 4, |
446 | reinterpret_cast<const unsigned char*>(&a) + 2, 2); |
447 | } |
448 | }; |
449 | |
450 | |
451 | // long double (>64 bits), All other processors -------------------------------- |
452 | |
453 | #else |
454 | |
455 | // IEEE extended double precision format with 15 exponent bits (128 bits) |
456 | |
457 | template<> |
458 | struct fp_traits_non_native<long double, extended_double_precision> |
459 | { |
460 | typedef ieee_copy_leading_bits_tag method; |
461 | |
462 | BOOST_STATIC_CONSTANT(uint32_t, sign = 0x80000000u); |
463 | BOOST_STATIC_CONSTANT(uint32_t, exponent = 0x7fff0000); |
464 | BOOST_STATIC_CONSTANT(uint32_t, flag = 0x00000000); |
465 | BOOST_STATIC_CONSTANT(uint32_t, significand = 0x0000ffff); |
466 | |
467 | typedef uint32_t bits; |
468 | |
469 | static void get_bits(long double x, uint32_t& a) |
470 | { |
471 | std::memcpy(&a, reinterpret_cast<const unsigned char*>(&x) + offset_, 4); |
472 | } |
473 | |
474 | static void set_bits(long double& x, uint32_t a) |
475 | { |
476 | std::memcpy(reinterpret_cast<unsigned char*>(&x) + offset_, &a, 4); |
477 | } |
478 | |
479 | private: |
480 | |
481 | #if BOOST_ENDIAN_BIG_BYTE |
482 | BOOST_STATIC_CONSTANT(int, offset_ = 0); |
483 | #elif BOOST_ENDIAN_LITTLE_BYTE |
484 | BOOST_STATIC_CONSTANT(int, offset_ = 12); |
485 | #else |
486 | BOOST_STATIC_ASSERT(false); |
487 | #endif |
488 | }; |
489 | |
490 | #endif |
491 | |
492 | //------------------------------------------------------------------------------ |
493 | |
494 | // size_to_precision is a type switch for converting a C++ floating point type |
495 | // to the corresponding precision type. |
496 | |
497 | template<int n, bool fp> struct size_to_precision |
498 | { |
499 | typedef unknown_precision type; |
500 | }; |
501 | |
502 | template<> struct size_to_precision<4, true> |
503 | { |
504 | typedef single_precision type; |
505 | }; |
506 | |
507 | template<> struct size_to_precision<8, true> |
508 | { |
509 | typedef double_precision type; |
510 | }; |
511 | |
512 | template<> struct size_to_precision<10, true> |
513 | { |
514 | typedef extended_double_precision type; |
515 | }; |
516 | |
517 | template<> struct size_to_precision<12, true> |
518 | { |
519 | typedef extended_double_precision type; |
520 | }; |
521 | |
522 | template<> struct size_to_precision<16, true> |
523 | { |
524 | typedef extended_double_precision type; |
525 | }; |
526 | |
527 | //------------------------------------------------------------------------------ |
528 | // |
529 | // Figure out whether to use native classification functions based on |
530 | // whether T is a built in floating point type or not: |
531 | // |
532 | template <class T> |
533 | struct select_native |
534 | { |
535 | typedef BOOST_DEDUCED_TYPENAME size_to_precision<sizeof(T), ::boost::is_floating_point<T>::value>::type precision; |
536 | typedef fp_traits_non_native<T, precision> type; |
537 | }; |
538 | template<> |
539 | struct select_native<float> |
540 | { |
541 | typedef fp_traits_native<float> type; |
542 | }; |
543 | template<> |
544 | struct select_native<double> |
545 | { |
546 | typedef fp_traits_native<double> type; |
547 | }; |
548 | template<> |
549 | struct select_native<long double> |
550 | { |
551 | typedef fp_traits_native<long double> type; |
552 | }; |
553 | |
554 | //------------------------------------------------------------------------------ |
555 | |
556 | // fp_traits is a type switch that selects the right fp_traits_non_native |
557 | |
558 | #if (defined(BOOST_MATH_USE_C99) && !(defined(__GNUC__) && (__GNUC__ < 4))) \ |
559 | && !defined(__hpux) \ |
560 | && !defined(__DECCXX)\ |
561 | && !defined(__osf__) \ |
562 | && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION)\ |
563 | && !defined(__FAST_MATH__)\ |
564 | && !defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY)\ |
565 | && !defined(BOOST_INTEL)\ |
566 | && !defined(sun)\ |
567 | && !defined(__VXWORKS__) |
568 | # define BOOST_MATH_USE_STD_FPCLASSIFY |
569 | #endif |
570 | |
571 | template<class T> struct fp_traits |
572 | { |
573 | typedef BOOST_DEDUCED_TYPENAME size_to_precision<sizeof(T), ::boost::is_floating_point<T>::value>::type precision; |
574 | #if defined(BOOST_MATH_USE_STD_FPCLASSIFY) && !defined(BOOST_MATH_DISABLE_STD_FPCLASSIFY) |
575 | typedef typename select_native<T>::type type; |
576 | #else |
577 | typedef fp_traits_non_native<T, precision> type; |
578 | #endif |
579 | typedef fp_traits_non_native<T, precision> sign_change_type; |
580 | }; |
581 | |
582 | //------------------------------------------------------------------------------ |
583 | |
584 | } // namespace detail |
585 | } // namespace math |
586 | } // namespace boost |
587 | |
588 | #endif |
589 | |