summaryrefslogtreecommitdiff
path: root/src/mongo/platform/endian.h
blob: f94586dc84a39af9f1cd9b04629f6a4d04b10d6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*    Copyright 2014 MongoDB Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects
 *    for all of the code used other than as permitted herein. If you modify
 *    file(s) with this exception, you may extend this exception to your
 *    version of the file(s), but you are not obligated to do so. If you do not
 *    wish to do so, delete this exception statement from your version. If you
 *    delete this exception statement from all source files in the program,
 *    then also delete it in the license file.
 */

#pragma once

#include <climits>
#include <cstdint>
#include <cstring>
#include <type_traits>

#include "mongo/base/static_assert.h"
#include "mongo/config.h"
#include "mongo/platform/decimal128.h"

#pragma push_macro("MONGO_UINT16_SWAB")
#pragma push_macro("MONGO_UINT32_SWAB")
#pragma push_macro("MONGO_UINT64_SWAB")
#pragma push_macro("htobe16")
#pragma push_macro("htobe32")
#pragma push_macro("htobe64")
#pragma push_macro("htole16")
#pragma push_macro("htole32")
#pragma push_macro("htole64")
#pragma push_macro("be16toh")
#pragma push_macro("be32toh")
#pragma push_macro("be64toh")
#pragma push_macro("le16toh")
#pragma push_macro("le32toh")
#pragma push_macro("le64toh")

#undef MONGO_UINT16_SWAB
#undef MONGO_UINT32_SWAB
#undef MONGO_UINT64_SWAB
#undef htobe16
#undef htobe32
#undef htobe64
#undef htole16
#undef htole32
#undef htole64
#undef be16toh
#undef be32toh
#undef be64toh
#undef le16toh
#undef le32toh
#undef le64toh

#define MONGO_LITTLE_ENDIAN 1234
#define MONGO_BIG_ENDIAN 4321

#if defined(_MSC_VER)
#include <cstdlib>
#define MONGO_UINT16_SWAB(v) _byteswap_ushort(v)
#define MONGO_UINT32_SWAB(v) _byteswap_ulong(v)
#define MONGO_UINT64_SWAB(v) _byteswap_uint64(v)
#elif defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__) && \
    (__clang_major__ >= 3) && (__clang_minor__ >= 1)
#if __has_builtin(__builtin_bswap16)
#define MONGO_UINT16_SWAB(v) __builtin_bswap16(v)
#endif
#if __has_builtin(__builtin_bswap32)
#define MONGO_UINT32_SWAB(v) __builtin_bswap32(v)
#endif
#if __has_builtin(__builtin_bswap64)
#define MONGO_UINT64_SWAB(v) __builtin_bswap64(v)
#endif
#elif defined(__GNUC__) && (__GNUC__ >= 4)
#if __GNUC__ >= 4 && defined(__GNUC_MINOR__) && __GNUC_MINOR__ >= 3
#define MONGO_UINT32_SWAB(v) __builtin_bswap32(v)
#define MONGO_UINT64_SWAB(v) __builtin_bswap64(v)
#endif
#if __GNUC__ >= 4 && defined(__GNUC_MINOR__) && __GNUC_MINOR__ >= 8
#define MONGO_UINT16_SWAB(v) __builtin_bswap16(v)
#endif
#elif defined(__sun)
#include <sys/byteorder.h>
#define MONGO_UINT16_SWAB(v) BSWAP_16(v)
#define MONGO_UINT32_SWAB(v) BSWAP_32(v)
#define MONGO_UINT64_SWAB(v) BSWAP_64(v)
#endif

#ifndef MONGO_UINT16_SWAB
#define MONGO_UINT16_SWAB(v) endian::bswap_slow16(v)
#endif

#ifndef MONGO_UINT32_SWAB
#define MONGO_UINT32_SWAB(v) endian::bswap_slow32(v)
#endif

#ifndef MONGO_UINT64_SWAB
#define MONGO_UINT64_SWAB(v) endian::bswap_slow64(v)
#endif

