如何对64位寄存器的低32位进行BSWAP? [英] How to BSWAP the lower 32-bit of 64-bit register?
问题描述
我一直在寻找有关如何将BSWAP用于64位寄存器的低32位子寄存器的答案.例如,0x0123456789abcdef
在RAX寄存器中,我想用一条指令将其更改为0x01234567efcdab89
(由于性能).
I've been looking for the answer for how to use BSWAP for lower 32-bit sub-register of 64-bit register. For example, 0x0123456789abcdef
is inside RAX register, and I want to change it to 0x01234567efcdab89
with a single instruction (because of performance).
所以我尝试了以下内联函数:
So I tried following inline function:
#define BSWAP(T) { \
__asm__ __volatile__ ( \
"bswap %k0" \
: "=q" (T) \
: "q" (T)); \
}
结果为0x00000000efcdab89
.我不明白为什么编译器会这样.有人知道有效的解决方案吗?
And the result was 0x00000000efcdab89
. I don't understand why the compiler acts like this. Does anybody know the efficient solution?
推荐答案
啊,是的,我现在明白了这个问题:
Ah, yes, I understand the problem now:
x86-64处理器 隐式零扩展 在执行32位操作(在%eax,%ebx等上)时将32位寄存器更改为64位.据我了解,这是为了与希望这些寄存器具有32位语义的旧代码保持兼容性.
the x86-64 processors implicitly zero-extend the 32-bit registers to 64-bit when doing 32-bit operations (on %eax, %ebx, etc). This is to maintain compatibility with legacy code that expects 32-bit semantics for these registers, as I understand it.
因此,我担心没有办法仅对64位寄存器的低32位执行ror
.您将必须使用一系列的多个说明...
So I'm afraid that there is no way to do ror
on just the lower 32 bits of a 64-bit register. You'll have to do use a series of several instructions...
这篇关于如何对64位寄存器的低32位进行BSWAP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!