1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-28 23:42:10 +03:00

Add basic support for using the POPCNT and SSE4.2s LZCNT opcodes

These opcodes have been around in the AMD world since 2007, and 2008 in
the case of intel.  They're supported in GCC and Clang via some __builtin
macros.  The opcodes may be unavailable during runtime, in which case we
fall back on a C-based implementation of the code.  In order to get the
POPCNT instruction we must pass the -mpopcnt option to the compiler.  We
do this only for the pg_bitutils.c file.

David Rowley (with fragments taken from a patch by Thomas Munro)

Discussion: https://postgr.es/m/CAKJS1f9WTAGG1tPeJnD18hiQW5gAk59fQ6WK-vfdAKEHyRg2RA@mail.gmail.com
This commit is contained in:
Alvaro Herrera
2019-02-13 16:10:06 -03:00
parent 754ca99314
commit 711bab1e4d
13 changed files with 912 additions and 167 deletions

View File

@ -378,6 +378,122 @@ fi])# PGAC_C_BUILTIN_OP_OVERFLOW
# PGAC_C_BUILTIN_POPCOUNT
# -------------------------
AC_DEFUN([PGAC_C_BUILTIN_POPCOUNT],
[define([Ac_cachevar], [AS_TR_SH([pgac_cv_popcount])])dnl
AC_CACHE_CHECK([for __builtin_popcount], [Ac_cachevar],
[pgac_save_CFLAGS=$CFLAGS
CFLAGS="$pgac_save_CFLAGS -mpopcnt"
AC_COMPILE_IFELSE([AC_LANG_SOURCE(
[static int x = __builtin_popcount(255);])],
[Ac_cachevar=yes],
[Ac_cachevar=no])
CFLAGS="$pgac_save_CFLAGS"])
if test x"$Ac_cachevar" = x"yes"; then
CFLAGS_POPCNT="-mpopcnt"
AC_DEFINE(HAVE__BUILTIN_POPCOUNT, 1,
[Define to 1 if your compiler understands __builtin_popcount.])
fi
undefine([Ac_cachevar])dnl
])# PGAC_C_BUILTIN_POPCOUNT
# PGAC_C_BUILTIN_POPCOUNTL
# -------------------------
AC_DEFUN([PGAC_C_BUILTIN_POPCOUNTL],
[define([Ac_cachevar], [AS_TR_SH([pgac_cv_popcountl])])dnl
AC_CACHE_CHECK([for __builtin_popcountl], [Ac_cachevar],
[pgac_save_CFLAGS=$CFLAGS
CFLAGS="$pgac_save_CFLAGS -mpopcnt"
AC_COMPILE_IFELSE([AC_LANG_SOURCE(
[static int x = __builtin_popcountl(255);])],
[Ac_cachevar=yes],
[Ac_cachevar=no])
CFLAGS="$pgac_save_CFLAGS"])
if test x"$Ac_cachevar" = x"yes"; then
CFLAGS_POPCNT="-mpopcnt"
AC_DEFINE(HAVE__BUILTIN_POPCOUNTL, 1,
[Define to 1 if your compiler understands __builtin_popcountl.])
fi
undefine([Ac_cachevar])dnl
])# PGAC_C_BUILTIN_POPCOUNTL
# PGAC_C_BUILTIN_CTZ
# -------------------------
# Check if the C compiler understands __builtin_ctz(),
# and define HAVE__BUILTIN_CTZ if so.
AC_DEFUN([PGAC_C_BUILTIN_CTZ],
[AC_CACHE_CHECK(for __builtin_ctz, pgac_cv__builtin_ctz,
[AC_COMPILE_IFELSE([AC_LANG_SOURCE(
[static int x = __builtin_ctz(256);]
)],
[pgac_cv__builtin_ctz=yes],
[pgac_cv__builtin_ctz=no])])
if test x"$pgac_cv__builtin_ctz" = xyes ; then
AC_DEFINE(HAVE__BUILTIN_CTZ, 1,
[Define to 1 if your compiler understands __builtin_ctz.])
fi])# PGAC_C_BUILTIN_CTZ
# PGAC_C_BUILTIN_CTZL
# -------------------------
# Check if the C compiler understands __builtin_ctzl(),
# and define HAVE__BUILTIN_CTZL if so.
AC_DEFUN([PGAC_C_BUILTIN_CTZL],
[AC_CACHE_CHECK(for __builtin_ctzl, pgac_cv__builtin_ctzl,
[AC_COMPILE_IFELSE([AC_LANG_SOURCE(
[static int x = __builtin_ctzl(256);]
)],
[pgac_cv__builtin_ctzl=yes],
[pgac_cv__builtin_ctzl=no])])
if test x"$pgac_cv__builtin_ctzl" = xyes ; then
AC_DEFINE(HAVE__BUILTIN_CTZL, 1,
[Define to 1 if your compiler understands __builtin_ctzl.])
fi])# PGAC_C_BUILTIN_CTZL
# PGAC_C_BUILTIN_CLZ
# -------------------------
# Check if the C compiler understands __builtin_clz(),
# and define HAVE__BUILTIN_CLZ if so.
AC_DEFUN([PGAC_C_BUILTIN_CLZ],
[AC_CACHE_CHECK(for __builtin_clz, pgac_cv__builtin_clz,
[AC_COMPILE_IFELSE([AC_LANG_SOURCE(
[static int x = __builtin_clz(256);]
)],
[pgac_cv__builtin_clz=yes],
[pgac_cv__builtin_clz=no])])
if test x"$pgac_cv__builtin_clz" = xyes ; then
AC_DEFINE(HAVE__BUILTIN_CLZ, 1,
[Define to 1 if your compiler understands __builtin_clz.])
fi])# PGAC_C_BUILTIN_CLZ
# PGAC_C_BUILTIN_CLZL
# -------------------------
# Check if the C compiler understands __builtin_clzl(),
# and define HAVE__BUILTIN_CLZL if so.
AC_DEFUN([PGAC_C_BUILTIN_CLZL],
[AC_CACHE_CHECK(for __builtin_clzl, pgac_cv__builtin_clzl,
[AC_COMPILE_IFELSE([AC_LANG_SOURCE(
[static int x = __builtin_clzl(256);]
)],
[pgac_cv__builtin_clzl=yes],
[pgac_cv__builtin_clzl=no])])
if test x"$pgac_cv__builtin_clzl" = xyes ; then
AC_DEFINE(HAVE__BUILTIN_CLZL, 1,
[Define to 1 if your compiler understands __builtin_clzl.])
fi])# PGAC_C_BUILTIN_CLZL
# PGAC_C_BUILTIN_UNREACHABLE
# --------------------------
# Check if the C compiler understands __builtin_unreachable(),