1 /* Boost interval/detail/sparc_rounding_control.hpp file
2  *
3  * Copyright 2000 Jens Maurer
4  * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
5  *
6  * Distributed under the Boost Software License, Version 1.0.
7  * (See accompanying file LICENSE_1_0.txt or
8  * copy at http://www.boost.org/LICENSE_1_0.txt)
9  *
10  * The basic code in this file was kindly provided by Jeremy Siek.
11  */
12 
13 #ifndef BOOST_NUMERIC_INTERVAL_DETAIL_SPARC_ROUNDING_CONTROL_HPP
14 #define BOOST_NUMERIC_INTERVAL_DETAIL_SPARC_ROUNDING_CONTROL_HPP
15 
16 #if !defined(sparc) && !defined(__sparc__)
17 #  error This header is only intended for SPARC CPUs.
18 #endif
19 
20 #ifdef __SUNPRO_CC
21 #  include <ieeefp.h>
22 #endif
23 
24 
25 namespace boost {
26 namespace numeric {
27 namespace interval_lib {
28 namespace detail {
29 
30 struct sparc_rounding_control
31 {
32   typedef unsigned int rounding_mode;
33 
set_rounding_modeboost::numeric::interval_lib::detail::sparc_rounding_control34   static void set_rounding_mode(const rounding_mode& mode)
35   {
36 #   if defined(__GNUC__)
37     __asm__ __volatile__("ld %0, %%fsr" : : "m"(mode));
38 #   elif defined (__SUNPRO_CC)
39     fpsetround(fp_rnd(mode));
40 #   elif defined(__KCC)
41     asm("sethi %hi(mode), %o1");
42     asm("ld [%o1+%lo(mode)], %fsr");
43 #   else
44 #     error Unsupported compiler for Sparc rounding control.
45 #   endif
46   }
47 
get_rounding_modeboost::numeric::interval_lib::detail::sparc_rounding_control48   static void get_rounding_mode(rounding_mode& mode)
49   {
50 #   if defined(__GNUC__)
51     __asm__ __volatile__("st %%fsr, %0" : "=m"(mode));
52 #   elif defined (__SUNPRO_CC)
53     mode = fpgetround();
54 #   elif defined(__KCC)
55 #     error KCC on Sun SPARC get_round_mode: please fix me
56     asm("st %fsr, [mode]");
57 #   else
58 #     error Unsupported compiler for Sparc rounding control.
59 #   endif
60   }
61 
62 #if defined(__SUNPRO_CC)
downwardboost::numeric::interval_lib::detail::sparc_rounding_control63   static void downward()    { set_rounding_mode(FP_RM); }
upwardboost::numeric::interval_lib::detail::sparc_rounding_control64   static void upward()      { set_rounding_mode(FP_RP); }
to_nearestboost::numeric::interval_lib::detail::sparc_rounding_control65   static void to_nearest()  { set_rounding_mode(FP_RN); }
toward_zeroboost::numeric::interval_lib::detail::sparc_rounding_control66   static void toward_zero() { set_rounding_mode(FP_RZ); }
67 #else
downwardboost::numeric::interval_lib::detail::sparc_rounding_control68   static void downward()    { set_rounding_mode(0xc0000000); }
upwardboost::numeric::interval_lib::detail::sparc_rounding_control69   static void upward()      { set_rounding_mode(0x80000000); }
to_nearestboost::numeric::interval_lib::detail::sparc_rounding_control70   static void to_nearest()  { set_rounding_mode(0x00000000); }
toward_zeroboost::numeric::interval_lib::detail::sparc_rounding_control71   static void toward_zero() { set_rounding_mode(0x40000000); }
72 #endif
73 };
74 
75 } // namespace detail
76 
77 extern "C" {
78   float rintf(float);
79   double rint(double);
80 }
81 
82 template<>
83 struct rounding_control<float>:
84   detail::sparc_rounding_control
85 {
force_roundingboost::numeric::interval_lib::rounding_control86   static const float& force_rounding(const float& x) { return x; }
to_intboost::numeric::interval_lib::rounding_control87   static float to_int(const float& x) { return rintf(x); }
88 };
89 
90 template<>
91 struct rounding_control<double>:
92   detail::sparc_rounding_control
93 {
force_roundingboost::numeric::interval_lib::rounding_control94   static const double& force_rounding(const double& x) { return x; }
to_intboost::numeric::interval_lib::rounding_control95   static double to_int(const double& x) { return rint(x); }
96 };
97 
98 template<>
99 struct rounding_control<long double>:
100   detail::sparc_rounding_control
101 {
force_roundingboost::numeric::interval_lib::rounding_control102   static const long double& force_rounding(const long double& x) { return x; }
to_intboost::numeric::interval_lib::rounding_control103   static long double to_int(const long double& x) { return rint(x); }
104 };
105 
106 } // namespace interval_lib
107 } // namespace numeric
108 } // namespace boost
109 
110 #undef BOOST_NUMERIC_INTERVAL_NO_HARDWARE
111 
112 #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_SPARC_ROUNDING_CONTROL_HPP */
113