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