Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vnK4a-00575k-28 for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Feb 2026 17:20:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vnK4Z-006a1K-1y for pgsql-hackers@arkaria.postgresql.org; Tue, 03 Feb 2026 17:20:19 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vnK4Z-006a1B-0B for pgsql-hackers@lists.postgresql.org; Tue, 03 Feb 2026 17:20:19 +0000 Received: from fout-a4-smtp.messagingengine.com ([103.168.172.147]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1vnK4T-00000000ONa-38T6 for pgsql-hackers@postgresql.org; Tue, 03 Feb 2026 17:20:17 +0000 Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailfout.phl.internal (Postfix) with ESMTP id 15015EC0321; Tue, 3 Feb 2026 12:20:13 -0500 (EST) Received: from phl-imap-14 ([10.202.2.87]) by phl-compute-02.internal (MEProxy); Tue, 03 Feb 2026 12:20:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=burd.me; h=cc:cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1770139213; x=1770225613; bh=QPQGchRHn5OA7atK5xma/vWMNIozWcPlUgaNvE2e240=; b= DPSZkP60noALnnPyMYMNkv/9CfHIGwEzikXrusafpWG0zfOiYs72gbiKyzkzswJm ycQfQs3C1j9c0IvpAk1LCH4vTUKDnrvRCC6YIMeBTiJHjoXAaV6oOj70+KApHuNZ UY1fU+vT5ks+sjqsKgRY3AjEkIKPsV7hbEXU/pJeYtL4ZITN1zZSBt5wEKye3Nnl kOw8FUtpGA0hPWdNlQX5jNnwrYh0poGerktmxF0T+hbY5qK0oNHPjLFcpdFDHDti 0KkssvSDm13tDSV4wQa+bztae3jJ3qQjIWi5dG0b38B8vt+Tjm1kRArvyAd8EmPX wBkCPb2cEKZCQ3pPUewCsw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1770139213; x= 1770225613; bh=QPQGchRHn5OA7atK5xma/vWMNIozWcPlUgaNvE2e240=; b=o grK/V6oRyl9g5GjMp+TKCXoc8u6+6t3nXtLDFM+EP01GzLhOV3wDDbAYPIVkXmJN wT4t5PwfRPalWNwSBJ6kK9AuOPJN9WmjxjmYUNLBpqhebOsluD3XGKggKwTQ0BTb Dej4560QsaAyVZk1bGFW732v1jCJT9yrYII5VR5Unhm8iK6/gCdmHiY0PqRJahXY jurDHDB9ozASmyIE4kDr6lN+V/ScQzz4hB2fBiuCMYWR+IJW7HuaCJOd8jURRebJ c53I4mVNFoltdXoYJLEN1MvGmM4fdrLuDor4QcTV2j/55whThJGA9UJlWNpbXhOE txhwe3wVtUAuT5hrCl5lw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddukedtieefucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujf gurhepofggfffhvfevkfgjfhfutgfgsehtjeertdertddtnecuhfhrohhmpedfifhrvghg uceuuhhrugdfuceoghhrvghgsegsuhhrugdrmhgvqeenucggtffrrghtthgvrhhnpeehve efledtueehgffhueelhffhffefteeihfefjeejgeefudegveeludduueffffenucffohhm rghinhepghhouggsohhlthdrohhrghenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehgrhgvghessghurhgurdhmvgdpnhgspghrtghpthhtohep gedpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepjhhohhhntghnrgihlhhorhhlsh esghhmrghilhdrtghomhdprhgtphhtthhopehnrghthhgrnhgusghoshhsrghrthesghhm rghilhdrtghomhdprhgtphhtthhopehhlhhinhhnrghkrgesihhkihdrfhhipdhrtghpth htohepphhgshhqlhdqhhgrtghkvghrshesphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i675e48f3:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id CDE79C4006E; Tue, 3 Feb 2026 12:20:12 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface MIME-Version: 1.0 X-ThreadId: A5gIJecusshw Date: Tue, 03 Feb 2026 12:19:48 -0500 From: "Greg Burd" To: "Nathan Bossart" , "John Naylor" Cc: "Heikki Linnakangas" , pgsql-hackers Message-Id: <3a3ca14a-dada-4920-a614-42f2e5eb3f5d@app.fastmail.com> In-Reply-To: References: Subject: Re: refactor architecture-specific popcount code Content-Type: text/plain Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Feb 2, 2026, at 5:51 PM, Nathan Bossart wrote: > On Mon, Feb 02, 2026 at 09:16:42PM +0700, John Naylor wrote: >> It might be a good idea to do a little new testing, and I see a use >> for a special 8-byte path independent of AVX512: v6 seems to regress a >> little for single-words. But, it turns out that when gcc turns >> __builtin_popcountl into a single instruction, it's inline, but if it >> emits portable bitwise ops, it does so in a function called >> __popcountdi2(). That can be avoided by hand-coding in C for normal >> builds (and for 32-bit looks cleaner anyway), as in the attached 0005. > > Oh, interesting. I looked into this a little more [0]. Both gcc and clang > generate cnt instructions for aarch64, so we're good there. However, clang > on x86-64 generates the bit-twiddling version, and gcc on x86-64 generates > a call to __popcountdi2() (which I imagine does something similar). It's > not until you provide a compiler flag like -march=x86-64-v2 that gcc/clang > start generating popcnt instructions for x86-64, which makes sense. 0005 > seems like the correct move to me... > > [0] https://godbolt.org/z/he3WozG3E > > -- > nathan Nathan, John, Thanks for the focus on this area of the code. I've been looking into what to do with popcnt when building Win11/ARM64/MSVC. I know that when _MSC_VER and _M_ARM64 are defined we can make use of the __popcnt(unsigned int) and __popcnt64(unsigned __int64) intrinsics which have been available since VS 2022 17.11+. I thought I'd check that combo out and it turns out that it is identical to clang/gcc on that platform [0]. I'll wait for your work to land before proposing a patch to add these unless it is really easy to fit it and you feel like giving it a go. :) best. -greg [0] https://godbolt.org/z/TrxjzcPGE