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 1guM9h-0003Dl-6I for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Feb 2019 18:54:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1guM9g-00054S-0E for pgsql-hackers@arkaria.postgresql.org; Thu, 14 Feb 2019 18:54:40 +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 1guM9f-000539-LX for pgsql-hackers@lists.postgresql.org; Thu, 14 Feb 2019 18:54:39 +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 1guM9d-0007kF-By for pgsql-hackers@lists.postgresql.org; Thu, 14 Feb 2019 18:54:38 +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 x1EIsW2o025935; Thu, 14 Feb 2019 13:54:32 -0500 From: Tom Lane To: Alvaro Herrera cc: Thomas Munro , Andrew Gierth , David Rowley , PostgreSQL Hackers Subject: Re: Using POPCNT and other advanced bit manipulation instructions In-reply-to: <20190214184713.GA6080@alvherre.pgsql> References: <20190214184713.GA6080@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Thu, 14 Feb 2019 15:47:13 -0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <25933.1550170472.1@sss.pgh.pa.us> Date: Thu, 14 Feb 2019 13:54:32 -0500 Message-ID: <25934.1550170472@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alvaro Herrera writes: > 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 > to > lzcntq %rax, %rax > Significant? I'd bet a fair amount of money that we'd be better off *not* using lzcnt, even if available, because then we could just expose things along this line: static inline int pg_clz(...) { #ifdef HAVE__BUILTIN_CLZ return __builtin_clz(x); #else handwritten implementation; #endif } Avoiding a function call (that has to indirect through a pointer) probably saves much more than the difference between lzcnt and the other way. The tradeoff might be different for popcount, though, especially since it looks like __builtin_popcount() is not nearly as widely available as the clz/ctz builtins. regards, tom lane