• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

[Delphi]字节序交换函数

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

最佳性能版:

function Swap16(const Value: Word): Word; inline;
begin
  Result := Swap(Value);
end;

function Swap32(const Value: LongWord): LongWord; inline;
begin
  Result := Swap(Word(Value)) shl 16 + Swap(Word(Value shr 16));
end;

function Swap64(const Value: Int64): Int64;
{$IFDEF WIN32}
asm
  mov     edx, [ebp + $08]
  mov     eax, [ebp + $0c]
  bswap   edx
  bswap   eax
{$ELSE}
{$IFDEF WIN64}
asm
  mov     rax, rcx
  bswap   rax
{$ELSE}
begin
  Result := Swap32(LongWord(Value));
  Result := (Result shl 32) or Swap32(LongWord(Value shr 32));
{$ENDIF}
{$ENDIF}
end;

function SwapFloat(const Value: Single): Single; overload; inline;
var
  R: LongWord absolute Result;
  V: LongWord absolute Value;
begin
  R := Swap32(V);
end;

function SwapFloat(const Value: Double): Double; overload; inline;
var
  R: Int64 absolute Result;
  V: Int64 absolute Value;
begin
  R := Swap64(V);
end;

 

其它版本:

function ExchangeByteOrderDouble(const PV: PAnsiChar): Double;
var
  pd: PAnsiChar;
begin
  pd := @Result;
  pd[0] := pv[7];
  pd[1] := pv[6];
  pd[2] := pv[5];
  pd[3] := pv[4];
  pd[4] := pv[3];
  pd[5] := pv[2];
  pd[6] := pv[1];
  pd[7] := pv[0];
end;

 

好吧,这个最慢

function SwapByteOrder(const Value: Double): Double; overload;
var
  V: Int64 absolute Result;
  VV: Int64 absolute Value;
begin
  V :=
    ((((VV and $00000000000000ff) shl 56) or
    ((VV and $000000000000ff00) shl 40) or
    ((VV and $0000000000ff0000) shl 24) or
    ((VV and $00000000ff000000) shl 8) or
    ((VV and $000000ff00000000) shr 8) or
    ((VV and $0000ff0000000000) shr 24) or
    ((VV and $00ff000000000000) shr 40) or
    ((VV and $ff00000000000000) shr 56)));
end;

 

感谢 网友

@[合肥]全能地图 
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python实现MATLAB中的bwlabel函数发布时间:2022-07-18
下一篇:
MATLAB描绘极坐标图像——polar发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap