public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tom de Vries <vries@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-7799] [libatomic] Fix return value in libat_test_and_set
Date: Thu, 24 Mar 2022 12:31:06 +0000 (GMT)	[thread overview]
Message-ID: <20220324123106.494273858C2C@sourceware.org> (raw)

https://gcc.gnu.org/g:11fb784ac592567dbcb7874c27e67ee0feb8fbf0

commit r12-7799-g11fb784ac592567dbcb7874c27e67ee0feb8fbf0
Author: Tom de Vries <tdevries@suse.de>
Date:   Wed Mar 23 16:37:45 2022 +0100

    [libatomic] Fix return value in libat_test_and_set
    
    On nvptx (using a Quadro K2000 with driver 470.103.01) I ran into this:
    ...
    FAIL: gcc.dg/atomic/stdatomic-flag-2.c -O1 execution test
    ...
    which mimimized to:
    ...
      #include <stdatomic.h>
      atomic_flag a = ATOMIC_FLAG_INIT;
      int main () {
        if ((atomic_flag_test_and_set) (&a))
          __builtin_abort ();
        return 0;
      }
    ...
    
    The atomic_flag_test_and_set is implemented using __atomic_test_and_set_1,
    which corresponds to the "word-sized compare-and-swap loop" version of
    libat_test_and_set in libatomic/tas_n.c.
    
    The semantics of a test-and-set is that the return value is "true if and only
    if the previous contents were 'set'".
    
    But the code uses:
    ...
      return woldval != 0;
    ...
    which means it doesn't look only at the byte that was either set or not set,
    but at the entire word.
    
    Fix this by using instead:
    ...
      return (woldval & ((UTYPE) ~(UTYPE) 0 << shift)) != 0;
    ...
    
    Tested on nvptx.
    
    libatomic/ChangeLog:
    
    2022-03-24  Tom de Vries  <tdevries@suse.de>
    
            PR target/105011
            * tas_n.c (libat_test_and_set): Fix return value.

Diff:
---
 libatomic/tas_n.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libatomic/tas_n.c b/libatomic/tas_n.c
index d0d8c283b49..524312e7d8d 100644
--- a/libatomic/tas_n.c
+++ b/libatomic/tas_n.c
@@ -73,7 +73,7 @@ SIZE(libat_test_and_set) (UTYPE *mptr, int smodel)
 				     __ATOMIC_RELAXED, __ATOMIC_RELAXED));
 
   post_barrier (smodel);
-  return woldval != 0;
+  return (woldval & ((UTYPE) ~(UTYPE) 0 << shift)) != 0;
 }
 
 #define DONE 1


                 reply	other threads:[~2022-03-24 12:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220324123106.494273858C2C@sourceware.org \
    --to=vries@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).