Fix atomic bit-integer fetch-and-nand semantics
Corrects the implementation of atomic fetch-and-nand operations for _BitInt types to match GCC 4.4+ semantics.
This commit addresses an issue in GCC’s handling of atomic fetch-and-nand operations for _BitInt types. The previous implementation incorrectly applied bitwise logic, deviating from the documented behavior introduced in GCC 4.4. The fix ensures that the operations now correctly compute ~(*ptr & value) instead of (~*ptr) & value, aligning with the standard.
In Details
The atomic_bitint_fetch_using_cas_loop function in c-common.cc was incorrectly implementing the bitwise NAND for __atomic_fetch_nand, __atomic_nand_fetch, __sync_fetch_and_nand, and __sync_nand_and_fetch operations on _BitInt types. It used (~*ptr) & value which differs from the GCC 4.4+ specification of ~(*ptr & value). This patch corrects the logic to match the specified semantics for these atomic bitwise operations.
- _BitInt
- A C/C++ integer type of arbitrary size, introduced in C++23 and GCC extensions, allowing for integers larger than standard types like
long long. - atomic operations
- Operations that are indivisible and guaranteed to complete without interruption from other threads, crucial for concurrent programming to prevent race conditions.
- fetch-and-NAND
- An atomic bitwise operation that reads the current value of a memory location, performs a bitwise NAND with a given value, stores the result back, and returns the original value.
- CAS loop
- Compare-and-Swap loop. A common pattern for implementing atomic operations by repeatedly attempting a read-modify-write operation until it succeeds without interference.