blob: 4a08d82ff666d00e46ca0b84c34501e33c7b8a4b (
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
|
#include "utility.h"
#include <climits>
namespace opentracing {
BEGIN_OPENTRACING_ABI_NAMESPACE
namespace mocktracer {
// Converts swaps the endianness of a number.
//
// Taken from https://stackoverflow.com/a/105339/4447365
template <typename T>
static T SwapEndian(T u) {
static_assert(CHAR_BIT == 8, "CHAR_BIT != 8");
union {
T u;
unsigned char u8[sizeof(T)];
} source, dest;
source.u = u;
for (size_t k = 0; k < sizeof(T); k++)
dest.u8[k] = source.u8[sizeof(T) - k - 1];
return dest.u;
}
// Determines whether the architecture is big endian.
//
// Taken from https://stackoverflow.com/a/1001373/4447365
static bool IsBigEndian() {
union {
uint32_t i;
char c[4];
} bint = {0x01020304};
return bint.c[0] == 1;
}
uint64_t SwapEndianIfBig(uint64_t x) {
if (IsBigEndian()) {
return SwapEndian(x);
} else {
return x;
}
}
uint32_t SwapEndianIfBig(uint32_t x) {
if (IsBigEndian()) {
return SwapEndian(x);
} else {
return x;
}
}
} // namespace mocktracer
END_OPENTRACING_ABI_NAMESPACE
} // namespace opentracing
|