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_generic.hpp
11 * This header contains generic implementation of the extra floating point atomic operations.
14 #ifndef BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_GENERIC_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_EXTRA_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_type.hpp>
22 #include <boost/atomic/detail/extra_fp_operations_fwd.hpp>
23 #include <boost/atomic/detail/type_traits/is_iec559.hpp>
24 #include <boost/atomic/detail/type_traits/is_integral.hpp>
26 #ifdef BOOST_HAS_PRAGMA_ONCE
30 #if defined(BOOST_GCC) && (BOOST_GCC+0) >= 60000
31 #pragma GCC diagnostic push
32 // ignoring attributes on template argument X - this warning is because we need to pass storage_type as a template argument; no problem in this case
33 #pragma GCC diagnostic ignored "-Wignored-attributes"
40 //! Negate implementation
45 #if defined(BOOST_ATOMIC_DETAIL_INT_FP_ENDIAN_MATCH)
46 , bool = atomics::detail::is_iec559< Value >::value && atomics::detail::is_integral< typename Base::storage_type >::value
49 struct generic_extra_fp_negate :
52 typedef Base base_type;
53 typedef typename base_type::storage_type storage_type;
54 typedef Value value_type;
56 static BOOST_FORCEINLINE value_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
58 storage_type old_storage, new_storage;
59 value_type old_val, new_val;
60 atomics::detail::non_atomic_load(storage, old_storage);
63 old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
65 new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
67 while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
71 static BOOST_FORCEINLINE value_type negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
73 storage_type old_storage, new_storage;
74 value_type old_val, new_val;
75 atomics::detail::non_atomic_load(storage, old_storage);
78 old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
80 new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
82 while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
86 static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
88 fetch_negate(storage, order);
92 #if defined(BOOST_ATOMIC_DETAIL_INT_FP_ENDIAN_MATCH)
94 //! Negate implementation for IEEE 754 / IEC 559 floating point types. We leverage the fact that the sign bit is the most significant bit in the value.
95 template< typename Base, typename Value, std::size_t Size >
96 struct generic_extra_fp_negate< Base, Value, Size, true > :
99 typedef Base base_type;
100 typedef typename base_type::storage_type storage_type;
101 typedef Value value_type;
103 //! The mask with only one sign bit set to 1
104 static BOOST_CONSTEXPR_OR_CONST storage_type sign_mask = static_cast< storage_type >(1u) << (atomics::detail::value_sizeof< value_type >::value * 8u - 1u);
106 static BOOST_FORCEINLINE value_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
108 return atomics::detail::bitwise_fp_cast< value_type >(base_type::fetch_xor(storage, sign_mask, order));
111 static BOOST_FORCEINLINE value_type negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
113 return atomics::detail::bitwise_fp_cast< value_type >(base_type::bitwise_xor(storage, sign_mask, order));
116 static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
118 base_type::opaque_xor(storage, sign_mask, order);
122 #endif // defined(BOOST_ATOMIC_DETAIL_INT_FP_ENDIAN_MATCH)
124 //! Generic implementation of floating point operations
125 template< typename Base, typename Value, std::size_t Size >
126 struct generic_extra_fp_operations :
127 public generic_extra_fp_negate< Base, Value, Size >
129 typedef generic_extra_fp_negate< Base, Value, Size > base_type;
130 typedef typename base_type::storage_type storage_type;
131 typedef Value value_type;
133 static BOOST_FORCEINLINE value_type add(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
135 storage_type old_storage, new_storage;
136 value_type old_val, new_val;
137 atomics::detail::non_atomic_load(storage, old_storage);
140 old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
141 new_val = old_val + v;
142 new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
144 while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
148 static BOOST_FORCEINLINE value_type sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
150 storage_type old_storage, new_storage;
151 value_type old_val, new_val;
152 atomics::detail::non_atomic_load(storage, old_storage);
155 old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
156 new_val = old_val - v;
157 new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
159 while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
163 static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
165 base_type::fetch_add(storage, v, order);
168 static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
170 base_type::fetch_sub(storage, v, order);
174 // Default extra_fp_operations template definition will be used unless specialized for a specific platform
175 template< typename Base, typename Value, std::size_t Size >
176 struct extra_fp_operations< Base, Value, Size, true > :
177 public generic_extra_fp_operations< Base, Value, Size >
181 } // namespace detail
182 } // namespace atomics
185 #if defined(BOOST_GCC) && (BOOST_GCC+0) >= 60000
186 #pragma GCC diagnostic pop
189 #endif // BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_