extra_fp_ops_emulated.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2018 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/extra_fp_ops_emulated.hpp
  10. *
  11. * This header contains emulated (lock-based) implementation of the extra floating point atomic operations.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_EMULATED_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_EMULATED_HPP_INCLUDED_
  15. #include <cstddef>
  16. #include <boost/memory_order.hpp>
  17. #include <boost/atomic/detail/config.hpp>
  18. #include <boost/atomic/detail/bitwise_fp_cast.hpp>
  19. #include <boost/atomic/detail/extra_fp_operations_fwd.hpp>
  20. #include <boost/atomic/detail/header.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. namespace boost {
  25. namespace atomics {
  26. namespace detail {
  27. //! Emulated implementation of extra floating point operations
  28. template< typename Base, typename Value, std::size_t Size >
  29. struct extra_fp_operations_emulated :
  30. public Base
  31. {
  32. typedef Base base_type;
  33. typedef typename base_type::storage_type storage_type;
  34. typedef Value value_type;
  35. typedef typename base_type::scoped_lock scoped_lock;
  36. static value_type fetch_negate(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  37. {
  38. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  39. storage_type& s = const_cast< storage_type& >(storage);
  40. scoped_lock lock(&storage);
  41. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  42. value_type new_val = -old_val;
  43. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  44. return old_val;
  45. }
  46. static value_type negate(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
  47. {
  48. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  49. storage_type& s = const_cast< storage_type& >(storage);
  50. scoped_lock lock(&storage);
  51. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  52. value_type new_val = -old_val;
  53. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  54. return new_val;
  55. }
  56. static value_type add(storage_type volatile& storage, value_type v, memory_order) BOOST_NOEXCEPT
  57. {
  58. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  59. storage_type& s = const_cast< storage_type& >(storage);
  60. scoped_lock lock(&storage);
  61. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  62. value_type new_val = old_val + v;
  63. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  64. return new_val;
  65. }
  66. static value_type sub(storage_type volatile& storage, value_type v, memory_order) BOOST_NOEXCEPT
  67. {
  68. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  69. storage_type& s = const_cast< storage_type& >(storage);
  70. scoped_lock lock(&storage);
  71. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  72. value_type new_val = old_val - v;
  73. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  74. return new_val;
  75. }
  76. static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
  77. {
  78. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  79. fetch_negate(storage, order);
  80. }
  81. static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
  82. {
  83. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  84. base_type::fetch_add(storage, v, order);
  85. }
  86. static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
  87. {
  88. static_assert(!base_type::is_interprocess, "Boost.Atomic: operation invoked on a non-lock-free inter-process atomic object");
  89. base_type::fetch_sub(storage, v, order);
  90. }
  91. };
  92. template< typename Base, typename Value, std::size_t Size >
  93. struct extra_fp_operations< Base, Value, Size, false > :
  94. public extra_fp_operations_emulated< Base, Value, Size >
  95. {
  96. };
  97. } // namespace detail
  98. } // namespace atomics
  99. } // namespace boost
  100. #include <boost/atomic/detail/footer.hpp>
  101. #endif // BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_EMULATED_HPP_INCLUDED_