Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guOsR-0002yi-Iv for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Feb 2019 21:49:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1guOsQ-0002qC-9w for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Feb 2019 21:49:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guOpJ-0006vh-87 for pgsql-hackers@lists.postgresql.org; Thu, 14 Feb 2019 21:45:49 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1guOpG-0003kh-PL for pgsql-hackers@lists.postgresql.org; Thu, 14 Feb 2019 21:45:48 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x1ELjcJJ000823; Thu, 14 Feb 2019 16:45:38 -0500 From: Tom Lane To: Andres Freund cc: Alvaro Herrera , Thomas Munro , Andrew Gierth , David Rowley , PostgreSQL Hackers Subject: Re: Using POPCNT and other advanced bit manipulation instructions In-reply-to: <20190214185905.kwvrjy6qu52xdxyh@alap3.anarazel.de> References: <16168.1550121890@sss.pgh.pa.us> <20190214184713.GA6080@alvherre.pgsql> <20190214185905.kwvrjy6qu52xdxyh@alap3.anarazel.de> Comments: In-reply-to Andres Freund message dated "Thu, 14 Feb 2019 10:59:05 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <821.1550180738.1@sss.pgh.pa.us> Date: Thu, 14 Feb 2019 16:45:38 -0500 Message-ID: <822.1550180738@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Andres Freund 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