| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233 |
- /*
- * Distributed under the Boost Software License, Version 1.0.
- * (See accompanying file LICENSE_1_0.txt or copy at
- * http://www.boost.org/LICENSE_1_0.txt)
- *
- * Copyright (c) 2009 Helge Bahmann
- * Copyright (c) 2013 Tim Blechmann
- * Copyright (c) 2014 Andrey Semashev
- */
- /*!
- * \file atomic/detail/ops_gcc_ppc.hpp
- *
- * This header contains implementation of the \c operations template.
- */
- #ifndef BOOST_ATOMIC_DETAIL_OPS_GCC_PPC_HPP_INCLUDED_
- #define BOOST_ATOMIC_DETAIL_OPS_GCC_PPC_HPP_INCLUDED_
- #include <cstddef>
- #include <boost/memory_order.hpp>
- #include <boost/atomic/detail/config.hpp>
- #include <boost/atomic/detail/storage_type.hpp>
- #include <boost/atomic/detail/operations_fwd.hpp>
- #include <boost/atomic/detail/ops_gcc_ppc_common.hpp>
- #include <boost/atomic/capabilities.hpp>
- #ifdef BOOST_HAS_PRAGMA_ONCE
- #pragma once
- #endif
- namespace boost {
- namespace atomics {
- namespace detail {
- // The implementation below uses information from this document:
- // http://www.rdrop.com/users/paulmck/scalability/paper/N2745r.2010.02.19a.html
- /*
- Refer to: Motorola: "Programming Environments Manual for 32-Bit
- Implementations of the PowerPC Architecture", Appendix E:
- "Synchronization Programming Examples" for an explanation of what is
- going on here (can be found on the web at various places by the
- name "MPCFPE32B.pdf", Google is your friend...)
- Most of the atomic operations map to instructions in a relatively
- straight-forward fashion, but "load"s may at first glance appear
- a bit strange as they map to:
- lwz %rX, addr
- cmpw %rX, %rX
- bne- 1f
- 1:
- That is, the CPU is forced to perform a branch that "formally" depends
- on the value retrieved from memory. This scheme has an overhead of
- about 1-2 clock cycles per load, but it allows to map "acquire" to
- the "isync" instruction instead of "sync" uniformly and for all type
- of atomic operations. Since "isync" has a cost of about 15 clock
- cycles, while "sync" hast a cost of about 50 clock cycles, the small
- penalty to atomic loads more than compensates for this.
- Byte- and halfword-sized atomic values are implemented in two ways.
- When 8 and 16-bit instructions are available (in Power8 and later),
- they are used. Otherwise operations are realized by encoding the
- value to be represented into a word, performing sign/zero extension
- as appropriate. This means that after add/sub operations the value
- needs fixing up to accurately preserve the wrap-around semantic of
- the smaller type. (Nothing special needs to be done for the bit-wise
- and the "exchange type" operators as the compiler already sees to
- it that values carried in registers are extended appropriately and
- everything falls into place naturally).
- The register constraint "b" instructs gcc to use any register
- except r0; this is sometimes required because the encoding for
- r0 is used to signify "constant zero" in a number of instructions,
- making r0 unusable in this place. For simplicity this constraint
- is used everywhere since I am to lazy to look this up on a
- per-instruction basis, and ppc has enough registers for this not
- to pose a problem.
- */
- template< bool Signed >
- struct operations< 4u, Signed > :
- public gcc_ppc_operations_base
- {
- typedef typename make_storage_type< 4u >::type storage_type;
- typedef typename make_storage_type< 4u >::aligned aligned_storage_type;
- static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 4u;
- static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
- static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- fence_before(order);
- __asm__ __volatile__
- (
- "stw %1, %0\n\t"
- : "+m" (storage)
- : "r" (v)
- );
- }
- static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- storage_type v;
- if (order == memory_order_seq_cst)
- __asm__ __volatile__ ("sync" ::: "memory");
- if ((static_cast< unsigned int >(order) & (static_cast< unsigned int >(memory_order_consume) | static_cast< unsigned int >(memory_order_acquire))) != 0u)
- {
- __asm__ __volatile__
- (
- "lwz %0, %1\n\t"
- "cmpw %0, %0\n\t"
- "bne- 1f\n\t"
- "1:\n\t"
- "isync\n\t"
- : "=&r" (v)
- : "m" (storage)
- : "cr0", "memory"
- );
- }
- else
- {
- __asm__ __volatile__
- (
- "lwz %0, %1\n\t"
- : "=&r" (v)
- : "m" (storage)
- );
- }
- return v;
- }
- static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y1\n\t"
- "stwcx. %2,%y1\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "+Z" (storage)
- : "b" (v)
- : "cr0"
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE bool compare_exchange_weak(
- storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
- {
- int success;
- fence_before(success_order);
- __asm__ __volatile__
- (
- "li %1, 0\n\t"
- "lwarx %0,%y2\n\t"
- "cmpw %0, %3\n\t"
- "bne- 1f\n\t"
- "stwcx. %4,%y2\n\t"
- "bne- 1f\n\t"
- "li %1, 1\n\t"
- "1:\n\t"
- : "=&b" (expected), "=&b" (success), "+Z" (storage)
- : "b" (expected), "b" (desired)
- : "cr0"
- );
- if (success)
- fence_after(success_order);
- else
- fence_after(failure_order);
- return !!success;
- }
- static BOOST_FORCEINLINE bool compare_exchange_strong(
- storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
- {
- int success;
- fence_before(success_order);
- __asm__ __volatile__
- (
- "li %1, 0\n\t"
- "0: lwarx %0,%y2\n\t"
- "cmpw %0, %3\n\t"
- "bne- 1f\n\t"
- "stwcx. %4,%y2\n\t"
- "bne- 0b\n\t"
- "li %1, 1\n\t"
- "1:\n\t"
- : "=&b" (expected), "=&b" (success), "+Z" (storage)
- : "b" (expected), "b" (desired)
- : "cr0"
- );
- if (success)
- fence_after(success_order);
- else
- fence_after(failure_order);
- return !!success;
- }
- static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "add %1,%0,%3\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "sub %1,%0,%3\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "and %1,%0,%3\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "or %1,%0,%3\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "xor %1,%0,%3\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- return !!exchange(storage, (storage_type)1, order);
- }
- static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- store(storage, 0, order);
- }
- };
- #if defined(BOOST_ATOMIC_DETAIL_PPC_HAS_LBARX_STBCX)
- template< bool Signed >
- struct operations< 1u, Signed > :
- public gcc_ppc_operations_base
- {
- typedef typename make_storage_type< 1u >::type storage_type;
- typedef typename make_storage_type< 1u >::aligned aligned_storage_type;
- static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 1u;
- static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
- static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- fence_before(order);
- __asm__ __volatile__
- (
- "stb %1, %0\n\t"
- : "+m" (storage)
- : "r" (v)
- );
- }
- static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- storage_type v;
- if (order == memory_order_seq_cst)
- __asm__ __volatile__ ("sync" ::: "memory");
- if ((static_cast< unsigned int >(order) & (static_cast< unsigned int >(memory_order_consume) | static_cast< unsigned int >(memory_order_acquire))) != 0u)
- {
- __asm__ __volatile__
- (
- "lbz %0, %1\n\t"
- "cmpw %0, %0\n\t"
- "bne- 1f\n\t"
- "1:\n\t"
- "isync\n\t"
- : "=&r" (v)
- : "m" (storage)
- : "cr0", "memory"
- );
- }
- else
- {
- __asm__ __volatile__
- (
- "lbz %0, %1\n\t"
- : "=&r" (v)
- : "m" (storage)
- );
- }
- return v;
- }
- static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lbarx %0,%y1\n\t"
- "stbcx. %2,%y1\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "+Z" (storage)
- : "b" (v)
- : "cr0"
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE bool compare_exchange_weak(
- storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
- {
- int success;
- fence_before(success_order);
- __asm__ __volatile__
- (
- "li %1, 0\n\t"
- "lbarx %0,%y2\n\t"
- "cmpw %0, %3\n\t"
- "bne- 1f\n\t"
- "stbcx. %4,%y2\n\t"
- "bne- 1f\n\t"
- "li %1, 1\n\t"
- "1:\n\t"
- : "=&b" (expected), "=&b" (success), "+Z" (storage)
- : "b" (expected), "b" (desired)
- : "cr0"
- );
- if (success)
- fence_after(success_order);
- else
- fence_after(failure_order);
- return !!success;
- }
- static BOOST_FORCEINLINE bool compare_exchange_strong(
- storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
- {
- int success;
- fence_before(success_order);
- __asm__ __volatile__
- (
- "li %1, 0\n\t"
- "0: lbarx %0,%y2\n\t"
- "cmpw %0, %3\n\t"
- "bne- 1f\n\t"
- "stbcx. %4,%y2\n\t"
- "bne- 0b\n\t"
- "li %1, 1\n\t"
- "1:\n\t"
- : "=&b" (expected), "=&b" (success), "+Z" (storage)
- : "b" (expected), "b" (desired)
- : "cr0"
- );
- if (success)
- fence_after(success_order);
- else
- fence_after(failure_order);
- return !!success;
- }
- static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lbarx %0,%y2\n\t"
- "add %1,%0,%3\n\t"
- "stbcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lbarx %0,%y2\n\t"
- "sub %1,%0,%3\n\t"
- "stbcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lbarx %0,%y2\n\t"
- "and %1,%0,%3\n\t"
- "stbcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lbarx %0,%y2\n\t"
- "or %1,%0,%3\n\t"
- "stbcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lbarx %0,%y2\n\t"
- "xor %1,%0,%3\n\t"
- "stbcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- return !!exchange(storage, (storage_type)1, order);
- }
- static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- store(storage, 0, order);
- }
- };
- #else // defined(BOOST_ATOMIC_DETAIL_PPC_HAS_LBARX_STBCX)
- template< >
- struct operations< 1u, false > :
- public operations< 4u, false >
- {
- typedef operations< 4u, false > base_type;
- typedef base_type::storage_type storage_type;
- static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "add %1,%0,%3\n\t"
- "rlwinm %1, %1, 0, 0xff\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "sub %1,%0,%3\n\t"
- "rlwinm %1, %1, 0, 0xff\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- };
- template< >
- struct operations< 1u, true > :
- public operations< 4u, true >
- {
- typedef operations< 4u, true > base_type;
- typedef base_type::storage_type storage_type;
- static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "add %1,%0,%3\n\t"
- "extsb %1, %1\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "sub %1,%0,%3\n\t"
- "extsb %1, %1\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- };
- #endif // defined(BOOST_ATOMIC_DETAIL_PPC_HAS_LBARX_STBCX)
- #if defined(BOOST_ATOMIC_DETAIL_PPC_HAS_LHARX_STHCX)
- template< bool Signed >
- struct operations< 2u, Signed > :
- public gcc_ppc_operations_base
- {
- typedef typename make_storage_type< 2u >::type storage_type;
- typedef typename make_storage_type< 2u >::aligned aligned_storage_type;
- static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 2u;
- static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
- static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- fence_before(order);
- __asm__ __volatile__
- (
- "sth %1, %0\n\t"
- : "+m" (storage)
- : "r" (v)
- );
- }
- static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- storage_type v;
- if (order == memory_order_seq_cst)
- __asm__ __volatile__ ("sync" ::: "memory");
- if ((static_cast< unsigned int >(order) & (static_cast< unsigned int >(memory_order_consume) | static_cast< unsigned int >(memory_order_acquire))) != 0u)
- {
- __asm__ __volatile__
- (
- "lhz %0, %1\n\t"
- "cmpw %0, %0\n\t"
- "bne- 1f\n\t"
- "1:\n\t"
- "isync\n\t"
- : "=&r" (v)
- : "m" (storage)
- : "cr0", "memory"
- );
- }
- else
- {
- __asm__ __volatile__
- (
- "lhz %0, %1\n\t"
- : "=&r" (v)
- : "m" (storage)
- );
- }
- return v;
- }
- static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lharx %0,%y1\n\t"
- "sthcx. %2,%y1\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "+Z" (storage)
- : "b" (v)
- : "cr0"
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE bool compare_exchange_weak(
- storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
- {
- int success;
- fence_before(success_order);
- __asm__ __volatile__
- (
- "li %1, 0\n\t"
- "lharx %0,%y2\n\t"
- "cmpw %0, %3\n\t"
- "bne- 1f\n\t"
- "sthcx. %4,%y2\n\t"
- "bne- 1f\n\t"
- "li %1, 1\n\t"
- "1:\n\t"
- : "=&b" (expected), "=&b" (success), "+Z" (storage)
- : "b" (expected), "b" (desired)
- : "cr0"
- );
- if (success)
- fence_after(success_order);
- else
- fence_after(failure_order);
- return !!success;
- }
- static BOOST_FORCEINLINE bool compare_exchange_strong(
- storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
- {
- int success;
- fence_before(success_order);
- __asm__ __volatile__
- (
- "li %1, 0\n\t"
- "0: lharx %0,%y2\n\t"
- "cmpw %0, %3\n\t"
- "bne- 1f\n\t"
- "sthcx. %4,%y2\n\t"
- "bne- 0b\n\t"
- "li %1, 1\n\t"
- "1:\n\t"
- : "=&b" (expected), "=&b" (success), "+Z" (storage)
- : "b" (expected), "b" (desired)
- : "cr0"
- );
- if (success)
- fence_after(success_order);
- else
- fence_after(failure_order);
- return !!success;
- }
- static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lharx %0,%y2\n\t"
- "add %1,%0,%3\n\t"
- "sthcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lharx %0,%y2\n\t"
- "sub %1,%0,%3\n\t"
- "sthcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lharx %0,%y2\n\t"
- "and %1,%0,%3\n\t"
- "sthcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lharx %0,%y2\n\t"
- "or %1,%0,%3\n\t"
- "sthcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lharx %0,%y2\n\t"
- "xor %1,%0,%3\n\t"
- "sthcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- return !!exchange(storage, (storage_type)1, order);
- }
- static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- store(storage, 0, order);
- }
- };
- #else // defined(BOOST_ATOMIC_DETAIL_PPC_HAS_LHARX_STHCX)
- template< >
- struct operations< 2u, false > :
- public operations< 4u, false >
- {
- typedef operations< 4u, false > base_type;
- typedef base_type::storage_type storage_type;
- static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "add %1,%0,%3\n\t"
- "rlwinm %1, %1, 0, 0xffff\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "sub %1,%0,%3\n\t"
- "rlwinm %1, %1, 0, 0xffff\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- };
- template< >
- struct operations< 2u, true > :
- public operations< 4u, true >
- {
- typedef operations< 4u, true > base_type;
- typedef base_type::storage_type storage_type;
- static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "add %1,%0,%3\n\t"
- "extsh %1, %1\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "lwarx %0,%y2\n\t"
- "sub %1,%0,%3\n\t"
- "extsh %1, %1\n\t"
- "stwcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- };
- #endif // defined(BOOST_ATOMIC_DETAIL_PPC_HAS_LHARX_STHCX)
- #if defined(BOOST_ATOMIC_DETAIL_PPC_HAS_LDARX_STDCX)
- template< bool Signed >
- struct operations< 8u, Signed > :
- public gcc_ppc_operations_base
- {
- typedef typename make_storage_type< 8u >::type storage_type;
- typedef typename make_storage_type< 8u >::aligned aligned_storage_type;
- static BOOST_CONSTEXPR_OR_CONST std::size_t storage_size = 8u;
- static BOOST_CONSTEXPR_OR_CONST bool is_signed = Signed;
- static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- fence_before(order);
- __asm__ __volatile__
- (
- "std %1, %0\n\t"
- : "+m" (storage)
- : "r" (v)
- );
- }
- static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- storage_type v;
- if (order == memory_order_seq_cst)
- __asm__ __volatile__ ("sync" ::: "memory");
- if ((static_cast< unsigned int >(order) & (static_cast< unsigned int >(memory_order_consume) | static_cast< unsigned int >(memory_order_acquire))) != 0u)
- {
- __asm__ __volatile__
- (
- "ld %0, %1\n\t"
- "cmpd %0, %0\n\t"
- "bne- 1f\n\t"
- "1:\n\t"
- "isync\n\t"
- : "=&b" (v)
- : "m" (storage)
- : "cr0", "memory"
- );
- }
- else
- {
- __asm__ __volatile__
- (
- "ld %0, %1\n\t"
- : "=&b" (v)
- : "m" (storage)
- );
- }
- return v;
- }
- static BOOST_FORCEINLINE storage_type exchange(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "ldarx %0,%y1\n\t"
- "stdcx. %2,%y1\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "+Z" (storage)
- : "b" (v)
- : "cr0"
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE bool compare_exchange_weak(
- storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
- {
- int success;
- fence_before(success_order);
- __asm__ __volatile__
- (
- "li %1, 0\n\t"
- "ldarx %0,%y2\n\t"
- "cmpd %0, %3\n\t"
- "bne- 1f\n\t"
- "stdcx. %4,%y2\n\t"
- "bne- 1f\n\t"
- "li %1, 1\n\t"
- "1:"
- : "=&b" (expected), "=&b" (success), "+Z" (storage)
- : "b" (expected), "b" (desired)
- : "cr0"
- );
- if (success)
- fence_after(success_order);
- else
- fence_after(failure_order);
- return !!success;
- }
- static BOOST_FORCEINLINE bool compare_exchange_strong(
- storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
- {
- int success;
- fence_before(success_order);
- __asm__ __volatile__
- (
- "li %1, 0\n\t"
- "0: ldarx %0,%y2\n\t"
- "cmpd %0, %3\n\t"
- "bne- 1f\n\t"
- "stdcx. %4,%y2\n\t"
- "bne- 0b\n\t"
- "li %1, 1\n\t"
- "1:\n\t"
- : "=&b" (expected), "=&b" (success), "+Z" (storage)
- : "b" (expected), "b" (desired)
- : "cr0"
- );
- if (success)
- fence_after(success_order);
- else
- fence_after(failure_order);
- return !!success;
- }
- static BOOST_FORCEINLINE storage_type fetch_add(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "ldarx %0,%y2\n\t"
- "add %1,%0,%3\n\t"
- "stdcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_sub(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "ldarx %0,%y2\n\t"
- "sub %1,%0,%3\n\t"
- "stdcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_and(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "ldarx %0,%y2\n\t"
- "and %1,%0,%3\n\t"
- "stdcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_or(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "ldarx %0,%y2\n\t"
- "or %1,%0,%3\n\t"
- "stdcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE storage_type fetch_xor(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
- {
- storage_type original, result;
- fence_before(order);
- __asm__ __volatile__
- (
- "1:\n\t"
- "ldarx %0,%y2\n\t"
- "xor %1,%0,%3\n\t"
- "stdcx. %1,%y2\n\t"
- "bne- 1b\n\t"
- : "=&b" (original), "=&b" (result), "+Z" (storage)
- : "b" (v)
- : BOOST_ATOMIC_DETAIL_ASM_CLOBBER_CC
- );
- fence_after(order);
- return original;
- }
- static BOOST_FORCEINLINE bool test_and_set(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- return !!exchange(storage, (storage_type)1, order);
- }
- static BOOST_FORCEINLINE void clear(storage_type volatile& storage, memory_order order) BOOST_NOEXCEPT
- {
- store(storage, 0, order);
- }
- };
- #endif // defined(BOOST_ATOMIC_DETAIL_PPC_HAS_LDARX_STDCX)
- BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
- {
- if (order != memory_order_relaxed)
- {
- #if defined(__powerpc64__) || defined(__PPC64__)
- if (order != memory_order_seq_cst)
- __asm__ __volatile__ ("lwsync" ::: "memory");
- else
- __asm__ __volatile__ ("sync" ::: "memory");
- #else
- __asm__ __volatile__ ("sync" ::: "memory");
- #endif
- }
- }
- BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
- {
- if (order != memory_order_relaxed)
- #if defined(__ibmxl__) || defined(__IBMCPP__)
- __fence();
- #else
- __asm__ __volatile__ ("" ::: "memory");
- #endif
- }
- } // namespace detail
- } // namespace atomics
- } // namespace boost
- #endif // BOOST_ATOMIC_DETAIL_OPS_GCC_PPC_HPP_INCLUDED_
|