Saturday, April 30, 2011

Bit functions

 I've been adding new functions to BB.Utils.Bits, some of this functions cannot be that fast with the funcionality from a high level language:


function TBit.PopFirstBitSet(var aValue: integer): integer;
//In = [EDX] Out = EAX
ASM
  PUSH ECX
  MOV ECX, [EDX]
  BSF EAX, ECX
  BTR [EDX], EAX
  POP ECX
END;
function TBit.LastBitSet(aValue: integer): integer;
//EDX
ASM
  BSR EAX, EDX
END;
function TBit.FirstBitSet(aValue: integer): integer;
//EDX
ASM
  BSF EAX, EDX
END;
function TBit.BitSetCount(aValue: integer): integer;
begin
  { TODO : optimize with array[0..65535] }
  result := 0;
  while aValue <> 0 do
  begin
    aValue := aValue and (aValue - 1);
    Inc(result);
  end;
end;
function TBit.PopLastBitSet(var aValue: integer): integer;
begin
  result := LastBitSet(aValue);
  _Clear(aValue, result);
end;

No comments:

Post a Comment