1 /* Boost interval/detail/ppc_rounding_control.hpp file
3 * Copyright 2000 Jens Maurer
4 * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
5 * Copyright 2005 Guillaume Melquiond
7 * Distributed under the Boost Software License, Version 1.0.
8 * (See accompanying file LICENSE_1_0.txt or
9 * copy at http://www.boost.org/LICENSE_1_0.txt)
12 #ifndef BOOST_NUMERIC_INTERVAL_DETAIL_PPC_ROUNDING_CONTROL_HPP
13 #define BOOST_NUMERIC_INTERVAL_DETAIL_PPC_ROUNDING_CONTROL_HPP
15 #if !defined(powerpc) && !defined(__powerpc__) && !defined(__ppc__)
16 #error This header only works on PPC CPUs.
19 #if defined(__GNUC__ ) || (__IBMCPP__ >= 700)
23 namespace interval_lib {
27 ::boost::long_long_type imode;
29 } rounding_mode_struct;
31 static const rounding_mode_struct mode_upward = { 0xFFF8000000000002LL };
32 static const rounding_mode_struct mode_downward = { 0xFFF8000000000003LL };
33 static const rounding_mode_struct mode_to_nearest = { 0xFFF8000000000000LL };
34 static const rounding_mode_struct mode_toward_zero = { 0xFFF8000000000001LL };
36 struct ppc_rounding_control
38 typedef double rounding_mode;
40 static void set_rounding_mode(const rounding_mode mode)
41 { __asm__ __volatile__ ("mtfsf 255,%0" : : "f"(mode)); }
43 static void get_rounding_mode(rounding_mode& mode)
44 { __asm__ __volatile__ ("mffs %0" : "=f"(mode)); }
46 static void downward() { set_rounding_mode(mode_downward.dmode); }
47 static void upward() { set_rounding_mode(mode_upward.dmode); }
48 static void to_nearest() { set_rounding_mode(mode_to_nearest.dmode); }
49 static void toward_zero() { set_rounding_mode(mode_toward_zero.dmode); }
54 // Do not declare the following C99 symbols if <math.h> provides them.
55 // Otherwise, conflicts may occur, due to differences between prototypes.
56 #if !defined(_ISOC99_SOURCE) && !defined(__USE_ISOC99)
64 struct rounding_control<float>:
65 detail::ppc_rounding_control
67 static float force_rounding(const float r)
70 __asm__ __volatile__ ("frsp %0, %1" : "=f" (tmp) : "f" (r));
73 static float to_int(const float& x) { return rintf(x); }
77 struct rounding_control<double>:
78 detail::ppc_rounding_control
80 static const double & force_rounding(const double& r) { return r; }
81 static double to_int(const double& r) { return rint(r); }
85 struct rounding_control<long double>:
86 detail::ppc_rounding_control
88 static const long double & force_rounding(const long double& r) { return r; }
89 static long double to_int(const long double& r) { return rint(r); }
92 } // namespace interval_lib
93 } // namespace numeric
96 #undef BOOST_NUMERIC_INTERVAL_NO_HARDWARE
99 #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_PPC_ROUNDING_CONTROL_HPP */