x86_rounding_control.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* Boost interval/detail/x86_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. #ifndef BOOST_NUMERIC_INTERVAL_DETAIL_X86_ROUNDING_CONTROL_HPP
  11. #define BOOST_NUMERIC_INTERVAL_DETAIL_X86_ROUNDING_CONTROL_HPP
  12. #ifdef __GNUC__
  13. # include <boost/numeric/interval/detail/x86gcc_rounding_control.hpp>
  14. #elif defined(__BORLANDC__)
  15. # include <boost/numeric/interval/detail/bcc_rounding_control.hpp>
  16. #elif defined(_MSC_VER)
  17. # include <boost/numeric/interval/detail/msvc_rounding_control.hpp>
  18. #elif defined(__MWERKS__) || defined(__ICC) || defined (__SUNPRO_CC)
  19. # define BOOST_NUMERIC_INTERVAL_USE_C99_SUBSYSTEM
  20. # include <boost/numeric/interval/detail/c99sub_rounding_control.hpp>
  21. #else
  22. # error Unsupported C++ compiler.
  23. #endif
  24. namespace boost {
  25. namespace numeric {
  26. namespace interval_lib {
  27. namespace detail {
  28. #ifdef BOOST_NUMERIC_INTERVAL_USE_C99_SUBSYSTEM
  29. typedef c99_rounding_control x86_rounding_control;
  30. #undef BOOST_NUMERIC_INTERVAL_USE_C99_SUBSYSTEM
  31. #else
  32. struct fpu_rounding_modes
  33. {
  34. unsigned short to_nearest;
  35. unsigned short downward;
  36. unsigned short upward;
  37. unsigned short toward_zero;
  38. };
  39. // exceptions masked, extended precision
  40. // hardware default is 0x037f (0x1000 only has a meaning on 287)
  41. static const fpu_rounding_modes rnd_mode = { 0x137f, 0x177f, 0x1b7f, 0x1f7f };
  42. struct x86_rounding_control: x86_rounding
  43. {
  44. static void to_nearest() { set_rounding_mode(rnd_mode.to_nearest); }
  45. static void downward() { set_rounding_mode(rnd_mode.downward); }
  46. static void upward() { set_rounding_mode(rnd_mode.upward); }
  47. static void toward_zero() { set_rounding_mode(rnd_mode.toward_zero); }
  48. };
  49. #endif // BOOST_NUMERIC_INTERVAL_USE_C99_SUBSYSTEM
  50. } // namespace detail
  51. template<>
  52. struct rounding_control<float>: detail::x86_rounding_control
  53. {
  54. static float force_rounding(const float& r)
  55. { volatile float r_ = r; return r_; }
  56. };
  57. template<>
  58. struct rounding_control<double>: detail::x86_rounding_control
  59. {
  60. /*static double force_rounding(double r)
  61. { asm volatile ("" : "+m"(r) : ); return r; }*/
  62. static double force_rounding(const double& r)
  63. { volatile double r_ = r; return r_; }
  64. };
  65. namespace detail {
  66. template<bool>
  67. struct x86_rounding_control_long_double;
  68. template<>
  69. struct x86_rounding_control_long_double<false>: x86_rounding_control
  70. {
  71. static long double force_rounding(long double const &r)
  72. { volatile long double r_ = r; return r_; }
  73. };
  74. template<>
  75. struct x86_rounding_control_long_double<true>: x86_rounding_control
  76. {
  77. static long double const &force_rounding(long double const &r)
  78. { return r; }
  79. };
  80. } // namespace detail
  81. template<>
  82. struct rounding_control<long double>:
  83. detail::x86_rounding_control_long_double< (sizeof(long double) >= 10) >
  84. {};
  85. } // namespace interval_lib
  86. } // namespace numeric
  87. } // namespace boost
  88. #undef BOOST_NUMERIC_INTERVAL_NO_HARDWARE
  89. #endif /* BOOST_NUMERIC_INTERVAL_DETAIL_X86_ROUNDING_CONTROL_HPP */