[fpc-pascal] PopCount

reyno reynolight at googlemail.com
Sat Apr 27 15:24:57 CEST 2013


Unfortunately, for POPCNT assembler, SSE 4.2 is required. For older 
CPUs, after some tests, I'd finally recommend

function CShift(ANum : QWord) : Cardinal;
var
   n1, n2 : QWord;
begin
   n1 := ANum - (ANum shr 1 and $5555555555555555);
   n2 := (n1 and $3333333333333333) + (n1 shr 2 and $3333333333333333);
   Result :=
     (((n2 + (n2 shr 4)) and $0f0f0f0f0f0f0f0f) * $0101010101010101) shr 56;
end;

What do you do, when it comes to fast pop. counts on vintage CPUs?

The "official" alternative, provided with FPC 2.7.1, seems ca. 10 x 
slower. Did I grab the wrong version?

Is there a even better solution?




More information about the fpc-pascal mailing list