__sync_fetch_and_andatomic 在使用 Clang 的单线程程序中给出错误的结果

发布于 2024-12-21 15:20:01 字数 1138 浏览 3 评论 0原文

我遇到 __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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

彡翼 2024-12-28 15:20:01

是的,这是一个错误;它已在较新版本的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文