#if MONGO_CONFIG_BYTE_ORDER == MONGO_LITTLE_ENDIAN
#define htobe16(v) MONGO_UINT16_SWAB(v)
#define htobe32(v) MONGO_UINT32_SWAB(v)
#define htobe64(v) MONGO_UINT64_SWAB(v)
#define htole16(v) (v)
#define htole32(v) (v)
#define htole64(v) (v)
#define be16toh(v) MONGO_UINT16_SWAB(v)
#define be32toh(v) MONGO_UINT32_SWAB(v)
#define be64toh(v) MONGO_UINT64_SWAB(v)
#define le16toh(v) (v)
#define le32toh(v) (v)
#define le64toh(v) (v)
#elif MONGO_CONFIG_BYTE_ORDER == MONGO_BIG_ENDIAN
#define htobe16(v) (v)
#define htobe32(v) (v)
#define htobe64(v) (v)
#define htole16(v) MONGO_UINT16_SWAB(v)
#define htole32(v) MONGO_UINT32_SWAB(v)
#define htole64(v) MONGO_UINT64_SWAB(v)
#define be16toh(v) (v)
#define be32toh(v) (v)
#define be64toh(v) (v)
#define le16toh(v) MONGO_UINT16_SWAB(v)
#define le32toh(v) MONGO_UINT32_SWAB(v)
#define le64toh(v) MONGO_UINT64_SWAB(v)
#else
#error \
    "The endianness of target architecture is unknown. " \
        "Please define MONGO_CONFIG_BYTE_ORDER"
#endif

namespace mongo {
namespace endian {

static inline uint16_t bswap_slow16(uint16_t v) {
    return ((v & 0x00FF) << 8) | ((v & 0xFF00) >> 8);
}

static inline uint32_t bswap_slow32(uint32_t v) {
    return ((v & 0x000000FFUL) << 24) | ((v & 0x0000FF00UL) << 8) | ((v & 0x00FF0000UL) >> 8) |
        ((v & 0xFF000000UL) >> 24);
}

static inline uint64_t bswap_slow64(uint64_t v) {
    return ((v & 0x00000000000000FFULL) << 56) | ((v & 0x000000000000FF00ULL) << 40) |
        ((v & 0x0000000000FF0000ULL) << 24) | ((v & 0x00000000FF000000ULL) << 8) |
        ((v & 0x000000FF00000000ULL) >> 8) | ((v & 0x0000FF0000000000ULL) >> 24) |
        ((v & 0x00FF000000000000ULL) >> 40) | ((v & 0xFF00000000000000ULL) >> 56);
}

template <typename T>
struct ByteOrderConverter;

template <>
struct ByteOrderConverter<uint8_t> {
    typedef uint8_t T;

    inline static T nativeToBig(T t) {
        return t;
    }

    inline static T bigToNative(T t) {
        return t;
    }

    inline static T nativeToLittle(T t) {
        return t;
    }

    inline static T littleToNative(T t) {
        return t;
    }
};

template <>
struct ByteOrderConverter<uint16_t> {
    typedef uint16_t T;

    inline static T nativeToBig(T t) {
        return htobe16(t);
    }

    inline static T bigToNative(T t) {
        return be16toh(t);
    }

    inline static T nativeToLittle(T t) {
        return htole16(t);
    }

    inline static T littleToNative(T t) {
        return le16toh(t);
    }
};

template <>
struct ByteOrderConverter<uint32_t> {
    typedef uint32_t T;

    inline static T nativeToBig(T t) {
        return htobe32(t);
    }

    inline static T bigToNative(T t) {
        return be32toh(t);
    }

    inline static T nativeToLittle(T t) {
        return htole32(t);
    }

    inline static T littleToNative(T t) {
        return le32toh(t);
    }
};

template <>
struct ByteOrderConverter<uint64_t> {
    typedef uint64_t T;

    inline static T nativeToBig(T t) {
        return htobe64(t);
    }

    inline static T bigToNative(T t) {
        return be64toh(t);
    }

