<p dir="ltr">This is a weird behavior of gcc's built-in, which clang faithfully emulates.</p>
<div class="gmail_quote">On 20 May 2013 17:07, "Howard Hinnant" <<a href="mailto:hhinnant@apple.com">hhinnant@apple.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Clang or libc++ bug for __atomic_fetch_add using pointer types?<br>
<br>
#include <atomic><br>
#include <iostream><br>
<br>
template <class T><br>
T<br>
simulated_atomic_fetch_add(T* ptr, ptrdiff_t val, int)<br>
{<br>
    T tmp = *ptr;<br>
    *ptr += val;<br>
    return tmp;<br>
}<br>
<br>
int<br>
main()<br>
{<br>
    int* p = nullptr;<br>
    __atomic_fetch_add(&p, 1, __ATOMIC_RELAXED);<br>
    std::cout << (void*)p << '\n';<br>
    p = nullptr;<br>
    simulated_atomic_fetch_add(&p, 1, __ATOMIC_RELAXED);<br>
    std::cout << (void*)p << '\n';<br>
}<br>
<br>
0x1<br>
0x4<br>
<br>
I.e. __atomic_fetch_add (and __atomic_fetch_sub) assumes the pointer is a pointer to char.<br>
<br>
I can fix this in libc++, or should it be fixed in clang?  The "official" spec at:<br>
<br>
<a href="http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html" target="_blank">http://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html</a><br>
<br>
is sloppy, and doesn't clarify.  I'm happy to do whatever gcc is doing, and I have no idea what that is.<br>
<br>
Howard<br>
<br>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>