Chris@16: /* Boost interval/detail/msvc_rounding_control.hpp file Chris@16: * Chris@16: * Copyright 2000 Maarten Keijzer Chris@16: * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion Chris@16: * Chris@16: * Distributed under the Boost Software License, Version 1.0. Chris@16: * (See accompanying file LICENSE_1_0.txt or Chris@16: * copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: */ Chris@16: Chris@16: #ifndef BOOST_NUMERIC_INTERVAL_DETAIL_MSVC_ROUNDING_CONTROL_HPP Chris@16: #define BOOST_NUMERIC_INTERVAL_DETAIL_MSVC_ROUNDING_CONTROL_HPP Chris@16: Chris@16: #ifndef _MSC_VER Chris@16: # error This header is only intended for MSVC, but might work for Borland as well Chris@16: #endif Chris@16: Chris@16: #include // MSVC rounding control Chris@16: Chris@16: // Although the function is called _control87, it seems to work for Chris@16: // other FPUs too, so it does not have to be changed to _controlfp. Chris@16: Chris@16: namespace boost { Chris@16: namespace numeric { Chris@16: namespace interval_lib { Chris@16: namespace detail { Chris@16: Chris@16: #if BOOST_MSVC < 1400 || defined(_WIN64) Chris@16: extern "C" { double rint(double); } Chris@16: #else Chris@16: inline double rint(double x) Chris@16: { Chris@16: _asm FLD [x] ; Chris@16: _asm FRNDINT ; Chris@16: //_asm RET ; Chris@16: } Chris@16: #endif Chris@16: Chris@16: struct x86_rounding Chris@16: { Chris@16: static unsigned int hard2msvc(unsigned short m) { Chris@16: unsigned int n = 0; Chris@16: if (m & 0x01) n |= _EM_INVALID; Chris@16: if (m & 0x02) n |= _EM_DENORMAL; Chris@16: if (m & 0x04) n |= _EM_ZERODIVIDE; Chris@16: if (m & 0x08) n |= _EM_OVERFLOW; Chris@16: if (m & 0x10) n |= _EM_UNDERFLOW; Chris@16: if (m & 0x20) n |= _EM_INEXACT; Chris@16: switch (m & 0x300) { Chris@16: case 0x000: n |= _PC_24; break; Chris@16: case 0x200: n |= _PC_53; break; Chris@16: case 0x300: n |= _PC_64; break; Chris@16: } Chris@16: switch (m & 0xC00) { Chris@16: case 0x000: n |= _RC_NEAR; break; Chris@16: case 0x400: n |= _RC_DOWN; break; Chris@16: case 0x800: n |= _RC_UP; break; Chris@16: case 0xC00: n |= _RC_CHOP; break; Chris@16: } Chris@16: if (m & 0x1000) n |= _IC_AFFINE; // only useful on 287 Chris@16: return n; Chris@16: } Chris@16: Chris@16: static unsigned short msvc2hard(unsigned int n) { Chris@16: unsigned short m = 0; Chris@16: if (n & _EM_INVALID) m |= 0x01; Chris@16: if (n & _EM_DENORMAL) m |= 0x02; Chris@16: if (n & _EM_ZERODIVIDE) m |= 0x04; Chris@16: if (n & _EM_OVERFLOW) m |= 0x08; Chris@16: if (n & _EM_UNDERFLOW) m |= 0x10; Chris@16: if (n & _EM_INEXACT) m |= 0x20; Chris@16: switch (n & _MCW_RC) { Chris@16: case _RC_NEAR: m |= 0x000; break; Chris@16: case _RC_DOWN: m |= 0x400; break; Chris@16: case _RC_UP: m |= 0x800; break; Chris@16: case _RC_CHOP: m |= 0xC00; break; Chris@16: } Chris@16: switch (n & _MCW_PC) { Chris@16: case _PC_24: m |= 0x000; break; Chris@16: case _PC_53: m |= 0x200; break; Chris@16: case _PC_64: m |= 0x300; break; Chris@16: } Chris@16: if ((n & _MCW_IC) == _IC_AFFINE) m |= 0x1000; Chris@16: return m; Chris@16: } Chris@16: Chris@16: typedef unsigned short rounding_mode; Chris@16: static void get_rounding_mode(rounding_mode& mode) Chris@16: { mode = msvc2hard(_control87(0, 0)); } Chris@16: static void set_rounding_mode(const rounding_mode mode) Chris@101: { Chris@101: _control87(hard2msvc(mode), Chris@101: _MCW_EM | _MCW_RC Chris@101: #if !defined(_M_AMD64) && !defined(_M_ARM) Chris@101: // x64 ignores _MCW_PC and _MCW_IC, and the Debug CRT library actually Chris@101: // asserts when these are passed to _control87. Chris@101: // MSDN says on '_control87' that changing precision (_MCW_PC) or Chris@101: // infinity (_MCW_IC) handling is not supported on the ARM and x64 Chris@101: // architectures and that _control87 raises an assertion Chris@101: // and the invalid parameter handler is invoked. Chris@101: | _MCW_PC | _MCW_IC Chris@101: #endif Chris@101: ); Chris@101: } Chris@16: static double to_int(const double& x) { return rint(x); } Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace interval_lib Chris@16: } // namespace numeric Chris@16: } // namespace boost Chris@16: Chris@16: #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_MSVC_ROUNDING_CONTROL_HPP */