merlin2011
merlin2011

Reputation: 75585

Where is `__atomic_compare_exchange` defined in the g++ source?

In the g++ source code under the file /usr/include/c++/4.9/atomic in my system, the function atomic::compare_exchange_strong has the following body.

  bool
  compare_exchange_strong(_Tp& __e, _Tp __i, memory_order __s, 
              memory_order __f) noexcept
  {
return __atomic_compare_exchange(&_M_i, &__e, &__i, false, __s, __f); 
  }

However, this file includes only bits/atomic_base.h and I did not find a definition for __atomic_compare_exchange in this file.

Where is __atomic_compare_exchange defined?

Upvotes: 2

Views: 2166

Answers (2)

Cameron
Cameron

Reputation: 98816

__atomic_compare_exchange is a compiler builtin. It's not actually defined in the headers anywhere -- the compiler itself knows what it is.

In the GCC source itself, it's declared in sync-builtins.def. I'm not familiar with GCC's source, so I'm not sure exactly how that percolates down to architecture-specific implementations (though it seems likely to pass through maybe_emit_atomic_exchange), but the source of e.g. x86 instructions representing it can be found in another platform-specific generator file named sync.md:

(define_insn "atomic_exchange<mode>"
  [(set (match_operand:SWI 0 "register_operand" "=<r>")     ;; output
    (unspec_volatile:SWI
      [(match_operand:SWI 1 "memory_operand" "+m")      ;; memory
       (match_operand:SI 3 "const_int_operand")]        ;; model
      UNSPECV_XCHG))
   (set (match_dup 1)
    (match_operand:SWI 2 "register_operand" "0"))]      ;; input
  ""
  "%K3xchg{<imodesuffix>}\t{%1, %0|%0, %1}")

Upvotes: 3

Qaz
Qaz

Reputation: 61970

It's probably a compiler intrinsic - something the compiler itself takes and works with as opposed to being a pure library solution. Some compiler help is necessary in order to implement the full standard library, such as some type traits, and in other cases, the compiler can do a much better job, like in std::make_index_sequence, which has more recently been getting this treatment in order to perform well for non-small values.

Upvotes: 0

Related Questions