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