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