This repository has been archived by the owner on Nov 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathbyte_order.h
115 lines (98 loc) · 3.22 KB
/
byte_order.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
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
/*
* Copyright 2018 The Ripple Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MYSQL_RIPPLE_MYSQL_BYTE_ORDER_H
#define MYSQL_RIPPLE_MYSQL_BYTE_ORDER_H
#include <cassert>
#include <cstdint>
namespace mysql_ripple {
namespace byte_order {
static inline void store1(void *ptr, uint64_t val) {
assert(val < (uint64_t(1) << 8));
uint8_t *tmp = reinterpret_cast<uint8_t*>(ptr);
tmp[0] = (val >> 0) & 0xFF;
}
static inline void store2(void *ptr, uint64_t val) {
assert(val < (uint64_t(1) << 16));
uint8_t *tmp = reinterpret_cast<uint8_t*>(ptr);
tmp[0] = (val >> 0) & 0xFF;
tmp[1] = (val >> 8) & 0xFF;
}
static inline void store3(void *ptr, uint64_t val) {
assert(val < (uint64_t(1) << 24));
uint8_t *tmp = reinterpret_cast<uint8_t*>(ptr);
tmp[0] = (val >> 0) & 0xFF;
tmp[1] = (val >> 8) & 0xFF;
tmp[2] = (val >> 16) & 0xFF;
}
static inline void store4(void *ptr, uint64_t val) {
assert(val < (uint64_t(1) << 32));
uint8_t *tmp = reinterpret_cast<uint8_t*>(ptr);
tmp[0] = (val >> 0) & 0xFF;
tmp[1] = (val >> 8) & 0xFF;
tmp[2] = (val >> 16) & 0xFF;
tmp[3] = (val >> 24) & 0xFF;
}
static inline void store8(void *ptr, uint64_t val) {
uint8_t *tmp = reinterpret_cast<uint8_t*>(ptr);
tmp[0] = (val >> 0) & 0xFF;
tmp[1] = (val >> 8) & 0xFF;
tmp[2] = (val >> 16) & 0xFF;
tmp[3] = (val >> 24) & 0xFF;
tmp[4] = (val >> 32) & 0xFF;
tmp[5] = (val >> 40) & 0xFF;
tmp[6] = (val >> 48) & 0xFF;
tmp[7] = (val >> 56) & 0xFF;
}
static inline uint8_t load1(const void *ptr) {
const uint8_t *tmp = reinterpret_cast<const uint8_t*>(ptr);
return tmp[0];
}
static inline uint16_t load2(const void *ptr) {
const uint8_t *tmp = reinterpret_cast<const uint8_t*>(ptr);
return
(((uint16_t)tmp[0]) << 0) +
(((uint16_t)tmp[1]) << 8);
}
static inline uint32_t load3(const void *ptr) {
const uint8_t *tmp = reinterpret_cast<const uint8_t*>(ptr);
return
(((uint32_t)tmp[0]) << 0) +
(((uint32_t)tmp[1]) << 8) +
(((uint32_t)tmp[2]) << 16);
}
static inline uint32_t load4(const void *ptr) {
const uint8_t *tmp = reinterpret_cast<const uint8_t*>(ptr);
return
(((uint32_t)tmp[0]) << 0) +
(((uint32_t)tmp[1]) << 8) +
(((uint32_t)tmp[2]) << 16) +
(((uint32_t)tmp[3]) << 24);
}
static inline uint64_t load8(const void *ptr) {
const uint8_t *tmp = reinterpret_cast<const uint8_t*>(ptr);
return
(((uint64_t)tmp[0]) << 0) +
(((uint64_t)tmp[1]) << 8) +
(((uint64_t)tmp[2]) << 16) +
(((uint64_t)tmp[3]) << 24) +
(((uint64_t)tmp[4]) << 32) +
(((uint64_t)tmp[5]) << 40) +
(((uint64_t)tmp[6]) << 48) +
(((uint64_t)tmp[7]) << 56);
}
} // namespace byte_order
} // namespace mysql_ripple
#endif // MYSQL_RIPPLE_MYSQL_BYTE_ORDER_H