__sync_fetch_and_andatomic 在使用 Clang 的单线程程序中给出错误的结果
我遇到 __sync_fetch_and_and
执行不正确的问题。我编写了以下代码来说明它:
bool equal;
int64_t mask = 0x01234567BEEFDEAD;
int64_t orig = 0xDEADBEEF01234567;
int64_t test1, test2;
test1 = test2 = orig;
equal = (test1 == test2);
printf("Before anding\n");
printf("test1:\t0x%016llX\n", test1);
printf("test2:\t0x%016llX\n", test2);
printf("equal:\t%d\n", equal);
// Try anding
test1 &= mask;
__sync_fetch_and_and(&test2, mask);
equal = (test1 == test2);
printf("After anding\n");
printf("test1:\t0x%016llX\n", test1);
printf("test2:\t0x%016llX\n", test2);
printf("equal:\t%d\n", equal);
输出是:
Before anding
test1: 0xDEADBEEF01234567
test2: 0xDEADBEEF01234567
equal: 1
After anding
test1: 0x0021046700234425
test2: 0xDFAFFFEFBFEFDFEF
equal: 0
...这显然是不正确的。我尝试替换 __sync_and_and_fetch
但这并不能解决问题。 Or-ing 与 ``__sync_fetch_and_or` 工作正常。我使用 Xcode 4.2.1 并使用默认编译器 Apple LLVM Compiler 3.0 (Clang) 进行编译。当我切换到使用 GCC 4.2 时,它可以正常工作。
这看起来确实像是一个编译器错误,但我不确定我是否在 Clang 上做错了。 Clang 中是否存在一些我没有考虑到的差异,或者这确实是一个错误?
编辑:我还没有尝试最新版本的 Clang (3.0),因为我现在一直在使用 Xcode。
I'm having a problem with __sync_fetch_and_and
incorrectly performing. I wrote the following code to illustrate it:
bool equal;
int64_t mask = 0x01234567BEEFDEAD;
int64_t orig = 0xDEADBEEF01234567;
int64_t test1, test2;
test1 = test2 = orig;
equal = (test1 == test2);
printf("Before anding\n");
printf("test1:\t0x%016llX\n", test1);
printf("test2:\t0x%016llX\n", test2);
printf("equal:\t%d\n", equal);
// Try anding
test1 &= mask;
__sync_fetch_and_and(&test2, mask);
equal = (test1 == test2);
printf("After anding\n");
printf("test1:\t0x%016llX\n", test1);
printf("test2:\t0x%016llX\n", test2);
printf("equal:\t%d\n", equal);
The output from this is:
Before anding
test1: 0xDEADBEEF01234567
test2: 0xDEADBEEF01234567
equal: 1
After anding
test1: 0x0021046700234425
test2: 0xDFAFFFEFBFEFDFEF
equal: 0
...which is obviously not correct. I've tried replacing __sync_and_and_fetch
but that doesn't fix it. Or-ing with ``__sync_fetch_and_or` works correctly. I'm using Xcode 4.2.1 and compiling with the default compiler Apple LLVM Compiler 3.0 (Clang). When I switch to using GCC 4.2, it works correctly.
This certainly seems like a compiler bug, but I'm not sure if I'm somehow doing this wrong on Clang. Are there some differences in Clang that I'm not accounting for, or is this indeed a bug?
EDIT: I haven't tried the latest release of Clang (3.0) because I'm stuck using Xcode for now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是一个错误;它已在较新版本的 clang 中得到修复。作为解决方法,您可以将“-no-integrated-as”添加到编译器标志中。
Yes, it's a bug; it's been fixed in newer versions of clang. As a workaround, you can add "-no-integrated-as" to your compiler flags.