Project

General

Profile

Actions

Misc #21690

open

Inconsistent `rb_popcount64()` definition

Misc #21690: Inconsistent `rb_popcount64()` definition

Added by daniel.domjan (Dániel Domján) about 4 hours ago.

Status:
Open
Assignee:
-
[ruby-core:123827]

Description

The rb_popcount64() function is defined both in internal/bits.h and parser_bits.h, but the definition of these functions is different on the #else branch.

// internal/bits.h
static inline unsigned int
rb_popcount64(uint64_t x)
{
...
#else
    x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
    x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
    x = (x & 0x0707070707070707) + (x >> 4 & 0x0707070707070707);
    x = (x & 0x000f000f000f000f) + (x >> 8 & 0x000f000f000f000f);
    x = (x & 0x0000001f0000001f) + (x >>16 & 0x0000001f0000001f);
    x = (x & 0x000000000000003f) + (x >>32 & 0x000000000000003f);
    return (unsigned int)x;

#endif
}
// parser_bits.h
static inline unsigned int
rb_popcount64(uint64_t x)
{
...
#else
    x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
    x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
    x = (x & 0x0707070707070707) + (x >> 4 & 0x0707070707070707);
    x = (x & 0x001f001f001f001f) + (x >> 8 & 0x001f001f001f001f);
    x = (x & 0x0000003f0000003f) + (x >>16 & 0x0000003f0000003f);
    x = (x & 0x000000000000007f) + (x >>32 & 0x000000000000007f);
    return (unsigned int)x;

#endif
}
    x = (x & 0x5555555555555555) + (x >> 1 & 0x5555555555555555);
    x = (x & 0x3333333333333333) + (x >> 2 & 0x3333333333333333);
    x = (x & 0x0707070707070707) + (x >> 4 & 0x0707070707070707);
-   x = (x & 0x000f000f000f000f) + (x >> 8 & 0x000f000f000f000f);
+   x = (x & 0x001f001f001f001f) + (x >> 8 & 0x001f001f001f001f);
-   x = (x & 0x0000001f0000001f) + (x >>16 & 0x0000001f0000001f);
+   x = (x & 0x0000003f0000003f) + (x >>16 & 0x0000003f0000003f);
-   x = (x & 0x000000000000003f) + (x >>32 & 0x000000000000003f);
+   x = (x & 0x000000000000007f) + (x >>32 & 0x000000000000007f);

IIUC, this is a modified hamming weight based on this SO answer, so both implementations give the same result, but it's a bit confusing at first look, so I flagged it as Misc instead of Bug.

No data to display

Actions

Also available in: PDF Atom