From 884b08065a17ddcbb472ca4dc919b0a710b848f5 Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 12 Jun 2006 06:12:12 +0000 Subject: [PATCH] Merged revisions 3543 via svnmerge from http://peter@svn.freepascal.org/svn/fpc/trunk ........ r3543 | florian | 2006-05-15 21:42:13 +0200 (Mon, 15 May 2006) | 2 lines * endian conversion functions ........ git-svn-id: branches/fixes_2_0@3848 - --- rtl/inc/generic.inc | 297 ++++++++++++++++++++++++++++++++++++++++++++ rtl/inc/systemh.inc | 28 +++++ 2 files changed, 325 insertions(+) diff --git a/rtl/inc/generic.inc b/rtl/inc/generic.inc index eea3c72e84..92cff4c868 100644 --- a/rtl/inc/generic.inc +++ b/rtl/inc/generic.inc @@ -1143,3 +1143,300 @@ end; {$endif FPC_SYSTEM_HAS_SYSRESETFPU} +{$ifndef FPC_SYSTEM_HAS_SWAPENDIAN} +function SwapEndian(const AValue: SmallInt): SmallInt; + begin + Result := (AValue shr 8) or (AValue shl 8); + end; + + +function SwapEndian(const AValue: Word): Word; + begin + Result := (AValue shr 8) or (AValue shl 8); + end; + + +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); + end; + + +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); + end; + + +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); + end; + + +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); + end; +{$endif FPC_SYSTEM_HAS_SWAPENDIAN} + +function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + + +function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + + +function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_BIG} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; + + +function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif} + begin + {$IFDEF ENDIAN_LITTLE} + Result := AValue; + {$ELSE} + Result := SwapEndian(AValue); + {$ENDIF} + end; diff --git a/rtl/inc/systemh.inc b/rtl/inc/systemh.inc index f7c8e1a490..62b87605ce 100644 --- a/rtl/inc/systemh.inc +++ b/rtl/inc/systemh.inc @@ -432,6 +432,34 @@ Function odd(l:Longword):Boolean;[internconst:fpc_in_const_odd];{$ifdef SYSTEMIN Function odd(l:Int64):Boolean;[internconst:fpc_in_const_odd];{$ifdef SYSTEMINLINE}inline;{$endif} Function odd(l:QWord):Boolean;[internconst:fpc_in_const_odd];{$ifdef SYSTEMINLINE}inline;{$endif} +function BEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif} +function BEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif} +function BEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif} +function BEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif} +function BEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif} +function BEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif} + +function LEtoN(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif} +function LEtoN(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif} +function LEtoN(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif} +function LEtoN(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif} +function LEtoN(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif} +function LEtoN(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif} + +function NtoBE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif} +function NtoBE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif} +function NtoBE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif} +function NtoBE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif} +function NtoBE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif} +function NtoBE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif} + +function NtoLE(const AValue: SmallInt): SmallInt;{$ifdef SYSTEMINLINE}inline;{$endif} +function NtoLE(const AValue: Word): Word;{$ifdef SYSTEMINLINE}inline;{$endif} +function NtoLE(const AValue: LongInt): LongInt;{$ifdef SYSTEMINLINE}inline;{$endif} +function NtoLE(const AValue: DWord): DWord;{$ifdef SYSTEMINLINE}inline;{$endif} +function NtoLE(const AValue: Int64): Int64;{$ifdef SYSTEMINLINE}inline;{$endif} +function NtoLE(const AValue: QWord): QWord;{$ifdef SYSTEMINLINE}inline;{$endif} + { float math routines } {$I mathh.inc}