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)
6 * Copyright (c) 2018 Andrey Semashev
9 * \file atomic/detail/fp_ops_generic.hpp
11 * This header contains generic implementation of the floating point atomic operations.
14 #ifndef BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_
18 #include <boost/memory_order.hpp>
19 #include <boost/atomic/detail/config.hpp>
20 #include <boost/atomic/detail/bitwise_fp_cast.hpp>
21 #include <boost/atomic/detail/storage_traits.hpp>
22 #include <boost/atomic/detail/fp_operations_fwd.hpp>
23 #include <boost/atomic/detail/header.hpp>
25 #ifdef BOOST_HAS_PRAGMA_ONCE
33 //! Generic implementation of floating point operations
34 template< typename Base, typename Value, std::size_t Size >
35 struct fp_operations_generic :
38 typedef Base base_type;
39 typedef typename base_type::storage_type storage_type;
40 typedef Value value_type;
42 static BOOST_FORCEINLINE value_type fetch_add(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
44 storage_type old_storage, new_storage;
45 value_type old_val, new_val;
46 atomics::detail::non_atomic_load(storage, old_storage);
49 old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
50 new_val = old_val + v;
51 new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
53 while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
57 static BOOST_FORCEINLINE value_type fetch_sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
59 storage_type old_storage, new_storage;
60 value_type old_val, new_val;
61 atomics::detail::non_atomic_load(storage, old_storage);
64 old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
65 new_val = old_val - v;
66 new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
68 while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
73 // Default fp_operations template definition will be used unless specialized for a specific platform
74 template< typename Base, typename Value, std::size_t Size >
75 struct fp_operations< Base, Value, Size, true > :
76 public fp_operations_generic< Base, Value, Size >
81 } // namespace atomics
84 #include <boost/atomic/detail/footer.hpp>
86 #endif // BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_