123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #ifndef BASE_SYS_BYTEORDER_H_
- #define BASE_SYS_BYTEORDER_H_
- #include <stdint.h>
- #include "build/build_config.h"
- #if defined(COMPILER_MSVC)
- #include <stdlib.h>
- #endif
- namespace base {
- inline uint16_t ByteSwap(uint16_t x) {
- #if defined(COMPILER_MSVC)
- return _byteswap_ushort(x);
- #else
- return __builtin_bswap16(x);
- #endif
- }
- inline uint32_t ByteSwap(uint32_t x) {
- #if defined(COMPILER_MSVC)
- return _byteswap_ulong(x);
- #else
- return __builtin_bswap32(x);
- #endif
- }
- inline uint64_t ByteSwap(uint64_t x) {
- #if defined(COMPILER_MSVC)
- return _byteswap_uint64(x);
- #else
- return __builtin_bswap64(x);
- #endif
- }
- inline uintptr_t ByteSwapUintPtrT(uintptr_t x) {
-
-
-
-
-
- static_assert(sizeof(uintptr_t) == 4 || sizeof(uintptr_t) == 8,
- "Unsupported uintptr_t size");
- if (sizeof(uintptr_t) == 4)
- return ByteSwap(static_cast<uint32_t>(x));
- return ByteSwap(static_cast<uint64_t>(x));
- }
- inline uint16_t ByteSwapToLE16(uint16_t x) {
- #if defined(ARCH_CPU_LITTLE_ENDIAN)
- return x;
- #else
- return ByteSwap(x);
- #endif
- }
- inline uint32_t ByteSwapToLE32(uint32_t x) {
- #if defined(ARCH_CPU_LITTLE_ENDIAN)
- return x;
- #else
- return ByteSwap(x);
- #endif
- }
- inline uint64_t ByteSwapToLE64(uint64_t x) {
- #if defined(ARCH_CPU_LITTLE_ENDIAN)
- return x;
- #else
- return ByteSwap(x);
- #endif
- }
- inline uint16_t NetToHost16(uint16_t x) {
- #if defined(ARCH_CPU_LITTLE_ENDIAN)
- return ByteSwap(x);
- #else
- return x;
- #endif
- }
- inline uint32_t NetToHost32(uint32_t x) {
- #if defined(ARCH_CPU_LITTLE_ENDIAN)
- return ByteSwap(x);
- #else
- return x;
- #endif
- }
- inline uint64_t NetToHost64(uint64_t x) {
- #if defined(ARCH_CPU_LITTLE_ENDIAN)
- return ByteSwap(x);
- #else
- return x;
- #endif
- }
- inline uint16_t HostToNet16(uint16_t x) {
- #if defined(ARCH_CPU_LITTLE_ENDIAN)
- return ByteSwap(x);
- #else
- return x;
- #endif
- }
- inline uint32_t HostToNet32(uint32_t x) {
- #if defined(ARCH_CPU_LITTLE_ENDIAN)
- return ByteSwap(x);
- #else
- return x;
- #endif
- }
- inline uint64_t HostToNet64(uint64_t x) {
- #if defined(ARCH_CPU_LITTLE_ENDIAN)
- return ByteSwap(x);
- #else
- return x;
- #endif
- }
- }
- #endif
|