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) 2009 Helge Bahmann
7 * Copyright (c) 2012 Tim Blechmann
8 * Copyright (c) 2014 Andrey Semashev
11 * \file atomic/detail/core_arch_ops_gcc_x86.hpp
13 * This header contains implementation of the \c core_arch_operations template.
16 #ifndef BOOST_ATOMIC_DETAIL_CORE_ARCH_OPS_GCC_X86_HPP_INCLUDED_
17 #define BOOST_ATOMIC_DETAIL_CORE_ARCH_OPS_GCC_X86_HPP_INCLUDED_
20 #include <boost/memory_order.hpp>
21 #include <boost/atomic/detail/config.hpp>
22 #include <boost/atomic/detail/storage_traits.hpp>
23 #include <boost/atomic/detail/core_arch_operations_fwd.hpp>
24 #include <boost/atomic/detail/capabilities.hpp>
25 #if defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B) || defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B)
26 #include <boost/cstdint.hpp>
27 #include <boost/atomic/detail/intptr.hpp>
28 #include <boost/atomic/detail/string_ops.hpp>
29 #include <boost/atomic/detail/core_ops_cas_based.hpp>
31 #include <boost/atomic/detail/header.hpp>
33 #ifdef BOOST_HAS_PRAGMA_ONCE
41 struct core_arch_operations_gcc_x86_base
43 static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = false;
44 static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
46 static BOOST_FORCEINLINE void fence_before(memory_order order) BOOST_NOEXCEPT
48 if ((static_cast< unsigned int >(order) & static_cast< unsigned int >(memory_order_release)) != 0u)
49 __asm__ __volatile__ ("" ::: "memory");
52 static BOOST_FORCEINLINE void fence_after(memory_order order) BOOST_NOEXCEPT
54 if ((static_cast< unsigned int >(order) & (static_cast< unsigned int >(memory_order_consume) | static_cast< unsigned int >(memory_order_acquire))) != 0u)
55 __asm__ __volatile__ ("" ::: "memory");
59 template< std::size_t Size, bool Signed, bool Interprocess, typename Derived >
60 struct core_arch_operations_gcc_x86 :
61 public core_arch_operations_gcc_x86_base
63 typedef typename storage_traits< Size >::type storage_type;
65 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = Size;
66 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = Size;
67 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
68 static BOOST_CONSTEXPR_OR_CONST bool is_interprocess = Interprocess;
70 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
72 if (order != memory_order_seq_cst)
80 Derived::exchange(storage, v, order);
84 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
86 storage_type v = storage;
91 static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
93 return Derived::fetch_add(storage, -v, order);
96 static BOOST_FORCEINLINE bool compare_exchange_weak(
97 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
99 return Derived::compare_exchange_strong(storage, expected, desired, success_order, failure_order);
102 static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
104 return !!Derived::exchange(storage, (storage_type)1, order);
107 static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
109 store(storage, (storage_type)0, order);
113 template< bool Signed, bool Interprocess >
114 struct core_arch_operations< 1u, Signed, Interprocess > :
115 public core_arch_operations_gcc_x86< 1u, Signed, Interprocess, core_arch_operations< 1u, Signed, Interprocess > >
117 typedef core_arch_operations_gcc_x86< 1u, Signed, Interprocess, core_arch_operations< 1u, Signed, Interprocess > > base_type;
118 typedef typename base_type::storage_type storage_type;
119 typedef typename storage_traits< 4u >::type temp_storage_type;
121 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
126 : "+q" (v), "+m" (storage)
128 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
133 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
138 : "+q" (v), "+m" (storage)
145 static BOOST_FORCEINLINE bool compare_exchange_strong(
146 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
148 storage_type previous = expected;
150 #if defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
153 "lock; cmpxchgb %3, %1"
154 : "+a" (previous), "+m" (storage), "=@ccz" (success)
156 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
158 #else // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
161 "lock; cmpxchgb %3, %1\n\t"
163 : "+a" (previous), "+m" (storage), "=q" (success)
165 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
167 #endif // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
172 #define BOOST_ATOMIC_DETAIL_CAS_LOOP(op, argument, result)\
173 temp_storage_type new_val;\
174 __asm__ __volatile__\
177 "1: mov %[arg], %2\n\t"\
179 "lock; cmpxchgb %b2, %[storage]\n\t"\
181 : [res] "+a" (result), [storage] "+m" (storage), "=&q" (new_val)\
182 : [arg] "ir" ((temp_storage_type)argument)\
183 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"\
186 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
188 storage_type res = storage;
189 BOOST_ATOMIC_DETAIL_CAS_LOOP("andb", v, res);
193 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
195 storage_type res = storage;
196 BOOST_ATOMIC_DETAIL_CAS_LOOP("orb", v, res);
200 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
202 storage_type res = storage;
203 BOOST_ATOMIC_DETAIL_CAS_LOOP("xorb", v, res);
207 #undef BOOST_ATOMIC_DETAIL_CAS_LOOP
210 template< bool Signed, bool Interprocess >
211 struct core_arch_operations< 2u, Signed, Interprocess > :
212 public core_arch_operations_gcc_x86< 2u, Signed, Interprocess, core_arch_operations< 2u, Signed, Interprocess > >
214 typedef core_arch_operations_gcc_x86< 2u, Signed, Interprocess, core_arch_operations< 2u, Signed, Interprocess > > base_type;
215 typedef typename base_type::storage_type storage_type;
216 typedef typename storage_traits< 4u >::type temp_storage_type;
218 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
223 : "+q" (v), "+m" (storage)
225 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
230 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
235 : "+q" (v), "+m" (storage)
242 static BOOST_FORCEINLINE bool compare_exchange_strong(
243 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
245 storage_type previous = expected;
247 #if defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
250 "lock; cmpxchgw %3, %1"
251 : "+a" (previous), "+m" (storage), "=@ccz" (success)
253 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
255 #else // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
258 "lock; cmpxchgw %3, %1\n\t"
260 : "+a" (previous), "+m" (storage), "=q" (success)
262 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
264 #endif // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
269 #define BOOST_ATOMIC_DETAIL_CAS_LOOP(op, argument, result)\
270 temp_storage_type new_val;\
271 __asm__ __volatile__\
274 "1: mov %[arg], %2\n\t"\
276 "lock; cmpxchgw %w2, %[storage]\n\t"\
278 : [res] "+a" (result), [storage] "+m" (storage), "=&q" (new_val)\
279 : [arg] "ir" ((temp_storage_type)argument)\
280 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"\
283 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
285 storage_type res = storage;
286 BOOST_ATOMIC_DETAIL_CAS_LOOP("andw", v, res);
290 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
292 storage_type res = storage;
293 BOOST_ATOMIC_DETAIL_CAS_LOOP("orw", v, res);
297 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
299 storage_type res = storage;
300 BOOST_ATOMIC_DETAIL_CAS_LOOP("xorw", v, res);
304 #undef BOOST_ATOMIC_DETAIL_CAS_LOOP
307 template< bool Signed, bool Interprocess >
308 struct core_arch_operations< 4u, Signed, Interprocess > :
309 public core_arch_operations_gcc_x86< 4u, Signed, Interprocess, core_arch_operations< 4u, Signed, Interprocess > >
311 typedef core_arch_operations_gcc_x86< 4u, Signed, Interprocess, core_arch_operations< 4u, Signed, Interprocess > > base_type;
312 typedef typename base_type::storage_type storage_type;
314 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
319 : "+r" (v), "+m" (storage)
321 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
326 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
331 : "+r" (v), "+m" (storage)
338 static BOOST_FORCEINLINE bool compare_exchange_strong(
339 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
341 storage_type previous = expected;
343 #if defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
346 "lock; cmpxchgl %3, %1"
347 : "+a" (previous), "+m" (storage), "=@ccz" (success)
349 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
351 #else // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
354 "lock; cmpxchgl %3, %1\n\t"
356 : "+a" (previous), "+m" (storage), "=q" (success)
358 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
360 #endif // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
365 #define BOOST_ATOMIC_DETAIL_CAS_LOOP(op, argument, result)\
366 storage_type new_val;\
367 __asm__ __volatile__\
370 "1: mov %[arg], %[new_val]\n\t"\
371 op " %%eax, %[new_val]\n\t"\
372 "lock; cmpxchgl %[new_val], %[storage]\n\t"\
374 : [res] "+a" (result), [storage] "+m" (storage), [new_val] "=&r" (new_val)\
375 : [arg] "ir" (argument)\
376 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"\
379 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
381 storage_type res = storage;
382 BOOST_ATOMIC_DETAIL_CAS_LOOP("andl", v, res);
386 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
388 storage_type res = storage;
389 BOOST_ATOMIC_DETAIL_CAS_LOOP("orl", v, res);
393 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
395 storage_type res = storage;
396 BOOST_ATOMIC_DETAIL_CAS_LOOP("xorl", v, res);
400 #undef BOOST_ATOMIC_DETAIL_CAS_LOOP
403 #if defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B)
405 // Note: In the 32-bit PIC code guarded with BOOST_ATOMIC_DETAIL_X86_ASM_PRESERVE_EBX below we have to avoid using memory
406 // operand constraints because the compiler may choose to use ebx as the base register for that operand. At least, clang
407 // is known to do that. For this reason we have to pre-compute a pointer to storage and pass it in edi. For the same reason
408 // we cannot save ebx to the stack with a mov instruction, so we use esi as a scratch register and restore it afterwards.
409 // Alternatively, we could push/pop the register to the stack, but exchanging the registers is faster.
410 // The need to pass a pointer in edi is a bit wasteful because normally the memory operand would use a base pointer
411 // with an offset (e.g. `this` + offset). But unfortunately, there seems to be no way around it.
413 template< bool Signed, bool Interprocess >
416 typedef typename storage_traits< 8u >::type storage_type;
417 typedef uint32_t BOOST_ATOMIC_DETAIL_MAY_ALIAS aliasing_uint32_t;
419 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 8u;
420 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = 8u;
421 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
422 static BOOST_CONSTEXPR_OR_CONST bool is_interprocess = Interprocess;
423 static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = true;
424 static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
426 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
428 if (BOOST_LIKELY((((uintptr_t)&storage) & 0x00000007) == 0u))
431 typedef float xmm_t __attribute__((__vector_size__(16)));
436 "vmovq %[value], %[xmm_scratch]\n\t"
437 "vmovq %[xmm_scratch], %[storage]\n\t"
438 #elif defined(__SSE2__)
439 "movq %[value], %[xmm_scratch]\n\t"
440 "movq %[xmm_scratch], %[storage]\n\t"
442 "xorps %[xmm_scratch], %[xmm_scratch]\n\t"
443 "movlps %[value], %[xmm_scratch]\n\t"
444 "movlps %[xmm_scratch], %[storage]\n\t"
446 : [storage] "=m" (storage), [xmm_scratch] "=x" (xmm_scratch)
453 "fildll %[value]\n\t"
454 "fistpll %[storage]\n\t"
455 : [storage] "=m" (storage)
463 #if defined(BOOST_ATOMIC_DETAIL_X86_ASM_PRESERVE_EBX)
466 "xchgl %%ebx, %%esi\n\t"
467 "movl %%eax, %%ebx\n\t"
468 "movl (%[dest]), %%eax\n\t"
469 "movl 4(%[dest]), %%edx\n\t"
471 "1: lock; cmpxchg8b (%[dest])\n\t"
473 "xchgl %%ebx, %%esi\n\t"
475 : "a" ((uint32_t)v), "c" ((uint32_t)(v >> 32)), [dest] "D" (&storage)
476 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "edx", "memory"
478 #else // defined(BOOST_ATOMIC_DETAIL_X86_ASM_PRESERVE_EBX)
481 "movl %[dest_lo], %%eax\n\t"
482 "movl %[dest_hi], %%edx\n\t"
484 "1: lock; cmpxchg8b %[dest_lo]\n\t"
486 : [dest_lo] "=m" (storage), [dest_hi] "=m" (reinterpret_cast< volatile aliasing_uint32_t* >(&storage)[1])
487 : [value_lo] "b" ((uint32_t)v), "c" ((uint32_t)(v >> 32))
488 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "eax", "edx", "memory"
490 #endif // defined(BOOST_ATOMIC_DETAIL_X86_ASM_PRESERVE_EBX)
494 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order) BOOST_NOEXCEPT
498 if (BOOST_LIKELY((((uintptr_t)&storage) & 0x00000007) == 0u))
501 typedef float xmm_t __attribute__((__vector_size__(16)));
506 "vmovq %[storage], %[xmm_scratch]\n\t"
507 "vmovq %[xmm_scratch], %[value]\n\t"
508 #elif defined(__SSE2__)
509 "movq %[storage], %[xmm_scratch]\n\t"
510 "movq %[xmm_scratch], %[value]\n\t"
512 "xorps %[xmm_scratch], %[xmm_scratch]\n\t"
513 "movlps %[storage], %[xmm_scratch]\n\t"
514 "movlps %[xmm_scratch], %[value]\n\t"
516 : [value] "=m" (value), [xmm_scratch] "=x" (xmm_scratch)
517 : [storage] "m" (storage)
523 "fildll %[storage]\n\t"
524 "fistpll %[value]\n\t"
525 : [value] "=m" (value)
526 : [storage] "m" (storage)
533 // Note that despite const qualification cmpxchg8b below may issue a store to the storage. The storage value
534 // will not change, but this prevents the storage to reside in read-only memory.
536 #if defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
538 uint32_t value_bits[2];
540 // We don't care for comparison result here; the previous value will be stored into value anyway.
541 // Also we don't care for ebx and ecx values, they just have to be equal to eax and edx before cmpxchg8b.
544 "movl %%ebx, %%eax\n\t"
545 "movl %%ecx, %%edx\n\t"
546 "lock; cmpxchg8b %[storage]\n\t"
547 : "=&a" (value_bits[0]), "=&d" (value_bits[1])
548 : [storage] "m" (storage)
549 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
551 BOOST_ATOMIC_DETAIL_MEMCPY(&value, value_bits, sizeof(value));
553 #else // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
555 // We don't care for comparison result here; the previous value will be stored into value anyway.
556 // Also we don't care for ebx and ecx values, they just have to be equal to eax and edx before cmpxchg8b.
559 "movl %%ebx, %%eax\n\t"
560 "movl %%ecx, %%edx\n\t"
561 "lock; cmpxchg8b %[storage]\n\t"
563 : [storage] "m" (storage)
564 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
567 #endif // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
573 static BOOST_FORCEINLINE bool compare_exchange_strong(
574 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
576 #if defined(__clang__)
578 // Clang cannot allocate eax:edx register pairs but it has sync intrinsics
579 storage_type old_expected = expected;
580 expected = __sync_val_compare_and_swap(&storage, old_expected, desired);
581 return expected == old_expected;
583 #elif defined(BOOST_ATOMIC_DETAIL_X86_ASM_PRESERVE_EBX)
587 #if defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
590 "xchgl %%ebx, %%esi\n\t"
591 "lock; cmpxchg8b (%[dest])\n\t"
592 "xchgl %%ebx, %%esi\n\t"
593 : "+A" (expected), [success] "=@ccz" (success)
594 : "S" ((uint32_t)desired), "c" ((uint32_t)(desired >> 32)), [dest] "D" (&storage)
595 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
597 #else // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
600 "xchgl %%ebx, %%esi\n\t"
601 "lock; cmpxchg8b (%[dest])\n\t"
602 "xchgl %%ebx, %%esi\n\t"
603 "sete %[success]\n\t"
604 : "+A" (expected), [success] "=qm" (success)
605 : "S" ((uint32_t)desired), "c" ((uint32_t)(desired >> 32)), [dest] "D" (&storage)
606 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
608 #endif // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
612 #else // defined(BOOST_ATOMIC_DETAIL_X86_ASM_PRESERVE_EBX)
616 #if defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
619 "lock; cmpxchg8b %[dest]\n\t"
620 : "+A" (expected), [dest] "+m" (storage), [success] "=@ccz" (success)
621 : "b" ((uint32_t)desired), "c" ((uint32_t)(desired >> 32))
622 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
624 #else // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
627 "lock; cmpxchg8b %[dest]\n\t"
628 "sete %[success]\n\t"
629 : "+A" (expected), [dest] "+m" (storage), [success] "=qm" (success)
630 : "b" ((uint32_t)desired), "c" ((uint32_t)(desired >> 32))
631 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
633 #endif // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
637 #endif // defined(BOOST_ATOMIC_DETAIL_X86_ASM_PRESERVE_EBX)
640 static BOOST_FORCEINLINE bool compare_exchange_weak(
641 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
643 return compare_exchange_strong(storage, expected, desired, success_order, failure_order);
646 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
648 #if defined(BOOST_ATOMIC_DETAIL_X86_ASM_PRESERVE_EBX)
649 #if defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
651 uint32_t old_bits[2];
654 "xchgl %%ebx, %%esi\n\t"
655 "movl (%[dest]), %%eax\n\t"
656 "movl 4(%[dest]), %%edx\n\t"
658 "1: lock; cmpxchg8b (%[dest])\n\t"
660 "xchgl %%ebx, %%esi\n\t"
661 : "=a" (old_bits[0]), "=d" (old_bits[1])
662 : "S" ((uint32_t)v), "c" ((uint32_t)(v >> 32)), [dest] "D" (&storage)
663 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
666 storage_type old_value;
667 BOOST_ATOMIC_DETAIL_MEMCPY(&old_value, old_bits, sizeof(old_value));
670 #else // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
672 storage_type old_value;
675 "xchgl %%ebx, %%esi\n\t"
676 "movl (%[dest]), %%eax\n\t"
677 "movl 4(%[dest]), %%edx\n\t"
679 "1: lock; cmpxchg8b (%[dest])\n\t"
681 "xchgl %%ebx, %%esi\n\t"
683 : "S" ((uint32_t)v), "c" ((uint32_t)(v >> 32)), [dest] "D" (&storage)
684 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
688 #endif // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
689 #else // defined(BOOST_ATOMIC_DETAIL_X86_ASM_PRESERVE_EBX)
690 #if defined(__MINGW32__) && ((__GNUC__+0) * 100 + (__GNUC_MINOR__+0)) < 407
692 // MinGW gcc up to 4.6 has problems with allocating registers in the asm blocks below
693 uint32_t old_bits[2];
696 "movl (%[dest]), %%eax\n\t"
697 "movl 4(%[dest]), %%edx\n\t"
699 "1: lock; cmpxchg8b (%[dest])\n\t"
701 : "=&a" (old_bits[0]), "=&d" (old_bits[1])
702 : "b" ((uint32_t)v), "c" ((uint32_t)(v >> 32)), [dest] "DS" (&storage)
703 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
706 storage_type old_value;
707 BOOST_ATOMIC_DETAIL_MEMCPY(&old_value, old_bits, sizeof(old_value));
710 #elif defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
712 uint32_t old_bits[2];
715 "movl %[dest_lo], %%eax\n\t"
716 "movl %[dest_hi], %%edx\n\t"
718 "1: lock; cmpxchg8b %[dest_lo]\n\t"
720 : "=&a" (old_bits[0]), "=&d" (old_bits[1]), [dest_lo] "+m" (storage), [dest_hi] "+m" (reinterpret_cast< volatile aliasing_uint32_t* >(&storage)[1])
721 : "b" ((uint32_t)v), "c" ((uint32_t)(v >> 32))
722 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
725 storage_type old_value;
726 BOOST_ATOMIC_DETAIL_MEMCPY(&old_value, old_bits, sizeof(old_value));
729 #else // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
731 storage_type old_value;
734 "movl %[dest_lo], %%eax\n\t"
735 "movl %[dest_hi], %%edx\n\t"
737 "1: lock; cmpxchg8b %[dest_lo]\n\t"
739 : "=&A" (old_value), [dest_lo] "+m" (storage), [dest_hi] "+m" (reinterpret_cast< volatile aliasing_uint32_t* >(&storage)[1])
740 : "b" ((uint32_t)v), "c" ((uint32_t)(v >> 32))
741 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
745 #endif // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
746 #endif // defined(BOOST_ATOMIC_DETAIL_X86_ASM_PRESERVE_EBX)
750 template< bool Signed, bool Interprocess >
751 struct core_arch_operations< 8u, Signed, Interprocess > :
752 public core_operations_cas_based< gcc_dcas_x86< Signed, Interprocess > >
756 #elif defined(__x86_64__)
758 template< bool Signed, bool Interprocess >
759 struct core_arch_operations< 8u, Signed, Interprocess > :
760 public core_arch_operations_gcc_x86< 8u, Signed, Interprocess, core_arch_operations< 8u, Signed, Interprocess > >
762 typedef core_arch_operations_gcc_x86< 8u, Signed, Interprocess, core_arch_operations< 8u, Signed, Interprocess > > base_type;
763 typedef typename base_type::storage_type storage_type;
765 static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
770 : "+r" (v), "+m" (storage)
772 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
777 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
782 : "+r" (v), "+m" (storage)
789 static BOOST_FORCEINLINE bool compare_exchange_strong(
790 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
792 storage_type previous = expected;
794 #if defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
797 "lock; cmpxchgq %3, %1"
798 : "+a" (previous), "+m" (storage), "=@ccz" (success)
800 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
802 #else // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
805 "lock; cmpxchgq %3, %1\n\t"
807 : "+a" (previous), "+m" (storage), "=q" (success)
809 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
811 #endif // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
816 #define BOOST_ATOMIC_DETAIL_CAS_LOOP(op, argument, result)\
817 storage_type new_val;\
818 __asm__ __volatile__\
821 "1: movq %[arg], %[new_val]\n\t"\
822 op " %%rax, %[new_val]\n\t"\
823 "lock; cmpxchgq %[new_val], %[storage]\n\t"\
825 : [res] "+a" (result), [storage] "+m" (storage), [new_val] "=&r" (new_val)\
826 : [arg] "r" (argument)\
827 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"\
830 static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
832 storage_type res = storage;
833 BOOST_ATOMIC_DETAIL_CAS_LOOP("andq", v, res);
837 static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
839 storage_type res = storage;
840 BOOST_ATOMIC_DETAIL_CAS_LOOP("orq", v, res);
844 static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
846 storage_type res = storage;
847 BOOST_ATOMIC_DETAIL_CAS_LOOP("xorq", v, res);
851 #undef BOOST_ATOMIC_DETAIL_CAS_LOOP
856 #if defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B)
858 template< bool Signed, bool Interprocess >
859 struct gcc_dcas_x86_64
861 typedef typename storage_traits< 16u >::type storage_type;
862 typedef uint64_t BOOST_ATOMIC_DETAIL_MAY_ALIAS aliasing_uint64_t;
864 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 16u;
865 static BOOST_CONSTEXPR_OR_CONST std::size_t storage_alignment = 16u;
866 static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
867 static BOOST_CONSTEXPR_OR_CONST bool is_interprocess = Interprocess;
868 static BOOST_CONSTEXPR_OR_CONST bool full_cas_based = true;
869 static BOOST_CONSTEXPR_OR_CONST bool is_always_lock_free = true;
871 static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
875 "movq %[dest_lo], %%rax\n\t"
876 "movq %[dest_hi], %%rdx\n\t"
878 "1: lock; cmpxchg16b %[dest_lo]\n\t"
880 : [dest_lo] "=m" (storage), [dest_hi] "=m" (reinterpret_cast< volatile aliasing_uint64_t* >(&storage)[1])
881 : "b" (reinterpret_cast< const aliasing_uint64_t* >(&v)[0]), "c" (reinterpret_cast< const aliasing_uint64_t* >(&v)[1])
882 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "rax", "rdx", "memory"
886 static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order) BOOST_NOEXCEPT
888 // Note that despite const qualification cmpxchg16b below may issue a store to the storage. The storage value
889 // will not change, but this prevents the storage to reside in read-only memory.
891 #if defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
893 uint64_t value_bits[2];
895 // We don't care for comparison result here; the previous value will be stored into value anyway.
896 // Also we don't care for rbx and rcx values, they just have to be equal to rax and rdx before cmpxchg16b.
899 "movq %%rbx, %%rax\n\t"
900 "movq %%rcx, %%rdx\n\t"
901 "lock; cmpxchg16b %[storage]\n\t"
902 : "=&a" (value_bits[0]), "=&d" (value_bits[1])
903 : [storage] "m" (storage)
904 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
908 BOOST_ATOMIC_DETAIL_MEMCPY(&value, value_bits, sizeof(value));
911 #else // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
915 // We don't care for comparison result here; the previous value will be stored into value anyway.
916 // Also we don't care for rbx and rcx values, they just have to be equal to rax and rdx before cmpxchg16b.
919 "movq %%rbx, %%rax\n\t"
920 "movq %%rcx, %%rdx\n\t"
921 "lock; cmpxchg16b %[storage]\n\t"
923 : [storage] "m" (storage)
924 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
929 #endif // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
932 static BOOST_FORCEINLINE bool compare_exchange_strong(
933 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
935 #if defined(__clang__)
937 // Clang cannot allocate rax:rdx register pairs but it has sync intrinsics
938 storage_type old_expected = expected;
939 expected = __sync_val_compare_and_swap(&storage, old_expected, desired);
940 return expected == old_expected;
942 #elif defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
944 // Some compilers can't allocate rax:rdx register pair either but also don't support 128-bit __sync_val_compare_and_swap
948 "lock; cmpxchg16b %[dest]\n\t"
949 "sete %[success]\n\t"
950 : [dest] "+m" (storage), "+a" (reinterpret_cast< aliasing_uint64_t* >(&expected)[0]), "+d" (reinterpret_cast< aliasing_uint64_t* >(&expected)[1]), [success] "=q" (success)
951 : "b" (reinterpret_cast< const aliasing_uint64_t* >(&desired)[0]), "c" (reinterpret_cast< const aliasing_uint64_t* >(&desired)[1])
952 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
957 #else // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
961 #if defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
964 "lock; cmpxchg16b %[dest]\n\t"
965 : "+A" (expected), [dest] "+m" (storage), "=@ccz" (success)
966 : "b" (reinterpret_cast< const aliasing_uint64_t* >(&desired)[0]), "c" (reinterpret_cast< const aliasing_uint64_t* >(&desired)[1])
967 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
969 #else // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
972 "lock; cmpxchg16b %[dest]\n\t"
973 "sete %[success]\n\t"
974 : "+A" (expected), [dest] "+m" (storage), [success] "=qm" (success)
975 : "b" (reinterpret_cast< const aliasing_uint64_t* >(&desired)[0]), "c" (reinterpret_cast< const aliasing_uint64_t* >(&desired)[1])
976 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
978 #endif // defined(BOOST_ATOMIC_DETAIL_ASM_HAS_FLAG_OUTPUTS)
982 #endif // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
985 static BOOST_FORCEINLINE bool compare_exchange_weak(
986 storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
988 return compare_exchange_strong(storage, expected, desired, success_order, failure_order);
991 static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order) BOOST_NOEXCEPT
993 #if defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
994 uint64_t old_bits[2];
997 "movq %[dest_lo], %%rax\n\t"
998 "movq %[dest_hi], %%rdx\n\t"
1000 "1: lock; cmpxchg16b %[dest_lo]\n\t"
1002 : [dest_lo] "+m" (storage), [dest_hi] "+m" (reinterpret_cast< volatile aliasing_uint64_t* >(&storage)[1]), "=&a" (old_bits[0]), "=&d" (old_bits[1])
1003 : "b" (reinterpret_cast< const aliasing_uint64_t* >(&v)[0]), "c" (reinterpret_cast< const aliasing_uint64_t* >(&v)[1])
1004 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
1007 storage_type old_value;
1008 BOOST_ATOMIC_DETAIL_MEMCPY(&old_value, old_bits, sizeof(old_value));
1010 #else // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
1011 storage_type old_value;
1012 __asm__ __volatile__
1014 "movq %[dest_lo], %%rax\n\t"
1015 "movq %[dest_hi], %%rdx\n\t"
1017 "1: lock; cmpxchg16b %[dest_lo]\n\t"
1019 : "=&A" (old_value), [dest_lo] "+m" (storage), [dest_hi] "+m" (reinterpret_cast< volatile aliasing_uint64_t* >(&storage)[1])
1020 : "b" (reinterpret_cast< const aliasing_uint64_t* >(&v)[0]), "c" (reinterpret_cast< const aliasing_uint64_t* >(&v)[1])
1021 : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC_COMMA "memory"
1025 #endif // defined(BOOST_ATOMIC_DETAIL_X86_NO_ASM_AX_DX_PAIRS)
1029 template< bool Signed, bool Interprocess >
1030 struct core_arch_operations< 16u, Signed, Interprocess > :
1031 public core_operations_cas_based< gcc_dcas_x86_64< Signed, Interprocess > >
1035 #endif // defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B)
1037 } // namespace detail
1038 } // namespace atomics
1039 } // namespace boost
1041 #include <boost/atomic/detail/footer.hpp>
1043 #endif // BOOST_ATOMIC_DETAIL_CORE_ARCH_OPS_GCC_X86_HPP_INCLUDED_