Chris@16: /* Boost interval/detail/alpha_rounding_control.hpp file Chris@16: * Chris@16: * Copyright 2005 Felix Höfling, Guillaume Melquiond 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_ALPHA_ROUNDING_CONTROL_HPP Chris@16: #define BOOST_NUMERIC_INTERVAL_DETAIL_ALPHA_ROUNDING_CONTROL_HPP Chris@16: Chris@16: #if !defined(alpha) && !defined(__alpha__) Chris@16: #error This header only works on Alpha CPUs. Chris@16: #endif Chris@16: Chris@16: #if defined(__GNUC__) || defined(__digital__) || defined(__DECCXX) Chris@16: Chris@16: #include // write_rnd() and read_rnd() Chris@16: Chris@16: namespace boost { Chris@16: namespace numeric { Chris@16: namespace interval_lib { Chris@16: Chris@16: namespace detail { Chris@16: #if defined(__GNUC__ ) Chris@16: typedef union { Chris@16: ::boost::long_long_type imode; Chris@16: double dmode; Chris@16: } rounding_mode_struct; Chris@16: Chris@16: // set bits 59-58 (DYN), Chris@16: // clear all exception bits and disable overflow (51) and inexact exceptions (62) Chris@16: static const rounding_mode_struct mode_upward = { 0x4C08000000000000LL }; Chris@16: static const rounding_mode_struct mode_downward = { 0x4408000000000000LL }; Chris@16: static const rounding_mode_struct mode_to_nearest = { 0x4808000000000000LL }; Chris@16: static const rounding_mode_struct mode_toward_zero = { 0x4008000000000000LL }; Chris@16: Chris@16: struct alpha_rounding_control Chris@16: { Chris@16: typedef double rounding_mode; Chris@16: Chris@16: static void set_rounding_mode(const rounding_mode mode) Chris@16: { __asm__ __volatile__ ("mt_fpcr %0" : : "f"(mode)); } Chris@16: Chris@16: static void get_rounding_mode(rounding_mode& mode) Chris@16: { __asm__ __volatile__ ("mf_fpcr %0" : "=f"(mode)); } Chris@16: Chris@16: static void downward() { set_rounding_mode(mode_downward.dmode); } Chris@16: static void upward() { set_rounding_mode(mode_upward.dmode); } Chris@16: static void to_nearest() { set_rounding_mode(mode_to_nearest.dmode); } Chris@16: static void toward_zero() { set_rounding_mode(mode_toward_zero.dmode); } Chris@16: }; Chris@16: #elif defined(__digital__) || defined(__DECCXX) Chris@16: Chris@16: #if defined(__DECCXX) && !(defined(__FLT_ROUNDS) && __FLT_ROUNDS == -1) Chris@16: #error Dynamic rounding mode not enabled. See cxx man page for details. Chris@16: #endif Chris@16: Chris@16: struct alpha_rounding_control Chris@16: { Chris@16: typedef unsigned int rounding_mode; Chris@16: Chris@16: static void set_rounding_mode(const rounding_mode& mode) { write_rnd(mode); } Chris@16: static void get_rounding_mode(rounding_mode& mode) { mode = read_rnd(); } Chris@16: Chris@16: static void downward() { set_rounding_mode(FP_RND_RM); } Chris@16: static void upward() { set_rounding_mode(FP_RND_RP); } Chris@16: static void to_nearest() { set_rounding_mode(FP_RND_RN); } Chris@16: static void toward_zero() { set_rounding_mode(FP_RND_RZ); } Chris@16: }; Chris@16: #endif Chris@16: } // namespace detail Chris@16: Chris@16: extern "C" { Chris@16: float rintf(float); Chris@16: double rint(double); Chris@16: long double rintl(long double); Chris@16: } Chris@16: Chris@16: template<> Chris@16: struct rounding_control: Chris@16: detail::alpha_rounding_control Chris@16: { Chris@16: static float force_rounding(const float r) Chris@16: { volatile float _r = r; return _r; } Chris@16: static float to_int(const float& x) { return rintf(x); } Chris@16: }; Chris@16: Chris@16: template<> Chris@16: struct rounding_control: Chris@16: detail::alpha_rounding_control Chris@16: { Chris@16: static const double & force_rounding(const double& r) { return r; } Chris@16: static double to_int(const double& r) { return rint(r); } Chris@16: }; Chris@16: Chris@16: template<> Chris@16: struct rounding_control: Chris@16: detail::alpha_rounding_control Chris@16: { Chris@16: static const long double & force_rounding(const long double& r) { return r; } Chris@16: static long double to_int(const long double& r) { return rintl(r); } Chris@16: }; Chris@16: Chris@16: } // namespace interval_lib Chris@16: } // namespace numeric Chris@16: } // namespace boost Chris@16: Chris@16: #undef BOOST_NUMERIC_INTERVAL_NO_HARDWARE Chris@16: #endif Chris@16: Chris@16: #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_ALPHA_ROUNDING_CONTROL_HPP */