Chris@102: /* Chris@102: * Distributed under the Boost Software License, Version 1.0. Chris@102: * (See accompanying file LICENSE_1_0.txt or copy at Chris@102: * http://www.boost.org/LICENSE_1_0.txt) Chris@102: * Chris@102: * Copyright (c) 2009 Helge Bahmann Chris@102: * Copyright (c) 2013 Tim Blechmann Chris@102: * Copyright (c) 2014 Andrey Semashev Chris@102: */ Chris@102: /*! Chris@102: * \file atomic/detail/ops_gcc_alpha.hpp Chris@102: * Chris@102: * This header contains implementation of the \c operations template. Chris@102: */ Chris@102: Chris@102: #ifndef BOOST_ATOMIC_DETAIL_OPS_GCC_ALPHA_HPP_INCLUDED_ Chris@102: #define BOOST_ATOMIC_DETAIL_OPS_GCC_ALPHA_HPP_INCLUDED_ Chris@102: Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: #include Chris@102: Chris@102: #ifdef BOOST_HAS_PRAGMA_ONCE Chris@102: #pragma once Chris@102: #endif Chris@102: Chris@102: namespace boost { Chris@102: namespace atomics { Chris@102: namespace detail { Chris@102: Chris@102: /* Chris@102: Refer to http://h71000.www7.hp.com/doc/82final/5601/5601pro_004.html Chris@102: (HP OpenVMS systems documentation) and the Alpha Architecture Reference Manual. Chris@102: */ Chris@102: Chris@102: /* Chris@102: NB: The most natural thing would be to write the increment/decrement Chris@102: operators along the following lines: Chris@102: Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1: ldl_l %0,%1 \n" Chris@102: "addl %0,1,%0 \n" Chris@102: "stl_c %0,%1 \n" Chris@102: "beq %0,1b\n" Chris@102: : "=&b" (tmp) Chris@102: : "m" (value) Chris@102: : "cc" Chris@102: ); Chris@102: Chris@102: However according to the comments on the HP website and matching Chris@102: comments in the Linux kernel sources this defies branch prediction, Chris@102: as the cpu assumes that backward branches are always taken; so Chris@102: instead copy the trick from the Linux kernel, introduce a forward Chris@102: branch and back again. Chris@102: Chris@102: I have, however, had a hard time measuring the difference between Chris@102: the two versions in microbenchmarks -- I am leaving it in nevertheless Chris@102: as it apparently does not hurt either. Chris@102: */ Chris@102: Chris@102: struct gcc_alpha_operations_base Chris@102: { Chris@102: static BOOST_FORCEINLINE void fence_before(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if ((order & memory_order_release) != 0) Chris@102: __asm__ __volatile__ ("mb" ::: "memory"); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void fence_after(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if ((order & (memory_order_consume | memory_order_acquire)) != 0) Chris@102: __asm__ __volatile__ ("mb" ::: "memory"); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void fence_after_store(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if (order == memory_order_seq_cst) Chris@102: __asm__ __volatile__ ("mb" ::: "memory"); Chris@102: } Chris@102: }; Chris@102: Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 4u, Signed > : Chris@102: public gcc_alpha_operations_base Chris@102: { Chris@102: typedef typename make_storage_type< 4u, Signed >::type storage_type; Chris@102: Chris@102: static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: fence_before(order); Chris@102: storage = v; Chris@102: fence_after_store(order); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type v = storage; Chris@102: fence_after(order); Chris@102: return v; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, tmp; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "mov %3, %1\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (tmp) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool compare_exchange_weak( Chris@102: storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT Chris@102: { Chris@102: fence_before(success_order); Chris@102: int success; Chris@102: storage_type current; Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %2, %4\n" // current = *(&storage) Chris@102: "cmpeq %2, %0, %3\n" // success = current == expected Chris@102: "mov %2, %0\n" // expected = current Chris@102: "beq %3, 2f\n" // if (success == 0) goto end Chris@102: "stl_c %1, %4\n" // storage = desired; desired = store succeeded Chris@102: "mov %1, %3\n" // success = desired Chris@102: "2:\n" Chris@102: : "+&r" (expected), // %0 Chris@102: "+&r" (desired), // %1 Chris@102: "=&r" (current), // %2 Chris@102: "=&r" (success) // %3 Chris@102: : "m" (storage) // %4 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: if (success) Chris@102: fence_after(success_order); Chris@102: else Chris@102: fence_after(failure_order); Chris@102: return !!success; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool compare_exchange_strong( Chris@102: storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT Chris@102: { Chris@102: int success; Chris@102: storage_type current, tmp; Chris@102: fence_before(success_order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "mov %5, %1\n" // tmp = desired Chris@102: "ldl_l %2, %4\n" // current = *(&storage) Chris@102: "cmpeq %2, %0, %3\n" // success = current == expected Chris@102: "mov %2, %0\n" // expected = current Chris@102: "beq %3, 2f\n" // if (success == 0) goto end Chris@102: "stl_c %1, %4\n" // storage = tmp; tmp = store succeeded Chris@102: "beq %1, 3f\n" // if (tmp == 0) goto retry Chris@102: "mov %1, %3\n" // success = tmp Chris@102: "2:\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "3: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "+&r" (expected), // %0 Chris@102: "=&r" (tmp), // %1 Chris@102: "=&r" (current), // %2 Chris@102: "=&r" (success) // %3 Chris@102: : "m" (storage), // %4 Chris@102: "r" (desired) // %5 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: if (success) Chris@102: fence_after(success_order); Chris@102: else Chris@102: fence_after(failure_order); Chris@102: return !!success; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "addl %0, %3, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "subl %0, %3, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "and %0, %3, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "bis %0, %3, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "xor %0, %3, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return !!exchange(storage, (storage_type)1, order); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: store(storage, 0, order); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT Chris@102: { Chris@102: return true; Chris@102: } Chris@102: }; Chris@102: Chris@102: Chris@102: template< > Chris@102: struct operations< 1u, false > : Chris@102: public operations< 4u, false > Chris@102: { Chris@102: typedef operations< 4u, false > base_type; Chris@102: typedef base_type::storage_type storage_type; Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "addl %0, %3, %1\n" Chris@102: "zapnot %1, #1, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "subl %0, %3, %1\n" Chris@102: "zapnot %1, #1, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: }; Chris@102: Chris@102: template< > Chris@102: struct operations< 1u, true > : Chris@102: public operations< 4u, true > Chris@102: { Chris@102: typedef operations< 4u, true > base_type; Chris@102: typedef base_type::storage_type storage_type; Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "addl %0, %3, %1\n" Chris@102: "sextb %1, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "subl %0, %3, %1\n" Chris@102: "sextb %1, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: }; Chris@102: Chris@102: Chris@102: template< > Chris@102: struct operations< 2u, false > : Chris@102: public operations< 4u, false > Chris@102: { Chris@102: typedef operations< 4u, false > base_type; Chris@102: typedef base_type::storage_type storage_type; Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "addl %0, %3, %1\n" Chris@102: "zapnot %1, #3, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "subl %0, %3, %1\n" Chris@102: "zapnot %1, #3, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: }; Chris@102: Chris@102: template< > Chris@102: struct operations< 2u, true > : Chris@102: public operations< 4u, true > Chris@102: { Chris@102: typedef operations< 4u, true > base_type; Chris@102: typedef base_type::storage_type storage_type; Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "addl %0, %3, %1\n" Chris@102: "sextw %1, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldl_l %0, %2\n" Chris@102: "subl %0, %3, %1\n" Chris@102: "sextw %1, %1\n" Chris@102: "stl_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: }; Chris@102: Chris@102: Chris@102: template< bool Signed > Chris@102: struct operations< 8u, Signed > : Chris@102: public gcc_alpha_operations_base Chris@102: { Chris@102: typedef typename make_storage_type< 8u, Signed >::type storage_type; Chris@102: Chris@102: static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: fence_before(order); Chris@102: storage = v; Chris@102: fence_after_store(order); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type v = storage; Chris@102: fence_after(order); Chris@102: return v; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, tmp; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "mov %3, %1\n" Chris@102: "ldq_l %0, %2\n" Chris@102: "stq_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (tmp) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool compare_exchange_weak( Chris@102: storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT Chris@102: { Chris@102: fence_before(success_order); Chris@102: int success; Chris@102: storage_type current; Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldq_l %2, %4\n" // current = *(&storage) Chris@102: "cmpeq %2, %0, %3\n" // success = current == expected Chris@102: "mov %2, %0\n" // expected = current Chris@102: "beq %3, 2f\n" // if (success == 0) goto end Chris@102: "stq_c %1, %4\n" // storage = desired; desired = store succeeded Chris@102: "mov %1, %3\n" // success = desired Chris@102: "2:\n" Chris@102: : "+&r" (expected), // %0 Chris@102: "+&r" (desired), // %1 Chris@102: "=&r" (current), // %2 Chris@102: "=&r" (success) // %3 Chris@102: : "m" (storage) // %4 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: if (success) Chris@102: fence_after(success_order); Chris@102: else Chris@102: fence_after(failure_order); Chris@102: return !!success; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool compare_exchange_strong( Chris@102: storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT Chris@102: { Chris@102: int success; Chris@102: storage_type current, tmp; Chris@102: fence_before(success_order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "mov %5, %1\n" // tmp = desired Chris@102: "ldq_l %2, %4\n" // current = *(&storage) Chris@102: "cmpeq %2, %0, %3\n" // success = current == expected Chris@102: "mov %2, %0\n" // expected = current Chris@102: "beq %3, 2f\n" // if (success == 0) goto end Chris@102: "stq_c %1, %4\n" // storage = tmp; tmp = store succeeded Chris@102: "beq %1, 3f\n" // if (tmp == 0) goto retry Chris@102: "mov %1, %3\n" // success = tmp Chris@102: "2:\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "3: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "+&r" (expected), // %0 Chris@102: "=&r" (tmp), // %1 Chris@102: "=&r" (current), // %2 Chris@102: "=&r" (success) // %3 Chris@102: : "m" (storage), // %4 Chris@102: "r" (desired) // %5 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: if (success) Chris@102: fence_after(success_order); Chris@102: else Chris@102: fence_after(failure_order); Chris@102: return !!success; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldq_l %0, %2\n" Chris@102: "addq %0, %3, %1\n" Chris@102: "stq_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldq_l %0, %2\n" Chris@102: "subq %0, %3, %1\n" Chris@102: "stq_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldq_l %0, %2\n" Chris@102: "and %0, %3, %1\n" Chris@102: "stq_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldq_l %0, %2\n" Chris@102: "bis %0, %3, %1\n" Chris@102: "stq_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: storage_type original, modified; Chris@102: fence_before(order); Chris@102: __asm__ __volatile__ Chris@102: ( Chris@102: "1:\n" Chris@102: "ldq_l %0, %2\n" Chris@102: "xor %0, %3, %1\n" Chris@102: "stq_c %1, %2\n" Chris@102: "beq %1, 2f\n" Chris@102: Chris@102: ".subsection 2\n" Chris@102: "2: br 1b\n" Chris@102: ".previous\n" Chris@102: Chris@102: : "=&r" (original), // %0 Chris@102: "=&r" (modified) // %1 Chris@102: : "m" (storage), // %2 Chris@102: "r" (v) // %3 Chris@102: : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC Chris@102: ); Chris@102: fence_after(order); Chris@102: return original; Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: return !!exchange(storage, (storage_type)1, order); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: store(storage, 0, order); Chris@102: } Chris@102: Chris@102: static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT Chris@102: { Chris@102: return true; Chris@102: } Chris@102: }; Chris@102: Chris@102: Chris@102: BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if (order != memory_order_relaxed) Chris@102: __asm__ __volatile__ ("mb" ::: "memory"); Chris@102: } Chris@102: Chris@102: BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT Chris@102: { Chris@102: if (order != memory_order_relaxed) Chris@102: __asm__ __volatile__ ("" ::: "memory"); Chris@102: } Chris@102: Chris@102: } // namespace detail Chris@102: } // namespace atomics Chris@102: } // namespace boost Chris@102: Chris@102: #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_ALPHA_HPP_INCLUDED_