6

I'm using GCC 4.7.2 on Debian, and getting linker errors whenever I try to use the <atomic> facilities with 16-byte values. I'm running an x86_64 VM which can support the CMPXCHG16B instruction - but even if I didn't have the necessary hardware, I don't see why a linker error should be produced here. As far as I know, the <atomic> library is supposed to fall back on using regular locks if the hardware doesn't support the necessary CAS operation.

Anyway, here's a very simple test case to reproduce this problem:

#include <atomic>
#include <cstdint>

struct foo
{
    std::uint64_t x;
    std::uint64_t y;
};

int main()
{
    std::atomic<foo> f1({0,0});
    foo f2 = {0,0};
    foo f3 = {1,1};
    f1.compare_exchange_strong(f2, f3);
}

When I compile this, I get:

# g++ test.cpp -o test -std=c++11 -g3
/tmp/ccziKZis.o: In function `std::atomic<foo>::compare_exchange_strong(foo&, foo, std::memory_order, std::memory_order)':
/usr/include/c++/4.7/atomic:259: undefined reference to `__atomic_compare_exchange_16'
collect2: error: ld returned 1 exit status

Note that if I change the program so that foo is only 8 bytes, I don't get the linker error. What's going on here?

1
  • Interesting, I can repro this with g++ 4.8.2 and clang++ 3.6. Not sure what the underlying problem is - but seems like a bug in the standard library, or some such. Nov 1, 2014 at 12:15

1 Answer 1

7

Simple answer, once you know it:

Invoke g++ with -mcx16.

The g++ docs say:

This option will enable GCC to use CMPXCHG16B instruction in generated code. CMPXCHG16B allows for atomic operations on 128-bit double quadword (or oword) data types. This is useful for high resolution counters that could be updated by multiple processors (or cores). This instruction is generated as part of atomic built-in functions: see *note Atomic Builtins:: for details.

(Note that this doesn't work for clang - I think that is a bug!)

4
  • Huh... strange. I would think GCC is supposed to just automatically support cmpxchg16b if available, or else fall back on using a lock
    – Siler
    Nov 1, 2014 at 12:22
  • Yes, one would expect so. I think it goes back to some early versions [of Intel's since I seem to remember that AMD had it from the very beginning - but can't say for sure] of x86-64 didn't have this, or some such. Nov 1, 2014 at 12:33
  • 1
    I would think GCC is supposed to just automatically support cmpxchg16b if available, or else fall back on using a lock Right, but how does the machine you compile on know if cmpxchg16b is available on the machine you run on? One way is to tell it with a flag. Another is to have the binary call to a CPU-specific dynamic library, which will do the cmpxchg16b if supported otherwise do the same thing with locks. Apparently all Pentiums and greater, except a couple 2002 AMD's have had this instruction so it's a shame the compiler doesn't simply generate the instruction. May 8, 2020 at 9:39
  • 2
    As of GCC : "The compiler uses this instruction to implement __sync Builtins. However, for __atomic Builtins operating on 128-bit integers, a library call is always used." May 8, 2020 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.