    inline static T nativeToLittle(T t) {
        return htole64(t);
    }

    inline static T littleToNative(T t) {
        return le64toh(t);
    }
};

template <>
struct ByteOrderConverter<int8_t> {
    typedef int8_t T;

    inline static T nativeToBig(T t) {
        return t;
    }

    inline static T bigToNative(T t) {
        return t;
    }

    inline static T nativeToLittle(T t) {
        return t;
    }

    inline static T littleToNative(T t) {
        return t;
    }
};

template <>
struct ByteOrderConverter<int16_t> {
    typedef int16_t T;

    inline static T nativeToBig(T t) {
        return htobe16(static_cast<uint16_t>(t));
    }

    inline static T bigToNative(T t) {
        return be16toh(static_cast<uint16_t>(t));
    }

    inline static T nativeToLittle(T t) {
        return htole16(static_cast<uint16_t>(t));
    }

    inline static T littleToNative(T t) {
        return le16toh(static_cast<uint16_t>(t));
    }
};

template <>
struct ByteOrderConverter<int32_t> {
    typedef int32_t T;

    inline static T nativeToBig(T t) {
        return htobe32(static_cast<uint32_t>(t));
    }

    inline static T bigToNative(T t) {
        return be32toh(static_cast<uint32_t>(t));
    }

    inline static T nativeToLittle(T t) {
        return htole32(static_cast<uint32_t>(t));
    }

    inline static T littleToNative(T t) {
        return le32toh(static_cast<uint32_t>(t));
    }
};

template <>
struct ByteOrderConverter<int64_t> {
    typedef int64_t T;

    inline static T nativeToBig(T t) {
        return htobe64(static_cast<uint64_t>(t));
    }

    inline static T bigToNative(T t) {
        return be64toh(static_cast<uint64_t>(t));
    }

    inline static T nativeToLittle(T t) {
        return htole64(static_cast<uint64_t>(t));
    }

    inline static T littleToNative(T t) {
        return le64toh(static_cast<uint64_t>(t));
    }
};

template <>
struct ByteOrderConverter<float> {
    typedef float T;

    inline static T nativeToBig(T t) {
        MONGO_STATIC_ASSERT(sizeof(T) == sizeof(uint32_t));

        uint32_t temp;
        std::memcpy(&temp, &t, sizeof(t));
        temp = htobe32(temp);
        std::memcpy(&t, &temp, sizeof(t));
        return t;
    }

    inline static T bigToNative(T t) {
        uint32_t temp;
        std::memcpy(&temp, &t, sizeof(t));
        temp = be32toh(temp);
        std::memcpy(&t, &temp, sizeof(t));
        return t;
    }

    inline static T nativeToLittle(T t) {
        uint32_t temp;
        std::memcpy(&temp, &t, sizeof(t));
        temp = htole32(temp);
        std::memcpy(&t, &temp, sizeof(t));
        return t;
    }

    inline static T littleToNative(T t) {
        uint32_t temp;
        std::memcpy(&temp, &t, sizeof(t));
        temp = le32toh(temp);
        std::memcpy(&t, &temp, sizeof(t));
        return t;
    }
};

template <>
struct ByteOrderConverter<double> {
    typedef double T;

    inline static T nativeToBig(T t) {
        MONGO_STATIC_ASSERT(sizeof(T) == sizeof(uint64_t));

        uint64_t temp;
        std::memcpy(&temp, &t, sizeof(t));
        temp = htobe64(temp);
        std::memcpy(&t, &temp, sizeof(t));
        return t;
    }

    inline static T bigToNative(T t) {
        uint64_t temp;
        std::memcpy(&temp, &t, sizeof(t));
        temp = be64toh(temp);
        std::memcpy(&t, &temp, sizeof(t));
        return t;
    }

    inline static T nativeToLittle(T t) {
        uint64_t temp;
        std::memcpy(&temp, &t, sizeof(t));
        temp = htole64(temp);
        std::memcpy(&t, &temp, sizeof(t));
        return t;
    }

