简体   繁体   English

x86-64 movl和cmpl的区别

[英]x86-64 movl and cmpl difference

Is cmpl instruction equivalent to movl + a compare. cmpl指令等效于movl +一个比较。 If so, what's the difference b/w: (1) 如果是这样,黑白有什么区别:(1)

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

and: (2) 和:(2)

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

(1) is generated for while (!data_ready); (1)生成了while (!data_ready); where data_ready is volatile int data_ready = 0x0; 其中data_ready是volatile int data_ready = 0x0;

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

In both cases data_ready is set to 1 by another thread. 在这两种情况下,另一个线程都会将data_ready设置为1。 Intel guarantees movl to be atomic for aligned memory access and it seems like cmpl should be atomic too. 英特尔保证movl对于对齐的内存访问是原子的,似乎cmpl应该是原子的。 If that's the case why is clang generating different codes? 如果是这样,为什么clang会生成不同的代码? (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). 另外,这是否意味着volatile变量与x86-64平台上的std :: atomic“等效”(这当然没有任何意义,C ++标准也不保证)。

The code that generates this is available in this github repo 生成此代码的代码在此github存储库中可用

The test instruction does a bitwise AND of its instructions, and sets the flags based on the result of that AND (but the result of the and itself is discarded). test指令执行按位AND它的指令,并基于因此而引起的旗帜AND (不过的结果and自身被丢弃)。

The cmp is similar in doing an operation only to set the flags, but it does subtraction instead of bitwise AND . cmp在执行操作时仅设置标志类似,但是它会进行减法运算,而不是按位AND运算。

The operation doesn't really have much (if anything) to do with volatile or atomic though. 该操作实际上与volatileatomic无关(如果有的话)。 To the extent that either of those affects code generation, the effect would be on the addressing mode used--ie, where the first one compares an immediate value directly to a value in memory, but the second does a load, then manipulates the value in the register. 在某种程度上,这会影响代码的生成,其影响将取决于所使用的寻址模式,即,第一个模式将立即数直接与内存中的值进行比较,而第二个模式则进行加载,然后操纵该值在寄存器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM