From 05e72f52c67ebca77f3bbee7cf0192641ebc7b88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A1roly=20Balogh?= Date: Sun, 7 Sep 2014 23:19:57 +0000 Subject: [PATCH] a slightly better generic implementation for SwapEndian() 32 bit and 64 bit ints git-svn-id: trunk@28614 - --- rtl/inc/generic.inc | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/rtl/inc/generic.inc b/rtl/inc/generic.inc index 629f2346c0..4774d639e4 100644 --- a/rtl/inc/generic.inc +++ b/rtl/inc/generic.inc @@ -1854,45 +1854,35 @@ function SwapEndian(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endi function SwapEndian(const AValue: LongInt): LongInt; begin - Result := (AValue shl 24) - or ((AValue and $0000FF00) shl 8) - or ((AValue and $00FF0000) shr 8) - or (AValue shr 24); + Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF); + Result := (Result shl 16) or (Result shr 16); end; {$ifndef cpujvm} function SwapEndian(const AValue: DWord): DWord; begin - Result := (AValue shl 24) - or ((AValue and $0000FF00) shl 8) - or ((AValue and $00FF0000) shr 8) - or (AValue shr 24); + Result := ((AValue shl 8) and $FF00FF00) or ((AValue shr 8) and $00FF00FF); + Result := (Result shl 16) or (Result shr 16); end; {$endif} function SwapEndian(const AValue: Int64): Int64; begin - Result := (AValue shl 56) - or ((AValue and $000000000000FF00) shl 40) - or ((AValue and $0000000000FF0000) shl 24) - or ((AValue and $00000000FF000000) shl 8) - or ((AValue and $000000FF00000000) shr 8) - or ((AValue and $0000FF0000000000) shr 24) - or ((AValue and $00FF000000000000) shr 40) - or (AValue shr 56); + Result := ((AValue shl 8) and $FF00FF00FF00FF00) or + ((AValue shr 8) and $00FF00FF00FF00FF); + Result := ((Result shl 16) and $FFFF0000FFFF0000) or + ((Result shr 16) and $0000FFFF0000FFFF); + Result := (Result shl 32) or ((Result shr 32)); end; {$ifndef cpujvm} function SwapEndian(const AValue: QWord): QWord; begin - Result := (AValue shl 56) - or ((AValue and $000000000000FF00) shl 40) - or ((AValue and $0000000000FF0000) shl 24) - or ((AValue and $00000000FF000000) shl 8) - or ((AValue and $000000FF00000000) shr 8) - or ((AValue and $0000FF0000000000) shr 24) - or ((AValue and $00FF000000000000) shr 40) - or (AValue shr 56); + Result := ((AValue shl 8) and $FF00FF00FF00FF00) or + ((AValue shr 8) and $00FF00FF00FF00FF); + Result := ((Result shl 16) and $FFFF0000FFFF0000) or + ((Result shr 16) and $0000FFFF0000FFFF); + Result := (Result shl 32) or ((Result shr 32)); end; {$endif} {$endif FPC_SYSTEM_HAS_SWAPENDIAN}