Chris@16
|
1 /* Boost interval/detail/msvc_rounding_control.hpp file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright 2000 Maarten Keijzer
|
Chris@16
|
4 * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
|
Chris@16
|
5 *
|
Chris@16
|
6 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
7 * (See accompanying file LICENSE_1_0.txt or
|
Chris@16
|
8 * copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 */
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_NUMERIC_INTERVAL_DETAIL_MSVC_ROUNDING_CONTROL_HPP
|
Chris@16
|
12 #define BOOST_NUMERIC_INTERVAL_DETAIL_MSVC_ROUNDING_CONTROL_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #ifndef _MSC_VER
|
Chris@16
|
15 # error This header is only intended for MSVC, but might work for Borland as well
|
Chris@16
|
16 #endif
|
Chris@16
|
17
|
Chris@16
|
18 #include <float.h> // MSVC rounding control
|
Chris@16
|
19
|
Chris@16
|
20 // Although the function is called _control87, it seems to work for
|
Chris@16
|
21 // other FPUs too, so it does not have to be changed to _controlfp.
|
Chris@16
|
22
|
Chris@16
|
23 namespace boost {
|
Chris@16
|
24 namespace numeric {
|
Chris@16
|
25 namespace interval_lib {
|
Chris@16
|
26 namespace detail {
|
Chris@16
|
27
|
Chris@16
|
28 #if BOOST_MSVC < 1400 || defined(_WIN64)
|
Chris@16
|
29 extern "C" { double rint(double); }
|
Chris@16
|
30 #else
|
Chris@16
|
31 inline double rint(double x)
|
Chris@16
|
32 {
|
Chris@16
|
33 _asm FLD [x] ;
|
Chris@16
|
34 _asm FRNDINT ;
|
Chris@16
|
35 //_asm RET ;
|
Chris@16
|
36 }
|
Chris@16
|
37 #endif
|
Chris@16
|
38
|
Chris@16
|
39 struct x86_rounding
|
Chris@16
|
40 {
|
Chris@16
|
41 static unsigned int hard2msvc(unsigned short m) {
|
Chris@16
|
42 unsigned int n = 0;
|
Chris@16
|
43 if (m & 0x01) n |= _EM_INVALID;
|
Chris@16
|
44 if (m & 0x02) n |= _EM_DENORMAL;
|
Chris@16
|
45 if (m & 0x04) n |= _EM_ZERODIVIDE;
|
Chris@16
|
46 if (m & 0x08) n |= _EM_OVERFLOW;
|
Chris@16
|
47 if (m & 0x10) n |= _EM_UNDERFLOW;
|
Chris@16
|
48 if (m & 0x20) n |= _EM_INEXACT;
|
Chris@16
|
49 switch (m & 0x300) {
|
Chris@16
|
50 case 0x000: n |= _PC_24; break;
|
Chris@16
|
51 case 0x200: n |= _PC_53; break;
|
Chris@16
|
52 case 0x300: n |= _PC_64; break;
|
Chris@16
|
53 }
|
Chris@16
|
54 switch (m & 0xC00) {
|
Chris@16
|
55 case 0x000: n |= _RC_NEAR; break;
|
Chris@16
|
56 case 0x400: n |= _RC_DOWN; break;
|
Chris@16
|
57 case 0x800: n |= _RC_UP; break;
|
Chris@16
|
58 case 0xC00: n |= _RC_CHOP; break;
|
Chris@16
|
59 }
|
Chris@16
|
60 if (m & 0x1000) n |= _IC_AFFINE; // only useful on 287
|
Chris@16
|
61 return n;
|
Chris@16
|
62 }
|
Chris@16
|
63
|
Chris@16
|
64 static unsigned short msvc2hard(unsigned int n) {
|
Chris@16
|
65 unsigned short m = 0;
|
Chris@16
|
66 if (n & _EM_INVALID) m |= 0x01;
|
Chris@16
|
67 if (n & _EM_DENORMAL) m |= 0x02;
|
Chris@16
|
68 if (n & _EM_ZERODIVIDE) m |= 0x04;
|
Chris@16
|
69 if (n & _EM_OVERFLOW) m |= 0x08;
|
Chris@16
|
70 if (n & _EM_UNDERFLOW) m |= 0x10;
|
Chris@16
|
71 if (n & _EM_INEXACT) m |= 0x20;
|
Chris@16
|
72 switch (n & _MCW_RC) {
|
Chris@16
|
73 case _RC_NEAR: m |= 0x000; break;
|
Chris@16
|
74 case _RC_DOWN: m |= 0x400; break;
|
Chris@16
|
75 case _RC_UP: m |= 0x800; break;
|
Chris@16
|
76 case _RC_CHOP: m |= 0xC00; break;
|
Chris@16
|
77 }
|
Chris@16
|
78 switch (n & _MCW_PC) {
|
Chris@16
|
79 case _PC_24: m |= 0x000; break;
|
Chris@16
|
80 case _PC_53: m |= 0x200; break;
|
Chris@16
|
81 case _PC_64: m |= 0x300; break;
|
Chris@16
|
82 }
|
Chris@16
|
83 if ((n & _MCW_IC) == _IC_AFFINE) m |= 0x1000;
|
Chris@16
|
84 return m;
|
Chris@16
|
85 }
|
Chris@16
|
86
|
Chris@16
|
87 typedef unsigned short rounding_mode;
|
Chris@16
|
88 static void get_rounding_mode(rounding_mode& mode)
|
Chris@16
|
89 { mode = msvc2hard(_control87(0, 0)); }
|
Chris@16
|
90 static void set_rounding_mode(const rounding_mode mode)
|
Chris@101
|
91 {
|
Chris@101
|
92 _control87(hard2msvc(mode),
|
Chris@101
|
93 _MCW_EM | _MCW_RC
|
Chris@101
|
94 #if !defined(_M_AMD64) && !defined(_M_ARM)
|
Chris@101
|
95 // x64 ignores _MCW_PC and _MCW_IC, and the Debug CRT library actually
|
Chris@101
|
96 // asserts when these are passed to _control87.
|
Chris@101
|
97 // MSDN says on '_control87' that changing precision (_MCW_PC) or
|
Chris@101
|
98 // infinity (_MCW_IC) handling is not supported on the ARM and x64
|
Chris@101
|
99 // architectures and that _control87 raises an assertion
|
Chris@101
|
100 // and the invalid parameter handler is invoked.
|
Chris@101
|
101 | _MCW_PC | _MCW_IC
|
Chris@101
|
102 #endif
|
Chris@101
|
103 );
|
Chris@101
|
104 }
|
Chris@16
|
105 static double to_int(const double& x) { return rint(x); }
|
Chris@16
|
106 };
|
Chris@16
|
107
|
Chris@16
|
108 } // namespace detail
|
Chris@16
|
109 } // namespace interval_lib
|
Chris@16
|
110 } // namespace numeric
|
Chris@16
|
111 } // namespace boost
|
Chris@16
|
112
|
Chris@16
|
113 #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_MSVC_ROUNDING_CONTROL_HPP */
|