annotate win32-mingw/include/capnp/endian.h @ 50:37d53a7e8262

Headers for KJ/Capnp Win32
author Chris Cannam
date Wed, 26 Oct 2016 13:18:45 +0100
parents
children eccd51b72864
rev   line source
Chris@50 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@50 2 // Licensed under the MIT License:
Chris@50 3 //
Chris@50 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@50 5 // of this software and associated documentation files (the "Software"), to deal
Chris@50 6 // in the Software without restriction, including without limitation the rights
Chris@50 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@50 8 // copies of the Software, and to permit persons to whom the Software is
Chris@50 9 // furnished to do so, subject to the following conditions:
Chris@50 10 //
Chris@50 11 // The above copyright notice and this permission notice shall be included in
Chris@50 12 // all copies or substantial portions of the Software.
Chris@50 13 //
Chris@50 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@50 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@50 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@50 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@50 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@50 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@50 20 // THE SOFTWARE.
Chris@50 21
Chris@50 22 #ifndef CAPNP_ENDIAN_H_
Chris@50 23 #define CAPNP_ENDIAN_H_
Chris@50 24
Chris@50 25 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
Chris@50 26 #pragma GCC system_header
Chris@50 27 #endif
Chris@50 28
Chris@50 29 #include "common.h"
Chris@50 30 #include <inttypes.h>
Chris@50 31 #include <string.h> // memcpy
Chris@50 32
Chris@50 33 namespace capnp {
Chris@50 34 namespace _ { // private
Chris@50 35
Chris@50 36 // WireValue
Chris@50 37 //
Chris@50 38 // Wraps a primitive value as it appears on the wire. Namely, values are little-endian on the
Chris@50 39 // wire, because little-endian is the most common endianness in modern CPUs.
Chris@50 40 //
Chris@50 41 // Note: In general, code that depends cares about byte ordering is bad. See:
Chris@50 42 // http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html
Chris@50 43 // Cap'n Proto is special because it is essentially doing compiler-like things, fussing over
Chris@50 44 // allocation and layout of memory, in order to squeeze out every last drop of performance.
Chris@50 45
Chris@50 46 #if _MSC_VER
Chris@50 47 // Assume Windows is little-endian.
Chris@50 48 //
Chris@50 49 // TODO(msvc): This is ugly. Maybe refactor later checks to be based on CAPNP_BYTE_ORDER or
Chris@50 50 // CAPNP_SWAP_BYTES or something, and define that in turn based on _MSC_VER or the GCC
Chris@50 51 // intrinsics.
Chris@50 52
Chris@50 53 #ifndef __ORDER_BIG_ENDIAN__
Chris@50 54 #define __ORDER_BIG_ENDIAN__ 4321
Chris@50 55 #endif
Chris@50 56 #ifndef __ORDER_LITTLE_ENDIAN__
Chris@50 57 #define __ORDER_LITTLE_ENDIAN__ 1234
Chris@50 58 #endif
Chris@50 59 #ifndef __BYTE_ORDER__
Chris@50 60 #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__
Chris@50 61 #endif
Chris@50 62 #endif
Chris@50 63
Chris@50 64 #if CAPNP_REVERSE_ENDIAN
Chris@50 65 #define CAPNP_WIRE_BYTE_ORDER __ORDER_BIG_ENDIAN__
Chris@50 66 #define CAPNP_OPPOSITE_OF_WIRE_BYTE_ORDER __ORDER_LITTLE_ENDIAN__
Chris@50 67 #else
Chris@50 68 #define CAPNP_WIRE_BYTE_ORDER __ORDER_LITTLE_ENDIAN__
Chris@50 69 #define CAPNP_OPPOSITE_OF_WIRE_BYTE_ORDER __ORDER_BIG_ENDIAN__
Chris@50 70 #endif
Chris@50 71
Chris@50 72 #if defined(__BYTE_ORDER__) && \
Chris@50 73 __BYTE_ORDER__ == CAPNP_WIRE_BYTE_ORDER && \
Chris@50 74 !CAPNP_DISABLE_ENDIAN_DETECTION
Chris@50 75 // CPU is little-endian. We can just read/write the memory directly.
Chris@50 76
Chris@50 77 template <typename T>
Chris@50 78 class DirectWireValue {
Chris@50 79 public:
Chris@50 80 KJ_ALWAYS_INLINE(T get() const) { return value; }
Chris@50 81 KJ_ALWAYS_INLINE(void set(T newValue)) { value = newValue; }
Chris@50 82
Chris@50 83 private:
Chris@50 84 T value;
Chris@50 85 };
Chris@50 86
Chris@50 87 template <typename T>
Chris@50 88 using WireValue = DirectWireValue<T>;
Chris@50 89 // To prevent ODR problems when endian-test, endian-reverse-test, and endian-fallback-test are
Chris@50 90 // linked together, we define each implementation with a different name and define an alias to the
Chris@50 91 // one we want to use.
Chris@50 92
Chris@50 93 #elif defined(__BYTE_ORDER__) && \
Chris@50 94 __BYTE_ORDER__ == CAPNP_OPPOSITE_OF_WIRE_BYTE_ORDER && \
Chris@50 95 defined(__GNUC__) && !CAPNP_DISABLE_ENDIAN_DETECTION
Chris@50 96 // Big-endian, but GCC's __builtin_bswap() is available.
Chris@50 97
Chris@50 98 // TODO(perf): Use dedicated instructions to read little-endian data on big-endian CPUs that have
Chris@50 99 // them.
Chris@50 100
Chris@50 101 // TODO(perf): Verify that this code optimizes reasonably. In particular, ensure that the
Chris@50 102 // compiler optimizes away the memcpy()s and keeps everything in registers.
Chris@50 103
Chris@50 104 template <typename T, size_t size = sizeof(T)>
Chris@50 105 class SwappingWireValue;
Chris@50 106
Chris@50 107 template <typename T>
Chris@50 108 class SwappingWireValue<T, 1> {
Chris@50 109 public:
Chris@50 110 KJ_ALWAYS_INLINE(T get() const) { return value; }
Chris@50 111 KJ_ALWAYS_INLINE(void set(T newValue)) { value = newValue; }
Chris@50 112
Chris@50 113 private:
Chris@50 114 T value;
Chris@50 115 };
Chris@50 116
Chris@50 117 template <typename T>
Chris@50 118 class SwappingWireValue<T, 2> {
Chris@50 119 public:
Chris@50 120 KJ_ALWAYS_INLINE(T get() const) {
Chris@50 121 // Not all platforms have __builtin_bswap16() for some reason. In particular, it is missing
Chris@50 122 // on gcc-4.7.3-cygwin32 (but present on gcc-4.8.1-cygwin64).
Chris@50 123 uint16_t swapped = (value << 8) | (value >> 8);
Chris@50 124 T result;
Chris@50 125 memcpy(&result, &swapped, sizeof(T));
Chris@50 126 return result;
Chris@50 127 }
Chris@50 128 KJ_ALWAYS_INLINE(void set(T newValue)) {
Chris@50 129 uint16_t raw;
Chris@50 130 memcpy(&raw, &newValue, sizeof(T));
Chris@50 131 // Not all platforms have __builtin_bswap16() for some reason. In particular, it is missing
Chris@50 132 // on gcc-4.7.3-cygwin32 (but present on gcc-4.8.1-cygwin64).
Chris@50 133 value = (raw << 8) | (raw >> 8);
Chris@50 134 }
Chris@50 135
Chris@50 136 private:
Chris@50 137 uint16_t value;
Chris@50 138 };
Chris@50 139
Chris@50 140 template <typename T>
Chris@50 141 class SwappingWireValue<T, 4> {
Chris@50 142 public:
Chris@50 143 KJ_ALWAYS_INLINE(T get() const) {
Chris@50 144 uint32_t swapped = __builtin_bswap32(value);
Chris@50 145 T result;
Chris@50 146 memcpy(&result, &swapped, sizeof(T));
Chris@50 147 return result;
Chris@50 148 }
Chris@50 149 KJ_ALWAYS_INLINE(void set(T newValue)) {
Chris@50 150 uint32_t raw;
Chris@50 151 memcpy(&raw, &newValue, sizeof(T));
Chris@50 152 value = __builtin_bswap32(raw);
Chris@50 153 }
Chris@50 154
Chris@50 155 private:
Chris@50 156 uint32_t value;
Chris@50 157 };
Chris@50 158
Chris@50 159 template <typename T>
Chris@50 160 class SwappingWireValue<T, 8> {
Chris@50 161 public:
Chris@50 162 KJ_ALWAYS_INLINE(T get() const) {
Chris@50 163 uint64_t swapped = __builtin_bswap64(value);
Chris@50 164 T result;
Chris@50 165 memcpy(&result, &swapped, sizeof(T));
Chris@50 166 return result;
Chris@50 167 }
Chris@50 168 KJ_ALWAYS_INLINE(void set(T newValue)) {
Chris@50 169 uint64_t raw;
Chris@50 170 memcpy(&raw, &newValue, sizeof(T));
Chris@50 171 value = __builtin_bswap64(raw);
Chris@50 172 }
Chris@50 173
Chris@50 174 private:
Chris@50 175 uint64_t value;
Chris@50 176 };
Chris@50 177
Chris@50 178 template <typename T>
Chris@50 179 using WireValue = SwappingWireValue<T>;
Chris@50 180 // To prevent ODR problems when endian-test, endian-reverse-test, and endian-fallback-test are
Chris@50 181 // linked together, we define each implementation with a different name and define an alias to the
Chris@50 182 // one we want to use.
Chris@50 183
Chris@50 184 #else
Chris@50 185 // Unknown endianness. Fall back to bit shifts.
Chris@50 186
Chris@50 187 #if !CAPNP_DISABLE_ENDIAN_DETECTION
Chris@50 188 #if _MSC_VER
Chris@50 189 #pragma message("Couldn't detect endianness of your platform. Using unoptimized fallback implementation.")
Chris@50 190 #pragma message("Consider changing this code to detect your platform and send us a patch!")
Chris@50 191 #else
Chris@50 192 #warning "Couldn't detect endianness of your platform. Using unoptimized fallback implementation."
Chris@50 193 #warning "Consider changing this code to detect your platform and send us a patch!"
Chris@50 194 #endif
Chris@50 195 #endif // !CAPNP_DISABLE_ENDIAN_DETECTION
Chris@50 196
Chris@50 197 template <typename T, size_t size = sizeof(T)>
Chris@50 198 class ShiftingWireValue;
Chris@50 199
Chris@50 200 template <typename T>
Chris@50 201 class ShiftingWireValue<T, 1> {
Chris@50 202 public:
Chris@50 203 KJ_ALWAYS_INLINE(T get() const) { return value; }
Chris@50 204 KJ_ALWAYS_INLINE(void set(T newValue)) { value = newValue; }
Chris@50 205
Chris@50 206 private:
Chris@50 207 T value;
Chris@50 208 };
Chris@50 209
Chris@50 210 template <typename T>
Chris@50 211 class ShiftingWireValue<T, 2> {
Chris@50 212 public:
Chris@50 213 KJ_ALWAYS_INLINE(T get() const) {
Chris@50 214 uint16_t raw = (static_cast<uint16_t>(bytes[0]) ) |
Chris@50 215 (static_cast<uint16_t>(bytes[1]) << 8);
Chris@50 216 T result;
Chris@50 217 memcpy(&result, &raw, sizeof(T));
Chris@50 218 return result;
Chris@50 219 }
Chris@50 220 KJ_ALWAYS_INLINE(void set(T newValue)) {
Chris@50 221 uint16_t raw;
Chris@50 222 memcpy(&raw, &newValue, sizeof(T));
Chris@50 223 bytes[0] = raw;
Chris@50 224 bytes[1] = raw >> 8;
Chris@50 225 }
Chris@50 226
Chris@50 227 private:
Chris@50 228 union {
Chris@50 229 byte bytes[2];
Chris@50 230 uint16_t align;
Chris@50 231 };
Chris@50 232 };
Chris@50 233
Chris@50 234 template <typename T>
Chris@50 235 class ShiftingWireValue<T, 4> {
Chris@50 236 public:
Chris@50 237 KJ_ALWAYS_INLINE(T get() const) {
Chris@50 238 uint32_t raw = (static_cast<uint32_t>(bytes[0]) ) |
Chris@50 239 (static_cast<uint32_t>(bytes[1]) << 8) |
Chris@50 240 (static_cast<uint32_t>(bytes[2]) << 16) |
Chris@50 241 (static_cast<uint32_t>(bytes[3]) << 24);
Chris@50 242 T result;
Chris@50 243 memcpy(&result, &raw, sizeof(T));
Chris@50 244 return result;
Chris@50 245 }
Chris@50 246 KJ_ALWAYS_INLINE(void set(T newValue)) {
Chris@50 247 uint32_t raw;
Chris@50 248 memcpy(&raw, &newValue, sizeof(T));
Chris@50 249 bytes[0] = raw;
Chris@50 250 bytes[1] = raw >> 8;
Chris@50 251 bytes[2] = raw >> 16;
Chris@50 252 bytes[3] = raw >> 24;
Chris@50 253 }
Chris@50 254
Chris@50 255 private:
Chris@50 256 union {
Chris@50 257 byte bytes[4];
Chris@50 258 uint32_t align;
Chris@50 259 };
Chris@50 260 };
Chris@50 261
Chris@50 262 template <typename T>
Chris@50 263 class ShiftingWireValue<T, 8> {
Chris@50 264 public:
Chris@50 265 KJ_ALWAYS_INLINE(T get() const) {
Chris@50 266 uint64_t raw = (static_cast<uint64_t>(bytes[0]) ) |
Chris@50 267 (static_cast<uint64_t>(bytes[1]) << 8) |
Chris@50 268 (static_cast<uint64_t>(bytes[2]) << 16) |
Chris@50 269 (static_cast<uint64_t>(bytes[3]) << 24) |
Chris@50 270 (static_cast<uint64_t>(bytes[4]) << 32) |
Chris@50 271 (static_cast<uint64_t>(bytes[5]) << 40) |
Chris@50 272 (static_cast<uint64_t>(bytes[6]) << 48) |
Chris@50 273 (static_cast<uint64_t>(bytes[7]) << 56);
Chris@50 274 T result;
Chris@50 275 memcpy(&result, &raw, sizeof(T));
Chris@50 276 return result;
Chris@50 277 }
Chris@50 278 KJ_ALWAYS_INLINE(void set(T newValue)) {
Chris@50 279 uint64_t raw;
Chris@50 280 memcpy(&raw, &newValue, sizeof(T));
Chris@50 281 bytes[0] = raw;
Chris@50 282 bytes[1] = raw >> 8;
Chris@50 283 bytes[2] = raw >> 16;
Chris@50 284 bytes[3] = raw >> 24;
Chris@50 285 bytes[4] = raw >> 32;
Chris@50 286 bytes[5] = raw >> 40;
Chris@50 287 bytes[6] = raw >> 48;
Chris@50 288 bytes[7] = raw >> 56;
Chris@50 289 }
Chris@50 290
Chris@50 291 private:
Chris@50 292 union {
Chris@50 293 byte bytes[8];
Chris@50 294 uint64_t align;
Chris@50 295 };
Chris@50 296 };
Chris@50 297
Chris@50 298 template <typename T>
Chris@50 299 using WireValue = ShiftingWireValue<T>;
Chris@50 300 // To prevent ODR problems when endian-test, endian-reverse-test, and endian-fallback-test are
Chris@50 301 // linked together, we define each implementation with a different name and define an alias to the
Chris@50 302 // one we want to use.
Chris@50 303
Chris@50 304 #endif
Chris@50 305
Chris@50 306 } // namespace _ (private)
Chris@50 307 } // namespace capnp
Chris@50 308
Chris@50 309 #endif // CAPNP_ENDIAN_H_