Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POPCNT in Delphi XE/ XE2 64bit

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

like image 720
user1423467 Avatar asked Nov 04 '22 23:11

user1423467


1 Answers

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;
like image 196
GJ. Avatar answered Nov 09 '22 14:11

GJ.