[m-rev.] for review: Check C compiler supports __builtin_bswap functions.

Peter Wang novalazy at gmail.com
Mon Mar 19 14:40:59 AEDT 2018


Fixes bug #445.

configure.ac:
runtime/mercury_conf.h.in:
    Define MR_HAVE_BUILTIN_BSWAP{16,32,64} if __builtin_bswap* are
    supported by the C compiler.

runtime/mercury_int.h:
    Check for MR_HAVE_BUILTIN_BSWAP* before using __builtin_bswap*.
---
 configure.ac              | 44 ++++++++++++++++++++++++++++++++++++++++++++
 runtime/mercury_conf.h.in |  3 +++
 runtime/mercury_int.h     |  6 +++---
 3 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/configure.ac b/configure.ac
index 3d827e8e8..c23a6b560 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1421,6 +1421,50 @@ fi
 
 MERCURY_CHECK_FOR_FENV_FUNC([fesetround], [$MATH_LIB])
 
+#-----------------------------------------------------------------------------#
+#
+# Check whether we have __builtin_bswap builtin functions
+#
+
+AC_MSG_CHECKING(whether we have __builtin_bswap16)
+AC_CACHE_VAL(mercury_cv_have_builtin_bswap16,
+    AC_TRY_LINK(
+        [],
+        [__builtin_bswap16(1);],
+        [mercury_cv_have_builtin_bswap16=yes],
+        [mercury_cv_have_builtin_bswap16=no])
+)
+AC_MSG_RESULT($mercury_cv_have_builtin_bswap16)
+if test "$mercury_cv_have_builtin_bswap16" = yes; then
+    AC_DEFINE(MR_HAVE_BUILTIN_BSWAP16)
+fi
+
+AC_MSG_CHECKING(whether we have __builtin_bswap32)
+AC_CACHE_VAL(mercury_cv_have_builtin_bswap32,
+    AC_TRY_LINK(
+        [],
+        [__builtin_bswap32(1);],
+        [mercury_cv_have_builtin_bswap32=yes],
+        [mercury_cv_have_builtin_bswap32=no])
+)
+AC_MSG_RESULT($mercury_cv_have_builtin_bswap32)
+if test "$mercury_cv_have_builtin_bswap32" = yes; then
+    AC_DEFINE(MR_HAVE_BUILTIN_BSWAP32)
+fi
+
+AC_MSG_CHECKING(whether we have __builtin_bswap64)
+AC_CACHE_VAL(mercury_cv_have_builtin_bswap64,
+    AC_TRY_LINK(
+        [],
+        [__builtin_bswap64(1);],
+        [mercury_cv_have_builtin_bswap64=yes],
+        [mercury_cv_have_builtin_bswap64=no])
+)
+AC_MSG_RESULT($mercury_cv_have_builtin_bswap64)
+if test "$mercury_cv_have_builtin_bswap64" = yes; then
+    AC_DEFINE(MR_HAVE_BUILTIN_BSWAP64)
+fi
+
 #-----------------------------------------------------------------------------#
 #
 # Check the basics of sigaction
diff --git a/runtime/mercury_conf.h.in b/runtime/mercury_conf.h.in
index 8801b6805..b7b613f17 100644
--- a/runtime/mercury_conf.h.in
+++ b/runtime/mercury_conf.h.in
@@ -363,6 +363,9 @@
 #undef  MR_HAVE_FINITE
 #undef  MR_HAVE_FESETROUND
 #undef  MR_HAVE_FMA
+#undef  MR_HAVE_BUILTIN_BSWAP16
+#undef  MR_HAVE_BUILTIN_BSWAP32
+#undef  MR_HAVE_BUILTIN_BSWAP64
 
 // We use mprotect() and signals to catch stack and heap overflows.
 // In order to detect such overflows, we need to be able to figure out
diff --git a/runtime/mercury_int.h b/runtime/mercury_int.h
index bb8fbdf6f..9620953ff 100644
--- a/runtime/mercury_int.h
+++ b/runtime/mercury_int.h
@@ -94,7 +94,7 @@
 extern MR_Integer MR_hash_int64(int64_t);
 extern MR_Integer MR_hash_uint64(uint64_t);
 
-#if defined(MR_GNUC) || defined(MR_CLANG)
+#if defined(MR_HAVE_BUILTIN_BSWAP16)
   #define MR_uint16_reverse_bytes(U) __builtin_bswap16((U))
 #elif defined(MR_MSVC)
   #define MR_uint16_reverse_bytes(U) _byteswap_ushort((U))
@@ -103,7 +103,7 @@ extern MR_Integer MR_hash_uint64(uint64_t);
                                       ((U & 0x00ff) << 8))
 #endif
 
-#if defined(MR_GNUC) || defined(MR_CLANG)
+#if defined(MR_HAVE_BUILTIN_BSWAP32)
   #define MR_uint32_reverse_bytes(U) __builtin_bswap32((U))
 #elif defined(MR_MSVC)
   #define MR_uint32_reverse_bytes(U) _byteswap_ulong((U))
@@ -114,7 +114,7 @@ extern MR_Integer MR_hash_uint64(uint64_t);
                                       (U & UINT32_C(0xff000000)) >> 24 )
 #endif
 
-#if defined(MR_GNUC) || defined(MR_CLANG)
+#if defined(MR_HAVE_BUILTIN_BSWAP64)
   #define MR_uint64_reverse_bytes(U) __builtin_bswap64((U))
 #elif defined(MR_MSVC)
   #define MR_uint64_reverse_bytes(U) _byteswap_uint64((UU))
-- 
2.16.2



More information about the reviews mailing list