fp_ops_generic.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_generic.hpp
  10. *
  11. * This header contains generic implementation of the floating point atomic operations.
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_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/storage_traits.hpp>
  20. #include <boost/atomic/detail/fp_operations_fwd.hpp>
  21. #include <boost/atomic/detail/header.hpp>
  22. #ifdef BOOST_HAS_PRAGMA_ONCE
  23. #pragma once
  24. #endif
  25. namespace boost {
  26. namespace atomics {
  27. namespace detail {
  28. //! Generic implementation of floating point operations
  29. template< typename Base, typename Value, std::size_t Size >
  30. struct fp_operations_generic :
  31. public Base
  32. {
  33. typedef Base base_type;
  34. typedef typename base_type::storage_type storage_type;
  35. typedef Value value_type;
  36. static BOOST_FORCEINLINE value_type fetch_add(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
  37. {
  38. storage_type old_storage, new_storage;
  39. value_type old_val, new_val;
  40. atomics::detail::non_atomic_load(storage, old_storage);
  41. do
  42. {
  43. old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
  44. new_val = old_val + v;
  45. new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  46. }
  47. while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
  48. return old_val;
  49. }
  50. static BOOST_FORCEINLINE value_type fetch_sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
  51. {
  52. storage_type old_storage, new_storage;
  53. value_type old_val, new_val;
  54. atomics::detail::non_atomic_load(storage, old_storage);
  55. do
  56. {
  57. old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
  58. new_val = old_val - v;
  59. new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  60. }
  61. while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
  62. return old_val;
  63. }
  64. };
  65. // Default fp_operations template definition will be used unless specialized for a specific platform
  66. template< typename Base, typename Value, std::size_t Size >
  67. struct fp_operations< Base, Value, Size, true > :
  68. public fp_operations_generic< Base, Value, Size >
  69. {
  70. };
  71. } // namespace detail
  72. } // namespace atomics
  73. } // namespace boost
  74. #include <boost/atomic/detail/footer.hpp>
  75. #endif // BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_