Re: A population of population counts

From: Thomas Munro <thomas(dot)munro(at)enterprisedb(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: David Rowley <david(dot)rowley(at)2ndquadrant(dot)com>, Pg Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: A population of population counts
Date: 2016-05-08 23:46:46
Message-ID: CAEepm=3g1_fjJGp38QGv+38BC2HHVkzUq6s69nk3mWLgPHqC3A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, May 7, 2016 at 4:26 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> David Rowley <david(dot)rowley(at)2ndquadrant(dot)com> writes:
>> I'd like to see us using those functions, when they're available and
>> falling back on the array when they're not. Sounds like that would
>> just be a new configure test. Perhaps a good home for some shared code
>> would be numutils.c.
>
> Meh --- numutils.c is about numbers. Maybe "bitutils.c"?
>
> Another point here is that there might easily be a use-case for such
> functionality in frontend code, so I'd lean towards putting the support in
> src/common if we do this.

I played around with this a bit on the weekend (see rough sketch
attached). The trouble with __builtin_popcount and the POPCNT
instruction is that you only get them if you ask for -msse4.2 or
-mpopcnt, and then you get an illegal instruction trap instead of
sensible fallback behaviour on ancient hardware, so no packager would
be able to do that. So I guess we'd have to use a runtime test like
we do for CRC32 hardware support (the test may actually be identical
since both depend on the SSE4.2 instruction set) and maybe inline
assembler rather the GCC builtin, and that seems a bit over the top
unless someone can show that it's worth it.

My aim with this thread was mainly reducing code duplication and
needless code: perhaps at least the other ideas in the attached
sketch, namely using ffs instead of the rightmost_one_pos table loop
and consolidation of popcount into a reusable API (without trying to
get hardware support) could be worth polishing for the next CF?
Annoyingly, it seems Windows doesn't have POSIX/SUSv2 ffs, though
apparently it can reach that instruction with MSVC intrinsic
_BitScanReverse or MingW __builtin_ffs.

--
Thomas Munro
http://www.enterprisedb.com

Attachment Content-Type Size
popcount-and-rightmostbitpos-table-cleanup.patch application/octet-stream 23.1 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message David Rowley 2016-05-09 00:46:21 Re: parallel.c is not marked as test covered
Previous Message Gavin Flower 2016-05-08 22:29:45 Re: First-draft release notes for next week's back-branch releases