How do I implement a count of 1-bits within a 16/32/64bit word using the very fast Intel POPCNT instruction, under Delphi XE or XE2? Is there a library routine giving direct access to this instruction? Can someone write a demo asm section illustrating its use please? And finally, what are the options for 64bit Delphi (no asm available)? thanks in advance t
As Rob Kennedy sugested, here you have functions for 32bit and 64bit Delphi IDE.
function GetBitCount(num: integer): integer;
asm
POPCNT eax, num
end;
function GetBitCount(num: Int64): integer;
asm
POPCNT rax, num
end;
EDIT: This is 32bit and 64bit Delphi compatible version
{$IF CompilerVersion < 23} //pre-XE2
NativeInt = integer;
{$IFEND}
function GetBitCount(num: NativeInt): integer;
asm
{$IFNDEF CPUX64}
POPCNT eax, num
{$ELSE CPUX64}
POPCNT rax, num
{$ENDIF CPUX64}
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With