summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack Pappas <jackp@server.fake>2014-09-14 17:57:16 -0400
committerJack Pappas <jackp@server.fake>2014-09-14 17:57:16 -0400
commit71992cb562c7432a2a836982cbc7764fa062af63 (patch)
treeeeae78b0435fe18d5dad179a36ebbabdaea844ec
parent50893291621658f355bc5b4d450a8d06a563053d (diff)
downloadProject-Tick-71992cb562c7432a2a836982cbc7764fa062af63.tar.gz
Project-Tick-71992cb562c7432a2a836982cbc7764fa062af63.zip
Use compiler intrinsics for the ZSWAP32 macro to take advantage of hardware support when available.
-rw-r--r--zutil.h25
1 files changed, 24 insertions, 1 deletions
diff --git a/zutil.h b/zutil.h
index 24ab06b1cf..d6a06dc6bc 100644
--- 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 */