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/extra_fp_ops_generic.hpp
10  *
11  * This header contains generic implementation of the extra floating point atomic operations.
12  */
13 
14 #ifndef BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_GENERIC_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_EXTRA_FP_OPS_GENERIC_HPP_INCLUDED_
16 
17 #include <cstddef>
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>
25 
26 #ifdef BOOST_HAS_PRAGMA_ONCE
27 #pragma once
28 #endif
29 
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"
34 #endif
35 
36 namespace boost {
37 namespace atomics {
38 namespace detail {
39 
40 //! Negate implementation
41 template<
42     typename Base,
43     typename Value,
44     std::size_t Size
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
47 #endif
48 >
49 struct generic_extra_fp_negate :
50     public Base
51 {
52     typedef Base base_type;
53     typedef typename base_type::storage_type storage_type;
54     typedef Value value_type;
55 
fetch_negateboost::atomics::detail::generic_extra_fp_negate56     static BOOST_FORCEINLINE value_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
57     {
58         storage_type old_storage, new_storage;
59         value_type old_val, new_val;
60         atomics::detail::non_atomic_load(storage, old_storage);
61         do
62         {
63             old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
64             new_val = -old_val;
65             new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
66         }
67         while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
68         return old_val;
69     }
70 
negateboost::atomics::detail::generic_extra_fp_negate71     static BOOST_FORCEINLINE value_type negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
72     {
73         storage_type old_storage, new_storage;
74         value_type old_val, new_val;
75         atomics::detail::non_atomic_load(storage, old_storage);
76         do
77         {
78             old_val = atomics::detail::bitwise_fp_cast< value_type >(old_storage);
79             new_val = -old_val;
80             new_storage = atomics::detail::bitwise_fp_cast< storage_type >(new_val);
81         }
82         while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
83         return new_val;
84     }
85 
opaque_negateboost::atomics::detail::generic_extra_fp_negate86     static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
87     {
88         fetch_negate(storage, order);
89     }
90 };
91 
92 #if defined(BOOST_ATOMIC_DETAIL_INT_FP_ENDIAN_MATCH)
93 
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 > :
97     public Base
98 {
99     typedef Base base_type;
100     typedef typename base_type::storage_type storage_type;
101     typedef Value value_type;
102 
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);
105 
fetch_negateboost::atomics::detail::generic_extra_fp_negate106     static BOOST_FORCEINLINE value_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
107     {
108         return atomics::detail::bitwise_fp_cast< value_type >(base_type::fetch_xor(storage, sign_mask, order));
109     }
110 
negateboost::atomics::detail::generic_extra_fp_negate111     static BOOST_FORCEINLINE value_type negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
112     {
113         return atomics::detail::bitwise_fp_cast< value_type >(base_type::bitwise_xor(storage, sign_mask, order));
114     }
115 
opaque_negateboost::atomics::detail::generic_extra_fp_negate116     static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
117     {
118         base_type::opaque_xor(storage, sign_mask, order);
119     }
120 };
121 
122 #endif // defined(BOOST_ATOMIC_DETAIL_INT_FP_ENDIAN_MATCH)
123 
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 >
128 {
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;
132 
addboost::atomics::detail::generic_extra_fp_operations133     static BOOST_FORCEINLINE value_type add(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
134     {
135         storage_type old_storage, new_storage;
136         value_type old_val, new_val;
137         atomics::detail::non_atomic_load(storage, old_storage);
138         do
139         {
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);
143         }
144         while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
145         return new_val;
146     }
147 
subboost::atomics::detail::generic_extra_fp_operations148     static BOOST_FORCEINLINE value_type sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
149     {
150         storage_type old_storage, new_storage;
151         value_type old_val, new_val;
152         atomics::detail::non_atomic_load(storage, old_storage);
153         do
154         {
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);
158         }
159         while (!base_type::compare_exchange_weak(storage, old_storage, new_storage, order, memory_order_relaxed));
160         return new_val;
161     }
162 
opaque_addboost::atomics::detail::generic_extra_fp_operations163     static BOOST_FORCEINLINE void opaque_add(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
164     {
165         base_type::fetch_add(storage, v, order);
166     }
167 
opaque_subboost::atomics::detail::generic_extra_fp_operations168     static BOOST_FORCEINLINE void opaque_sub(storage_type volatile& storage, value_type v, memory_order order) BOOST_NOEXCEPT
169     {
170         base_type::fetch_sub(storage, v, order);
171     }
172 };
173 
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 >
178 {
179 };
180 
181 } // namespace detail
182 } // namespace atomics
183 } // namespace boost
184 
185 #if defined(BOOST_GCC) && (BOOST_GCC+0) >= 60000
186 #pragma GCC diagnostic pop
187 #endif
188 
189 #endif // BOOST_ATOMIC_DETAIL_FP_OPS_GENERIC_HPP_INCLUDED_
190