Re: Using POPCNT and other advanced bit manipulation instructions - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Using POPCNT and other advanced bit manipulation instructions
Date
Msg-id 822.1550180738@sss.pgh.pa.us
Whole thread Raw
In response to Re: Using POPCNT and other advanced bit manipulation instructions  (Andres Freund <andres@anarazel.de>)
Responses Re: Using POPCNT and other advanced bit manipulation instructions
Re: Using POPCNT and other advanced bit manipulation instructions
List pgsql-hackers
Andres Freund <andres@anarazel.de> writes:
> On 2019-02-14 15:47:13 -0300, Alvaro Herrera wrote:
>> 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.

No.  If you compile

int myclz(unsigned long long x)
{
  return __builtin_clzll(x);
}

at -O2, on just about any x86_64 gcc, you will get

myclz:
.LFB1:
        .cfi_startproc
        bsrq    %rdi, %rax
        xorq    $63, %rax
        ret
        .cfi_endproc

            regards, tom lane


pgsql-hackers by date:

Previous
From: legrand legrand
Date:
Subject: Re: Planning counters in pg_stat_statements (using pgss_store)
Next
From: Fabien COELHO
Date:
Subject: Re: libpq host/hostaddr/conninfo inconsistencies