annotate osx/include/capnp/endian.h @ 49:3ab5a40c4e3b

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