* Patch from Zeljko Avramovic to extend ordinal/float helpers

git-svn-id: trunk@49622 -
This commit is contained in:
michael 2021-07-21 11:36:05 +00:00
parent 415bf22e01
commit e6a593c5fa
4 changed files with 706 additions and 93 deletions

View File

@ -523,6 +523,9 @@
function TObject.FieldAddress(const name : shortstring) : pointer;
{The following is copied to the typinfo unit. If it is changed here, change it there as well ! }
type
PFieldInfo = ^TFieldInfo;
TFieldInfo =

View File

@ -1567,7 +1567,11 @@ end;
{$define TORDINALHELPER:=TByteHelper}
{$define TORDINALTYPE:=Byte}
{$define TORDINALBITINDEX:=TByteBitIndex}
{$define TORDINALNIBBLEINDEX:=TByteNibbleIndex}
{$define TORDINALOVERLAY:=TByteOverlay}
{$define TORDINALTYPESIZE1}
{$i syshelpo.inc}
{$undef TORDINALTYPESIZE1}
{ ---------------------------------------------------------------------
TShortintHelper
@ -1576,7 +1580,11 @@ end;
{$define TORDINALHELPER:=TShortIntHelper}
{$define TORDINALTYPE:=ShortInt}
{$define TORDINALBITINDEX:=TShortIntBitIndex}
{$define TORDINALNIBBLEINDEX:=TShortIntNibbleIndex}
{$define TORDINALOVERLAY:=TShortIntOverlay}
{$define TORDINALTYPESIZE1}
{$i syshelpo.inc}
{$undef TORDINALTYPESIZE1}
{ ---------------------------------------------------------------------
TSmallintHelper
@ -1585,7 +1593,12 @@ end;
{$define TORDINALHELPER:=TSmallIntHelper}
{$define TORDINALTYPE:=SmallInt}
{$define TORDINALBITINDEX:=TSmallIntBitIndex}
{$define TORDINALNIBBLEINDEX:=TSmallIntNibbleIndex}
{$define TORDINALBYTEINDEX:=TWordByteIndex}
{$define TORDINALOVERLAY:=TWordOverlay}
{$define TORDINALTYPESIZE2}
{$i syshelpo.inc}
{$undef TORDINALTYPESIZE2}
{ ---------------------------------------------------------------------
TWordHelper
@ -1594,7 +1607,12 @@ end;
{$define TORDINALHELPER:=TWordHelper}
{$define TORDINALTYPE:=Word}
{$define TORDINALBITINDEX:=TWordBitIndex}
{$define TORDINALNIBBLEINDEX:=TWordNibbleIndex}
{$define TORDINALBYTEINDEX:=TWordByteIndex}
{$define TORDINALOVERLAY:=TWordOverlay}
{$define TORDINALTYPESIZE2}
{$i syshelpo.inc}
{$undef TORDINALTYPESIZE2}
{ ---------------------------------------------------------------------
TCardinalHelper
@ -1603,7 +1621,13 @@ end;
{$define TORDINALHELPER:=TCardinalHelper}
{$define TORDINALTYPE:=Cardinal}
{$define TORDINALBITINDEX:=TCardinalBitIndex}
{$define TORDINALNIBBLEINDEX:=TCardinalNibbleIndex}
{$define TORDINALBYTEINDEX:=TCardinalByteIndex}
{$define TORDINALWORDINDEX:=TCardinalWordIndex}
{$define TORDINALOVERLAY:=TDwordOverlay}
{$define TORDINALTYPESIZE4}
{$i syshelpo.inc}
{$undef TORDINALTYPESIZE4}
{ ---------------------------------------------------------------------
TIntegerHelper
@ -1612,7 +1636,13 @@ end;
{$define TORDINALHELPER:=TIntegerHelper}
{$define TORDINALTYPE:=Integer}
{$define TORDINALBITINDEX:=TIntegerBitIndex}
{$define TORDINALNIBBLEINDEX:=TIntegerNibbleIndex}
{$define TORDINALBYTEINDEX:=TIntegerByteIndex}
{$define TORDINALWORDINDEX:=TIntegerWordIndex}
{$define TORDINALOVERLAY:=TDwordOverlay}
{$define TORDINALTYPESIZE4}
{$i syshelpo.inc}
{$undef TORDINALTYPESIZE4}
{ ---------------------------------------------------------------------
TInt64Helper
@ -1621,7 +1651,14 @@ end;
{$define TORDINALHELPER:=TInt64Helper}
{$define TORDINALTYPE:=Int64}
{$define TORDINALBITINDEX:=TInt64BitIndex}
{$define TORDINALNIBBLEINDEX:=TInt64NibbleIndex}
{$define TORDINALBYTEINDEX:=TInt64ByteIndex}
{$define TORDINALWORDINDEX:=TInt64WordIndex}
{$define TORDINALDWORDINDEX:=TInt64DWordIndex}
{$define TORDINALOVERLAY:=TQwordOverlay}
{$define TORDINALTYPESIZE8}
{$i syshelpo.inc}
{$undef TORDINALTYPESIZE8}
{ ---------------------------------------------------------------------
TQWordHelper
@ -1630,7 +1667,14 @@ end;
{$define TORDINALHELPER:=TQWordHelper}
{$define TORDINALTYPE:=QWord}
{$define TORDINALBITINDEX:=TQwordBitIndex}
{$define TORDINALNIBBLEINDEX:=TQwordNibbleIndex}
{$define TORDINALBYTEINDEX:=TQwordByteIndex}
{$define TORDINALWORDINDEX:=TQWordWordIndex}
{$define TORDINALDWORDINDEX:=TQWordDWordIndex}
{$define TORDINALOVERLAY:=TQwordOverlay}
{$define TORDINALTYPESIZE8}
{$i syshelpo.inc}
{$undef TORDINALTYPESIZE8}
{ ---------------------------------------------------------------------
TNativeIntHelper
@ -1639,7 +1683,31 @@ end;
{$define TORDINALHELPER:=TNativeIntHelper}
{$define TORDINALTYPE:=NativeInt}
{$define TORDINALBITINDEX:=TNativeIntBitIndex}
{$ifdef cpu16}
{$define TORDINALNIBBLEINDEX:=TSmallIntNibbleIndex}
{$define TORDINALBYTEINDEX:=TSmallIntByteIndex}
{$define TORDINALOVERLAY:=TSmallIntOverlay}
{$define TORDINALTYPESIZE2}
{$endif}
{$ifdef cpu32}
{$define TORDINALNIBBLEINDEX:=TIntegerNibbleIndex}
{$define TORDINALBYTEINDEX:=TIntegerByteIndex}
{$define TORDINALWORDINDEX:=TIntegerWordIndex}
{$define TORDINALOVERLAY:=TIntegerOverlay}
{$define TORDINALTYPESIZE4}
{$endif}
{$ifdef cpu64}
{$define TORDINALNIBBLEINDEX:=TInt64NibbleIndex}
{$define TORDINALBYTEINDEX:=TInt64ByteIndex}
{$define TORDINALWORDINDEX:=TInt64WordIndex}
{$define TORDINALDWORDINDEX:=TInt64DWordIndex}
{$define TORDINALOVERLAY:=TInt64Overlay}
{$define TORDINALTYPESIZE8}
{$endif}
{$i syshelpo.inc}
{$undef TORDINALTYPESIZE2}
{$undef TORDINALTYPESIZE4}
{$undef TORDINALTYPESIZE8}
{ ---------------------------------------------------------------------
TNativeUIntHelper
@ -1648,7 +1716,31 @@ end;
{$define TORDINALHELPER:=TNativeUIntHelper}
{$define TORDINALTYPE:=NativeUInt}
{$define TORDINALBITINDEX:=TNativeUIntBitIndex}
{$ifdef cpu16}
{$define TORDINALNIBBLEINDEX:=TWordNibbleIndex}
{$define TORDINALBYTEINDEX:=TWordByteIndex}
{$define TORDINALOVERLAY:=TWordOverlay}
{$define TORDINALTYPESIZE2}
{$endif}
{$ifdef cpu32}
{$define TORDINALNIBBLEINDEX:=TDwordNibbleIndex}
{$define TORDINALBYTEINDEX:=TDwordByteIndex}
{$define TORDINALWORDINDEX:=TDwordWordIndex}
{$define TORDINALOVERLAY:=TDwordOverlay}
{$define TORDINALTYPESIZE4}
{$endif}
{$ifdef cpu64}
{$define TORDINALNIBBLEINDEX:=TQwordNibbleIndex}
{$define TORDINALBYTEINDEX:=TQwordByteIndex}
{$define TORDINALWORDINDEX:=TQwordWordIndex}
{$define TORDINALDWORDINDEX:=TQwordDWordIndex}
{$define TORDINALOVERLAY:=TQwordOverlay}
{$define TORDINALTYPESIZE8}
{$endif}
{$i syshelpo.inc}
{$undef TORDINALTYPESIZE2}
{$undef TORDINALTYPESIZE4}
{$undef TORDINALTYPESIZE8}
{ ---------------------------------------------------------------------
TBooleanHelper

View File

@ -251,7 +251,7 @@ Type
Function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
Function ToString: string; overload; inline;
property Bytes[AIndex: Cardinal]: Byte read GetB write SetB;
property Bytes [AIndex: Cardinal]: Byte read GetB write SetB;
property Words[AIndex: Cardinal]: Word read GetW write SetW;
property Sign: Boolean read GetS write SetS;
property Exp: QWord read GetE write SetE;
@ -312,8 +312,8 @@ Type
Function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
Function ToString: string; overload; inline;
property Bytes[AIndex: Cardinal]: Byte read GetB write SetB; // 0..7
property Words[AIndex: Cardinal]: Word read GetW write SetW; // 0..3
property Bytes [AIndex: Cardinal]: Byte read GetB write SetB; // 0..7
property Words [AIndex: Cardinal]: Word read GetW write SetW; // 0..3
property Sign: Boolean read GetS write SetS;
property Exp: QWord read GetE write SetE;
property Frac: QWord read GetF write SetF;
@ -373,25 +373,45 @@ Type
Function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
Function ToString: string; overload; inline;
property Bytes[AIndex: Cardinal]: Byte read GetB write SetB;
property Words[AIndex: Cardinal]: Word read GetW write SetW;
property Bytes [AIndex: Cardinal]: Byte read GetB write SetB;
property Words [AIndex: Cardinal]: Word read GetW write SetW;
property Sign: Boolean read GetS write SetS;
property Exp: QWord read GetE write SetE;
property Frac: QWord read GetF write SetF;
end;
{$ENDIF FPC_HAS_TYPE_EXTENDED}
nibble = 0..15;
TNibbleBitIndex = 0..3;
// TByteBitIndex is already defined
TByteNibbleIndex = 0..1;
TByteOverlay = bitpacked record case integer of // for fast extraction of bits
0: (AsBit: bitpacked array[TByteBitIndex] of boolean);
1: (AsNibble: bitpacked array[TByteNibbleIndex] of nibble);
2: (AsByte: byte);
end;
TByteHelper = Type Helper for Byte
public
const
MaxValue = 255;
MinValue = 0;
const
MaxValue = High(byte);
MinValue = Low(byte);
MaxBit = High(TByteBitIndex);
MinBit = Low(TByteBitIndex);
MaxNibble = High(TByteNibbleIndex);
MinNibble = Low(TByteNibbleIndex);
public
Class Function Parse(const AString: string): Byte; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: Byte): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: Byte): Boolean; inline; static;
Public
protected
function GetBit(const aIndex: TByteBitIndex): boolean; inline;
procedure PutBit(const aIndex: TByteBitIndex; const aNewValue: boolean); inline;
function GetNibble(const aIndex: TByteNibbleIndex): nibble; inline;
procedure PutNibble(const aIndex: TByteNibbleIndex; const aNewValue: nibble); inline;
public
Function ToBoolean: Boolean; inline;
Function ToDouble: Double; inline;
Function ToExtended: Extended; inline;
@ -404,18 +424,38 @@ Type
Function ClearBit(const Index: TByteBitIndex) : Byte; inline;
Function ToggleBit(const Index: TByteBitIndex) : Byte; inline;
Function TestBit(const Index:TByteBitIndex):Boolean; inline;
procedure Clear; inline;
function HighestSetBitPos: cardinal; inline;
function LowestSetBitPos: cardinal; inline;
function SetBitsCount: byte; inline;
property Bits [aIndex: TByteBitIndex]: boolean read GetBit write PutBit;
property Nibbles [aIndex: TByteNibbleIndex]: nibble read GetNibble write PutNibble;
end;
// TShortIntBitIndex is already defined
TShortIntNibbleIndex = TByteNibbleIndex;
TShortIntOverlay = TByteOverlay;
TShortIntHelper = Type Helper for ShortInt
public
const
MaxValue = 127;
MinValue = -128;
const
MaxValue = High(shortint);
MinValue = Low(shortint);
MaxBit = High(TShortIntBitIndex);
MinBit = Low(TShortIntBitIndex);
MaxNibble = High(TShortIntNibbleIndex);
MinNibble = Low(TShortIntNibbleIndex);
public
Class Function Parse(const AString: string): ShortInt; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: ShortInt): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: ShortInt): Boolean; inline; static;
protected
function GetBit(const aIndex: TShortIntBitIndex): boolean; inline;
procedure PutBit(const aIndex: TShortIntBitIndex; const aNewValue: boolean); inline;
function GetNibble(const aIndex: TShortIntNibbleIndex): nibble; inline;
procedure PutNibble(const aIndex: TShortIntNibbleIndex; const aNewValue: nibble); inline;
public
Function ToBoolean: Boolean; inline;
Function ToDouble: Double; inline;
@ -429,18 +469,99 @@ Type
Function ClearBit(const Index: TShortIntBitIndex): Shortint; inline;
Function ToggleBit(const Index: TShortIntBitIndex): Shortint; inline;
Function TestBit(const Index:TShortIntBitIndex):Boolean;
procedure Clear; inline;
function HighestSetBitPos: cardinal; inline;
function LowestSetBitPos: cardinal; inline;
function SetBitsCount: byte; inline;
property Bits [aIndex: TShortIntBitIndex]: boolean read GetBit write PutBit;
property Nibbles [aIndex: TShortIntNibbleIndex]: nibble read GetNibble write PutNibble;
end;
TSmallIntHelper = Type Helper for SmallInt
// TWordBitIndex is already defined
TWordByteIndex = 0..1;
TWordNibbleIndex = 0..3;
TWordOverlay = bitpacked record case integer of // for fast extraction of bytes and bits
0: (AsBit: bitpacked array[TWordBitIndex] of boolean);
1: (AsNibble: bitpacked array[TWordNibbleIndex] of nibble);
2: (AsByte: array[TWordByteIndex] of byte);
3: (AsWord: word);
// recursive overlay:
4: (AsByteOverlay: array[TWordByteIndex] of TByteOverlay);
end;
TWordHelper = Type Helper for Word
const
MaxValue = High(word);
MinValue = Low(word);
MaxBit = High(TWordBitIndex);
MinBit = Low(TWordBitIndex);
MaxNibble = High(TWordNibbleIndex);
MinNibble = Low(TWordNibbleIndex);
MaxByte = High(TWordByteIndex);
MinByte = Low(TWordByteIndex);
public
const
MaxValue = 32767;
MinValue = -32768;
Class Function Parse(const AString: string): Word; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: Word): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: Word): Boolean; inline; static;
protected
function GetBit(const aIndex: TWordBitIndex): boolean; inline;
procedure PutBit(const aIndex: TWordBitIndex; const aNewValue: boolean); inline;
function GetNibble(const aIndex: TWordNibbleIndex): nibble; inline;
procedure PutNibble(const aIndex: TWordNibbleIndex; const aNewValue: nibble); inline;
function GetByte(const aIndex: TWordByteIndex): byte;
procedure PutByte(const aIndex: TWordByteIndex; const aNewValue: byte);
public
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;
procedure Clear; inline;
function HighestSetBitPos: cardinal; inline;
function LowestSetBitPos: cardinal; inline;
function SetBitsCount: byte; inline;
property Bits [aIndex: TWordBitIndex]: boolean read GetBit write PutBit;
property Nibbles [aIndex: TWordNibbleIndex]: nibble read GetNibble write PutNibble;
property Bytes [aIndex: TWordByteIndex]: byte read GetByte write PutByte;
end;
// TSmallIntBitIndex is already defined
TSmallIntByteIndex = TWordByteIndex;
TSmallIntNibbleIndex = TWordNibbleIndex;
TSmallIntOverlay = TWordOverlay;
TSmallIntHelper = Type Helper for SmallInt
const
MaxValue = High(SmallInt);
MinValue = Low(SmallInt);
MaxBit = High(TSmallIntBitIndex);
MinBit = Low(TSmallIntBitIndex);
MaxNibble = High(TSmallIntNibbleIndex);
MinNibble = Low(TSmallIntNibbleIndex);
MaxByte = High(TSmallIntByteIndex);
MinByte = Low(TSmallIntByteIndex);
public
Class Function Parse(const AString: string): SmallInt; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: SmallInt): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: SmallInt): Boolean; inline; static;
protected
function GetBit(const aIndex: TSmallIntBitIndex): boolean; inline;
procedure PutBit(const aIndex: TSmallIntBitIndex; const aNewValue: boolean); inline;
function GetNibble(const aIndex: TSmallIntNibbleIndex): nibble; inline;
procedure PutNibble(const aIndex: TSmallIntNibbleIndex; const aNewValue: nibble); inline;
function GetByte(const aIndex: TSmallIntByteIndex): byte;
procedure PutByte(const aIndex: TSmallIntByteIndex; const aNewValue: byte);
public
Function ToString: string; overload; inline;
Function ToBoolean: Boolean; inline;
@ -454,44 +575,65 @@ Type
Function ClearBit(const Index: TSmallIntBitIndex) : Smallint; inline;
Function ToggleBit(const Index: TSmallIntBitIndex) : Smallint; inline;
Function TestBit(const Index:TSmallIntBitIndex):Boolean;
procedure Clear; inline;
function HighestSetBitPos: cardinal; inline;
function LowestSetBitPos: cardinal; inline;
function SetBitsCount: byte; inline;
property Bits [aIndex: TSmallIntBitIndex]: boolean read GetBit write PutBit;
property Nibbles [aIndex: TSmallIntNibbleIndex]: nibble read GetNibble write PutNibble;
property Bytes [aIndex: TSmallIntByteIndex]: byte read GetByte write PutByte;
end;
TDwordBitIndex = TCardinalBitIndex;
TDwordNibbleIndex = 0..7;
TDwordByteIndex = 0..3;
TDwordWordIndex = 0..1;
TDwordOverlay = bitpacked record case integer of // for fast extraction of words, bytes and bits
0: (AsBit: bitpacked array[TDwordBitIndex] of boolean);
1: (AsNibble: bitpacked array[TDwordNibbleIndex] of nibble);
2: (AsByte: array[TDwordByteIndex] of byte);
3: (AsWord: array[TDwordWordIndex] of word);
4: (AsDword: dword);
// recursive overlays:
5: (AsByteOverlay: array[TDwordByteIndex] of TByteOverlay);
6: (AsWordOverlay: array[TDwordWordIndex] of TWordOverlay);
end;
TWordHelper = Type Helper for Word
public
const
MaxValue = 65535;
MinValue = 0;
Public
Class Function Parse(const AString: string): Word; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: Word): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: Word): Boolean; inline; static;
Public
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;
// TCardinalBitIndex is already defined
TCardinalNibbleIndex = TDwordNibbleIndex;
TCardinalByteIndex = TDwordByteIndex;
TCardinalWordIndex = TDwordWordIndex;
TCardinalOverlay = TDwordOverlay;
TCardinalHelper = Type Helper for Cardinal { for LongWord Type too }
const
MaxValue = High(Cardinal);
MinValue = Low(Cardinal);
MaxBit = High(TCardinalBitIndex);
MinBit = Low(TCardinalBitIndex);
MaxNibble = High(TCardinalNibbleIndex);
MinNibble = Low(TCardinalNibbleIndex);
MaxByte = High(TCardinalByteIndex);
MinByte = Low(TCardinalByteIndex);
MaxWord = High(TCardinalWordIndex);
MinWord = Low(TCardinalWordIndex);
public
const
MaxValue = 4294967295;
MinValue = 0;
Public
Class Function Parse(const AString: string): Cardinal; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: Cardinal): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: Cardinal): Boolean; inline; static;
Public
protected
function GetBit(const aIndex: TCardinalBitIndex): boolean; inline;
procedure PutBit(const aIndex: TCardinalBitIndex; const aNewValue: boolean); inline;
function GetNibble(const aIndex: TCardinalNibbleIndex): nibble; inline;
procedure PutNibble(const aIndex: TCardinalNibbleIndex; const aNewValue: nibble); inline;
function GetByte(const aIndex: TCardinalByteIndex): byte;
procedure PutByte(const aIndex: TCardinalByteIndex; const aNewValue: byte);
function GetWord(const aIndex: TCardinalWordIndex): word;
procedure PutWord(const aIndex: TCardinalWordIndex; const aNewValue: word);
public
Function ToBoolean: Boolean; inline;
Function ToDouble: Double; inline;
Function ToExtended: Extended; inline;
@ -504,19 +646,50 @@ Type
Function ClearBit(const Index: TCardinalBitIndex) : Cardinal; inline;
Function ToggleBit(const Index: TCardinalBitIndex) : Cardinal; inline;
Function TestBit(const Index:TCardinalBitIndex):Boolean; inline;
procedure Clear; inline;
function HighestSetBitPos: cardinal; inline;
function LowestSetBitPos: cardinal; inline;
function SetBitsCount: byte; inline;
property Bits [aIndex: TCardinalBitIndex]: boolean read GetBit write PutBit;
property Nibbles[aIndex: TCardinalNibbleIndex]: nibble read GetNibble write PutNibble;
property Bytes [aIndex: TCardinalByteIndex]: byte read GetByte write PutByte;
property Words [aIndex: TCardinalWordIndex]: word read GetWord write PutWord;
end;
// TIntegerBitIndex is already defined
TIntegerNibbleIndex = TDwordNibbleIndex;
TIntegerByteIndex = TDwordByteIndex;
TIntegerWordIndex = TDwordWordIndex;
TIntegerOverlay = TDwordOverlay;
TIntegerHelper = Type Helper for Integer { for LongInt Type too }
const
MaxValue = High(integer);
MinValue = Low(integer);
MaxBit = High(TIntegerBitIndex);
MinBit = Low(TIntegerBitIndex);
MaxNibble = High(TIntegerNibbleIndex);
MinNibble = Low(TIntegerNibbleIndex);
MaxByte = High(TIntegerByteIndex);
MinByte = Low(TIntegerByteIndex);
MaxWord = High(TIntegerWordIndex);
MinWord = Low(TIntegerWordIndex);
public
const
MaxValue = 2147483647;
MinValue = -2147483648;
Public
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: Integer): string; overload; inline; static;
Class Function Parse(const AString: string): Integer; inline; static;
Class Function TryParse(const AString: string; out AValue: Integer): Boolean; inline; static;
Public
protected
function GetBit(const aIndex: TIntegerBitIndex): boolean; inline;
procedure PutBit(const aIndex: TIntegerBitIndex; const aNewValue: boolean); inline;
function GetNibble(const aIndex: TIntegerNibbleIndex): nibble; inline;
procedure PutNibble(const aIndex: TIntegerNibbleIndex; const aNewValue: nibble); inline;
function GetByte(const aIndex: TIntegerByteIndex): byte;
procedure PutByte(const aIndex: TIntegerByteIndex; const aNewValue: byte);
function GetWord(const aIndex: TIntegerWordIndex): word;
procedure PutWord(const aIndex: TIntegerWordIndex; const aNewValue: word);
public
Function ToBoolean: Boolean; inline;
Function ToDouble: Double; inline;
Function ToExtended: Extended; inline;
@ -529,19 +702,128 @@ Type
Function ClearBit(const Index: TIntegerBitIndex) : Integer; inline;
Function ToggleBit(const Index: TIntegerBitIndex) : Integer; inline;
Function TestBit(const Index:TIntegerBitIndex):Boolean; inline;
procedure Clear; inline;
function HighestSetBitPos: cardinal; inline;
function LowestSetBitPos: cardinal; inline;
function SetBitsCount: byte; inline;
property Bits [aIndex: TIntegerBitIndex]: boolean read GetBit write PutBit;
property Nibbles[aIndex: TCardinalNibbleIndex]: nibble read GetNibble write PutNibble;
property Bytes [aIndex: TIntegerByteIndex]: byte read GetByte write PutByte;
property Words [aIndex: TIntegerWordIndex]: word read GetWord write PutWord;
end;
TInt64Helper = Type Helper for Int64
// TQwordBitIndex is already defined
TQwordNibbleIndex = 0..15;
TQwordByteIndex = 0..7;
TQwordWordIndex = 0..3;
TQwordDwordIndex = 0..1;
TQwordOverlay = bitpacked record case integer of // for fast extraction of longwords, words, bytes and bits
0: (AsBit: bitpacked array[TQwordBitIndex] of boolean);
1: (AsNibble: bitpacked array[TQwordNibbleIndex] of nibble);
2: (AsByte: array[TQwordByteIndex] of byte);
3: (AsWord: array[TQwordWordIndex] of word);
4: (AsDword: array[TQwordDwordIndex] of dword);
5: (AsQword: qword);
// recursive overlays:
6: (AsByteOverlay: array[TQwordByteIndex] of TByteOverlay);
7: (AsWordOverlay: array[TQwordWordIndex] of TWordOverlay);
8: (AsDwordOverlay: array[TQwordDwordIndex] of TDwordOverlay);
end;
TQWordHelper = Type Helper for QWord
const
MaxValue = High(qword);
MinValue = Low(qword);
MaxBit = High(TQwordBitIndex);
MinBit = Low(TQwordBitIndex);
MaxNibble = High(TQwordNibbleIndex);
MinNibble = Low(TQwordNibbleIndex);
MaxByte = High(TQwordByteIndex);
MinByte = Low(TQwordByteIndex);
MaxWord = High(TQwordWordIndex);
MinWord = Low(TQwordWordIndex);
MaxDword = High(TQwordDwordIndex);
MinDword = Low(TQwordDwordIndex);
public
Class Function Parse(const AString: string): Qword; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: Qword): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: Qword): Boolean; inline; static;
protected
function GetBit(const aIndex: TQwordBitIndex): boolean; inline;
procedure PutBit(const aIndex: TQwordBitIndex; const aNewValue: boolean); inline;
function GetNibble(const aIndex: TQwordNibbleIndex): nibble; inline;
procedure PutNibble(const aIndex: TQwordNibbleIndex; const aNewValue: nibble); inline;
function GetByte(const aIndex: TQwordByteIndex): byte;
procedure PutByte(const aIndex: TQwordByteIndex; const aNewValue: byte);
function GetWord(const aIndex: TQwordWordIndex): word;
procedure PutWord(const aIndex: TQwordWordIndex; const aNewValue: word);
function GetDword(const aIndex: TQwordDwordIndex): dword;
procedure PutDword(const aIndex: TQwordDwordIndex; const aNewValue: dword);
public
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;
procedure Clear; inline;
function HighestSetBitPos: cardinal; inline;
function LowestSetBitPos: cardinal; inline;
function SetBitsCount: byte; inline;
property Bits [aIndex: TQwordBitIndex]: boolean read GetBit write PutBit;
property Nibbles[aIndex: TQwordNibbleIndex]: nibble read GetNibble write PutNibble;
property Bytes [aIndex: TQwordByteIndex]: byte read GetByte write PutByte;
property Words [aIndex: TQwordWordIndex]: word read GetWord write PutWord;
property DWords [aIndex: TQwordDwordIndex]: dword read GetDWord write PutDWord;
end;
// TInt64BitIndex is already defined
TInt64NibbleIndex = TQwordNibbleIndex;
TInt64ByteIndex = TQwordByteIndex;
TInt64WordIndex = TQwordWordIndex;
TInt64DwordIndex = TQwordDwordIndex;
TInt64Overlay = TQwordOverlay;
TInt64Helper = Type Helper for Int64
const
MaxValue = High(int64);
MinValue = Low(int64);
MaxBit = High(TInt64BitIndex);
MinBit = Low(TInt64BitIndex);
MaxNibble = High(TInt64NibbleIndex);
MinNibble = Low(TInt64NibbleIndex);
MaxByte = High(TInt64ByteIndex);
MinByte = Low(TInt64ByteIndex);
MaxWord = High(TInt64WordIndex);
MinWord = Low(TInt64WordIndex);
MaxDword = High(TInt64DwordIndex);
MinDword = Low(TInt64DwordIndex);
public
const
MaxValue = 9223372036854775807;
MinValue = -9223372036854775808;
Public
Class Function Parse(const AString: string): Int64; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: Int64): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: Int64): Boolean; inline; static;
Public
protected
function GetBit(const aIndex: TInt64BitIndex): boolean; inline;
procedure PutBit(const aIndex: TInt64BitIndex; const aNewValue: boolean); inline;
function GetNibble(const aIndex: TInt64NibbleIndex): nibble; inline;
procedure PutNibble(const aIndex: TInt64NibbleIndex; const aNewValue: nibble); inline;
function GetByte(const aIndex: TInt64ByteIndex): byte;
procedure PutByte(const aIndex: TInt64ByteIndex; const aNewValue: byte);
function GetWord(const aIndex: TInt64WordIndex): word;
procedure PutWord(const aIndex: TInt64WordIndex; const aNewValue: word);
function GetDword(const aIndex: TInt64DwordIndex): dword;
procedure PutDword(const aIndex: TInt64DwordIndex; const aNewValue: dword);
public
Function ToBoolean: Boolean; inline;
Function ToDouble: Double; inline;
Function ToExtended: Extended; inline;
@ -553,45 +835,83 @@ Type
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;
Function TestBit(const Index: TInt64BitIndex): Boolean; inline;
procedure Clear; inline;
function HighestSetBitPos: cardinal; inline;
function LowestSetBitPos: cardinal; inline;
function SetBitsCount: byte; inline;
property Bits [aIndex: TInt64BitIndex]: boolean read GetBit write PutBit;
property Nibbles[aIndex: TInt64NibbleIndex]: nibble read GetNibble write PutNibble;
property Bytes [aIndex: TInt64ByteIndex]: byte read GetByte write PutByte;
property Words [aIndex: TInt64WordIndex]: word read GetWord write PutWord;
property DWords [aIndex: TInt64DwordIndex]: dword read GetDWord write PutDWord;
end;
TQWordHelper = Type Helper for QWord
public
const
MaxValue = 18446744073709551615;
MinValue = 0;
Public
Class Function Parse(const AString: string): QWord; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: QWord): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: QWord): Boolean; inline; static;
Public
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;
// TNativeIntBitIndex is already defined
{$ifdef cpu16}
TNativeIntNibbleIndex = TSmallIntNibbleIndex;
TNativeIntByteIndex = TSmallIntByteIndex;
TNativeIntOverlay = TSmallIntOverlay;
{$endif}
{$ifdef cpu32}
TNativeIntNibbleIndex = TIntegerNibbleIndex;
TNativeIntByteIndex = TIntegerByteIndex;
TNativeIntWordIndex = TIntegerWordIndex;
TNativeIntOverlay = TIntegerOverlay;
{$endif}
{$ifdef cpu64}
TNativeIntNibbleIndex = TInt64NibbleIndex;
TNativeIntByteIndex = TInt64ByteIndex;
TNativeIntWordIndex = TInt64WordIndex;
TNativeIntDwordIndex = TInt64DWordIndex;
TNativeIntOverlay = TInt64Overlay;
{$endif}
TNativeIntHelper = Type Helper for NativeInt
const
MaxValue = High(NativeInt);
MinValue = Low(NativeInt);
MaxBit = High(TNativeIntBitIndex);
MinBit = Low(TNativeIntBitIndex);
MaxNibble = High(TNativeIntNibbleIndex);
MinNibble = Low(TNativeIntNibbleIndex);
// NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need MaxByte and MinByte
MaxByte = High(TNativeIntByteIndex);
MinByte = Low(TNativeIntByteIndex);
{$ifdef cpu32}
MaxWord = High(TNativeIntWordIndex);
MinWord = Low(TNativeIntWordIndex);
{$endif}
{$ifdef cpu64}
MaxWord = High(TNativeIntWordIndex);
MinWord = Low(TNativeIntWordIndex);
MaxDword = High(TNativeIntDwordIndex);
MinDword = Low(TNativeIntDwordIndex);
{$endif}
public
const
MaxValue = High(NativeInt);
MinValue = Low(NativeInt);
Public
Class Function Parse(const AString: string): NativeInt; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: NativeInt): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: NativeInt): Boolean; inline; static;
Public
protected
function GetBit(const aIndex: TNativeIntBitIndex): boolean; inline;
procedure PutBit(const aIndex: TNativeIntBitIndex; const aNewValue: boolean); inline;
function GetNibble(const aIndex: TNativeIntNibbleIndex): nibble; inline;
procedure PutNibble(const aIndex: TNativeIntNibbleIndex; const aNewValue: nibble); inline;
// NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need GetByte and PutByte
function GetByte(const aIndex: TNativeIntByteIndex): byte;
procedure PutByte(const aIndex: TNativeIntByteIndex; const aNewValue: byte);
{$ifdef cpu32}
function GetWord(const aIndex: TNativeIntWordIndex): word;
procedure PutWord(const aIndex: TNativeIntWordIndex; const aNewValue: word);
{$endif}
{$ifdef cpu64}
function GetWord(const aIndex: TNativeIntWordIndex): word;
procedure PutWord(const aIndex: TNativeIntWordIndex; const aNewValue: word);
function GetDword(const aIndex: TNativeIntDwordIndex): dword;
procedure PutDword(const aIndex: TNativeIntDwordIndex; const aNewValue: dword);
{$endif}
public
Function ToBoolean: Boolean; inline;
Function ToDouble: Double; inline;
Function ToExtended: Extended; inline;
@ -604,19 +924,88 @@ Type
Function ClearBit(const Index: TNativeIntBitIndex) : NativeInt; inline;
Function ToggleBit(const Index: TNativeIntBitIndex) : NativeInt; inline;
Function TestBit(const Index:TNativeIntBitIndex):Boolean; inline;
procedure Clear; inline;
function HighestSetBitPos: cardinal; inline;
function LowestSetBitPos: cardinal; inline;
function SetBitsCount: byte; inline;
property Bits [aIndex: TNativeIntBitIndex]: boolean read GetBit write PutBit;
property Nibbles [aIndex: TNativeIntNibbleIndex]: nibble read GetNibble write PutNibble;
// NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need Byte[]
property Bytes [aIndex: TNativeIntByteIndex]: byte read GetByte write PutByte;
{$ifdef cpu32}
property Words [aIndex: TNativeIntWordIndex]: word read GetWord write PutWord;
{$endif}
{$ifdef cpu64}
property Words [aIndex: TNativeIntWordIndex]: word read GetWord write PutWord;
property DWords [aIndex: TNativeIntDwordIndex]: dword read GetDWord write PutDWord;
{$endif}
end;
// TNativeUIntBitIndex is already defined
{$ifdef cpu16}
TNativeUIntNibbleIndex = TWordNibbleIndex;
TNativeUIntByteIndex = TWordByteIndex;
TNativeUIntOverlay = TWordOverlay;
{$endif}
{$ifdef cpu32}
TNativeUIntNibbleIndex = TDwordNibbleIndex;
TNativeUIntByteIndex = TDwordByteIndex;
TNativeUIntWordIndex = TDwordWordIndex;
TNativeUIntOverlay = TDwordOverlay;
{$endif}
{$ifdef cpu64}
TNativeUIntNibbleIndex = TQwordNibbleIndex;
TNativeUIntByteIndex = TQwordByteIndex;
TNativeUIntWordIndex = TQwordWordIndex;
TNativeUIntDwordIndex = TQwordDWordIndex;
TNativeUIntOverlay = TQwordOverlay;
{$endif}
TNativeUIntHelper = Type Helper for NativeUInt
const
MaxValue = High(NativeUInt);
MinValue = 0;
MaxBit = High(TNativeUIntBitIndex);
MinBit = Low(TNativeUIntBitIndex);
MaxNibble = High(TNativeUIntNibbleIndex);
MinNibble = Low(TNativeUIntNibbleIndex);
// NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need MaxByte and MinByte
MaxByte = High(TNativeUIntByteIndex);
MinByte = Low(TNativeUIntByteIndex);
{$ifdef cpu32}
MaxWord = High(TNativeUIntWordIndex);
MinWord = Low(TNativeUIntWordIndex);
{$endif}
{$ifdef cpu64}
MaxWord = High(TNativeUIntWordIndex);
MinWord = Low(TNativeUIntWordIndex);
MaxDword = High(TNativeUIntDwordIndex);
MinDword = Low(TNativeUIntDwordIndex);
{$endif}
public
const
MaxValue = High(NativeUInt);
MinValue = 0;
Public
Class Function Parse(const AString: string): NativeUInt; inline; static;
Class Function Size: Integer; inline; static;
Class Function ToString(const AValue: NativeUInt): string; overload; inline; static;
Class Function TryParse(const AString: string; out AValue: NativeUInt): Boolean; inline; static;
Public
protected
function GetBit(const aIndex: TNativeUIntBitIndex): boolean; inline;
procedure PutBit(const aIndex: TNativeUIntBitIndex; const aNewValue: boolean); inline;
function GetNibble(const aIndex: TNativeUIntNibbleIndex): nibble; inline;
procedure PutNibble(const aIndex: TNativeUIntNibbleIndex; const aNewValue: nibble); inline;
// NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need GetByte and PutByte
function GetByte(const aIndex: TNativeUIntByteIndex): byte;
procedure PutByte(const aIndex: TNativeUIntByteIndex; const aNewValue: byte);
{$ifdef cpu32}
function GetWord(const aIndex: TNativeUIntWordIndex): word;
procedure PutWord(const aIndex: TNativeUIntWordIndex; const aNewValue: word);
{$endif}
{$ifdef cpu64}
function GetWord(const aIndex: TNativeUIntWordIndex): word;
procedure PutWord(const aIndex: TNativeUIntWordIndex; const aNewValue: word);
function GetDword(const aIndex: TNativeUIntDwordIndex): dword;
procedure PutDword(const aIndex: TNativeUIntDwordIndex; const aNewValue: dword);
{$endif}
public
Function ToBoolean: Boolean; inline;
Function ToDouble: Double; inline;
Function ToExtended: Extended; inline;
@ -629,6 +1018,21 @@ Type
Function ClearBit(const Index: TNativeUIntBitIndex): NativeUint; inline;
Function ToggleBit(const Index: TNativeUIntBitIndex) : NativeUint; inline;
Function TestBit(const Index:TNativeUIntBitIndex) :Boolean; inline;
procedure Clear; inline;
function HighestSetBitPos: cardinal; inline;
function LowestSetBitPos: cardinal; inline;
function SetBitsCount: byte; inline;
property Bits [aIndex: TNativeUIntBitIndex]: boolean read GetBit write PutBit;
property Nibbles [aIndex: TNativeUIntNibbleIndex]: nibble read GetNibble write PutNibble;
// NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need Byte[]
property Bytes [aIndex: TNativeUIntByteIndex]: byte read GetByte write PutByte;
{$ifdef cpu32}
property Words [aIndex: TNativeUIntWordIndex]: word read GetWord write PutWord;
{$endif}
{$ifdef cpu64}
property Words [aIndex: TNativeUIntWordIndex]: word read GetWord write PutWord;
property DWords [aIndex: TNativeUIntDwordIndex]: dword read GetDWord write PutDWord;
{$endif}
end;
{$SCOPEDENUMS ON}

