This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/64843] miscompilation of atomic_fetch_add on atomic pointer type
- From: "joseph at codesourcery dot com" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Wed, 28 Jan 2015 23:20:31 +0000
- Subject: [Bug c/64843] miscompilation of atomic_fetch_add on atomic pointer type
- Auto-submitted: auto-generated
- References: <bug-64843-4 at http dot gcc dot gnu dot org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64843
--- Comment #3 from joseph at codesourcery dot com <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); \
})