3

Is cmpl instruction equivalent to movl + a compare. If so, what's the difference b/w: (1)

LBB1_2:    
     cmpl $0, _data_ready(%rip)
     je LBB1_2

and: (2)

LBB1_2:
    movl    _data_ready(%rip), %eax
    testl %eax, %eax
    je LBB1_2

(1) is generated for while (!data_ready); where data_ready is volatile int data_ready = 0x0;

(2) is generated for while (!data_ready.load(std::memory_order_acquire)); where data_ready is std::atomic<int> data_ready(0x0);

In both cases data_ready is set to 1 by another thread. Intel guarantees movl to be atomic for aligned memory access and it seems like cmpl should be atomic too. If that's the case why is clang generating different codes? (I am sure there is valid reasons that's why I am asking)

Also, does this mean that a volatile variable is "equivalent" to an std::atomic on x86-64 platforms (which means nothing of course and is not guaranteed by the C++ standard).

The code that generates this is available in this github repo

4

1 回答 1

0

test指令按位AND执行其指令,并根据其结果设置标志AND(但and本身的结果被丢弃)。

与仅设置标志的cmp操作类似,但它执行减法而不是按位AND

volatile该操作实际上与或atomic尽管没有太多关系(如果有的话) 。就其中任何一个影响代码生成而言,影响将取决于所使用的寻址模式——即,第一个将立即值直接与内存中的值进行比较,但第二个执行加载,然后操作该值在注册表中。

于 2015-02-16T00:11:43.317 回答