Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2013-04

PopCount

1 message · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

reyno

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?