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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|