pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: Andrew Gierth <andrew@tao11.riddles.org.uk>
Cc: David Rowley <david.rowley@2ndquadrant.com>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Cc: Thomas Munro <thomas.munro@enterprisedb.com>
Subject: Re: Using POPCNT and other advanced bit manipulation instructions
Date: Wed, 13 Feb 2019 19:17:19 -0300
Message-ID: <20190213221719.GA15976@alvherre.pgsql> (raw)
In-Reply-To: <20190213205904.GA13925@alvherre.pgsql>

On 2019-Feb-13, Alvaro Herrera wrote:

> It definitely is ... plans have changed from using IndexOnly scans to
> Seqscans, which is likely fallout from the visibilitymap_count() change.

I think the problem here is that "unsigned long" is 32 bits in this
machine:
  checking whether long int is 64 bits... no

and we have defined pg_popcount64() like this:

static int
pg_popcount64_sse42(uint64 word)
{
	return __builtin_popcountl(word);
}

so it's counting bits in the lower half of the uint64.

If that's correct, then I think we need something like this patch.  But
it makes me wonder whether we need a configure test for
__builtin_popcountll() and friends.  I wonder if there's any compiler
that implements __builtin_popcountl() but not __builtin_popcountll() ...
and if not, then the test for __builtin_popcountl() should be removed,
and have everything rely on the one for __builtin_popcount().

-- 
Álvaro Herrera                https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

Attachments:

  [text/x-diff] uint64-is-longlong.patch (1.2K, ../20190213221719.GA15976@alvherre.pgsql/2-uint64-is-longlong.patch)
  download | inline diff:
diff --git a/src/port/pg_bitutils.c b/src/port/pg_bitutils.c
index 97422e05040..fe6afb9fba5 100644
--- a/src/port/pg_bitutils.c
+++ b/src/port/pg_bitutils.c
@@ -299,7 +299,14 @@ pg_popcount64_choose(uint64 word)
 static int
 pg_popcount64_sse42(uint64 word)
 {
+#if defined(HAVE_LONG_INT_64)
 	return __builtin_popcountl(word);
+#elif defined(HAVE_LONG_LONG_INT_64)
+	return __builtin_popcountll(word);
+#else
+	/* shouldn't happen */
+#error must have a working 64-bit integer datatype
+#endif
 }
 
 #endif
@@ -407,7 +414,14 @@ pg_rightmost_one64_choose(uint64 word)
 static int
 pg_rightmost_one64_abm(uint64 word)
 {
+#if defined(HAVE_LONG_INT_64)
 	return __builtin_ctzl(word);
+#elif defined(HAVE_LONG_LONG_INT_64)
+	return __builtin_ctzll(word);
+#else
+	/* shouldn't happen */
+#error must have a working 64-bit integer datatype
+#endif
 }
 #endif
 
@@ -493,7 +507,15 @@ pg_leftmost_one64_choose(uint64 word)
 static int
 pg_leftmost_one64_abm(uint64 word)
 {
+#if defined(HAVE_LONG_INT_64)
 	return 63 - __builtin_clzl(word);
+#elif defined(HAVE_LONG_LONG_INT_64)
+	return 63 - __builtin_clzll(word);
+#else
+	/* shouldn't happen */
+#error must have a working 64-bit integer datatype
+#endif
+
 }
 #endif
 

view thread (49+ messages)  latest in thread

Message-ID: <20190213221719.GA15976@alvherre.pgsql>
Permalink:  ../20190213221719.GA15976@alvherre.pgsql/
Also on:    postgresql.org/message-id/20190213221719.GA15976@alvherre.pgsql

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: alvherre@2ndquadrant.com, andrew@tao11.riddles.org.uk, david.rowley@2ndquadrant.com, pgsql-hackers@lists.postgresql.org, thomas.munro@enterprisedb.com
  Subject: Re: Using POPCNT and other advanced bit manipulation instructions
  In-Reply-To: <20190213221719.GA15976@alvherre.pgsql>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox