Base gcc 4.2.1 makes incorrect code when swapping array elements by xor without optimization. Simple .c code attached as xor.txt. Checked on FreeBSD 9.0 on i386/amd64/sparc64 platforms. Fix: Patch attached with submission follows: How-To-Repeat: # gcc -o xor xor.txt # gcc -o xoro1 -O1 xor.txt # ./xor a0 = 127 b0 = 33 a1 = 127 b1 = 33 a2 = 127 b2[0] = 33 a3 = 127 b3[0] = 0 # ./xoro1 a0 = 127 b0 = 33 a1 = 127 b1 = 33 a2 = 127 b2[0] = 33 a3 = 127 b3[0] = 33 But in theory output must be identical.
The bug is still present in FreeBSD 9.3's gcc. The bug is not present with the bundled clang though. Unfortunately we don't plan to do major updates to the gcc version available in the FreeBSD 9.x series: 10.x and newer ship with clang by default. Please use clang or the gcc version from ports which should be a lot more up to date.