根据
http://www.freepascal.org/docs-html/rtl/system/roldword.html
RolDWord向左旋转4字节int(Free Pascal)。
Delphi中有RolDWord吗?
最佳答案
您可以使用以下功能:
function rolw(Value: Word; N: Integer): Word;
asm
{$IF Defined(CPUX86)}
MOV CL, DL
ROL AX, CL
{$ELSEIF Defined(CPUX64)}
MOV AX, CX
MOV CL, DL
ROL AX, CL
{$ELSE}
{$Message Fatal 'rolw has not been implemented for this architecture.'}
{$ENDIF}
end;
function rorw(Value: Word; N: Integer): Word;
asm
{$IF Defined(CPUX86)}
MOV CL, DL
ROR AX, CL
{$ELSEIF Defined(CPUX64)}
MOV AX, CX
MOV CL, DL
ROR AX, CL
{$ELSE}
{$Message Fatal 'rorw has not been implemented for this architecture.'}
{$ENDIF}
end;
function roldw(Value: Cardinal; N: Integer): Cardinal;
asm
{$IF Defined(CPUX86)}
MOV CL, DL
ROL EAX, CL
{$ELSEIF Defined(CPUX64)}
MOV EAX, ECX
MOV CL, DL
ROL EAX, CL
{$ELSE}
{$Message Fatal 'roldw has not been implemented for this architecture.'}
{$ENDIF}
end;
function rordw(Value: Cardinal; N: Integer): Cardinal;
asm
{$IF Defined(CPUX86)}
MOV CL, DL
ROR EAX, CL
{$ELSEIF Defined(CPUX64)}
MOV EAX, ECX
MOV CL, DL
ROR EAX, CL
{$ELSE}
{$Message Fatal 'rordw has not been implemented for this architecture.'}
{$ENDIF}
end;
但是,正如Johan和LU RD指出的那样,内联Pascal版本是可移植的,并且对于此功能而言通常更快,这是因为内联对于如此小的功能而言非常重要。
看起来像这样:
{$IFOPT Q+}{$DEFINE OVERFLOWCHECKSON}{$ENDIF}
{$Q-}
function rolw_pas(Value: Word; N: Integer): Word; inline;
begin
Result:= ((Value shl N) and $ffff) or (Value shr (16-N));
end;
function rorw_pas(Value: Word; N: Integer): Word; inline;
begin
Result:= (Value shr N) or ((Value shl (16-N)) and $ffff);
end;
function roldw_pas(Value: Cardinal; N: Integer): Cardinal; inline;
begin
Result:= (Value shl N) or (Value shr (32-N));
end;
function rordw_pas(Value: Cardinal; N: Integer): Cardinal; inline;
begin
Result:= (Value shr N) or (Value shl (32-N));
end;
{$IFDEF OVERFLOWCHECKSON}{$Q+}{$ENDIF}
请注意,我们确实需要确保禁用了溢出检查。
关于delphi - RolDWord在Delphi中实现(32位和64位)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32531879/