如何BSWAP 64位寄存器的低32位? [英] 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...
这篇关于如何BSWAP 64位寄存器的低32位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!