]> git.ipfire.org Git - thirdparty/zlib-ng.git/commitdiff
Use compiler intrinsics for the ZSWAP32 macro to take advantage of hardware support...
authorJack Pappas <jackp@server.fake>
Sun, 14 Sep 2014 21:57:16 +0000 (17:57 -0400)
committerJack Pappas <jackp@server.fake>
Sun, 14 Sep 2014 21:57:16 +0000 (17:57 -0400)
zutil.h

diff --git a/zutil.h b/zutil.h
index 24ab06b1cf60aeba4ade9ab36ff7ad5f73541960..d6a06dc6bc5b1a5f8f524a85275d3bd59ad661c8 100644 (file)
--- a/zutil.h
+++ b/zutil.h
@@ -246,8 +246,31 @@ extern z_const char * const z_errmsg[10]; /* indexed by 2-zlib_error */
 #define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
 #define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
 
-/* Reverse the bytes in a 32-bit value */
+/* Reverse the bytes in a 32-bit value. Use compiler intrinsics when
+   possible to take advantage of hardware implementations. */
+#if defined(_WIN32) && (_MSC_VER >= 1300)
+#  include <stdlib.h>
+#  pragma intrinsic(_byteswap_ulong)
+#  define ZSWAP32(q) _byteswap_ulong(q)
+
+#elif defined(__Clang__) || (defined(__GNUC__) &&
+        (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)))
+#  define ZSWAP32(q) __builtin_bswap32(q)
+
+#elif defined(__GNUC__) && (__GNUC__ >= 2) && defined(__linux__)
+#  include <byteswap.h>
+#  define ZSWAP32(q) bswap_32(q)
+
+#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
+#  include <sys/endian.h>
+#  define ZSWAP32(q) bswap32(q)
+
+#elif defined(__INTEL_COMPILER)
+#  define ZSWAP32(q) _bswap(q)
+
+#else
 #define ZSWAP32(q) ((((q) >> 24) & 0xff) + (((q) >> 8) & 0xff00) + \
                     (((q) & 0xff00) << 8) + (((q) & 0xff) << 24))
+#endif /* ZSWAP32 */
 
 #endif /* ZUTIL_H */