summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsnappy.mirrorbot@gmail.com <snappy.mirrorbot@gmail.com@03e5f5b5-db94-4691-08a0-1a8bf15f6143>2011-09-15 09:50:05 +0000
committersnappy.mirrorbot@gmail.com <snappy.mirrorbot@gmail.com@03e5f5b5-db94-4691-08a0-1a8bf15f6143>2011-09-15 09:50:05 +0000
commit85ec6d0ce7742833cc8f8d43a8120e42163e655c (patch)
tree38f4f3a6dc27891d5db4772e6a15e46b69111ca2
parent0493f8d450c8b28b455d969e5e1047b7917c6677 (diff)
downloadsnappy-85ec6d0ce7742833cc8f8d43a8120e42163e655c.tar.gz
Fix public issue #50: Include generic byteswap macros.
Also include Solaris 10 and FreeBSD versions. R=csilvers git-svn-id: http://snappy.googlecode.com/svn/trunk@49 03e5f5b5-db94-4691-08a0-1a8bf15f6143
-rw-r--r--configure.ac2
-rw-r--r--snappy-stubs-internal.h40
2 files changed, 40 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac
index 38c4a52..43fa0ed 100644
--- a/configure.ac
+++ b/configure.ac
@@ -18,7 +18,7 @@ AC_SUBST([LIBTOOL_DEPS])
AC_PROG_CXX
AC_LANG([C++])
AC_C_BIGENDIAN
-AC_CHECK_HEADERS([stdint.h stddef.h sys/mman.h sys/resource.h windows.h])
+AC_CHECK_HEADERS([stdint.h stddef.h sys/mman.h sys/resource.h windows.h byteswap.h sys/byteswap.h sys/endian.h])
# Don't use AC_FUNC_MMAP, as it checks for mappings of already-mapped memory,
# which we don't need (and does not exist on Windows).
diff --git a/snappy-stubs-internal.h b/snappy-stubs-internal.h
index cc51e26..0215288 100644
--- a/snappy-stubs-internal.h
+++ b/snappy-stubs-internal.h
@@ -229,6 +229,14 @@ inline void UNALIGNED_STORE64(void *p, uint64 v) {
// The following guarantees declaration of the byte swap functions.
#ifdef WORDS_BIGENDIAN
+#ifdef HAVE_SYS_BYTEORDER_H
+#include <sys/byteorder.h>
+#endif
+
+#ifdef HAVE_SYS_ENDIAN_H
+#include <sys/endian.h>
+#endif
+
#ifdef _MSC_VER
#include <stdlib.h>
#define bswap_16(x) _byteswap_ushort(x)
@@ -242,8 +250,38 @@ inline void UNALIGNED_STORE64(void *p, uint64 v) {
#define bswap_32(x) OSSwapInt32(x)
#define bswap_64(x) OSSwapInt64(x)
-#else
+#elif defined(HAVE_BYTESWAP_H)
#include <byteswap.h>
+
+#elif defined(bswap32)
+// FreeBSD defines bswap{16,32,64} in <sys/endian.h> (already #included).
+#define bswap_16(x) bswap16(x)
+#define bswap_32(x) bswap32(x)
+#define bswap_64(x) bswap64(x)
+
+#elif defined(BSWAP_64)
+// Solaris 10 defines BSWAP_{16,32,64} in <sys/byteorder.h> (already #included).
+#define bswap_16(x) BSWAP_16(x)
+#define bswap_32(x) BSWAP_32(x)
+#define bswap_64(x) BSWAP_64(x)
+
+#else
+
+inline uint16 bswap_16(uint16 x) {
+ return (x << 8) | (x >> 8);
+}
+
+inline uint32 bswap_32(uint32 x) {
+ x = ((x & 0xff00ff00UL) >> 8) | ((x & 0x00ff00ffUL) << 8);
+ return (x >> 16) | (x << 16);
+}
+
+inline uint64 bswap_64(uint64 x) {
+ x = ((x & 0xff00ff00ff00ff00ULL) >> 8) | ((x & 0x00ff00ff00ff00ffULL) << 8);
+ x = ((x & 0xffff0000ffff0000ULL) >> 16) | ((x & 0x0000ffff0000ffffULL) << 16);
+ return (x >> 32) | (x << 32);
+}
+
#endif
#endif // WORDS_BIGENDIAN