x86_fast_rounding_control.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* Boost interval/detail/x86gcc_rounding_control.hpp file
  2. *
  3. * This header provides a rounding control policy
  4. * that avoids flushing results to memory. In
  5. * order for this optimization to be reliable, it
  6. * should be used only when no underflow or
  7. * overflow would happen without it. Indeed, only
  8. * values in range are correctly rounded.
  9. *
  10. * Copyright 2002 Hervé Brönnimann, Guillaume Melquiond, Sylvain Pion
  11. *
  12. * Distributed under the Boost Software License, Version 1.0.
  13. * (See accompanying file LICENSE_1_0.txt or
  14. * copy at http://www.boost.org/LICENSE_1_0.txt)
  15. */
  16. #ifndef BOOST_NUMERIC_INTERVAL_EXT_X86_FAST_ROUNDING_CONTROL_HPP
  17. #define BOOST_NUMERIC_INTERVAL_EXT_X86_FAST_ROUNDING_CONTROL_HPP
  18. namespace boost {
  19. namespace numeric {
  20. namespace interval_lib {
  21. namespace detail {
  22. // exceptions masked, expected precision (the mask is 0x0300)
  23. static const fpu_rounding_modes rnd_mode_f = { 0x107f, 0x147f, 0x187f, 0x1c7f };
  24. static const fpu_rounding_modes rnd_mode_d = { 0x127f, 0x167f, 0x1a7f, 0x1e7f };
  25. static const fpu_rounding_modes rnd_mode_l = { 0x137f, 0x177f, 0x1b7f, 0x1f7f };
  26. } // namespace detail
  27. template<class T>
  28. struct x86_fast_rounding_control;
  29. template<>
  30. struct x86_fast_rounding_control<float>: detail::x86_rounding
  31. {
  32. static void to_nearest() { set_rounding_mode(detail::rnd_mode_f.to_nearest); }
  33. static void downward() { set_rounding_mode(detail::rnd_mode_f.downward); }
  34. static void upward() { set_rounding_mode(detail::rnd_mode_f.upward); }
  35. static void toward_zero() { set_rounding_mode(detail::rnd_mode_f.toward_zero); }
  36. static const float& force_rounding(const float& r) { return r; }
  37. };
  38. template<>
  39. struct x86_fast_rounding_control<double>: detail::x86_rounding
  40. {
  41. static void to_nearest() { set_rounding_mode(detail::rnd_mode_d.to_nearest); }
  42. static void downward() { set_rounding_mode(detail::rnd_mode_d.downward); }
  43. static void upward() { set_rounding_mode(detail::rnd_mode_d.upward); }
  44. static void toward_zero() { set_rounding_mode(detail::rnd_mode_d.toward_zero); }
  45. static const double& force_rounding(const double& r) { return r; }
  46. };
  47. template<>
  48. struct x86_fast_rounding_control<long double>: detail::x86_rounding
  49. {
  50. static void to_nearest() { set_rounding_mode(detail::rnd_mode_l.to_nearest); }
  51. static void downward() { set_rounding_mode(detail::rnd_mode_l.downward); }
  52. static void upward() { set_rounding_mode(detail::rnd_mode_l.upward); }
  53. static void toward_zero() { set_rounding_mode(detail::rnd_mode_l.toward_zero); }
  54. static const long double& force_rounding(const long double& r) { return r; }
  55. };
  56. } // namespace interval_lib
  57. } // namespace numeric
  58. } // namespace boost
  59. #endif // BOOST_NUMERIC_INTERVAL_EXT_X86_FAST_ROUNDING_CONTROL_HPP