Hi!

When implementing this function in GCC 14 for _BitInt it seems
that for NAND I've implemented the GCC < 4.4 semantics.
To quote the docs:
@{ tmp = *ptr; *ptr = ~(tmp & value); return tmp; @}   // nand
and
@emph{Note:} GCC 4.4 and later implement @code{__sync_fetch_and_nand}
as @code{*ptr = ~(tmp & value)} instead of @code{*ptr = ~tmp & value}.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk (and later backports)?

2026-11-19  Jakub Jelinek  <[email protected]>

        PR c/128184
        * c-common.cc (atomic_bitint_fetch_using_cas_loop): For
        __atomic_fetch_nand, __atomic_nand_fetch, __sync_fetch_and_nand
        and __sync_nand_and_fetch use ~(*ptr & value) rather than
        (~*ptr) & value.

        * gcc.dg/bitint-148.c: New test.

--- a/gcc/c-family/c-common.cc  2026-11-13 14:51:36.391182583 +0100
+++ b/gcc/c-family/c-common.cc  2026-11-17 10:14:31.317998188 +0100
@@ -8648,9 +8648,9 @@ atomic_bitint_fetch_using_cas_loop (loca
       rhs = build2_loc (loc, code, nonatomic_lhs_type, old, val);
       break;
     case ERROR_MARK:
-      rhs = build2_loc (loc, BIT_AND_EXPR, nonatomic_lhs_type,
-                       build1_loc (loc, BIT_NOT_EXPR,
-                                   nonatomic_lhs_type, old), val);
+      rhs = build1_loc (loc, BIT_NOT_EXPR, nonatomic_lhs_type,
+                       build2_loc (loc, BIT_AND_EXPR, nonatomic_lhs_type,
+                                   old, val));
       break;
     default:
       gcc_unreachable ();
--- a/gcc/testsuite/gcc.dg/bitint-148.c 2026-11-17 10:17:02.729978145 +0100
+++ b/gcc/testsuite/gcc.dg/bitint-148.c 2026-11-17 10:18:35.039746614 +0100
@@ -0,0 +1,17 @@
+/* PR c/128184 */
+/* { dg-do run { target { bitint && libatomic_available } } } */
+/* { dg-options "-O2 -latomic" } */
+
+#if __BITINT_MAXWIDTH__ >= 192
+_BitInt(192) a;
+#endif
+
+int
+main ()
+{
+#if __BITINT_MAXWIDTH__ >= 192
+  __atomic_fetch_nand (&a, 5, __ATOMIC_RELAXED);
+  if (a != -1)
+    __builtin_abort ();
+#endif
+}

        Jakub

Reply via email to