我使用以下函数来交换(无)符号 64 位整数值:
function Swap64(I: Int64): Int64;
begin
Int64Rec(Result).Bytes[0] := Int64Rec(I).Bytes[7];
Int64Rec(Result).Bytes[1] := Int64Rec(I).Bytes[6];
Int64Rec(Result).Bytes[2] := Int64Rec(I).Bytes[5];
Int64Rec(Result).Bytes[3] := Int64Rec(I).Bytes[4];
Int64Rec(Result).Bytes[4] := Int64Rec(I).Bytes[3];
Int64Rec(Result).Bytes[5] := Int64Rec(I).Bytes[2];
Int64Rec(Result).Bytes[6] := Int64Rec(I).Bytes[1];
Int64Rec(Result).Bytes[7] := Int64Rec(I).Bytes[0];
end;
我如何在 ASM 中做同样的事情以使其更快?
您可以使用bswap
交换字节的指令。对于 32 代码,您需要一次交换 32 位字节,有两种用途bswap
。对于 64 位代码,您可以直接操作 64 位寄存器,并使用一次交换所有 8 个字节bswap
.
以下是适用于 32 位和 64 位目标的函数:
function ByteSwap64(Value: Int64): Int64;
asm
{$IF Defined(CPUX86)}
mov edx, [ebp+$08]
mov eax, [ebp+$0c]
bswap edx
bswap eax
{$ELSEIF Defined(CPUX64)}
mov rax, rcx
bswap rax
{$ELSE}
{$Message Fatal 'ByteSwap64 has not been implemented for this architecture.'}
{$ENDIF}
end;
我不能说这个功能是否会带来任何明显的性能优势。在优化代码之前,您应该通过分析和计时代码来识别瓶颈。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)