annotate win64-msvc/include/capnp/endian.h @ 77:4edcd14160a5 pa_catalina

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