annotate osx/include/capnp/endian.h @ 147:45360b968bf4

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