-
Notifications
You must be signed in to change notification settings - Fork 7
/
endian.h
45 lines (40 loc) · 1.46 KB
/
endian.h
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
#pragma once
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
constexpr bool bswap = true;
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
constexpr bool bswap = false;
#else
#error "Endianness not supported!"
#endif
#define ROTR64(x, n) (((x) >> (n)) | ((x) << (64 - (n))))
typedef uint64_t seed_t;
template <bool bswap>
static inline uint64_t GET_U64(const uint8_t* data, size_t index) {
uint64_t result;
std::memcpy(&result, data + index, sizeof(result));
if(bswap) {
result = ((result & 0x00000000000000FF) << 56) |
((result & 0x000000000000FF00) << 40) |
((result & 0x0000000000FF0000) << 24) |
((result & 0x00000000FF000000) << 8) |
((result & 0x000000FF00000000) >> 8) |
((result & 0x0000FF0000000000) >> 24) |
((result & 0x00FF000000000000) >> 40) |
((result & 0xFF00000000000000) >> 56);
}
return result;
}
template <bool bswap>
static inline void PUT_U64(uint64_t value, uint8_t* data, size_t index) {
if(bswap) {
value = ((value & 0x00000000000000FF) << 56) |
((value & 0x000000000000FF00) << 40) |
((value & 0x0000000000FF0000) << 24) |
((value & 0x00000000FF000000) << 8) |
((value & 0x000000FF00000000) >> 8) |
((value & 0x0000FF0000000000) >> 24) |
((value & 0x00FF000000000000) >> 40) |
((value & 0xFF00000000000000) >> 56);
}
std::memcpy(data + index, &value, sizeof(value));
}