]> rtime.felk.cvut.cz Git - eurobot/public.git/blob - src/boost/boost/numeric/interval/detail/msvc_rounding_control.hpp
Add subset of boost library headers needed for compilation on PowerPC
[eurobot/public.git] / src / boost / boost / numeric / interval / detail / msvc_rounding_control.hpp
1 /* Boost interval/detail/msvc_rounding_control.hpp file
2  *
3  * Copyright 2000 Maarten Keijzer
4  * Copyright 2002 HervĂ© Brönnimann, Guillaume Melquiond, Sylvain Pion
5  *
6  * Distributed under the Boost Software License, Version 1.0.
7  * (See accompanying file LICENSE_1_0.txt or
8  * copy at http://www.boost.org/LICENSE_1_0.txt)
9  */
10
11 #ifndef BOOST_NUMERIC_INTERVAL_DETAIL_MSVC_ROUNDING_CONTROL_HPP
12 #define BOOST_NUMERIC_INTERVAL_DETAIL_MSVC_ROUNDING_CONTROL_HPP
13
14 #ifndef _MSC_VER
15 #  error This header is only intended for MSVC, but might work for Borland as well
16 #endif
17
18 #include <float.h>      // MSVC rounding control
19
20 // Although the function is called _control87, it seems to work for
21 // other FPUs too, so it does not have to be changed to _controlfp.
22
23 namespace boost {
24 namespace numeric {
25 namespace interval_lib {
26 namespace detail {
27
28 #if BOOST_MSVC < 1400 || defined(WIN64)
29 extern "C" { double rint(double); }
30 #else
31 inline double rint(double x)
32 {
33 _asm FLD [x] ;
34 _asm FRNDINT ;
35 //_asm RET ;
36 }
37 #endif
38
39 struct x86_rounding
40 {
41   static unsigned int hard2msvc(unsigned short m) {
42     unsigned int n = 0;
43     if (m & 0x01) n |= _EM_INVALID;
44     if (m & 0x02) n |= _EM_DENORMAL;
45     if (m & 0x04) n |= _EM_ZERODIVIDE;
46     if (m & 0x08) n |= _EM_OVERFLOW;
47     if (m & 0x10) n |= _EM_UNDERFLOW;
48     if (m & 0x20) n |= _EM_INEXACT;
49     switch (m & 0x300) {
50     case 0x000: n |= _PC_24; break;
51     case 0x200: n |= _PC_53; break;
52     case 0x300: n |= _PC_64; break;
53     }
54     switch (m & 0xC00) {
55     case 0x000: n |= _RC_NEAR; break;
56     case 0x400: n |= _RC_DOWN; break;
57     case 0x800: n |= _RC_UP;   break;
58     case 0xC00: n |= _RC_CHOP; break;
59     }
60     if (m & 0x1000) n |= _IC_AFFINE; // only useful on 287
61     return n;
62   }
63
64   static unsigned short msvc2hard(unsigned int n) {
65     unsigned short m = 0;
66     if (n & _EM_INVALID)    m |= 0x01;
67     if (n & _EM_DENORMAL)   m |= 0x02;
68     if (n & _EM_ZERODIVIDE) m |= 0x04;
69     if (n & _EM_OVERFLOW)   m |= 0x08;
70     if (n & _EM_UNDERFLOW)  m |= 0x10;
71     if (n & _EM_INEXACT)    m |= 0x20;
72     switch (n & _MCW_RC) {
73     case _RC_NEAR: m |= 0x000; break;
74     case _RC_DOWN: m |= 0x400; break;
75     case _RC_UP:   m |= 0x800; break;
76     case _RC_CHOP: m |= 0xC00; break;
77     }
78     switch (n & _MCW_PC) {
79     case _PC_24: m |= 0x000; break;
80     case _PC_53: m |= 0x200; break;
81     case _PC_64: m |= 0x300; break;
82     }
83     if ((n & _MCW_IC) == _IC_AFFINE) m |= 0x1000;
84     return m;
85   }
86
87   typedef unsigned short rounding_mode;
88   static void get_rounding_mode(rounding_mode& mode)
89   { mode = msvc2hard(_control87(0, 0)); }
90   static void set_rounding_mode(const rounding_mode mode)
91   { _control87(hard2msvc(mode), _MCW_EM | _MCW_RC | _MCW_PC | _MCW_IC); }
92   static double to_int(const double& x) { return rint(x); }
93 };
94
95 } // namespace detail
96 } // namespace interval_lib
97 } // namespace numeric
98 } // namespace boost
99
100 #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_MSVC_ROUNDING_CONTROL_HPP */