diff options
author | karoly <karoly@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2014-09-07 23:19:57 +0000 |
---|---|---|
committer | karoly <karoly@3ad0048d-3df7-0310-abae-a5850022a9f2> | 2014-09-07 23:19:57 +0000 |
commit | 71d269a2374e76162841725aba846b803765042d (patch) | |
tree | a6817c00485bf08ff67720ea666e1da5394481b8 /rtl/inc | |
parent | e0645bb159896511f5fffbe89bc4974adbdc2423 (diff) | |
download | fpc-71d269a2374e76162841725aba846b803765042d.tar.gz |
a slightly better generic implementation for SwapEndian() 32 bit and 64 bit ints
git-svn-id: http://svn.freepascal.org/svn/fpc/trunk@28614 3ad0048d-3df7-0310-abae-a5850022a9f2
Diffstat (limited to 'rtl/inc')
-rw-r--r-- | rtl/inc/generic.inc | 38 |
1 files 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} |