1 /* Boost interval/detail/alpha_rounding_control.hpp file
3 * Copyright 2005 Felix Höfling, Guillaume Melquiond
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE_1_0.txt or
7 * copy at http://www.boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_NUMERIC_INTERVAL_DETAIL_ALPHA_ROUNDING_CONTROL_HPP
11 #define BOOST_NUMERIC_INTERVAL_DETAIL_ALPHA_ROUNDING_CONTROL_HPP
13 #if !defined(alpha) && !defined(__alpha__)
14 #error This header only works on Alpha CPUs.
17 #if defined(__GNUC__) || defined(__digital__) || defined(__DECCXX)
19 #include <float.h> // write_rnd() and read_rnd()
23 namespace interval_lib {
26 #if defined(__GNUC__ )
28 ::boost::long_long_type imode;
30 } rounding_mode_struct;
32 // set bits 59-58 (DYN),
33 // clear all exception bits and disable overflow (51) and inexact exceptions (62)
34 static const rounding_mode_struct mode_upward = { 0x4C08000000000000LL };
35 static const rounding_mode_struct mode_downward = { 0x4408000000000000LL };
36 static const rounding_mode_struct mode_to_nearest = { 0x4808000000000000LL };
37 static const rounding_mode_struct mode_toward_zero = { 0x4008000000000000LL };
39 struct alpha_rounding_control
41 typedef double rounding_mode;
43 static void set_rounding_mode(const rounding_mode mode)
44 { __asm__ __volatile__ ("mt_fpcr %0" : : "f"(mode)); }
46 static void get_rounding_mode(rounding_mode& mode)
47 { __asm__ __volatile__ ("mf_fpcr %0" : "=f"(mode)); }
49 static void downward() { set_rounding_mode(mode_downward.dmode); }
50 static void upward() { set_rounding_mode(mode_upward.dmode); }
51 static void to_nearest() { set_rounding_mode(mode_to_nearest.dmode); }
52 static void toward_zero() { set_rounding_mode(mode_toward_zero.dmode); }
54 #elif defined(__digital__) || defined(__DECCXX)
56 #if defined(__DECCXX) && !(defined(__FLT_ROUNDS) && __FLT_ROUNDS == -1)
57 #error Dynamic rounding mode not enabled. See cxx man page for details.
60 struct alpha_rounding_control
62 typedef unsigned int rounding_mode;
64 static void set_rounding_mode(const rounding_mode& mode) { write_rnd(mode); }
65 static void get_rounding_mode(rounding_mode& mode) { mode = read_rnd(); }
67 static void downward() { set_rounding_mode(FP_RND_RM); }
68 static void upward() { set_rounding_mode(FP_RND_RP); }
69 static void to_nearest() { set_rounding_mode(FP_RND_RN); }
70 static void toward_zero() { set_rounding_mode(FP_RND_RZ); }
78 long double rintl(long double);
82 struct rounding_control<float>:
83 detail::alpha_rounding_control
85 static float force_rounding(const float r)
86 { volatile float _r = r; return _r; }
87 static float to_int(const float& x) { return rintf(x); }
91 struct rounding_control<double>:
92 detail::alpha_rounding_control
94 static const double & force_rounding(const double& r) { return r; }
95 static double to_int(const double& r) { return rint(r); }
99 struct rounding_control<long double>:
100 detail::alpha_rounding_control
102 static const long double & force_rounding(const long double& r) { return r; }
103 static long double to_int(const long double& r) { return rintl(r); }
106 } // namespace interval_lib
107 } // namespace numeric
110 #undef BOOST_NUMERIC_INTERVAL_NO_HARDWARE
113 #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_ALPHA_ROUNDING_CONTROL_HPP */