IIRC,第一代 Xeon Phi 基于 P5 内核(Pentium 和 Pentium MMX)。 cmov 直到 P6(又名 Pentium Pro)才推出。所以我觉得这很正常。
只需让编译器通过编写一个普通的三元运算符来完成它的工作。
其次,cmov 是比setc 更糟糕的选择,因为您想根据进位标志生成 0 或 1。请参阅下面的 asm 代码。
另请注意,带有内存操作数的bts 非常慢,因此无论如何您都不希望它生成该代码,尤其是。在将 x86 指令解码为 uops 的 CPU 上(如现代 Xeon)。根据http://agner.org/optimize/ 的说法,即使在 P5 上,bts m, r 也比 bts m, i 慢得多,所以不要那样做。
只需要求编译器将in 放在寄存器中,或者更好的是,不要为此使用内联汇编。
由于 OP 显然希望它以原子方式工作,因此最好的解决方案是使用 C++11 的 std::atomic::fetch_or,并将其留给编译器生成 lock bts。
std::atomic_flag 有一个 test_and_set 函数,但如果有办法将它们紧紧地打包,IDK。也许作为结构中的位域?不过不太可能。我也没有看到 std::bitset 的原子操作。
不幸的是,当前版本的 gcc 和 clang 不会从 fetch_or 生成 lock bts,即使可以使用更快的立即操作数形式也是如此。我想出了以下(godbolt link):
#include <atomic>
#include <stdio.h>
// wastes instructions when the return value isn't used.
// gcc 6.0 has syntax for using flags as output operands
// IDK if lock BTS is better than lock cmpxchg.
// However, gcc doesn't use lock BTS even with -Os
int atomic_bts_asm(std::atomic<unsigned> *x, int bit) {
int retval = 0; // the compiler still provides a zeroed reg as input even if retval isn't used after the asm :/
// Letting the compiler do the xor means we can use a m constraint, in case this is inlined where we're storing to already zeroed memory
// It unfortunately doesn't help for overwriting a value that's already known to be 0 or 1.
asm( // "xor %[rv], %[rv]\n\t"
"lock bts %[bit], %[x]\n\t"
"setc %b[rv]\n\t" // hope that the compiler zeroed with xor to avoid a partial-register stall
: [x] "+m" (*x), [rv] "+rm"(retval)
: [bit] "ri" (bit));
return retval;
}
// save an insn when retval isn't used, but still doesn't avoid the setc
// leads to the less-efficient setc/ movzbl sequence when the result is needed :/
int atomic_bts_asm2(std::atomic<unsigned> *x, int bit) {
uint8_t retval;
asm( "lock bts %[bit], %[x]\n\t"
"setc %b[rv]\n\t"
: [x] "+m" (*x), [rv] "=rm"(retval)
: [bit] "ri" (bit));
return retval;
}
int atomic_bts(std::atomic<unsigned> *x, unsigned int bit) {
// bit &= 31; // stops gcc from using shlx?
unsigned bitmask = 1<<bit;
//int oldval = x->fetch_or(bitmask, std::memory_order_relaxed);
int oldval = x->fetch_or(bitmask, std::memory_order_acq_rel);
// acquire and release semantics are free on x86
// Also, any atomic rmw needs a lock prefix, which is a full memory barrier (seq_cst) anyway.
if (oldval & bitmask)
return 1;
else
return 0;
}
正如What is the best way to set a register to zero in x86 assembly: xor, mov or and? 中所讨论的,xor / set-flags / setc 是所有现代 CPU 的最佳序列,当需要将结果作为 0 或 1 值时。我实际上并没有考虑过 P5,但setcc 在 P5 上很快,所以应该没问题。
当然,如果你想在 this 上分支而不是存储它,inline asm 和 C 之间的边界是一个障碍。花费两条指令来存储 0 或 1,只在其上进行测试/分支,这将是非常愚蠢的。
gcc6 的标志操作数语法当然值得研究,如果它是一个选项的话。 (如果您需要针对英特尔 MIC 的编译器,则可能不需要。)