fp_ops_emulated.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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/fp_ops_emulated.hpp
  10. *
  11. * This header contains emulated (lock-based) implementation of the floating point atomic operations.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_FP_OPS_EMULATED_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_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/fp_operations_fwd.hpp>
  20. #include <boost/atomic/detail/lockpool.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. namespace boost {
  25. namespace atomics {
  26. namespace detail {
  27. //! Generic implementation of floating point operations
  28. template< typename Base, typename Value, std::size_t Size >
  29. struct emulated_fp_operations :
  30. public Base
  31. {
  32. typedef Base base_type;
  33. typedef typename base_type::storage_type storage_type;
  34. typedef Value value_type;
  35. static BOOST_FORCEINLINE value_type fetch_add(storage_type volatile& storage, value_type v, memory_order) BOOST_NOEXCEPT
  36. {
  37. storage_type& s = const_cast< storage_type& >(storage);
  38. lockpool::scoped_lock lock(&storage);
  39. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  40. value_type new_val = old_val + v;
  41. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  42. return old_val;
  43. }
  44. static BOOST_FORCEINLINE value_type fetch_sub(storage_type volatile& storage, value_type v, memory_order) BOOST_NOEXCEPT
  45. {
  46. storage_type& s = const_cast< storage_type& >(storage);
  47. lockpool::scoped_lock lock(&storage);
  48. value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
  49. value_type new_val = old_val - v;
  50. s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  51. return old_val;
  52. }
  53. };
  54. template< typename Base, typename Value, std::size_t Size >
  55. struct fp_operations< Base, Value, Size, false > :
  56. public emulated_fp_operations< Base, Value, Size >
  57. {
  58. };
  59. } // namespace detail
  60. } // namespace atomics
  61. } // namespace boost
  62. #endif // BOOST_ATOMIC_DETAIL_FP_OPS_EMULATED_HPP_INCLUDED_