Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   FreePascal (https://www.delphipraxis.net/74-freepascal/)
-   -   Bitpacked Record (https://www.delphipraxis.net/181694-bitpacked-record.html)

SonnyBoyPro 3. Sep 2014 11:50

Bitpacked Record
 
Hallo zusammen,

hat jemand Erfahrung mit bitpacked record unter FPC ?

Bin gerade am übersetzen der DWrite.h und dabei gibt es folgende Struktur:
Code:
struct DWRITE_LINE_BREAKPOINT
{
    UINT8 breakConditionBefore : 2;
    UINT8 breakConditionAfter  : 2;
    UINT8 isWhitespace         : 1;
    UINT8 isSoftHyphen         : 1;
    UINT8 padding              : 2;
};
Das zu übersetzen ist ja unter Delphi problematisch. J. S. Bladen hat das in seiner Translation so gelöst
Delphi-Quellcode:
TDWrite_LineBreakpoint=record
    LineBreakpointConditions:Byte;
  end;

Da ich mein Projekt mit FPC machen will, die Lib aber auch unter Delphi verwendbar sein soll, würde ich folgende Translation anstreben:
Delphi-Quellcode:
{$IFDEF FPC}
type
    Unsigned_Bit2 = 0 .. (1 shl 2) - 1;
{$ENDIF}

type
    TDWRITE_LINE_BREAKPOINT = {$IFDEF FPC} bitpacked {$ENDIF} record
    {$IFDEF FPC}
        case Integer of
        0:(
        breakConditionBefore: Unsigned_Bit2;
        breakConditionAfter: Unsigned_Bit2;
        isWhitespace: boolean;
        isSoftHyphen: boolean;
        padding: Unsigned_Bit2);
        1: (LineBreakpointConditions: UINT8);
    {$ELSE}
        LineBreakpointConditions: UINT8;
    {$ENDIF}
    end;    
    DWRITE_LINE_BREAKPOINT = TDWRITE_LINE_BREAKPOINT;
    PDWRITE_LINE_BREAKPOINT = ^TDWRITE_LINE_BREAKPOINT;
Ist das so korrekt? Ich hab ein kleines Test-programm geschrieben, dort funktioniert das Setzen und Lesen der Bits, aber funktioniert das in Kombination mit der WinAPI/DirectDraw ? Hat da jemand Erfahrung?
bg

Der schöne Günther 3. Sep 2014 12:06

AW: Bitpacked Record
 
Die Delphi-Sprache gibt das leider wirklich überhaupt nicht her, ich habe das gleiche Problem auch aufgeschoben. Meine erste Idee war alle Felder über einzelne Getter und Setter umzusetzen- Weil auf lange Sicht geht doch da jegliche Übersicht flöten...

himitsu 3. Sep 2014 12:24

AW: Bitpacked Record
 
Jupp, Delphi kann nur Byteweise programmiert werden.
Selbst der Boolean ist ein Byte groß (1x False und 255x True)

Aber über Record-Property kannst du das selber lösen.

Delphi-Quellcode:
type
  TMyRec = record
  private
    function GetBit0: Boolean;
    function GetBool(Index: Integer):      Boolean;
    procedure SetBool(Index: Integer; Value: Boolean);
    function GetByte(Index: Integer):      Byte;
    procedure SetByte(Index: Integer; Value: Byte);
  public
    FFields: LongWord;
    property Bit0: Boolean read GetBit0 write GetBit0;
    property Bit1: Boolean index 1 read GetBool write SetBool;
    property Bit2: Boolean index 2 read GetBool write SetBool;
    property Bit3bis4: Byte index 0302 read GetByte write SetByte; // 2 lang, ab der 3
  end;
Und dann in den Gettern/Settern die gewünschten Bits rausholen.

Dejan Vu 3. Sep 2014 13:03

AW: Bitpacked Record
 
oder einfach nur eine Property (sollte doch so gehen, oder?)
Delphi-Quellcode:
Type
  TByte = Record
  private
    fByte : Byte;
    function GetBits(start,length : Integer) : Byte;
    procedure SetBits(start,length : Integer; value : Byte);
  public
    Property Bits[start,length : integer] : Byte Read GetBits Write SetBits;
  end

himitsu 3. Sep 2014 13:17

AW: Bitpacked Record
 
Joar.

Für einbittige Sachen lässt sich das in meine Struktur auch bereits mit einbinden.
Delphi-Quellcode:
property Bit[idx: Integer]: Boolean read GetBool write SetBool;

Ist ja egal, ob der Index von intern oder extern kommt.

Und intern wird dann einfach mit SHL/SHR/AND/OR rumgefummelt.

JamesTKirk 4. Sep 2014 06:32

AW: Bitpacked Record
 
Zitat:

Zitat von SonnyBoyPro (Beitrag 1270849)
Hallo zusammen,

hat jemand Erfahrung mit bitpacked record unter FPC ?

[...]

Ist das so korrekt? Ich hab ein kleines Test-programm geschrieben, dort funktioniert das Setzen und Lesen der Bits, aber funktioniert das in Kombination mit der WinAPI/DirectDraw ? Hat da jemand Erfahrung

Ja, das ist so korrekt (
Delphi-Quellcode:
BitSizeOf(TDWRITE_LINE_BREAKPOINT)
gibt zum Beispiel korrekterweise 8 zurück). Du musst nur aufpassen, da wir keinerlei Garantie für irgendwelche Ausrichtungen in
Delphi-Quellcode:
bitpacked
Records oder Arrays geben, dass es für dich wirklich korrekt funktioniert (was aktuell zumindest unter i386 jedoch der Fall ist). Außerdem unterstützen solche Typen keine RTTI (sollte in deinem Fall kein Problem sein, aber sicher ist sicher ;) ).
Der WinAPI bzw. DirectDraw ist es dann jedoch egal, ob das Record
Delphi-Quellcode:
bitpacked
ist oder nicht, da ja das Speicherlayout das gleiche ist. Solang die Daten sich alle innerhalb des einen Bytes befinden, solltest du da keine Probleme erhalten.

Ich hab auch mal nen kleinen Test gemacht als Vergleich zwischen dem Zugriff über
Delphi-Quellcode:
bitpacked
und über
Delphi-Quellcode:
inline
Getter/Setter:

Delphi-Quellcode:
program tbitpackedtest;

{$mode objfpc}
{$modeswitch advancedrecords}

{$IFDEF FPC}
type
    Unsigned_Bit2 = 0 .. (1 shl 2) - 1;
{$ENDIF}

type
    TDWRITE_LINE_BREAKPOINT = {$IFDEF FPC} bitpacked {$ENDIF} record
    private
      function GetIsWhitespaceProp: Boolean; inline;
      procedure SetIsWhitespaceProp(aValue: Boolean); inline;
    public
      property IsWhitespaceProp: Boolean read GetIsWhitespaceProp write SetIsWhitespaceProp;
    public
    {$IFDEF FPC}
        case Integer of
        0:(
        breakConditionBefore: Unsigned_Bit2;
        breakConditionAfter: Unsigned_Bit2;
        isWhitespace: boolean;
        isSoftHyphen: boolean;
        padding: Unsigned_Bit2);
        1: (LineBreakpointConditions: UINT8);
    {$ELSE}
        LineBreakpointConditions: UINT8;
    {$ENDIF}
    end;
    DWRITE_LINE_BREAKPOINT = TDWRITE_LINE_BREAKPOINT;
    PDWRITE_LINE_BREAKPOINT = ^TDWRITE_LINE_BREAKPOINT;

function TDWRITE_LINE_BREAKPOINT.GetIsWhitespaceProp: boolean;
begin
  Result := (LineBreakpointConditions and $10) <> 0;
end;

procedure TDWRITE_LINE_BREAKPOINT.SetIsWhitespaceProp(avalue: boolean);
begin
  LineBreakPointConditions := (LineBreakpointConditions and not $10) or (Ord(avalue) shl 4);
end;

var
  b: TDWRITE_LINE_BREAKPOINT;
begin
  Writeln(bitsizeof(TDWRITE_LINE_BREAKPOINT), ' ', sizeof(TDWRITE_LINE_BREAKPOINT));
  b.isWhitespace := True;
  b.isWhitespaceProp := True;
end.
Der generierte Assemblercode ist dabei nahezu identisch:

Code:
# [49] b.isWhitespace := True;
        movzbl U_P$TBITPACKEDTEST_B,%eax
        orl    $16,%eax
        movb   %al,U_P$TBITPACKEDTEST_B
# [50] b.isWhitespaceProp := True;
        movzbl U_P$TBITPACKEDTEST_B,%eax
        andl   $-17,%eax
        orl    $16,%eax
        movb   %al,U_P$TBITPACKEDTEST_B
Wobei du aufpassen musst, da
Delphi-Quellcode:
inline
mit Record Methoden erst mit 2.7.1 korrekt funktioniert (davor musstest du die Unit mit den
Delphi-Quellcode:
inline
Methoden immer frisch mitkompilieren, damit das geklappt hat; hab ich erst vor kurzem gefixt ;) ).

Gruß,
Sven


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:32 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz