From ff33e3e2ce1af2ceee94958c7273e3b1d083d5e5 Mon Sep 17 00:00:00 2001 From: John Paul Adrian Glaubitz Date: Fri, 31 Jul 2026 14:26:49 +0200 Subject: [PATCH] threading: Use modern GCC atomic builtins for __GNUC__ The __sync builtins have been deprecated and replaced since GCC 4.7 with the more flexible __atomic builtins. While the __sync builtins continue to work, they lack the 64-bit version __sync_fetch_and_add_8() which causes x265 failing to build on certain 32-bit Linux targets such as m68k, powerpc or sh4: /usr/bin/powerpc-linux-gnu-ld.bfd: libx265.so.216: undefined reference to `__sync_fetch_and_add_8' collect2: error: ld returned 1 exit status Switching over to the new __atomic builtins in GCC fixes this problem. --- source/common/threading.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/source/common/threading.h b/source/common/threading.h index 08a8affef..dd1f0bb9f 100644 --- a/source/common/threading.h +++ b/source/common/threading.h @@ -85,15 +85,15 @@ int64_t no_atomic_add64(int64_t* ptr, int64_t val); #define BSF(id, x) (id) = ((unsigned long)__builtin_ctz(x)) #define BSR64(id, x) (id) = ((unsigned long)__builtin_clzll(x) ^ 63) #define BSF64(id, x) (id) = ((unsigned long)__builtin_ctzll(x)) -#define ATOMIC_OR(ptr, mask) __sync_fetch_and_or(ptr, mask) -#define ATOMIC_AND(ptr, mask) __sync_fetch_and_and(ptr, mask) -#define ATOMIC_INC(ptr) __sync_add_and_fetch(reinterpret_cast(ptr), 1) -#define ATOMIC_DEC(ptr) __sync_add_and_fetch(reinterpret_cast(ptr), -1) -#define ATOMIC_ADD(ptr, val) __sync_fetch_and_add(reinterpret_cast(ptr), (__typeof__(*(ptr) + 0))(val)) -#define ATOMIC_STORE(ptr, val) __sync_lock_test_and_set(reinterpret_cast(ptr), (int32_t)(val)) -#define ATOMIC64_LOAD(ptr) __sync_fetch_and_add(reinterpret_cast(ptr), 0) -#define ATOMIC64_STORE(ptr, val) __sync_lock_test_and_set(reinterpret_cast(ptr), val) -#define ATOMIC64_ADD(ptr, val) __sync_fetch_and_add(reinterpret_cast(ptr), val) +#define ATOMIC_OR(ptr, mask) __atomic_fetch_or(ptr, mask, __ATOMIC_SEQ_CST) +#define ATOMIC_AND(ptr, mask) __atomic_fetch_and(ptr, mask, __ATOMIC_SEQ_CST) +#define ATOMIC_INC(ptr) __atomic_add_fetch(reinterpret_cast(ptr), 1, __ATOMIC_SEQ_CST) +#define ATOMIC_DEC(ptr) __atomic_sub_fetch(reinterpret_cast(ptr), 1, __ATOMIC_SEQ_CST) +#define ATOMIC_ADD(ptr, val) __atomic_fetch_add(reinterpret_cast(ptr), (__typeof__(*(ptr) + 0))(val), __ATOMIC_SEQ_CST) +#define ATOMIC_STORE(ptr, val) __atomic_exchange_n(reinterpret_cast(ptr), (int32_t)(val), __ATOMIC_SEQ_CST) +#define ATOMIC64_LOAD(ptr) __atomic_fetch_add(reinterpret_cast(ptr), 0, __ATOMIC_SEQ_CST) +#define ATOMIC64_STORE(ptr, val) __atomic_exchange_n(reinterpret_cast(ptr), val, __ATOMIC_SEQ_CST) +#define ATOMIC64_ADD(ptr, val) __atomic_fetch_add(reinterpret_cast(ptr), val, __ATOMIC_SEQ_CST) #define GIVE_UP_TIME() usleep(0) #elif defined(_MSC_VER) /* Windows atomic intrinsics */