Einzelnen Beitrag anzeigen

Hawkeye219

Registriert seit: 18. Feb 2006
Ort: Stolberg
2.227 Beiträge
 
Delphi 2010 Professional
 
#7

Re: "Einsenfüller" in Assembler?

  Alt 16. Dez 2007, 22:26
Hi,

man kann die folgende Routine sicher noch optimieren, für den Anfang sollte sie aber genügen:

Delphi-Quellcode:
function BitCount (Data: Byte): Integer;
const
  Bits : array [0..15] of Byte
       = (0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4);
begin
  Result := Bits[Data shr 4] + Bits[Data and 15];
end;

procedure FillBits (var X; Size, NumBits: Integer);
var
  BitPos : Integer;
  Index : Integer;
  Space : Integer;
  Buffer : array [0..MaxInt - 1] of Byte absolute X;
begin
  for Index := 0 to Size - 1 do
    Dec (NumBits, BitCount(Buffer[Index]));

  Index := 0;
  while ((Index < Size) and (NumBits > 0)) do
    begin
      Space := 8 - BitCount(Buffer[Index]);
      if (NumBits >= Space) then
        Buffer[Index] := $FF
      else
        for BitPos := 0 to 7 do
          if (not Odd(Buffer[Index] shr BitPos)) then
            begin
              Inc (Buffer[Index], 1 shl BitPos);
              Dec (NumBits);
              if (NumBits = 0) then
                Exit;
            end;
      Dec (NumBits, Space);
      Inc (Index);
    end;
end;
Gruß Hawkeye
  Mit Zitat antworten Zitat