annotate win32-mingw/include/capnp/endian.h @ 149:279b18cc7785

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