aboutsummaryrefslogtreecommitdiff
path: root/src/port/pg_bitutils_hwpopcnt.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/port/pg_bitutils_hwpopcnt.c')
-rw-r--r--src/port/pg_bitutils_hwpopcnt.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/port/pg_bitutils_hwpopcnt.c b/src/port/pg_bitutils_hwpopcnt.c
new file mode 100644
index 00000000000..516efd586dd
--- /dev/null
+++ b/src/port/pg_bitutils_hwpopcnt.c
@@ -0,0 +1,36 @@
+/*-------------------------------------------------------------------------
+ *
+ * pg_bitutils_hwpopcnt.c
+ * CPU-optimized implementation of pg_popcount variants
+ *
+ * This file must be compiled with a compiler-specific flag to enable the
+ * POPCNT instruction.
+ *
+ * Portions Copyright (c) 2019, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * src/port/pg_bitutils_hwpopcnt.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "port/pg_bitutils.h"
+
+int
+pg_popcount32_hw(uint32 word)
+{
+ return __builtin_popcount(word);
+}
+
+int
+pg_popcount64_hw(uint64 word)
+{
+#if defined(HAVE_LONG_INT_64)
+ return __builtin_popcountl(word);
+#elif defined(HAVE_LONG_LONG_INT_64)
+ return __builtin_popcountll(word);
+#else
+#error must have a working 64-bit integer datatype
+#endif
+}