In the process of moving some code over to D, I found out I made a mistake in using intrinsic fpc functions which allow you to work much faster for bit operations, now I am attempting to make a unit that has those those functions for D, however, this thread put a spark in me since there is one thing that seems to be a code hog when it comes to bit manipulations.
I've tried Tbits of course and that is just simple over loaded and ok for basic stuff but does not fair well for speed.
So I've come up with some bit functions that maybe FPC should implement as intrinsic so they better fit the ABI in use and insert into the code better.
But here I use the Windows 64 bit ABI.
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
{ ABI in use is for Windows 64 bit}
{ 32 bit would need to use the proper registers}
{ Set Bit and return state of before}
Function BITSET(Const Abase:Byte;Var aValue):Boolean;Assembler; NoStackFrame;
Asm
BTSQ %CX,(%RDX);
SETC %AL;
end;
{Clear bit and return state before}
Function BITCLR(Const Abase:Byte;Var aValue):Boolean;Assembler; NoStackFrame;
Asm
BTRQ %CX,(%RDX);
SETC %AL;
end;
{Complement and return state of before}
Function BITCPT(Const Abase:Byte;Var aValue):Boolean;Assembler; NoStackFrame;
Asm
BTCQ %CX,(%RDX);
SETC %AL;
end;
procedure TForm1.Button1Click(Sender: TObject);
Var
I:LongWord;
begin
I:= 1;
Caption := BITCLR(0,I).ToString+','+I.ToString; //Etc.
end;
end.
ignore the typeless parameter, I do that all the time, it saves on code and its for real men!
if you feel a little nervous about using it, then make some OVERLOADed versions of it.
If implemented along with the other BIT functions that already exist in the FPC, they can then take care of that part.