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) 2017 Andrey Semashev
9 * \file atomic/detail/extra_ops_gcc_arm.hpp
11 * This header contains implementation of the extra atomic operations for ARM.
14 #ifndef BOOST_ATOMIC_DETAIL_EXTRA_OPS_GCC_ARM_HPP_INCLUDED_
15 #define BOOST_ATOMIC_DETAIL_EXTRA_OPS_GCC_ARM_HPP_INCLUDED_
18 #include <boost/cstdint.hpp>
19 #include <boost/memory_order.hpp>
20 #include <boost/atomic/detail/config.hpp>
21 #include <boost/atomic/detail/platform.hpp>
22 #include <boost/atomic/detail/storage_type.hpp>
23 #include <boost/atomic/detail/extra_operations_fwd.hpp>
24 #include <boost/atomic/detail/extra_ops_generic.hpp>
25 #include <boost/atomic/detail/ops_gcc_arm_common.hpp>
26 #include <boost/atomic/capabilities.hpp>
28 #ifdef BOOST_HAS_PRAGMA_ONCE
36 #if defined(BOOST_ATOMIC_DETAIL_ARM_HAS_LDREXB_STREXB)
38 template< typename Base, bool Signed >
39 struct extra_operations< Base, 1u, Signed > :
40 public generic_extra_operations< Base, 1u, Signed >
42 typedef generic_extra_operations< Base, 1u, Signed > base_type;
43 typedef typename base_type::storage_type storage_type;
44 typedef typename make_storage_type< 4u, Signed >::type extended_storage_type;
46 static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
48 gcc_arm_operations_base::fence_before(order);
50 extended_storage_type original, result;
53 BOOST_ATOMIC_DETAIL_ARM_ASM_START(%[tmp])
55 "ldrexb %[original], %[storage]\n" // original = *(&storage)
56 "rsb %[result], %[original], #0\n" // result = 0 - original
57 "strexb %[tmp], %[result], %[storage]\n" // *(&storage) = result, tmp = store failed
58 "teq %[tmp], #0\n" // flags = tmp==0
59 "bne 1b\n" // if (!flags.equal) goto retry
60 BOOST_ATOMIC_DETAIL_ARM_ASM_END(%[tmp])
61 : [original] "=&r" (original), // %0
62 [result] "=&r" (result), // %1
63 [tmp] "=&l" (tmp), // %2
64 [storage] "+Q" (storage) // %3
66 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
68 gcc_arm_operations_base::fence_after(order);
69 return static_cast< storage_type >(original);
72 static BOOST_FORCEINLINE storage_type fetch_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
74 gcc_arm_operations_base::fence_before(order);
76 extended_storage_type original, result;
79 BOOST_ATOMIC_DETAIL_ARM_ASM_START(%[tmp])
81 "ldrexb %[original], %[storage]\n" // original = *(&storage)
82 "mvn %[result], %[original]\n" // result = NOT original
83 "strexb %[tmp], %[result], %[storage]\n" // *(&storage) = result, tmp = store failed
84 "teq %[tmp], #0\n" // flags = tmp==0
85 "bne 1b\n" // if (!flags.equal) goto retry
86 BOOST_ATOMIC_DETAIL_ARM_ASM_END(%[tmp])
87 : [original] "=&r" (original), // %0
88 [result] "=&r" (result), // %1
89 [tmp] "=&l" (tmp), // %2
90 [storage] "+Q" (storage) // %3
92 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
94 gcc_arm_operations_base::fence_after(order);
95 return static_cast< storage_type >(original);
98 static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
100 fetch_negate(storage, order);
103 static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
105 fetch_complement(storage, order);
109 #endif // defined(BOOST_ATOMIC_DETAIL_ARM_HAS_LDREXB_STREXB)
111 #if defined(BOOST_ATOMIC_DETAIL_ARM_HAS_LDREXH_STREXH)
113 template< typename Base, bool Signed >
114 struct extra_operations< Base, 2u, Signed > :
115 public generic_extra_operations< Base, 2u, Signed >
117 typedef generic_extra_operations< Base, 2u, Signed > base_type;
118 typedef typename base_type::storage_type storage_type;
119 typedef typename make_storage_type< 4u, Signed >::type extended_storage_type;
121 static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
123 gcc_arm_operations_base::fence_before(order);
125 extended_storage_type original, result;
128 BOOST_ATOMIC_DETAIL_ARM_ASM_START(%[tmp])
130 "ldrexh %[original], %[storage]\n" // original = *(&storage)
131 "rsb %[result], %[original], #0\n" // result = 0 - original
132 "strexh %[tmp], %[result], %[storage]\n" // *(&storage) = result, tmp = store failed
133 "teq %[tmp], #0\n" // flags = tmp==0
134 "bne 1b\n" // if (!flags.equal) goto retry
135 BOOST_ATOMIC_DETAIL_ARM_ASM_END(%[tmp])
136 : [original] "=&r" (original), // %0
137 [result] "=&r" (result), // %1
138 [tmp] "=&l" (tmp), // %2
139 [storage] "+Q" (storage) // %3
141 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
143 gcc_arm_operations_base::fence_after(order);
144 return static_cast< storage_type >(original);
147 static BOOST_FORCEINLINE storage_type fetch_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
149 gcc_arm_operations_base::fence_before(order);
151 extended_storage_type original, result;
154 BOOST_ATOMIC_DETAIL_ARM_ASM_START(%[tmp])
156 "ldrexh %[original], %[storage]\n" // original = *(&storage)
157 "mvn %[result], %[original]\n" // result = NOT original
158 "strexh %[tmp], %[result], %[storage]\n" // *(&storage) = result, tmp = store failed
159 "teq %[tmp], #0\n" // flags = tmp==0
160 "bne 1b\n" // if (!flags.equal) goto retry
161 BOOST_ATOMIC_DETAIL_ARM_ASM_END(%[tmp])
162 : [original] "=&r" (original), // %0
163 [result] "=&r" (result), // %1
164 [tmp] "=&l" (tmp), // %2
165 [storage] "+Q" (storage) // %3
167 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
169 gcc_arm_operations_base::fence_after(order);
170 return static_cast< storage_type >(original);
173 static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
175 fetch_negate(storage, order);
178 static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
180 fetch_complement(storage, order);
184 #endif // defined(BOOST_ATOMIC_DETAIL_ARM_HAS_LDREXH_STREXH)
186 template< typename Base, bool Signed >
187 struct extra_operations< Base, 4u, Signed > :
188 public generic_extra_operations< Base, 4u, Signed >
190 typedef generic_extra_operations< Base, 4u, Signed > base_type;
191 typedef typename base_type::storage_type storage_type;
193 static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
195 gcc_arm_operations_base::fence_before(order);
197 storage_type original, result;
200 BOOST_ATOMIC_DETAIL_ARM_ASM_START(%[tmp])
202 "ldrex %[original], %[storage]\n" // original = *(&storage)
203 "rsb %[result], %[original], #0\n" // result = 0 - original
204 "strex %[tmp], %[result], %[storage]\n" // *(&storage) = result, tmp = store failed
205 "teq %[tmp], #0\n" // flags = tmp==0
206 "bne 1b\n" // if (!flags.equal) goto retry
207 BOOST_ATOMIC_DETAIL_ARM_ASM_END(%[tmp])
208 : [original] "=&r" (original), // %0
209 [result] "=&r" (result), // %1
210 [tmp] "=&l" (tmp), // %2
211 [storage] "+Q" (storage) // %3
213 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
215 gcc_arm_operations_base::fence_after(order);
219 static BOOST_FORCEINLINE storage_type fetch_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
221 gcc_arm_operations_base::fence_before(order);
223 storage_type original, result;
226 BOOST_ATOMIC_DETAIL_ARM_ASM_START(%[tmp])
228 "ldrex %[original], %[storage]\n" // original = *(&storage)
229 "mvn %[result], %[original]\n" // result = NOT original
230 "strex %[tmp], %[result], %[storage]\n" // *(&storage) = result, tmp = store failed
231 "teq %[tmp], #0\n" // flags = tmp==0
232 "bne 1b\n" // if (!flags.equal) goto retry
233 BOOST_ATOMIC_DETAIL_ARM_ASM_END(%[tmp])
234 : [original] "=&r" (original), // %0
235 [result] "=&r" (result), // %1
236 [tmp] "=&l" (tmp), // %2
237 [storage] "+Q" (storage) // %3
239 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
241 gcc_arm_operations_base::fence_after(order);
245 static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
247 fetch_negate(storage, order);
250 static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
252 fetch_complement(storage, order);
256 #if defined(BOOST_ATOMIC_DETAIL_ARM_HAS_LDREXD_STREXD)
258 template< typename Base, bool Signed >
259 struct extra_operations< Base, 8u, Signed > :
260 public generic_extra_operations< Base, 8u, Signed >
262 typedef generic_extra_operations< Base, 8u, Signed > base_type;
263 typedef typename base_type::storage_type storage_type;
265 static BOOST_FORCEINLINE storage_type fetch_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
267 gcc_arm_operations_base::fence_before(order);
268 storage_type original, result;
272 BOOST_ATOMIC_DETAIL_ARM_ASM_START(%0)
274 "ldrexd %1, %H1, [%3]\n" // original = *(&storage)
275 "mvn %2, %1\n" // result = NOT original
277 "adds %2, %2, #1\n" // result = result + 1
279 "strexd %0, %2, %H2, [%3]\n" // *(&storage) = result, tmp = store failed
280 "teq %0, #0\n" // flags = tmp==0
281 "bne 1b\n" // if (!flags.equal) goto retry
282 BOOST_ATOMIC_DETAIL_ARM_ASM_END(%0)
283 : BOOST_ATOMIC_DETAIL_ARM_ASM_TMPREG_CONSTRAINT(tmp), // %0
284 "=&r" (original), // %1
286 : "r" (&storage) // %3
287 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
289 gcc_arm_operations_base::fence_after(order);
293 static BOOST_FORCEINLINE storage_type fetch_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
295 gcc_arm_operations_base::fence_before(order);
296 storage_type original, result;
300 BOOST_ATOMIC_DETAIL_ARM_ASM_START(%0)
302 "ldrexd %1, %H1, [%3]\n" // original = *(&storage)
303 "mvn %2, %1\n" // result = NOT original
305 "strexd %0, %2, %H2, [%3]\n" // *(&storage) = result, tmp = store failed
306 "teq %0, #0\n" // flags = tmp==0
307 "bne 1b\n" // if (!flags.equal) goto retry
308 BOOST_ATOMIC_DETAIL_ARM_ASM_END(%0)
309 : BOOST_ATOMIC_DETAIL_ARM_ASM_TMPREG_CONSTRAINT(tmp), // %0
310 "=&r" (original), // %1
312 : "r" (&storage) // %3
313 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
315 gcc_arm_operations_base::fence_after(order);
319 static BOOST_FORCEINLINE void opaque_negate(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
321 fetch_negate(storage, order);
324 static BOOST_FORCEINLINE void opaque_complement(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
326 fetch_complement(storage, order);
330 #endif // defined(BOOST_ATOMIC_DETAIL_ARM_HAS_LDREXD_STREXD)
332 } // namespace detail
333 } // namespace atomics
336 #endif // BOOST_ATOMIC_DETAIL_EXTRA_OPS_GCC_ARM_HPP_INCLUDED_