fp_ops_generic.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_type.hpp>
  20. #include <boost/atomic/detail/fp_operations_fwd.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 generic_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 order) BOOST_NOEXCEPT
  36. {
  37. storage_type old_storage, new_storage;
  38. value_type old_val, new_val;
  39. atomics::detail::non_atomic_load(storage, old_storage);
  40. do
  41. {
  42. old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
  43. new_val = old_val + v;
  44. new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  45. }
  46. while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
  47. return old_val;
  48. }
  49. static BOOST_FORCEINLINE value_type fetch_sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
  50. {
  51. storage_type old_storage, new_storage;
  52. value_type old_val, new_val;
  53. atomics::detail::non_atomic_load(storage, old_storage);
  54. do
  55. {
  56. old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
  57. new_val = old_val - v;
  58. new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
  59. }
  60. while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
  61. return old_val;
  62. }
  63. };
  64. // Default fp_operations template definition will be used unless specialized for a specific platform
  65. template< typename Base, typename Value, std::size_t Size >
  66. struct fp_operations< Base, Value, Size, true > :
  67. public generic_fp_operations< Base, Value, Size >
  68. {
  69. };
  70. } // namespace detail
  71. } // namespace atomics
  72. } // namespace boost
  73. #endif // BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_