From ad5b6a5555f5d2ffa5c717a72da383781a777960 Mon Sep 17 00:00:00 2001
From: John Naylor <john.naylor@2ndquadrant.com>
Date: Tue, 2 Mar 2021 12:03:06 -0400
Subject: [PATCH v1 1/4] Use direct function calls for pg_popcount{32,64}().

Rather than indirect the call to different implementations, Use the
former popcount{32,64}_slow() for all cases.
---
 src/include/port/pg_bitutils.h |  4 +-
 src/port/pg_bitutils.c         | 69 +++-------------------------------
 2 files changed, 8 insertions(+), 65 deletions(-)

diff --git a/src/include/port/pg_bitutils.h b/src/include/port/pg_bitutils.h
index f9b77ec278..2c40784830 100644
--- a/src/include/port/pg_bitutils.h
+++ b/src/include/port/pg_bitutils.h
@@ -208,8 +208,8 @@ pg_ceil_log2_64(uint64 num)
 }
 
 /* Count the number of one-bits in a uint32 or uint64 */
-extern int	(*pg_popcount32) (uint32 word);
-extern int	(*pg_popcount64) (uint64 word);
+extern int	pg_popcount32 (uint32 word);
+extern int	pg_popcount64 (uint64 word);
 
 /* Count the number of one-bits in a byte array */
 extern uint64 pg_popcount(const char *buf, int bytes);
diff --git a/src/port/pg_bitutils.c b/src/port/pg_bitutils.c
index 2252021854..5dab793e49 100644
--- a/src/port/pg_bitutils.c
+++ b/src/port/pg_bitutils.c
@@ -116,23 +116,6 @@ const uint8 pg_number_of_ones[256] = {
 #endif
 #endif
 
-static int	pg_popcount32_slow(uint32 word);
-static int	pg_popcount64_slow(uint64 word);
-
-#ifdef USE_POPCNT_ASM
-static bool pg_popcount_available(void);
-static int	pg_popcount32_choose(uint32 word);
-static int	pg_popcount64_choose(uint64 word);
-static int	pg_popcount32_asm(uint32 word);
-static int	pg_popcount64_asm(uint64 word);
-
-int			(*pg_popcount32) (uint32 word) = pg_popcount32_choose;
-int			(*pg_popcount64) (uint64 word) = pg_popcount64_choose;
-#else
-int			(*pg_popcount32) (uint32 word) = pg_popcount32_slow;
-int			(*pg_popcount64) (uint64 word) = pg_popcount64_slow;
-#endif							/* USE_POPCNT_ASM */
-
 #ifdef USE_POPCNT_ASM
 
 /*
@@ -154,46 +137,6 @@ pg_popcount_available(void)
 	return (exx[2] & (1 << 23)) != 0;	/* POPCNT */
 }
 
-/*
- * These functions get called on the first call to pg_popcount32 etc.
- * They detect whether we can use the asm implementations, and replace
- * the function pointers so that subsequent calls are routed directly to
- * the chosen implementation.
- */
-static int
-pg_popcount32_choose(uint32 word)
-{
-	if (pg_popcount_available())
-	{
-		pg_popcount32 = pg_popcount32_asm;
-		pg_popcount64 = pg_popcount64_asm;
-	}
-	else
-	{
-		pg_popcount32 = pg_popcount32_slow;
-		pg_popcount64 = pg_popcount64_slow;
-	}
-
-	return pg_popcount32(word);
-}
-
-static int
-pg_popcount64_choose(uint64 word)
-{
-	if (pg_popcount_available())
-	{
-		pg_popcount32 = pg_popcount32_asm;
-		pg_popcount64 = pg_popcount64_asm;
-	}
-	else
-	{
-		pg_popcount32 = pg_popcount32_slow;
-		pg_popcount64 = pg_popcount64_slow;
-	}
-
-	return pg_popcount64(word);
-}
-
 /*
  * pg_popcount32_asm
  *		Return the number of 1 bits set in word
@@ -224,11 +167,11 @@ __asm__ __volatile__(" popcntq %1,%0\n":"=q"(res):"rm"(word):"cc");
 
 
 /*
- * pg_popcount32_slow
+ * pg_popcount32
  *		Return the number of 1 bits set in word
  */
-static int
-pg_popcount32_slow(uint32 word)
+int
+pg_popcount32(uint32 word)
 {
 #ifdef HAVE__BUILTIN_POPCOUNT
 	return __builtin_popcount(word);
@@ -246,11 +189,11 @@ pg_popcount32_slow(uint32 word)
 }
 
 /*
- * pg_popcount64_slow
+ * pg_popcount64
  *		Return the number of 1 bits set in word
  */
-static int
-pg_popcount64_slow(uint64 word)
+int
+pg_popcount64(uint64 word)
 {
 #ifdef HAVE__BUILTIN_POPCOUNT
 #if defined(HAVE_LONG_INT_64)
-- 
2.22.0

