POPCNT in Delphi XE / XE2 64bit

How to implement a 1-bit count in a 16/32/64 bit word using the very fast Intel POPCNT instruction in Delphi XE or XE2? Is there a library that provides direct access to this instruction? Can someone write an asm demo section illustrating its use, please? And finally, what are the options for 64-bit Delphi (no asm available)? thanks in advance t

+5
source share
1 answer

As Rob Kennedy said, here you have features for the 32-bit and 64-bit Delphi development environment.

function GetBitCount(num: integer): integer;
asm
  POPCNT    eax, num
end;

function GetBitCount(num: Int64): integer;
asm
  POPCNT    rax, num
end;

EDIT: This is a 32-bit and 64-bit version compatible with Delphi.

{$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;
+2
source

All Articles