18#include "mongo/config.h"
22#include <boost/static_assert.hpp>
23#include <boost/mpl/if.hpp>
24#include <boost/type_traits/is_signed.hpp>
25#include "mongo/platform/cstdint.h"
27#pragma push_macro("MONGO_UINT16_SWAB")
28#pragma push_macro("MONGO_UINT32_SWAB")
29#pragma push_macro("MONGO_UINT64_SWAB")
30#pragma push_macro("MONGO_LITTLE_ENDIAN")
31#pragma push_macro("MONGO_BIG_ENDIAN")
32#pragma push_macro("htobe16")
33#pragma push_macro("htobe32")
34#pragma push_macro("htobe64")
35#pragma push_macro("htole16")
36#pragma push_macro("htole32")
37#pragma push_macro("htole64")
38#pragma push_macro("be16toh")
39#pragma push_macro("be32toh")
40#pragma push_macro("be64toh")
41#pragma push_macro("le16toh")
42#pragma push_macro("le32toh")
43#pragma push_macro("le64toh")
45#undef MONGO_UINT16_SWAB
46#undef MONGO_UINT32_SWAB
47#undef MONGO_UINT64_SWAB
48#undef MONGO_LITTLE_ENDIAN
49#undef MONGO_BIG_ENDIAN
63#define MONGO_LITTLE_ENDIAN 1234
64#define MONGO_BIG_ENDIAN 4321
66#if defined(_MSC_VER) && (_MSC_VER >= 1300)
68#define MONGO_UINT16_SWAB(v) _byteswap_ushort(v)
69#define MONGO_UINT32_SWAB(v) _byteswap_ulong(v)
70#define MONGO_UINT64_SWAB(v) _byteswap_uint64(v)
71#elif defined(__clang__) && defined(__clang_major__) && defined(__clang_minor__) && \
72 (__clang_major__ >= 3) && (__clang_minor__ >= 1)
73#if __has_builtin(__builtin_bswap16)
74#define MONGO_UINT16_SWAB(v) __builtin_bswap16(v)
76#if __has_builtin(__builtin_bswap32)
77#define MONGO_UINT32_SWAB(v) __builtin_bswap32(v)
79#if __has_builtin(__builtin_bswap64)
80#define MONGO_UINT64_SWAB(v) __builtin_bswap64(v)
82#elif defined(__GNUC__) && (__GNUC__ >= 4)
83#if __GNUC__ >= 4 && defined(__GNUC_MINOR__) && __GNUC_MINOR__ >= 3
84#define MONGO_UINT32_SWAB(v) __builtin_bswap32(v)
85#define MONGO_UINT64_SWAB(v) __builtin_bswap64(v)
87#if __GNUC__ >= 4 && defined(__GNUC_MINOR__) && __GNUC_MINOR__ >= 8
88#define MONGO_UINT16_SWAB(v) __builtin_bswap16(v)
91#include <sys/byteorder.h>
92#define MONGO_UINT16_SWAB(v) BSWAP_16(v)
93#define MONGO_UINT32_SWAB(v) BSWAP_32(v)
94#define MONGO_UINT64_SWAB(v) BSWAP_64(v)
97#ifndef MONGO_UINT16_SWAB
98#define MONGO_UINT16_SWAB(v) endian::bswap_slow16(v)
101#ifndef MONGO_UINT32_SWAB
102#define MONGO_UINT32_SWAB(v) endian::bswap_slow32(v)
105#ifndef MONGO_UINT64_SWAB
106#define MONGO_UINT64_SWAB(v) endian::bswap_slow64(v)
109#if MONGO_BYTE_ORDER == MONGO_LITTLE_ENDIAN
110#define htobe16(v) MONGO_UINT16_SWAB(v)
111#define htobe32(v) MONGO_UINT32_SWAB(v)
112#define htobe64(v) MONGO_UINT64_SWAB(v)
113#define htole16(v) (v)
114#define htole32(v) (v)
115#define htole64(v) (v)
116#define be16toh(v) MONGO_UINT16_SWAB(v)
117#define be32toh(v) MONGO_UINT32_SWAB(v)
118#define be64toh(v) MONGO_UINT64_SWAB(v)
119#define le16toh(v) (v)
120#define le32toh(v) (v)
121#define le64toh(v) (v)
122#elif MONGO_BYTE_ORDER == MONGO_BIG_ENDIAN
123#define htobe16(v) (v)
124#define htobe32(v) (v)
125#define htobe64(v) (v)
126#define htole16(v) MONGO_UINT16_SWAB(v)
127#define htole32(v) MONGO_UINT32_SWAB(v)
128#define htole64(v) MONGO_UINT64_SWAB(v)
129#define be16toh(v) (v)
130#define be32toh(v) (v)
131#define be64toh(v) (v)
132#define le16toh(v) MONGO_UINT16_SWAB(v)
133#define le32toh(v) MONGO_UINT32_SWAB(v)
134#define le64toh(v) MONGO_UINT64_SWAB(v)
137 "The endianness of target architecture is unknown. " \
138 "Please define MONGO_BYTE_ORDER"
144static inline uint16_t bswap_slow16(uint16_t v) {
145 return ((v & 0x00FF) << 8) | ((v & 0xFF00) >> 8);
148static inline uint32_t bswap_slow32(uint32_t v) {
149 return ((v & 0x000000FFUL) << 24) | ((v & 0x0000FF00UL) << 8) | ((v & 0x00FF0000UL) >> 8) |
150 ((v & 0xFF000000UL) >> 24);
153static inline uint64_t bswap_slow64(uint64_t v) {
154 return ((v & 0x00000000000000FFULL) << 56) | ((v & 0x000000000000FF00ULL) << 40) |
155 ((v & 0x0000000000FF0000ULL) << 24) | ((v & 0x00000000FF000000ULL) << 8) |
156 ((v & 0x000000FF00000000ULL) >> 8) | ((v & 0x0000FF0000000000ULL) >> 24) |
157 ((v & 0x00FF000000000000ULL) >> 40) | ((v & 0xFF00000000000000ULL) >> 56);
167 inline static T nativeToBig(T t) {
171 inline static T bigToNative(T t) {
175 inline static T nativeToLittle(T t) {
179 inline static T littleToNative(T t) {
188 inline static T nativeToBig(T t) {
192 inline static T bigToNative(T t) {
196 inline static T nativeToLittle(T t) {
200 inline static T littleToNative(T t) {
209 inline static T nativeToBig(T t) {
213 inline static T bigToNative(T t) {
217 inline static T nativeToLittle(T t) {
221 inline static T littleToNative(T t) {
230 inline static T nativeToBig(T t) {
234 inline static T bigToNative(T t) {
238 inline static T nativeToLittle(T t) {
242 inline static T littleToNative(T t) {
251 inline static T nativeToBig(T t) {
255 inline static T bigToNative(T t) {
259 inline static T nativeToLittle(T t) {
263 inline static T littleToNative(T t) {
272 inline static T nativeToBig(T t) {
273 return htobe16(
static_cast<uint16_t
>(t));
276 inline static T bigToNative(T t) {
277 return be16toh(
static_cast<uint16_t
>(t));
280 inline static T nativeToLittle(T t) {
281 return htole16(
static_cast<uint16_t
>(t));
284 inline static T littleToNative(T t) {
285 return le16toh(
static_cast<uint16_t
>(t));
293 inline static T nativeToBig(T t) {
294 return htobe32(
static_cast<uint32_t
>(t));
297 inline static T bigToNative(T t) {
298 return be32toh(
static_cast<uint32_t
>(t));
301 inline static T nativeToLittle(T t) {
302 return htole32(
static_cast<uint32_t
>(t));
305 inline static T littleToNative(T t) {
306 return le32toh(
static_cast<uint32_t
>(t));
314 inline static T nativeToBig(T t) {
315 return htobe64(
static_cast<uint64_t
>(t));
318 inline static T bigToNative(T t) {
319 return be64toh(
static_cast<uint64_t
>(t));
322 inline static T nativeToLittle(T t) {
323 return htole64(
static_cast<uint64_t
>(t));
326 inline static T littleToNative(T t) {
327 return le64toh(
static_cast<uint64_t
>(t));
335 inline static T nativeToBig(T t) {
336 BOOST_STATIC_ASSERT(
sizeof(T) ==
sizeof(uint32_t));
339 std::memcpy(&temp, &t,
sizeof(t));
340 temp = htobe32(temp);
341 std::memcpy(&t, &temp,
sizeof(t));
345 inline static T bigToNative(T t) {
347 std::memcpy(&temp, &t,
sizeof(t));
348 temp = be32toh(temp);
349 std::memcpy(&t, &temp,
sizeof(t));
353 inline static T nativeToLittle(T t) {
355 std::memcpy(&temp, &t,
sizeof(t));
356 temp = htole32(temp);
357 std::memcpy(&t, &temp,
sizeof(t));
361 inline static T littleToNative(T t) {
363 std::memcpy(&temp, &t,
sizeof(t));
364 temp = le32toh(temp);
365 std::memcpy(&t, &temp,
sizeof(t));
374 inline static T nativeToBig(T t) {
375 BOOST_STATIC_ASSERT(
sizeof(T) ==
sizeof(uint64_t));
378 std::memcpy(&temp, &t,
sizeof(t));
379 temp = htobe64(temp);
380 std::memcpy(&t, &temp,
sizeof(t));
384 inline static T bigToNative(T t) {
386 std::memcpy(&temp, &t,
sizeof(t));
387 temp = be64toh(temp);
388 std::memcpy(&t, &temp,
sizeof(t));
392 inline static T nativeToLittle(T t) {
394 std::memcpy(&temp, &t,
sizeof(t));
395 temp = htole64(temp);
396 std::memcpy(&t, &temp,
sizeof(t));
400 inline static T littleToNative(T t) {
402 std::memcpy(&temp, &t,
sizeof(t));
403 temp = le64toh(temp);
404 std::memcpy(&t, &temp,
sizeof(t));
419 BOOST_STATIC_ASSERT(CHAR_BIT == 8);
425 BOOST_STATIC_ASSERT(CHAR_BIT == 8);
426 typedef uint8_t type;
431 BOOST_STATIC_ASSERT(CHAR_BIT == 8);
432 typedef boost::mpl::if_c<boost::is_signed<char>::value, int8_t, uint8_t>::type type;
437 BOOST_STATIC_ASSERT(
sizeof(
long long) ==
sizeof(int64_t));
438 typedef int64_t type;
443 BOOST_STATIC_ASSERT(
sizeof(
unsigned long long) ==
sizeof(uint64_t));
444 typedef uint64_t type;
448inline T nativeToBig(T t) {
453inline T bigToNative(T t) {
454 return ByteOrderConverter<typename IntegralTypeMap<T>::type>::bigToNative(t);
458inline T nativeToLittle(T t) {
459 return ByteOrderConverter<typename IntegralTypeMap<T>::type>::nativeToLittle(t);
463inline T littleToNative(T t) {
464 return ByteOrderConverter<typename IntegralTypeMap<T>::type>::littleToNative(t);
470#undef MONGO_UINT16_SWAB
471#undef MONGO_UINT32_SWAB
472#undef MONGO_UINT64_SWAB
473#undef MONGO_LITTLE_ENDIAN
474#undef MONGO_BIG_ENDIAN
488#pragma pop_macro("MONGO_UINT16_SWAB")
489#pragma pop_macro("MONGO_UINT32_SWAB")
490#pragma pop_macro("MONGO_UINT64_SWAB")
491#pragma pop_macro("MONGO_LITTLE_ENDIAN")
492#pragma pop_macro("MONGO_BIG_ENDIAN")
493#pragma pop_macro("htobe16")
494#pragma pop_macro("htobe32")
495#pragma pop_macro("htobe64")
496#pragma pop_macro("htole16")
497#pragma pop_macro("htole32")
498#pragma pop_macro("htole64")
499#pragma pop_macro("be16toh")
500#pragma pop_macro("be32toh")
501#pragma pop_macro("be64toh")
502#pragma pop_macro("le16toh")
503#pragma pop_macro("le32toh")
504#pragma pop_macro("le64toh")
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:32