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 | namespace boost {
|
---|
21 | namespace numeric {
|
---|
22 | namespace interval_lib {
|
---|
23 | namespace detail {
|
---|
24 |
|
---|
25 | extern "C" { double rint(double); }
|
---|
26 |
|
---|
27 | struct x86_rounding
|
---|
28 | {
|
---|
29 | static unsigned int hard2msvc(unsigned short m) {
|
---|
30 | unsigned int n = 0;
|
---|
31 | if (m & 0x01) n |= _EM_INVALID;
|
---|
32 | if (m & 0x02) n |= _EM_DENORMAL;
|
---|
33 | if (m & 0x04) n |= _EM_ZERODIVIDE;
|
---|
34 | if (m & 0x08) n |= _EM_OVERFLOW;
|
---|
35 | if (m & 0x10) n |= _EM_UNDERFLOW;
|
---|
36 | if (m & 0x20) n |= _EM_INEXACT;
|
---|
37 | switch (m & 0x300) {
|
---|
38 | case 0x000: n |= _PC_24; break;
|
---|
39 | case 0x200: n |= _PC_53; break;
|
---|
40 | case 0x300: n |= _PC_64; break;
|
---|
41 | }
|
---|
42 | switch (m & 0xC00) {
|
---|
43 | case 0x000: n |= _RC_NEAR; break;
|
---|
44 | case 0x400: n |= _RC_DOWN; break;
|
---|
45 | case 0x800: n |= _RC_UP; break;
|
---|
46 | case 0xC00: n |= _RC_CHOP; break;
|
---|
47 | }
|
---|
48 | if (m & 0x1000) n |= _IC_AFFINE; // only useful on 287
|
---|
49 | return n;
|
---|
50 | }
|
---|
51 |
|
---|
52 | static unsigned short msvc2hard(unsigned int n) {
|
---|
53 | unsigned short m = 0;
|
---|
54 | if (n & _EM_INVALID) m |= 0x01;
|
---|
55 | if (n & _EM_DENORMAL) m |= 0x02;
|
---|
56 | if (n & _EM_ZERODIVIDE) m |= 0x04;
|
---|
57 | if (n & _EM_OVERFLOW) m |= 0x08;
|
---|
58 | if (n & _EM_UNDERFLOW) m |= 0x10;
|
---|
59 | if (n & _EM_INEXACT) m |= 0x20;
|
---|
60 | switch (n & _MCW_RC) {
|
---|
61 | case _RC_NEAR: m |= 0x000; break;
|
---|
62 | case _RC_DOWN: m |= 0x400; break;
|
---|
63 | case _RC_UP: m |= 0x800; break;
|
---|
64 | case _RC_CHOP: m |= 0xC00; break;
|
---|
65 | }
|
---|
66 | switch (n & _MCW_PC) {
|
---|
67 | case _PC_24: m |= 0x000; break;
|
---|
68 | case _PC_53: m |= 0x200; break;
|
---|
69 | case _PC_64: m |= 0x300; break;
|
---|
70 | }
|
---|
71 | if ((n & _MCW_IC) == _IC_AFFINE) m |= 0x1000;
|
---|
72 | return m;
|
---|
73 | }
|
---|
74 |
|
---|
75 | typedef unsigned short rounding_mode;
|
---|
76 | static void get_rounding_mode(rounding_mode& mode)
|
---|
77 | { mode = msvc2hard(_control87(0, 0)); }
|
---|
78 | static void set_rounding_mode(const rounding_mode mode)
|
---|
79 | { _control87(hard2msvc(mode), _MCW_EM | _MCW_RC | _MCW_PC | _MCW_IC); }
|
---|
80 | static double to_int(const double& x) { return rint(x); }
|
---|
81 | };
|
---|
82 |
|
---|
83 | } // namespace detail
|
---|
84 | } // namespace interval_lib
|
---|
85 | } // namespace numeric
|
---|
86 | } // namespace boost
|
---|
87 |
|
---|
88 | #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_MSVC_ROUNDING_CONTROL_HPP */
|
---|