Chromium Code Reviews| Index: webrtc/base/byteorder.h |
| diff --git a/webrtc/base/byteorder.h b/webrtc/base/byteorder.h |
| index d579e6e185eb73de3ede51bddb809723ff2cfe72..c30711d5c1f7938edcf65810e813cf9816e0c0c9 100644 |
| --- a/webrtc/base/byteorder.h |
| +++ b/webrtc/base/byteorder.h |
| @@ -15,17 +15,60 @@ |
| #include <arpa/inet.h> |
| #endif |
| -#if defined(WEBRTC_WIN) |
| +#include "webrtc/base/basictypes.h" |
|
joachim
2017/03/09 23:36:53
Moved here so the "RTC_ARCH_CPU_" defines are avai
tommi
2017/03/16 16:37:02
Acknowledged.
|
| + |
| +#if defined(WEBRTC_MAC) |
| +#include <libkern/OSByteOrder.h> |
| + |
| +#define htobe16(v) OSSwapHostToBigInt16(v) |
| +#define htobe32(v) OSSwapHostToBigInt32(v) |
| +#define htobe64(v) OSSwapHostToBigInt64(v) |
| +#define be16toh(v) OSSwapBigToHostInt16(v) |
| +#define be32toh(v) OSSwapBigToHostInt32(v) |
| +#define be64toh(v) OSSwapBigToHostInt64(v) |
| + |
| +#define htole16(v) OSSwapHostToLittleInt16(v) |
| +#define htole32(v) OSSwapHostToLittleInt32(v) |
| +#define htole64(v) OSSwapHostToLittleInt64(v) |
| +#define le16toh(v) OSSwapLittleToHostInt16(v) |
| +#define le32toh(v) OSSwapLittleToHostInt32(v) |
| +#define le64toh(v) OSSwapLittleToHostInt64(v) |
| +#elif defined(WEBRTC_POSIX) |
| +#include <endian.h> |
| +#elif defined(WEBRTC_WIN) |
| #include <stdlib.h> |
| +#include <winsock2.h> |
| + |
| +#define htobe16(v) htons(v) |
| +#define htobe32(v) htonl(v) |
| +#define htobe64(v) htonll(v) |
| +#define be16toh(v) ntohs(v) |
| +#define be32toh(v) ntohl(v) |
| +#define be64toh(v) ntohll(v) |
| + |
| +#if defined(RTC_ARCH_CPU_LITTLE_ENDIAN) |
|
joachim
2017/03/09 23:36:53
To simplify things we could assume Windows is alwa
tommi
2017/03/16 16:37:02
Acknowledged.
|
| +#define htole16(v) (v) |
| +#define htole32(v) (v) |
| +#define htole64(v) (v) |
| +#define le16toh(v) (v) |
| +#define le32toh(v) (v) |
| +#define le64toh(v) (v) |
| +#elif defined(RTC_ARCH_CPU_BIG_ENDIAN) |
| +#define htole16(v) __builtin_bswap16(v) |
| +#define htole32(v) __builtin_bswap32(v) |
| +#define htole64(v) __builtin_bswap64(v) |
| +#define le16toh(v) __builtin_bswap16(v) |
| +#define le32toh(v) __builtin_bswap32(v) |
| +#define le64toh(v) __builtin_bswap64(v) |
| +#else |
| +#error RTC_ARCH_CPU_BIG_ENDIAN or RTC_ARCH_CPU_LITTLE_ENDIAN must be defined. |
| #endif |
| -#include "webrtc/base/basictypes.h" |
| +#endif // defined(WEBRTC_WIN) |
| namespace rtc { |
| // Reading and writing of little and big-endian numbers from memory |
| -// TODO: Optimized versions, with direct read/writes of |
| -// integers in host-endian format, when the platform supports it. |
| inline void Set8(void* memory, size_t offset, uint8_t v) { |
| static_cast<uint8_t*>(memory)[offset] = v; |
| @@ -36,129 +79,84 @@ inline uint8_t Get8(const void* memory, size_t offset) { |
| } |
| inline void SetBE16(void* memory, uint16_t v) { |
| - Set8(memory, 0, static_cast<uint8_t>(v >> 8)); |
| - Set8(memory, 1, static_cast<uint8_t>(v >> 0)); |
| + *static_cast<uint16_t*>(memory) = htobe16(v); |
| } |
| inline void SetBE32(void* memory, uint32_t v) { |
| - Set8(memory, 0, static_cast<uint8_t>(v >> 24)); |
| - Set8(memory, 1, static_cast<uint8_t>(v >> 16)); |
| - Set8(memory, 2, static_cast<uint8_t>(v >> 8)); |
| - Set8(memory, 3, static_cast<uint8_t>(v >> 0)); |
| + *static_cast<uint32_t*>(memory) = htobe32(v); |
| } |
| inline void SetBE64(void* memory, uint64_t v) { |
| - Set8(memory, 0, static_cast<uint8_t>(v >> 56)); |
| - Set8(memory, 1, static_cast<uint8_t>(v >> 48)); |
| - Set8(memory, 2, static_cast<uint8_t>(v >> 40)); |
| - Set8(memory, 3, static_cast<uint8_t>(v >> 32)); |
| - Set8(memory, 4, static_cast<uint8_t>(v >> 24)); |
| - Set8(memory, 5, static_cast<uint8_t>(v >> 16)); |
| - Set8(memory, 6, static_cast<uint8_t>(v >> 8)); |
| - Set8(memory, 7, static_cast<uint8_t>(v >> 0)); |
| + *static_cast<uint64_t*>(memory) = htobe64(v); |
| } |
| inline uint16_t GetBE16(const void* memory) { |
| - return static_cast<uint16_t>((Get8(memory, 0) << 8) | (Get8(memory, 1) << 0)); |
| + return be16toh(*static_cast<const uint16_t*>(memory)); |
| } |
| inline uint32_t GetBE32(const void* memory) { |
| - return (static_cast<uint32_t>(Get8(memory, 0)) << 24) | |
| - (static_cast<uint32_t>(Get8(memory, 1)) << 16) | |
| - (static_cast<uint32_t>(Get8(memory, 2)) << 8) | |
| - (static_cast<uint32_t>(Get8(memory, 3)) << 0); |
| + return be32toh(*static_cast<const uint32_t*>(memory)); |
| } |
| inline uint64_t GetBE64(const void* memory) { |
| - return (static_cast<uint64_t>(Get8(memory, 0)) << 56) | |
| - (static_cast<uint64_t>(Get8(memory, 1)) << 48) | |
| - (static_cast<uint64_t>(Get8(memory, 2)) << 40) | |
| - (static_cast<uint64_t>(Get8(memory, 3)) << 32) | |
| - (static_cast<uint64_t>(Get8(memory, 4)) << 24) | |
| - (static_cast<uint64_t>(Get8(memory, 5)) << 16) | |
| - (static_cast<uint64_t>(Get8(memory, 6)) << 8) | |
| - (static_cast<uint64_t>(Get8(memory, 7)) << 0); |
| + return be64toh(*static_cast<const uint64_t*>(memory)); |
| } |
| inline void SetLE16(void* memory, uint16_t v) { |
| - Set8(memory, 0, static_cast<uint8_t>(v >> 0)); |
| - Set8(memory, 1, static_cast<uint8_t>(v >> 8)); |
| + *static_cast<uint16_t*>(memory) = htole16(v); |
| } |
| inline void SetLE32(void* memory, uint32_t v) { |
| - Set8(memory, 0, static_cast<uint8_t>(v >> 0)); |
| - Set8(memory, 1, static_cast<uint8_t>(v >> 8)); |
| - Set8(memory, 2, static_cast<uint8_t>(v >> 16)); |
| - Set8(memory, 3, static_cast<uint8_t>(v >> 24)); |
| + *static_cast<uint32_t*>(memory) = htole32(v); |
| } |
| inline void SetLE64(void* memory, uint64_t v) { |
| - Set8(memory, 0, static_cast<uint8_t>(v >> 0)); |
| - Set8(memory, 1, static_cast<uint8_t>(v >> 8)); |
| - Set8(memory, 2, static_cast<uint8_t>(v >> 16)); |
| - Set8(memory, 3, static_cast<uint8_t>(v >> 24)); |
| - Set8(memory, 4, static_cast<uint8_t>(v >> 32)); |
| - Set8(memory, 5, static_cast<uint8_t>(v >> 40)); |
| - Set8(memory, 6, static_cast<uint8_t>(v >> 48)); |
| - Set8(memory, 7, static_cast<uint8_t>(v >> 56)); |
| + *static_cast<uint64_t*>(memory) = htole64(v); |
| } |
| inline uint16_t GetLE16(const void* memory) { |
| - return static_cast<uint16_t>((Get8(memory, 0) << 0) | (Get8(memory, 1) << 8)); |
| + return le16toh(*static_cast<const uint16_t*>(memory)); |
| } |
| inline uint32_t GetLE32(const void* memory) { |
| - return (static_cast<uint32_t>(Get8(memory, 0)) << 0) | |
| - (static_cast<uint32_t>(Get8(memory, 1)) << 8) | |
| - (static_cast<uint32_t>(Get8(memory, 2)) << 16) | |
| - (static_cast<uint32_t>(Get8(memory, 3)) << 24); |
| + return le32toh(*static_cast<const uint32_t*>(memory)); |
| } |
| inline uint64_t GetLE64(const void* memory) { |
| - return (static_cast<uint64_t>(Get8(memory, 0)) << 0) | |
| - (static_cast<uint64_t>(Get8(memory, 1)) << 8) | |
| - (static_cast<uint64_t>(Get8(memory, 2)) << 16) | |
| - (static_cast<uint64_t>(Get8(memory, 3)) << 24) | |
| - (static_cast<uint64_t>(Get8(memory, 4)) << 32) | |
| - (static_cast<uint64_t>(Get8(memory, 5)) << 40) | |
| - (static_cast<uint64_t>(Get8(memory, 6)) << 48) | |
| - (static_cast<uint64_t>(Get8(memory, 7)) << 56); |
| + return le64toh(*static_cast<const uint64_t*>(memory)); |
| } |
| // Check if the current host is big endian. |
| inline bool IsHostBigEndian() { |
| - static const int number = 1; |
| - return 0 == *reinterpret_cast<const char*>(&number); |
| +#if defined(RTC_ARCH_CPU_BIG_ENDIAN) |
| + return true; |
| +#else |
| + return false; |
| +#endif |
| } |
| inline uint16_t HostToNetwork16(uint16_t n) { |
| - uint16_t result; |
| - SetBE16(&result, n); |
| - return result; |
| + return htobe16(n); |
| } |
| inline uint32_t HostToNetwork32(uint32_t n) { |
| - uint32_t result; |
| - SetBE32(&result, n); |
| - return result; |
| + return htobe32(n); |
| } |
| inline uint64_t HostToNetwork64(uint64_t n) { |
| - uint64_t result; |
| - SetBE64(&result, n); |
| - return result; |
| + return htobe64(n); |
| } |
| inline uint16_t NetworkToHost16(uint16_t n) { |
| - return GetBE16(&n); |
| + return be16toh(n); |
| } |
| inline uint32_t NetworkToHost32(uint32_t n) { |
| - return GetBE32(&n); |
| + return be32toh(n); |
| } |
| inline uint64_t NetworkToHost64(uint64_t n) { |
| - return GetBE64(&n); |
| + return be64toh(n); |
| } |
| } // namespace rtc |