Re: Using POPCNT and other advanced bit manipulation instructions

From: Andres Freund <andres(at)anarazel(dot)de>
To: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Thomas Munro <thomas(dot)munro(at)enterprisedb(dot)com>, Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>, David Rowley <david(dot)rowley(at)2ndquadrant(dot)com>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: Using POPCNT and other advanced bit manipulation instructions
Date: 2019-02-14 18:59:05
Message-ID: 20190214185905.kwvrjy6qu52xdxyh@alap3.anarazel.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On 2019-02-14 15:47:13 -0300, Alvaro Herrera wrote:
> On 2019-Feb-14, Tom Lane wrote:
>
> > Some further thoughts here ...
> >
> > Does the "lzcnt" runtime probe actually do anything useful?
> > On the x86_64 compilers I tried (gcc 8.2.1 and 4.4.7), __builtin_clz
> > and __builtin_ctz compile to sequences involving bsrq and bsfq
> > regardless of -mpopcnt. It's fairly hard to see how lzcnt would
> > buy anything over those sequences even if there were zero overhead
> > involved in using it.
>
> Hah, I just realized you have to add -mlzcnt in order for these builtins
> to use the lzcnt instructions. It goes from something like
>
> bsrq %rax, %rax
> xorq $63, %rax

I'm confused how this is a general count leading zero operation? Did you
use constants or something that allowed ot infer a range in the test? If
so the compiler probably did some optimizations allowing it to do the
above.

> to
> lzcntq %rax, %rax
>
> Significant?

If I understand Agner's tables correctly, then no, this isn't faster
than the two instructions above.

Greetings,

Andres Freund

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2019-02-14 19:09:42 Re: Proposal for Signal Detection Refactoring
Previous Message Tom Lane 2019-02-14 18:54:32 Re: Using POPCNT and other advanced bit manipulation instructions