View File

@ -106,3 +106,117 @@ Function TORDINALHELPER.TestBit(const Index: TORDINALBITINDEX):Boolean; inline;
begin
Result := (Self and TORDINALTYPE((TORDINALTYPE(1) shl index)))<>0;
end;
procedure TORDINALHELPER.Clear;
begin
Self := 0;
end;
function TORDINALHELPER.HighestSetBitPos: cardinal;
begin
{$ifdef TORDINALTYPESIZE1}
Result := BsrByte(byte(Self));
{$else}
{$ifdef TORDINALTYPESIZE2}
Result := BsrWord(word(Self));
{$else}
{$ifdef TORDINALTYPESIZE4}
Result := BsrDWord(dword(Self));
{$else} // TORDINALTYPESIZE8
Result := BsrQWord(qword(Self));
{$endif}
{$endif}
{$endif}
end;
function TORDINALHELPER.LowestSetBitPos: cardinal;
begin
{$ifdef TORDINALTYPESIZE1}
Result := BsfByte(byte(Self));
{$else}
{$ifdef TORDINALTYPESIZE2}
Result := BsfWord(word(Self));
{$else}
{$ifdef TORDINALTYPESIZE4}
Result := BsfDWord(dword(Self));
{$else} // TORDINALTYPESIZE8
Result := BsfQWord(qword(Self));
{$endif}
{$endif}
{$endif}
end;
function TORDINALHELPER.SetBitsCount: byte;
begin
{$ifdef TORDINALTYPESIZE1}
Result := PopCnt(byte(Self));
{$else}
{$ifdef TORDINALTYPESIZE2}
Result := PopCnt(word(Self));
{$else}
{$ifdef TORDINALTYPESIZE4}
Result := PopCnt(dword(Self));
{$else} // TORDINALTYPESIZE8
Result := PopCnt(qword(Self));
{$endif}
{$endif}
{$endif}
end;
function TORDINALHELPER.GetBit(const aIndex: TORDINALBITINDEX): boolean;
begin
Result := ((Self shr aIndex) and TORDINALTYPE(1)) = TORDINALTYPE(1);
end;
procedure TORDINALHELPER.PutBit(const aIndex: TORDINALBITINDEX; const aNewValue: boolean);
begin
Self := Self or (TORDINALTYPE(1) shl aIndex) xor (TORDINALTYPE(not aNewValue) shl aIndex);
end;
function TORDINALHELPER.GetNibble(const aIndex: TORDINALNIBBLEINDEX): nibble;
begin
Result := TORDINALOVERLAY(Self).AsNibble[aIndex];
end;
procedure TORDINALHELPER.PutNibble(const aIndex: TORDINALNIBBLEINDEX; const aNewValue: nibble);
begin
TORDINALOVERLAY(Self).AsNibble[aIndex] := aNewValue;
end;
{$ifndef TORDINALTYPESIZE1} // TWordHelper, TDWordHelper, TQWordHelper jump in here (and others with 2, 4 and 8 bytes)
function TORDINALHELPER.GetByte(const aIndex: TORDINALBYTEINDEX): byte;
begin
Result := TORDINALOVERLAY(Self).AsByte[aIndex];
end;
procedure TORDINALHELPER.PutByte(const aIndex: TORDINALBYTEINDEX; const aNewValue: byte);
begin
TORDINALOVERLAY(Self).AsByte[aIndex] := aNewValue;
end;
{$ifndef TORDINALTYPESIZE2} // TDWordHelper, TQWordHelper jump in here (and others with 4 and 8 bytes)
function TORDINALHELPER.GetWord(const aIndex: TORDINALWORDINDEX): word;
begin
Result := TORDINALOVERLAY(Self).AsWord[aIndex];
end;
procedure TORDINALHELPER.PutWord(const aIndex: TORDINALWORDINDEX; const aNewValue: word);
begin
TORDINALOVERLAY(Self).AsWord[aIndex] := aNewValue;
end;
{$ifndef TORDINALTYPESIZE4} // TQWordHelper jumps in here (and others with 8 bytes)
function TORDINALHELPER.GetDword(const aIndex: TORDINALDWORDINDEX): dword;
begin
Result := TORDINALOVERLAY(Self).AsDword[aIndex];
end;
procedure TORDINALHELPER.PutDword(const aIndex: TORDINALDWORDINDEX; const aNewValue: dword);
begin
TORDINALOVERLAY(Self).AsDword[aIndex] := aNewValue;
end;
{$endif}
{$endif}
{$endif}