annotate win64-msvc/include/capnp/endian.h @ 166:cbd6d7e562c7

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