Chris@16
|
1 /* Boost interval/detail/alpha_rounding_control.hpp file
|
Chris@16
|
2 *
|
Chris@16
|
3 * Copyright 2005 Felix Höfling, Guillaume Melquiond
|
Chris@16
|
4 *
|
Chris@16
|
5 * Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
6 * (See accompanying file LICENSE_1_0.txt or
|
Chris@16
|
7 * copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8 */
|
Chris@16
|
9
|
Chris@16
|
10 #ifndef BOOST_NUMERIC_INTERVAL_DETAIL_ALPHA_ROUNDING_CONTROL_HPP
|
Chris@16
|
11 #define BOOST_NUMERIC_INTERVAL_DETAIL_ALPHA_ROUNDING_CONTROL_HPP
|
Chris@16
|
12
|
Chris@16
|
13 #if !defined(alpha) && !defined(__alpha__)
|
Chris@16
|
14 #error This header only works on Alpha CPUs.
|
Chris@16
|
15 #endif
|
Chris@16
|
16
|
Chris@16
|
17 #if defined(__GNUC__) || defined(__digital__) || defined(__DECCXX)
|
Chris@16
|
18
|
Chris@16
|
19 #include <float.h> // write_rnd() and read_rnd()
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost {
|
Chris@16
|
22 namespace numeric {
|
Chris@16
|
23 namespace interval_lib {
|
Chris@16
|
24
|
Chris@16
|
25 namespace detail {
|
Chris@16
|
26 #if defined(__GNUC__ )
|
Chris@16
|
27 typedef union {
|
Chris@16
|
28 ::boost::long_long_type imode;
|
Chris@16
|
29 double dmode;
|
Chris@16
|
30 } rounding_mode_struct;
|
Chris@16
|
31
|
Chris@16
|
32 // set bits 59-58 (DYN),
|
Chris@16
|
33 // clear all exception bits and disable overflow (51) and inexact exceptions (62)
|
Chris@16
|
34 static const rounding_mode_struct mode_upward = { 0x4C08000000000000LL };
|
Chris@16
|
35 static const rounding_mode_struct mode_downward = { 0x4408000000000000LL };
|
Chris@16
|
36 static const rounding_mode_struct mode_to_nearest = { 0x4808000000000000LL };
|
Chris@16
|
37 static const rounding_mode_struct mode_toward_zero = { 0x4008000000000000LL };
|
Chris@16
|
38
|
Chris@16
|
39 struct alpha_rounding_control
|
Chris@16
|
40 {
|
Chris@16
|
41 typedef double rounding_mode;
|
Chris@16
|
42
|
Chris@16
|
43 static void set_rounding_mode(const rounding_mode mode)
|
Chris@16
|
44 { __asm__ __volatile__ ("mt_fpcr %0" : : "f"(mode)); }
|
Chris@16
|
45
|
Chris@16
|
46 static void get_rounding_mode(rounding_mode& mode)
|
Chris@16
|
47 { __asm__ __volatile__ ("mf_fpcr %0" : "=f"(mode)); }
|
Chris@16
|
48
|
Chris@16
|
49 static void downward() { set_rounding_mode(mode_downward.dmode); }
|
Chris@16
|
50 static void upward() { set_rounding_mode(mode_upward.dmode); }
|
Chris@16
|
51 static void to_nearest() { set_rounding_mode(mode_to_nearest.dmode); }
|
Chris@16
|
52 static void toward_zero() { set_rounding_mode(mode_toward_zero.dmode); }
|
Chris@16
|
53 };
|
Chris@16
|
54 #elif defined(__digital__) || defined(__DECCXX)
|
Chris@16
|
55
|
Chris@16
|
56 #if defined(__DECCXX) && !(defined(__FLT_ROUNDS) && __FLT_ROUNDS == -1)
|
Chris@16
|
57 #error Dynamic rounding mode not enabled. See cxx man page for details.
|
Chris@16
|
58 #endif
|
Chris@16
|
59
|
Chris@16
|
60 struct alpha_rounding_control
|
Chris@16
|
61 {
|
Chris@16
|
62 typedef unsigned int rounding_mode;
|
Chris@16
|
63
|
Chris@16
|
64 static void set_rounding_mode(const rounding_mode& mode) { write_rnd(mode); }
|
Chris@16
|
65 static void get_rounding_mode(rounding_mode& mode) { mode = read_rnd(); }
|
Chris@16
|
66
|
Chris@16
|
67 static void downward() { set_rounding_mode(FP_RND_RM); }
|
Chris@16
|
68 static void upward() { set_rounding_mode(FP_RND_RP); }
|
Chris@16
|
69 static void to_nearest() { set_rounding_mode(FP_RND_RN); }
|
Chris@16
|
70 static void toward_zero() { set_rounding_mode(FP_RND_RZ); }
|
Chris@16
|
71 };
|
Chris@16
|
72 #endif
|
Chris@16
|
73 } // namespace detail
|
Chris@16
|
74
|
Chris@16
|
75 extern "C" {
|
Chris@16
|
76 float rintf(float);
|
Chris@16
|
77 double rint(double);
|
Chris@16
|
78 long double rintl(long double);
|
Chris@16
|
79 }
|
Chris@16
|
80
|
Chris@16
|
81 template<>
|
Chris@16
|
82 struct rounding_control<float>:
|
Chris@16
|
83 detail::alpha_rounding_control
|
Chris@16
|
84 {
|
Chris@16
|
85 static float force_rounding(const float r)
|
Chris@16
|
86 { volatile float _r = r; return _r; }
|
Chris@16
|
87 static float to_int(const float& x) { return rintf(x); }
|
Chris@16
|
88 };
|
Chris@16
|
89
|
Chris@16
|
90 template<>
|
Chris@16
|
91 struct rounding_control<double>:
|
Chris@16
|
92 detail::alpha_rounding_control
|
Chris@16
|
93 {
|
Chris@16
|
94 static const double & force_rounding(const double& r) { return r; }
|
Chris@16
|
95 static double to_int(const double& r) { return rint(r); }
|
Chris@16
|
96 };
|
Chris@16
|
97
|
Chris@16
|
98 template<>
|
Chris@16
|
99 struct rounding_control<long double>:
|
Chris@16
|
100 detail::alpha_rounding_control
|
Chris@16
|
101 {
|
Chris@16
|
102 static const long double & force_rounding(const long double& r) { return r; }
|
Chris@16
|
103 static long double to_int(const long double& r) { return rintl(r); }
|
Chris@16
|
104 };
|
Chris@16
|
105
|
Chris@16
|
106 } // namespace interval_lib
|
Chris@16
|
107 } // namespace numeric
|
Chris@16
|
108 } // namespace boost
|
Chris@16
|
109
|
Chris@16
|
110 #undef BOOST_NUMERIC_INTERVAL_NO_HARDWARE
|
Chris@16
|
111 #endif
|
Chris@16
|
112
|
Chris@16
|
113 #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_ALPHA_ROUNDING_CONTROL_HPP */
|