According to Dan, __sync_fetch_and_nand intrinsic should be implemented as ~(target & val). Uros's patch is correct.

At a minimum, it is incomplete. If one were to change the semantics of
__sync_fetch_and_nand, they probably should do it universally rather
than just for a subset of GCC's targets.

The MIPS port would have to be changed, and IIRC sparc as well.

mips, alpha, ia64 and rs6000 to be precise.
And certainly big fat warning in the documentation, both in the builtins
section and in gcc-4.4 release notes.

Attached (now for real :) patch corrects __sync nand intrinsics to
generate ~(target & val). The patch was tested on i686-pc-linux-gnu. I
propose to commit the patch, since all unfixed targets will fail fixed
sync tests and after all targets are fixed, we can backport complete
patch to release branches and add the note to release notes.

What is the motivation for getting this into 4.4 two days before the end
of stage3? It makes it more difficult for the maintainers of the ports
you don't fix to have clean test results for 4.4.

As asked by APH in a different reply, Where are the nand intrinsics
actually being used?