1 | /* Boost interval/detail/ppc_rounding_control.hpp file
|
---|
2 | *
|
---|
3 | * Copyright 2000 Jens Maurer
|
---|
4 | * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
|
---|
5 | * Copyright 2005 Guillaume Melquiond
|
---|
6 | *
|
---|
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)
|
---|
10 | */
|
---|
11 |
|
---|
12 | #ifndef BOOST_NUMERIC_INTERVAL_DETAIL_PPC_ROUNDING_CONTROL_HPP
|
---|
13 | #define BOOST_NUMERIC_INTERVAL_DETAIL_PPC_ROUNDING_CONTROL_HPP
|
---|
14 |
|
---|
15 | #if !defined(powerpc) && !defined(__powerpc__) && !defined(__ppc__)
|
---|
16 | #error This header only works on PPC CPUs.
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | #if defined(__GNUC__ ) || (__IBMCPP__ >= 700)
|
---|
20 |
|
---|
21 | namespace boost {
|
---|
22 | namespace numeric {
|
---|
23 | namespace interval_lib {
|
---|
24 | namespace detail {
|
---|
25 |
|
---|
26 | typedef union {
|
---|
27 | ::boost::long_long_type imode;
|
---|
28 | double dmode;
|
---|
29 | } rounding_mode_struct;
|
---|
30 |
|
---|
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 = { 0xFFF8000000000001LL };
|
---|
34 | static const rounding_mode_struct mode_toward_zero = { 0xFFF8000000000000LL };
|
---|
35 |
|
---|
36 | struct ppc_rounding_control
|
---|
37 | {
|
---|
38 | typedef double rounding_mode;
|
---|
39 |
|
---|
40 | static void set_rounding_mode(const rounding_mode mode)
|
---|
41 | { __asm__ __volatile__ ("mtfsf 255,%0" : : "f"(mode)); }
|
---|
42 |
|
---|
43 | static void get_rounding_mode(rounding_mode& mode)
|
---|
44 | { __asm__ __volatile__ ("mffs %0" : "=f"(mode)); }
|
---|
45 |
|
---|
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); }
|
---|
50 | };
|
---|
51 |
|
---|
52 | } // namespace detail
|
---|
53 |
|
---|
54 | extern "C" {
|
---|
55 | float rintf(float);
|
---|
56 | double rint(double);
|
---|
57 | }
|
---|
58 |
|
---|
59 | template<>
|
---|
60 | struct rounding_control<float>:
|
---|
61 | detail::ppc_rounding_control
|
---|
62 | {
|
---|
63 | static float force_rounding(const float r)
|
---|
64 | {
|
---|
65 | float tmp;
|
---|
66 | __asm__ __volatile__ ("frsp %0, %1" : "=f" (tmp) : "f" (r));
|
---|
67 | return tmp;
|
---|
68 | }
|
---|
69 | static float to_int(const float& x) { return rintf(x); }
|
---|
70 | };
|
---|
71 |
|
---|
72 | template<>
|
---|
73 | struct rounding_control<double>:
|
---|
74 | detail::ppc_rounding_control
|
---|
75 | {
|
---|
76 | static const double & force_rounding(const double& r) { return r; }
|
---|
77 | static double to_int(const double& r) { return rint(r); }
|
---|
78 | };
|
---|
79 |
|
---|
80 | template<>
|
---|
81 | struct rounding_control<long double>:
|
---|
82 | detail::ppc_rounding_control
|
---|
83 | {
|
---|
84 | static const long double & force_rounding(const long double& r) { return r; }
|
---|
85 | static long double to_int(const long double& r) { return rint(r); }
|
---|
86 | };
|
---|
87 |
|
---|
88 | } // namespace interval_lib
|
---|
89 | } // namespace numeric
|
---|
90 | } // namespace boost
|
---|
91 |
|
---|
92 | #undef BOOST_NUMERIC_INTERVAL_NO_HARDWARE
|
---|
93 | #endif
|
---|
94 |
|
---|
95 | #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_PPC_ROUNDING_CONTROL_HPP */
|
---|