GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
c-family

c-family: Use CAS loop for small _BitInt atomics with padding.

Uses CAS loop instead of RMW atomics for small _BitInt types with padding on certain architectures.

For small _BitInt types with padding, some atomic/sync builtins that operate on TYPE * arguments do not correctly sign or zero-extend the values. This commit changes the implementation to use a Compare-And-Swap (CAS) loop instead of Read-Modify-Write (RMW) atomics for these specific cases. This ensures correct behavior when the target architecture requires extension into the padding bits, addressing PR124948.

In Details

The sync_resolve_size function in gcc/c-family/c-common.cc now returns -1 for fetch operations on _BitInt types that have padding bits and where the target architecture mandates extension into those bits. This signals the need to use a CAS loop, implemented in atomic_bitint_fetch_using_cas_loop, which is also updated to handle __sync_* fetch builtins. This avoids incorrect behavior from RMW atomics that do not perform the necessary sign/zero extension.

For Context
_BitInt
A non-standard integer type in C and C++ that allows specifying arbitrary bit widths, potentially larger than standard integer types.
CAS loop
Compare-And-Swap loop. An atomic operation that reads a memory location, compares its value with an expected value, and if they match, writes a new value. A loop is often used to retry the operation if another thread modifies the memory location between the read and the write.
RMW atomic
Read-Modify-Write atomic operations. These are single, indivisible instructions that perform a read, modification, and write operation on memory atomically, such as fetch_add or fetch_sub.
Padding bits
Extra bits in a data type that are not part of the intended value but are present due to alignment or internal representation. These bits can sometimes be accidentally included or excluded by operations.
Filed Under: cc++atomicbitintbugfix