sparc_rounding_control.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef BOOST_NUMERIC_INTERVAL_DETAIL_SPARC_ROUNDING_CONTROL_HPP
  13. #define BOOST_NUMERIC_INTERVAL_DETAIL_SPARC_ROUNDING_CONTROL_HPP
  14. #if !defined(sparc) && !defined(__sparc__)
  15. # error This header is only intended for SPARC CPUs.
  16. #endif
  17. #ifdef __SUNPRO_CC
  18. # include <ieeefp.h>
  19. #endif
  20. namespace boost {
  21. namespace numeric {
  22. namespace interval_lib {
  23. namespace detail {
  24. struct sparc_rounding_control
  25. {
  26. typedef unsigned int rounding_mode;
  27. static void set_rounding_mode(const rounding_mode& mode)
  28. {
  29. # if defined(__GNUC__)
  30. __asm__ __volatile__("ld %0, %%fsr" : : "m"(mode));
  31. # elif defined (__SUNPRO_CC)
  32. fpsetround(fp_rnd(mode));
  33. # elif defined(__KCC)
  34. asm("sethi %hi(mode), %o1");
  35. asm("ld [%o1+%lo(mode)], %fsr");
  36. # else
  37. # error Unsupported compiler for Sparc rounding control.
  38. # endif
  39. }
  40. static void get_rounding_mode(rounding_mode& mode)
  41. {
  42. # if defined(__GNUC__)
  43. __asm__ __volatile__("st %%fsr, %0" : "=m"(mode));
  44. # elif defined (__SUNPRO_CC)
  45. mode = fpgetround();
  46. # elif defined(__KCC)
  47. # error KCC on Sun SPARC get_round_mode: please fix me
  48. asm("st %fsr, [mode]");
  49. # else
  50. # error Unsupported compiler for Sparc rounding control.
  51. # endif
  52. }
  53. #if defined(__SUNPRO_CC)
  54. static void downward() { set_rounding_mode(FP_RM); }
  55. static void upward() { set_rounding_mode(FP_RP); }
  56. static void to_nearest() { set_rounding_mode(FP_RN); }
  57. static void toward_zero() { set_rounding_mode(FP_RZ); }
  58. #else
  59. static void downward() { set_rounding_mode(0xc0000000); }
  60. static void upward() { set_rounding_mode(0x80000000); }
  61. static void to_nearest() { set_rounding_mode(0x00000000); }
  62. static void toward_zero() { set_rounding_mode(0x40000000); }
  63. #endif
  64. };
  65. } // namespace detail
  66. extern "C" {
  67. float rintf(float);
  68. double rint(double);
  69. }
  70. template<>
  71. struct rounding_control<float>:
  72. detail::sparc_rounding_control
  73. {
  74. static const float& force_rounding(const float& x) { return x; }
  75. static float to_int(const float& x) { return rintf(x); }
  76. };
  77. template<>
  78. struct rounding_control<double>:
  79. detail::sparc_rounding_control
  80. {
  81. static const double& force_rounding(const double& x) { return x; }
  82. static double to_int(const double& x) { return rint(x); }
  83. };
  84. template<>
  85. struct rounding_control<long double>:
  86. detail::sparc_rounding_control
  87. {
  88. static const long double& force_rounding(const long double& x) { return x; }
  89. static long double to_int(const long double& x) { return rint(x); }
  90. };
  91. } // namespace interval_lib
  92. } // namespace numeric
  93. } // namespace boost
  94. #undef BOOST_NUMERIC_INTERVAL_NO_HARDWARE
  95. #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_SPARC_ROUNDING_CONTROL_HPP */