Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: How to do popcount (aka Hamming weight) in Perl

by BrowserUk (Patriarch)
on Sep 24, 2017 at 14:51 UTC ( [id://1199994]=note: print w/replies, xml ) Need Help??


in reply to How to do popcount (aka Hamming weight) in Perl

This (labelled "six" ) is the fastest C/Perl method I found when I was looking a (long) while ago) that works on chips that don't have a popcnt instruction:

#! perl -slw use strict; package SparseBitVector; use Config; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => 'Junk', CLEAN_AFTER_BUILD =>0; typedef unsigned __int64 U64; typedef __int64 I64; unsigned popcnt( SV *sv ) { U64 x = (U64) SvUVX( sv ); x -=( x >> 1 ) & 0x5555555555555555ULL; x = ( x & 0x3333333333333333ULL ) + ( ( x >> 2 ) & 0x3333333333333 +333ULL ); x = ( x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL; return (unsigned)( ( x * 0x0101010101010101ull ) >> 56 ); } END_C use Benchmark qw( cmpthese ); sub popcount1 { my $x = shift; my $count; for ($count = 0; $x; ++$count) { $x &= $x - 1 } return $count; } sub popcount2 { return sprintf('%b', shift) =~ tr/1//; } sub popcount3 { return unpack('%32b*', pack('Q', shift)); } sub popcount4 { return unpack( '%32b*', pack('Q', $_[0] ) ); } my $start = 2**32 - 42; my $end = $start + 1000000; print "sanity test for correctness\n"; for my $i (0 .. 256, $start .. $end) { my $two = popcount2($i); my $three = popcount3($i); my $four = popcount4( $i ); my $six = popcnt( $i ); $three == $two or print "$i: $two $three\n" and die; $four == $six and $three == $six or print "$i: 2:$two 3:$three 6:$s +ix\n" and die; } cmpthese -3, { # One => sub { popcount1($_) for $start .. $end }, Two => sub { popcount2($_) for $start .. $end }, Three => sub { popcount3($_) for $start .. $end }, Four => sub { popcount4($_) for $start .. $end }, Six => sub { popcnt( $_) for $start .. $end }, };

Outputs:

C:\test>1199987.pl sanity test for correctness Rate Two Three Four Six Two 1.02/s -- -4% -5% -58% Three 1.06/s 4% -- -1% -57% Four 1.08/s 5% 1% -- -56% Six 2.45/s 140% 131% 128% --

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Replies are listed 'Best First'.
Re^2: How to do popcount (aka Hamming weight) in Perl
by marioroy (Prior) on Sep 27, 2017 at 07:13 UTC

    If needed, Inline C code supporting 64 and 32 bits.

    use strict; use warnings; use Inline 'C' => Config => BUILD_NOISY => 1, CLEAN_AFTER_BUILD => 0; use Inline 'C' => <<'END_C'; #include <stdint.h> unsigned popcnt2( SV *sv ) { unsigned count = 0; #ifdef __LP64__ static const uint64_t m1 = UINT64_C(0x5555555555555555); static const uint64_t m2 = UINT64_C(0x3333333333333333); static const uint64_t m4 = UINT64_C(0x0f0f0f0f0f0f0f0f); static const uint64_t h01 = UINT64_C(0x0101010101010101); uint64_t x = (uint64_t) SvUVX( sv ); x = x - ((x >> 1) & m1); x = (x & m2) + ((x >> 2) & m2); x = (x + (x >> 4)) & m4; count += (unsigned) ((x * h01) >> 56); #else static const uint32_t m1 = UINT32_C(0x55555555); static const uint32_t m2 = UINT32_C(0x33333333); static const uint32_t m4 = UINT32_C(0x0f0f0f0f); static const uint32_t h01 = UINT32_C(0x01010101); uint32_t x = (uint32_t) SvUVX( sv ); x = x - ((x >> 1) & m1); x = (x & m2) + ((x >> 2) & m2); x = (x + (x >> 4)) & m4; count += (unsigned) ((x * h01) >> 24); #endif return count; } END_C use Benchmark qw( cmpthese ); my $start = 2 ** 32 - 42; my $end = $start + 1000000; cmpthese -3, { Six2 => sub { popcnt2($_) for $start .. $end }, };

    Regards, Mario

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1199994]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found