From b7eb194c473ca399a8e8afcd279bb3480fe9dacf Mon Sep 17 00:00:00 2001
From: David Fetter <david@fetter.org>
Date: Fri, 31 Jan 2020 08:32:26 -0800
Subject: [PATCH v6 3/3] Reduced operations in floor_log2
To: hackers
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="------------2.24.1"

This is a multi-part message in MIME format.
--------------2.24.1
Content-Type: text/plain; charset=UTF-8; format=fixed
Content-Transfer-Encoding: 8bit


diff --git a/src/backend/utils/adt/array_selfuncs.c b/src/backend/utils/adt/array_selfuncs.c
index d97e60a3ab..12170cb7f2 100644
--- a/src/backend/utils/adt/array_selfuncs.c
+++ b/src/backend/utils/adt/array_selfuncs.c
@@ -20,6 +20,7 @@
 #include "catalog/pg_collation.h"
 #include "catalog/pg_operator.h"
 #include "catalog/pg_statistic.h"
+#include "port/pg_bitutils.h"
 #include "utils/array.h"
 #include "utils/builtins.h"
 #include "utils/lsyscache.h"
@@ -1089,35 +1090,9 @@ calc_distr(const float *p, int n, int m, float rest)
 static int
 floor_log2(uint32 n)
 {
-	int			logval = 0;
-
 	if (n == 0)
 		return -1;
-	if (n >= (1 << 16))
-	{
-		n >>= 16;
-		logval += 16;
-	}
-	if (n >= (1 << 8))
-	{
-		n >>= 8;
-		logval += 8;
-	}
-	if (n >= (1 << 4))
-	{
-		n >>= 4;
-		logval += 4;
-	}
-	if (n >= (1 << 2))
-	{
-		n >>= 2;
-		logval += 2;
-	}
-	if (n >= (1 << 1))
-	{
-		logval += 1;
-	}
-	return logval;
+	return 1 << pg_leftmost_one_pos32(n);
 }
 
 /*

--------------2.24.1--


