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/extra_fp_ops_emulated.hpp
11 * This header contains emulated (lock-based) implementation of the extra floating point atomic operations.
14 #ifndef BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_EMULATED_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_EMULATED_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/extra_fp_operations_fwd.hpp>
22 #include <boost/atomic/detail/lockpool.hpp>
24 #ifdef BOOST_HAS_PRAGMA_ONCE
32 //! Generic implementation of extra floating point operations
33 template< typename Base, typename Value, std::size_t Size >
34 struct emulated_extra_fp_operations :
37 typedef Base base_type;
38 typedef typename base_type::storage_type storage_type;
39 typedef Value value_type;
41 static BOOST_FORCEINLINE value_type fetch_negate(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
43 storage_type& s = const_cast< storage_type& >(storage);
44 lockpool::scoped_lock lock(&storage);
45 value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
46 value_type new_val = -old_val;
47 s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
51 static BOOST_FORCEINLINE value_type negate(storage_type volatile& storage, memory_order) BOOST_NOEXCEPT
53 storage_type& s = const_cast< storage_type& >(storage);
54 lockpool::scoped_lock lock(&storage);
55 value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
56 value_type new_val = -old_val;
57 s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
61 static BOOST_FORCEINLINE value_type add(storage_type volatile& storage, value_type v, memory_order) BOOST_NOEXCEPT
63 storage_type& s = const_cast< storage_type& >(storage);
64 lockpool::scoped_lock lock(&storage);
65 value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
66 value_type new_val = old_val + v;
67 s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
71 static BOOST_FORCEINLINE value_type sub(storage_type volatile& storage, value_type v, memory_order) BOOST_NOEXCEPT
73 storage_type& s = const_cast< storage_type& >(storage);
74 lockpool::scoped_lock lock(&storage);
75 value_type old_val = atomics::detail::bitwise_fp_cast< value_type >(s);
76 value_type new_val = old_val - v;
77 s = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
81 static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
83 fetch_negate(storage, order);
86 static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
88 base_type::fetch_add(storage, v, order);
91 static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
93 base_type::fetch_sub(storage, v, order);
97 template< typename Base, typename Value, std::size_t Size >
98 struct extra_fp_operations< Base, Value, Size, false > :
99 public emulated_extra_fp_operations< Base, Value, Size >
103 } // namespace detail
104 } // namespace atomics
107 #endif // BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_EMULATED_HPP_INCLUDED_