Chris@16: /* Boost interval/detail/ppc_rounding_control.hpp file Chris@16: * Chris@16: * Copyright 2000 Jens Maurer Chris@16: * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion Chris@16: * Copyright 2005 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_PPC_ROUNDING_CONTROL_HPP Chris@16: #define BOOST_NUMERIC_INTERVAL_DETAIL_PPC_ROUNDING_CONTROL_HPP Chris@16: Chris@16: #if !defined(powerpc) && !defined(__powerpc__) && !defined(__ppc__) Chris@16: #error This header only works on PPC CPUs. Chris@16: #endif Chris@16: Chris@16: #if defined(__GNUC__ ) || (__IBMCPP__ >= 700) Chris@16: Chris@16: namespace boost { Chris@16: namespace numeric { Chris@16: namespace interval_lib { Chris@16: namespace detail { Chris@16: Chris@16: typedef union { Chris@16: ::boost::long_long_type imode; Chris@16: double dmode; Chris@16: } rounding_mode_struct; Chris@16: Chris@16: static const rounding_mode_struct mode_upward = { 0xFFF8000000000002LL }; Chris@16: static const rounding_mode_struct mode_downward = { 0xFFF8000000000003LL }; Chris@16: static const rounding_mode_struct mode_to_nearest = { 0xFFF8000000000000LL }; Chris@16: static const rounding_mode_struct mode_toward_zero = { 0xFFF8000000000001LL }; Chris@16: Chris@16: struct ppc_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__ ("mtfsf 255,%0" : : "f"(mode)); } Chris@16: Chris@16: static void get_rounding_mode(rounding_mode& mode) Chris@16: { __asm__ __volatile__ ("mffs %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: Chris@16: } // namespace detail Chris@16: Chris@16: // Do not declare the following C99 symbols if provides them. Chris@16: // Otherwise, conflicts may occur, due to differences between prototypes. Chris@16: #if !defined(_ISOC99_SOURCE) && !defined(__USE_ISOC99) Chris@16: extern "C" { Chris@16: float rintf(float); Chris@16: double rint(double); Chris@16: } Chris@16: #endif Chris@16: Chris@16: template<> Chris@16: struct rounding_control: Chris@16: detail::ppc_rounding_control Chris@16: { Chris@16: static float force_rounding(const float r) Chris@16: { Chris@16: float tmp; Chris@16: __asm__ __volatile__ ("frsp %0, %1" : "=f" (tmp) : "f" (r)); Chris@16: return tmp; Chris@16: } 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::ppc_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::ppc_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 rint(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_PPC_ROUNDING_CONTROL_HPP */