From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5283 invoked by alias); 28 Jan 2015 23:20:42 -0000 Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org Received: (qmail 3462 invoked by uid 55); 28 Jan 2015 23:20:34 -0000 From: "joseph at codesourcery dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/64843] miscompilation of atomic_fetch_add on atomic pointer type Date: Wed, 28 Jan 2015 23:20:00 -0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 4.9.0 X-Bugzilla-Keywords: documentation, wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: joseph at codesourcery dot com X-Bugzilla-Status: NEW X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-SW-Source: 2015-01/txt/msg03330.txt.bz2 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64843 --- Comment #3 from joseph at codesourcery dot com --- The first question is whether this code is actually valid. C11 says "All of these operations are applicable to an object of any atomic integer type.", not mentioning pointer types as valid, but then refers to address types. Then, if it's valid to use pointer types here, something like the following (untested, and all four of the _add and _sub macros would need similar changes) should work without changing the built-in function semantics: /* EXPR1 if it has a pointer type, otherwise EXPR2. */ #define __atomic_ptr_choose(EXPR1, EXPR2) \ __builtin_choose_expr (__builtin_classify_type (EXPR1) == 5, \ (EXPR1), (EXPR2)) /* The size of *EXPR if EXPR has a pointer type, 1 otherwise. */ #define __atomic_ptr_size(EXPR) \ ((__PTRDIFF_TYPE__) \ sizeof (*(__typeof (__atomic_ptr_choose (EXPR, (char *) 0))) 0)) #define atomic_fetch_add(PTR, VAL) \ __extension__ \ ({ \ __auto_type __atomic_fetch_add_ptr = (PTR); \ __atomic_fetch_add (__atomic_fetch_add_ptr, \ (VAL) * __atomic_ptr_size (*__atomic_fetch_add_ptr), \ __ATOMIC_SEQ_CST); \ })