From 4c6149e1fd65e44d7d6471025e5c9159a90c17da Mon Sep 17 00:00:00 2001 From: michael Date: Mon, 23 Dec 2019 13:38:27 +0000 Subject: [PATCH] * Merging revisions r43417 from trunk: ------------------------------------------------------------------------ r43417 | michael | 2019-11-08 12:43:10 +0100 (Fri, 08 Nov 2019) | 1 line * Bit helpers for ordinal functions, adapted from batch by Thaddy De Koning (bug ID 0036146) ------------------------------------------------------------------------ git-svn-id: branches/fixes_3_2@43745 - --- rtl/objpas/sysutils/syshelp.inc | 10 +++++ rtl/objpas/sysutils/syshelph.inc | 72 ++++++++++++++++++++++++++++++++ rtl/objpas/sysutils/syshelpo.inc | 33 +++++++++++++++ 3 files changed, 115 insertions(+) diff --git a/rtl/objpas/sysutils/syshelp.inc b/rtl/objpas/sysutils/syshelp.inc index 07c29f3813..e30e473f1d 100644 --- a/rtl/objpas/sysutils/syshelp.inc +++ b/rtl/objpas/sysutils/syshelp.inc @@ -1550,6 +1550,7 @@ end; {$define TORDINALHELPER:=TByteHelper} {$define TORDINALTYPE:=Byte} +{$define TORDINALBITINDEX:=TByteBitIndex} {$i syshelpo.inc} { --------------------------------------------------------------------- @@ -1558,6 +1559,7 @@ end; {$define TORDINALHELPER:=TShortIntHelper} {$define TORDINALTYPE:=ShortInt} +{$define TORDINALBITINDEX:=TShortIntBitIndex} {$i syshelpo.inc} { --------------------------------------------------------------------- @@ -1566,6 +1568,7 @@ end; {$define TORDINALHELPER:=TSmallIntHelper} {$define TORDINALTYPE:=SmallInt} +{$define TORDINALBITINDEX:=TSmallIntBitIndex} {$i syshelpo.inc} { --------------------------------------------------------------------- @@ -1574,6 +1577,7 @@ end; {$define TORDINALHELPER:=TWordHelper} {$define TORDINALTYPE:=Word} +{$define TORDINALBITINDEX:=TWordBitIndex} {$i syshelpo.inc} { --------------------------------------------------------------------- @@ -1582,6 +1586,7 @@ end; {$define TORDINALHELPER:=TCardinalHelper} {$define TORDINALTYPE:=Cardinal} +{$define TORDINALBITINDEX:=TCardinalBitIndex} {$i syshelpo.inc} { --------------------------------------------------------------------- @@ -1590,6 +1595,7 @@ end; {$define TORDINALHELPER:=TIntegerHelper} {$define TORDINALTYPE:=Integer} +{$define TORDINALBITINDEX:=TIntegerBitIndex} {$i syshelpo.inc} { --------------------------------------------------------------------- @@ -1598,6 +1604,7 @@ end; {$define TORDINALHELPER:=TInt64Helper} {$define TORDINALTYPE:=Int64} +{$define TORDINALBITINDEX:=TInt64BitIndex} {$i syshelpo.inc} { --------------------------------------------------------------------- @@ -1606,6 +1613,7 @@ end; {$define TORDINALHELPER:=TQWordHelper} {$define TORDINALTYPE:=QWord} +{$define TORDINALBITINDEX:=TQwordBitIndex} {$i syshelpo.inc} { --------------------------------------------------------------------- @@ -1614,6 +1622,7 @@ end; {$define TORDINALHELPER:=TNativeIntHelper} {$define TORDINALTYPE:=NativeInt} +{$define TORDINALBITINDEX:=TNativeIntBitIndex} {$i syshelpo.inc} { --------------------------------------------------------------------- @@ -1622,6 +1631,7 @@ end; {$define TORDINALHELPER:=TNativeUIntHelper} {$define TORDINALTYPE:=NativeUInt} +{$define TORDINALBITINDEX:=TNativeUIntBitIndex} {$i syshelpo.inc} { --------------------------------------------------------------------- diff --git a/rtl/objpas/sysutils/syshelph.inc b/rtl/objpas/sysutils/syshelph.inc index a233c5afde..371fd3d38f 100644 --- a/rtl/objpas/sysutils/syshelph.inc +++ b/rtl/objpas/sysutils/syshelph.inc @@ -5,6 +5,28 @@ Type TStringArray = Array of string; TCharArray = Array of char; TEndian = ObjPas.TEndian; + TByteBitIndex = 0..7; + TShortIntBitIndex = 0..7; + TWordBitIndex = 0..15; + TSmallIntBitIndex = 0..15; + TCardinalBitIndex = 0..31; + TIntegerBitIndex = 0..31; + TLongIntBitIndex = TIntegerBitIndex; + TQwordBitIndex = 0..63; + TInt64BitIndex = 0..63; +{$IFDEF cpu16} + TNativeUIntBitIndex = TWordBitIndex; + TNativeIntBitIndex = TSmallIntBitIndex; +{$ENDIF} +{$IFDEF cpu32} + TNativeUIntBitIndex = TCardinalBitIndex; + TNativeIntBitIndex = TIntegerBitIndex; +{$ENDIF} +{$IFDEF cpu64} + TNativeUIntBitIndex = TQWordBitIndex; + TNativeIntBitIndex = TInt64BitIndex; +{$ENDIF} + Const CPUEndian = {$IFDEF FPC_LITTLE_ENDIAN}TEndian.Little{$ELSE}TEndian.Big{$ENDIF}; @@ -373,10 +395,15 @@ Type Function ToBoolean: Boolean; inline; Function ToDouble: Double; inline; Function ToExtended: Extended; inline; + Function ToBinString:string; Function ToHexString(const AMinDigits: Integer): string; overload; inline; Function ToHexString: string; overload; inline; Function ToSingle: Single; inline; Function ToString: string; overload; inline; + Function SetBit(const Index: TByteBitIndex) : Byte; inline; + Function ClearBit(const Index: TByteBitIndex) : Byte; inline; + Function ToggleBit(const Index: TByteBitIndex) : Byte; inline; + Function TestBit(const Index:TByteBitIndex):Boolean; inline; end; TShortIntHelper = Type Helper for ShortInt @@ -393,10 +420,15 @@ Type Function ToBoolean: Boolean; inline; Function ToDouble: Double; inline; Function ToExtended: Extended; inline; + Function ToBinString:string; inline; Function ToHexString(const AMinDigits: Integer): string; overload; inline; Function ToHexString: string; overload; inline; Function ToSingle: Single; inline; Function ToString: string; overload; inline; + Function SetBit(const Index: TShortIntBitIndex): Shortint; inline; + Function ClearBit(const Index: TShortIntBitIndex): Shortint; inline; + Function ToggleBit(const Index: TShortIntBitIndex): Shortint; inline; + Function TestBit(const Index:TShortIntBitIndex):Boolean; end; TSmallIntHelper = Type Helper for SmallInt @@ -412,11 +444,16 @@ Type public Function ToString: string; overload; inline; Function ToBoolean: Boolean; inline; + Function ToBinString:string; inline; Function ToHexString: string; overload; inline; Function ToHexString(const AMinDigits: Integer): string; overload; inline; Function ToSingle: Single; inline; Function ToDouble: Double; inline; Function ToExtended: Extended; inline; + Function SetBit(const Index: TSmallIntBitIndex) : Smallint; inline; + Function ClearBit(const Index: TSmallIntBitIndex) : Smallint; inline; + Function ToggleBit(const Index: TSmallIntBitIndex) : Smallint; inline; + Function TestBit(const Index:TSmallIntBitIndex):Boolean; end; TWordHelper = Type Helper for Word @@ -433,10 +470,15 @@ Type Function ToBoolean: Boolean; inline; Function ToDouble: Double; inline; Function ToExtended: Extended; inline; + Function ToBinString:string; inline; Function ToHexString(const AMinDigits: Integer): string; overload; inline; Function ToHexString: string; overload; inline; Function ToSingle: Single; inline; Function ToString: string; overload; inline; + Function SetBit(const Index: TWordBitIndex) : Word; inline; + Function ClearBit(const Index: TWordBitIndex) : Word; inline; + Function ToggleBit(const Index: TWordBitIndex) : Word; inline; + Function TestBit(const Index:TWordBitIndex):Boolean; inline; end; TCardinalHelper = Type Helper for Cardinal { for LongWord Type too } @@ -453,10 +495,15 @@ Type Function ToBoolean: Boolean; inline; Function ToDouble: Double; inline; Function ToExtended: Extended; inline; + Function ToBinString:string; inline; Function ToHexString(const AMinDigits: Integer): string; overload; inline; Function ToHexString: string; overload; inline; Function ToSingle: Single; inline; Function ToString: string; overload; inline; + Function SetBit(const Index: TCardinalBitIndex) : Cardinal; inline; + Function ClearBit(const Index: TCardinalBitIndex) : Cardinal; inline; + Function ToggleBit(const Index: TCardinalBitIndex) : Cardinal; inline; + Function TestBit(const Index:TCardinalBitIndex):Boolean; inline; end; TIntegerHelper = Type Helper for Integer { for LongInt Type too } @@ -473,10 +520,15 @@ Type Function ToBoolean: Boolean; inline; Function ToDouble: Double; inline; Function ToExtended: Extended; inline; + Function ToBinString:string; inline; Function ToHexString(const AMinDigits: Integer): string; overload; inline; Function ToHexString: string; overload; inline; Function ToSingle: Single; inline; Function ToString: string; overload; inline; + Function SetBit(const Index: TIntegerBitIndex) : Integer; inline; + Function ClearBit(const Index: TIntegerBitIndex) : Integer; inline; + Function ToggleBit(const Index: TIntegerBitIndex) : Integer; inline; + Function TestBit(const Index:TIntegerBitIndex):Boolean; inline; end; TInt64Helper = Type Helper for Int64 @@ -493,10 +545,15 @@ Type Function ToBoolean: Boolean; inline; Function ToDouble: Double; inline; Function ToExtended: Extended; inline; + Function ToBinString:string; inline; Function ToHexString(const AMinDigits: Integer): string; overload; inline; Function ToHexString: string; overload; inline; Function ToSingle: Single; inline; Function ToString: string; overload; inline; + Function SetBit(const Index: TInt64BitIndex) : Int64; inline; + Function ClearBit(const Index: TInt64BitIndex) : Int64; inline; + Function ToggleBit(const Index: TInt64BitIndex) : Int64; inline; + Function TestBit(const Index:TInt64BitIndex):Boolean; inline; end; TQWordHelper = Type Helper for QWord @@ -513,10 +570,15 @@ Type Function ToBoolean: Boolean; inline; Function ToDouble: Double; inline; Function ToExtended: Extended; inline; + Function ToBinString:string; inline; Function ToHexString(const AMinDigits: Integer): string; overload; inline; Function ToHexString: string; overload; inline; Function ToSingle: Single; inline; Function ToString: string; overload; inline; + Function SetBit(const Index: TQwordBitIndex) : QWord; inline; + Function ClearBit(const Index: TQwordBitIndex) : QWord; inline; + Function ToggleBit(const Index: TQwordBitIndex) : QWord; inline; + Function TestBit(const Index:TQwordBitIndex):Boolean; inline; end; TNativeIntHelper = Type Helper for NativeInt @@ -533,10 +595,15 @@ Type Function ToBoolean: Boolean; inline; Function ToDouble: Double; inline; Function ToExtended: Extended; inline; + Function ToBinString:string; inline; Function ToHexString(const AMinDigits: Integer): string; overload; inline; Function ToHexString: string; overload; inline; Function ToSingle: Single; inline; Function ToString: string; overload; inline; + Function SetBit(const Index: TNativeIntBitIndex) : NativeInt; inline; + Function ClearBit(const Index: TNativeIntBitIndex) : NativeInt; inline; + Function ToggleBit(const Index: TNativeIntBitIndex) : NativeInt; inline; + Function TestBit(const Index:TNativeIntBitIndex):Boolean; inline; end; TNativeUIntHelper = Type Helper for NativeUInt @@ -553,10 +620,15 @@ Type Function ToBoolean: Boolean; inline; Function ToDouble: Double; inline; Function ToExtended: Extended; inline; + Function ToBinString:string; inline; Function ToHexString(const AMinDigits: Integer): string; overload; inline; Function ToHexString: string; overload; inline; Function ToSingle: Single; inline; Function ToString: string; overload; inline; + Function SetBit(const Index: TNativeUIntBitIndex) : NativeUint; inline; + Function ClearBit(const Index: TNativeUIntBitIndex): NativeUint; inline; + Function ToggleBit(const Index: TNativeUIntBitIndex) : NativeUint; inline; + Function TestBit(const Index:TNativeUIntBitIndex) :Boolean; inline; end; {$SCOPEDENUMS ON} diff --git a/rtl/objpas/sysutils/syshelpo.inc b/rtl/objpas/sysutils/syshelpo.inc index 10dd9c7a7a..27d938e3af 100644 --- a/rtl/objpas/sysutils/syshelpo.inc +++ b/rtl/objpas/sysutils/syshelpo.inc @@ -45,6 +45,12 @@ begin Result:=Self; end; +Function TORDINALHELPER.ToBinString: string; inline; + +begin + Result:=BinStr(Self,SizeOf(TORDINALTYPE)*8); +end; + Function TORDINALHELPER.ToHexString(const AMinDigits: Integer): string; overload; inline; @@ -69,3 +75,30 @@ Function TORDINALHELPER.ToString: string; overload; inline; begin Result:=IntToStr(Self); end; + +Function TORDINALHELPER.SetBit(const index: TORDINALBITINDEX) : TORDINALTYPE; inline; + +begin + Self := Self or (TORDINALTYPE(1) shl index); + Result:=Self; +end; + +Function TORDINALHELPER.ClearBit(const index: TORDINALBITINDEX) : TORDINALTYPE; inline; + +begin + Self:=Self and not TORDINALTYPE((TORDINALTYPE(1) shl index)); + Result:=Self; +end; + +Function TORDINALHELPER.ToggleBit(const index: TORDINALBITINDEX) : TORDINALTYPE; inline; + +begin + Self := Self xor TORDINALTYPE((TORDINALTYPE(1) shl index)); + Result:=Self; +end; + +Function TORDINALHELPER.TestBit(const Index: TORDINALBITINDEX):Boolean; inline; + +begin + Result := (Self and TORDINALTYPE((TORDINALTYPE(1) shl index)))<>0; +end;