Chris@47: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@47: // Licensed under the MIT License: Chris@47: // Chris@47: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@47: // of this software and associated documentation files (the "Software"), to deal Chris@47: // in the Software without restriction, including without limitation the rights Chris@47: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@47: // copies of the Software, and to permit persons to whom the Software is Chris@47: // furnished to do so, subject to the following conditions: Chris@47: // Chris@47: // The above copyright notice and this permission notice shall be included in Chris@47: // all copies or substantial portions of the Software. Chris@47: // Chris@47: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@47: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@47: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@47: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@47: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@47: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@47: // THE SOFTWARE. Chris@47: Chris@47: #ifndef CAPNP_ENDIAN_H_ Chris@47: #define CAPNP_ENDIAN_H_ Chris@47: Chris@47: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) Chris@47: #pragma GCC system_header Chris@47: #endif Chris@47: Chris@47: #include "common.h" Chris@47: #include Chris@47: #include // memcpy Chris@47: Chris@47: namespace capnp { Chris@47: namespace _ { // private Chris@47: Chris@47: // WireValue Chris@47: // Chris@47: // Wraps a primitive value as it appears on the wire. Namely, values are little-endian on the Chris@47: // wire, because little-endian is the most common endianness in modern CPUs. Chris@47: // Chris@47: // Note: In general, code that depends cares about byte ordering is bad. See: Chris@47: // http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html Chris@47: // Cap'n Proto is special because it is essentially doing compiler-like things, fussing over Chris@47: // allocation and layout of memory, in order to squeeze out every last drop of performance. Chris@47: Chris@47: #if _MSC_VER Chris@47: // Assume Windows is little-endian. Chris@47: // Chris@47: // TODO(msvc): This is ugly. Maybe refactor later checks to be based on CAPNP_BYTE_ORDER or Chris@47: // CAPNP_SWAP_BYTES or something, and define that in turn based on _MSC_VER or the GCC Chris@47: // intrinsics. Chris@47: Chris@47: #ifndef __ORDER_BIG_ENDIAN__ Chris@47: #define __ORDER_BIG_ENDIAN__ 4321 Chris@47: #endif Chris@47: #ifndef __ORDER_LITTLE_ENDIAN__ Chris@47: #define __ORDER_LITTLE_ENDIAN__ 1234 Chris@47: #endif Chris@47: #ifndef __BYTE_ORDER__ Chris@47: #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ Chris@47: #endif Chris@47: #endif Chris@47: Chris@47: #if CAPNP_REVERSE_ENDIAN Chris@47: #define CAPNP_WIRE_BYTE_ORDER __ORDER_BIG_ENDIAN__ Chris@47: #define CAPNP_OPPOSITE_OF_WIRE_BYTE_ORDER __ORDER_LITTLE_ENDIAN__ Chris@47: #else Chris@47: #define CAPNP_WIRE_BYTE_ORDER __ORDER_LITTLE_ENDIAN__ Chris@47: #define CAPNP_OPPOSITE_OF_WIRE_BYTE_ORDER __ORDER_BIG_ENDIAN__ Chris@47: #endif Chris@47: Chris@47: #if defined(__BYTE_ORDER__) && \ Chris@47: __BYTE_ORDER__ == CAPNP_WIRE_BYTE_ORDER && \ Chris@47: !CAPNP_DISABLE_ENDIAN_DETECTION Chris@47: // CPU is little-endian. We can just read/write the memory directly. Chris@47: Chris@47: template Chris@47: class DirectWireValue { Chris@47: public: Chris@47: KJ_ALWAYS_INLINE(T get() const) { return value; } Chris@47: KJ_ALWAYS_INLINE(void set(T newValue)) { value = newValue; } Chris@47: Chris@47: private: Chris@47: T value; Chris@47: }; Chris@47: Chris@47: template Chris@47: using WireValue = DirectWireValue; Chris@47: // To prevent ODR problems when endian-test, endian-reverse-test, and endian-fallback-test are Chris@47: // linked together, we define each implementation with a different name and define an alias to the Chris@47: // one we want to use. Chris@47: Chris@47: #elif defined(__BYTE_ORDER__) && \ Chris@47: __BYTE_ORDER__ == CAPNP_OPPOSITE_OF_WIRE_BYTE_ORDER && \ Chris@47: defined(__GNUC__) && !CAPNP_DISABLE_ENDIAN_DETECTION Chris@47: // Big-endian, but GCC's __builtin_bswap() is available. Chris@47: Chris@47: // TODO(perf): Use dedicated instructions to read little-endian data on big-endian CPUs that have Chris@47: // them. Chris@47: Chris@47: // TODO(perf): Verify that this code optimizes reasonably. In particular, ensure that the Chris@47: // compiler optimizes away the memcpy()s and keeps everything in registers. Chris@47: Chris@47: template Chris@47: class SwappingWireValue; Chris@47: Chris@47: template Chris@47: class SwappingWireValue { Chris@47: public: Chris@47: KJ_ALWAYS_INLINE(T get() const) { return value; } Chris@47: KJ_ALWAYS_INLINE(void set(T newValue)) { value = newValue; } Chris@47: Chris@47: private: Chris@47: T value; Chris@47: }; Chris@47: Chris@47: template Chris@47: class SwappingWireValue { Chris@47: public: Chris@47: KJ_ALWAYS_INLINE(T get() const) { Chris@47: // Not all platforms have __builtin_bswap16() for some reason. In particular, it is missing Chris@47: // on gcc-4.7.3-cygwin32 (but present on gcc-4.8.1-cygwin64). Chris@47: uint16_t swapped = (value << 8) | (value >> 8); Chris@47: T result; Chris@47: memcpy(&result, &swapped, sizeof(T)); Chris@47: return result; Chris@47: } Chris@47: KJ_ALWAYS_INLINE(void set(T newValue)) { Chris@47: uint16_t raw; Chris@47: memcpy(&raw, &newValue, sizeof(T)); Chris@47: // Not all platforms have __builtin_bswap16() for some reason. In particular, it is missing Chris@47: // on gcc-4.7.3-cygwin32 (but present on gcc-4.8.1-cygwin64). Chris@47: value = (raw << 8) | (raw >> 8); Chris@47: } Chris@47: Chris@47: private: Chris@47: uint16_t value; Chris@47: }; Chris@47: Chris@47: template Chris@47: class SwappingWireValue { Chris@47: public: Chris@47: KJ_ALWAYS_INLINE(T get() const) { Chris@47: uint32_t swapped = __builtin_bswap32(value); Chris@47: T result; Chris@47: memcpy(&result, &swapped, sizeof(T)); Chris@47: return result; Chris@47: } Chris@47: KJ_ALWAYS_INLINE(void set(T newValue)) { Chris@47: uint32_t raw; Chris@47: memcpy(&raw, &newValue, sizeof(T)); Chris@47: value = __builtin_bswap32(raw); Chris@47: } Chris@47: Chris@47: private: Chris@47: uint32_t value; Chris@47: }; Chris@47: Chris@47: template Chris@47: class SwappingWireValue { Chris@47: public: Chris@47: KJ_ALWAYS_INLINE(T get() const) { Chris@47: uint64_t swapped = __builtin_bswap64(value); Chris@47: T result; Chris@47: memcpy(&result, &swapped, sizeof(T)); Chris@47: return result; Chris@47: } Chris@47: KJ_ALWAYS_INLINE(void set(T newValue)) { Chris@47: uint64_t raw; Chris@47: memcpy(&raw, &newValue, sizeof(T)); Chris@47: value = __builtin_bswap64(raw); Chris@47: } Chris@47: Chris@47: private: Chris@47: uint64_t value; Chris@47: }; Chris@47: Chris@47: template Chris@47: using WireValue = SwappingWireValue; Chris@47: // To prevent ODR problems when endian-test, endian-reverse-test, and endian-fallback-test are Chris@47: // linked together, we define each implementation with a different name and define an alias to the Chris@47: // one we want to use. Chris@47: Chris@47: #else Chris@47: // Unknown endianness. Fall back to bit shifts. Chris@47: Chris@47: #if !CAPNP_DISABLE_ENDIAN_DETECTION Chris@47: #if _MSC_VER Chris@47: #pragma message("Couldn't detect endianness of your platform. Using unoptimized fallback implementation.") Chris@47: #pragma message("Consider changing this code to detect your platform and send us a patch!") Chris@47: #else Chris@47: #warning "Couldn't detect endianness of your platform. Using unoptimized fallback implementation." Chris@47: #warning "Consider changing this code to detect your platform and send us a patch!" Chris@47: #endif Chris@47: #endif // !CAPNP_DISABLE_ENDIAN_DETECTION Chris@47: Chris@47: template Chris@47: class ShiftingWireValue; Chris@47: Chris@47: template Chris@47: class ShiftingWireValue { Chris@47: public: Chris@47: KJ_ALWAYS_INLINE(T get() const) { return value; } Chris@47: KJ_ALWAYS_INLINE(void set(T newValue)) { value = newValue; } Chris@47: Chris@47: private: Chris@47: T value; Chris@47: }; Chris@47: Chris@47: template Chris@47: class ShiftingWireValue { Chris@47: public: Chris@47: KJ_ALWAYS_INLINE(T get() const) { Chris@47: uint16_t raw = (static_cast(bytes[0]) ) | Chris@47: (static_cast(bytes[1]) << 8); Chris@47: T result; Chris@47: memcpy(&result, &raw, sizeof(T)); Chris@47: return result; Chris@47: } Chris@47: KJ_ALWAYS_INLINE(void set(T newValue)) { Chris@47: uint16_t raw; Chris@47: memcpy(&raw, &newValue, sizeof(T)); Chris@47: bytes[0] = raw; Chris@47: bytes[1] = raw >> 8; Chris@47: } Chris@47: Chris@47: private: Chris@47: union { Chris@47: byte bytes[2]; Chris@47: uint16_t align; Chris@47: }; Chris@47: }; Chris@47: Chris@47: template Chris@47: class ShiftingWireValue { Chris@47: public: Chris@47: KJ_ALWAYS_INLINE(T get() const) { Chris@47: uint32_t raw = (static_cast(bytes[0]) ) | Chris@47: (static_cast(bytes[1]) << 8) | Chris@47: (static_cast(bytes[2]) << 16) | Chris@47: (static_cast(bytes[3]) << 24); Chris@47: T result; Chris@47: memcpy(&result, &raw, sizeof(T)); Chris@47: return result; Chris@47: } Chris@47: KJ_ALWAYS_INLINE(void set(T newValue)) { Chris@47: uint32_t raw; Chris@47: memcpy(&raw, &newValue, sizeof(T)); Chris@47: bytes[0] = raw; Chris@47: bytes[1] = raw >> 8; Chris@47: bytes[2] = raw >> 16; Chris@47: bytes[3] = raw >> 24; Chris@47: } Chris@47: Chris@47: private: Chris@47: union { Chris@47: byte bytes[4]; Chris@47: uint32_t align; Chris@47: }; Chris@47: }; Chris@47: Chris@47: template Chris@47: class ShiftingWireValue { Chris@47: public: Chris@47: KJ_ALWAYS_INLINE(T get() const) { Chris@47: uint64_t raw = (static_cast(bytes[0]) ) | Chris@47: (static_cast(bytes[1]) << 8) | Chris@47: (static_cast(bytes[2]) << 16) | Chris@47: (static_cast(bytes[3]) << 24) | Chris@47: (static_cast(bytes[4]) << 32) | Chris@47: (static_cast(bytes[5]) << 40) | Chris@47: (static_cast(bytes[6]) << 48) | Chris@47: (static_cast(bytes[7]) << 56); Chris@47: T result; Chris@47: memcpy(&result, &raw, sizeof(T)); Chris@47: return result; Chris@47: } Chris@47: KJ_ALWAYS_INLINE(void set(T newValue)) { Chris@47: uint64_t raw; Chris@47: memcpy(&raw, &newValue, sizeof(T)); Chris@47: bytes[0] = raw; Chris@47: bytes[1] = raw >> 8; Chris@47: bytes[2] = raw >> 16; Chris@47: bytes[3] = raw >> 24; Chris@47: bytes[4] = raw >> 32; Chris@47: bytes[5] = raw >> 40; Chris@47: bytes[6] = raw >> 48; Chris@47: bytes[7] = raw >> 56; Chris@47: } Chris@47: Chris@47: private: Chris@47: union { Chris@47: byte bytes[8]; Chris@47: uint64_t align; Chris@47: }; Chris@47: }; Chris@47: Chris@47: template Chris@47: using WireValue = ShiftingWireValue; Chris@47: // To prevent ODR problems when endian-test, endian-reverse-test, and endian-fallback-test are Chris@47: // linked together, we define each implementation with a different name and define an alias to the Chris@47: // one we want to use. Chris@47: Chris@47: #endif Chris@47: Chris@47: } // namespace _ (private) Chris@47: } // namespace capnp Chris@47: Chris@47: #endif // CAPNP_ENDIAN_H_