    inline static T littleToNative(T t) {
        uint64_t temp;
        std::memcpy(&temp, &t, sizeof(t));
        temp = le64toh(temp);
        std::memcpy(&t, &temp, sizeof(t));
        return t;
    }
};

template <>
struct ByteOrderConverter<Decimal128::Value> {
    typedef Decimal128::Value T;

    inline static T nativeToBig(T t) {
        ByteOrderConverter<uint64_t>::nativeToBig(t.low64);
        ByteOrderConverter<uint64_t>::nativeToBig(t.high64);
        return t;
    }

    inline static T bigToNative(T t) {
        ByteOrderConverter<uint64_t>::bigToNative(t.low64);
        ByteOrderConverter<uint64_t>::bigToNative(t.high64);
        return t;
    }

    inline static T nativeToLittle(T t) {
        ByteOrderConverter<uint64_t>::nativeToLittle(t.low64);
        ByteOrderConverter<uint64_t>::nativeToLittle(t.high64);
        return t;
    }

    inline static T littleToNative(T t) {
        ByteOrderConverter<uint64_t>::littleToNative(t.low64);
        ByteOrderConverter<uint64_t>::littleToNative(t.high64);
        return t;
    }
};

// Use a typemape to normalize non-fixed-width integral types to the associated fixed width
// types.

template <typename T>
struct IntegralTypeMap {
    typedef T type;
};

template <>
struct IntegralTypeMap<signed char> {
    MONGO_STATIC_ASSERT(CHAR_BIT == 8);
    typedef int8_t type;
};

template <>
struct IntegralTypeMap<unsigned char> {
    MONGO_STATIC_ASSERT(CHAR_BIT == 8);
    typedef uint8_t type;
};

template <>
struct IntegralTypeMap<char> {
    MONGO_STATIC_ASSERT(CHAR_BIT == 8);
    typedef std::conditional<std::is_signed<char>::value, int8_t, uint8_t>::type type;
};

template <>
struct IntegralTypeMap<long long> {
    MONGO_STATIC_ASSERT(sizeof(long long) == sizeof(int64_t));
    typedef int64_t type;
};

template <>
struct IntegralTypeMap<unsigned long long> {
    MONGO_STATIC_ASSERT(sizeof(unsigned long long) == sizeof(uint64_t));
    typedef uint64_t type;
};

template <typename T>
inline T nativeToBig(T t) {
    return ByteOrderConverter<typename IntegralTypeMap<T>::type>::nativeToBig(t);
}

template <typename T>
inline T bigToNative(T t) {
    return ByteOrderConverter<typename IntegralTypeMap<T>::type>::bigToNative(t);
}

template <typename T>
inline T nativeToLittle(T t) {
    return ByteOrderConverter<typename IntegralTypeMap<T>::type>::nativeToLittle(t);
}

template <typename T>
inline T littleToNative(T t) {
    return ByteOrderConverter<typename IntegralTypeMap<T>::type>::littleToNative(t);
}

}  // namespace endian
}  // namespace mongo

#undef MONGO_UINT16_SWAB
#undef MONGO_UINT32_SWAB
#undef MONGO_UINT64_SWAB
#undef htobe16
#undef htobe32
#undef htobe64
#undef htole16
#undef htole32
#undef htole64
#undef be16toh
#undef be32toh
#undef be64toh
#undef le16toh
#undef le32toh
#undef le64toh

#pragma pop_macro("MONGO_UINT16_SWAB")
#pragma pop_macro("MONGO_UINT32_SWAB")
#pragma pop_macro("MONGO_UINT64_SWAB")
#pragma pop_macro("htobe16")
#pragma pop_macro("htobe32")
#pragma pop_macro("htobe64")
#pragma pop_macro("htole16")
#pragma pop_macro("htole32")
#pragma pop_macro("htole64")
#pragma pop_macro("be16toh")
#pragma pop_macro("be32toh")
#pragma pop_macro("be64toh")
#pragma pop_macro("le16toh")
#pragma pop_macro("le32toh")
#pragma pop_macro("le64toh")