Hello,
Here is my fast (latency 3) get bit routine:
Enjoy ! ;) =D
// *** Begin of Code ***
program Project1;
{$APPTYPE CONSOLE}
{
Skybuck presents FastGetBit
version 0.01 created on 4 may 2008 by Skybuck Flying.
This routine has just 3 latency on AMD X2 3800+.
Ofcourse it also has some call and ret latency as usual.
It is limited to bit positions 0 to 31.
Could still be a nice and interesting function or just asm code
demonstration
when having to deal with these kinds of limited situations ! ;) =D
I wish Delphi sup****ted something like this in high level language !
Would be nice ! =D and fast too ! ;) :)
Currentl Delphi compiler is limited to "test" and "set byte" instruction
for
set of bit enum.
which kinda sux anyway... special/fast bit operator/indexing would be
nice.
}
uses
SysUtils;
// returns true (1) if bit position set, or false(0) if bit position not
set.
// 3 latency plus ofcourse call + ret latency ;)
// bit position range: 0 to 31
function FastGetBit( Value : longword; BitPosition : longword ) : boolean;
asm
bt eax, edx // latency: 1
mov eax, 0 // latency: 1
adc eax, 0 // latency: 1
end;
// when you just wanna get 0 and 1 and not a boolean.
function FastGetBitInt( Value : longword; BitPosition : longword ) :
longword;
asm
bt eax, edx // latency: 1
mov eax, 0 // latency: 1
adc eax, 0 // latency: 1
end;
procedure Main;
var
B : boolean;
L : longword;
begin
B := false;
writeln( longword( B ) ); // 0
B := true;
writeln( longword( B ) ); // 1
L := 2147483648;
if FastGetBit( L, 31 ) then
begin
writeln( 'Bit Position is set');
end;
end;
begin
try
Main;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
readln;
end.
// *** End of Code ***
Bye,
Skybuck.


|