summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWouter van Oortmerssen <wvo@google.com>2016-08-10 10:09:39 -0700
committerWouter van Oortmerssen <wvo@google.com>2016-08-10 10:49:17 -0700
commitbe908939dae62d1dd128de6538b5492943315706 (patch)
tree5abb3be2223ae5ea549a30514aec3ec9392d61cc
parentc96fc7fcef4b415f561d5e1b6434cb708cbeb592 (diff)
downloadflatbuffers-be908939dae62d1dd128de6538b5492943315706.tar.gz
flatbuffers-be908939dae62d1dd128de6538b5492943315706.tar.bz2
flatbuffers-be908939dae62d1dd128de6538b5492943315706.zip
Fixed __builtin_bswap16 not being available on GCC <4.8
See e.g. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52624 Change-Id: Idae99196c47e015a2ba2416aaf1526f77498ce5d
-rw-r--r--include/flatbuffers/flatbuffers.h30
1 files changed, 16 insertions, 14 deletions
diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h
index 1a690826..4644f9c6 100644
--- a/include/flatbuffers/flatbuffers.h
+++ b/include/flatbuffers/flatbuffers.h
@@ -153,32 +153,34 @@ inline void EndianCheck() {
template<typename T> T EndianSwap(T t) {
#if defined(_MSC_VER)
- #pragma push_macro("__builtin_bswap16")
- #pragma push_macro("__builtin_bswap32")
- #pragma push_macro("__builtin_bswap64")
- #define __builtin_bswap16 _byteswap_ushort
- #define __builtin_bswap32 _byteswap_ulong
- #define __builtin_bswap64 _byteswap_uint64
+ #define FLATBUFFERS_BYTESWAP16 _byteswap_ushort
+ #define FLATBUFFERS_BYTESWAP32 _byteswap_ulong
+ #define FLATBUFFERS_BYTESWAP64 _byteswap_uint64
+ #else
+ #if defined(__GNUC__) && __GNUC__ * 100 + __GNUC_MINOR__ < 408
+ // __builtin_bswap16 was missing prior to GCC 4.8.
+ #define FLATBUFFERS_BYTESWAP16(x) \
+ static_cast<uint16_t>(__builtin_bswap32(static_cast<uint32_t>(x) << 16))
+ #else
+ #define FLATBUFFERS_BYTESWAP16 __builtin_bswap16
+ #endif
+ #define FLATBUFFERS_BYTESWAP32 __builtin_bswap32
+ #define FLATBUFFERS_BYTESWAP64 __builtin_bswap64
#endif
if (sizeof(T) == 1) { // Compile-time if-then's.
return t;
} else if (sizeof(T) == 2) {
- auto r = __builtin_bswap16(*reinterpret_cast<uint16_t *>(&t));
+ auto r = FLATBUFFERS_BYTESWAP16(*reinterpret_cast<uint16_t *>(&t));
return *reinterpret_cast<T *>(&r);
} else if (sizeof(T) == 4) {
- auto r = __builtin_bswap32(*reinterpret_cast<uint32_t *>(&t));
+ auto r = FLATBUFFERS_BYTESWAP32(*reinterpret_cast<uint32_t *>(&t));
return *reinterpret_cast<T *>(&r);
} else if (sizeof(T) == 8) {
- auto r = __builtin_bswap64(*reinterpret_cast<uint64_t *>(&t));
+ auto r = FLATBUFFERS_BYTESWAP64(*reinterpret_cast<uint64_t *>(&t));
return *reinterpret_cast<T *>(&r);
} else {
assert(0);
}
- #if defined(_MSC_VER)
- #pragma pop_macro("__builtin_bswap16")
- #pragma pop_macro("__builtin_bswap32")
- #pragma pop_macro("__builtin_bswap64")
- #endif
}
template<typename T> T EndianScalar(T t) {