forked from rui314/mold
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbyteorder.h
72 lines (56 loc) · 1.25 KB
/
byteorder.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
#pragma once
#include <cstdint>
namespace mold {
template <typename T>
class Int {
public:
Int() : Int(0) {}
Int(T x) {
*this = x;
}
operator T() const {
T ret = 0;
for (int i = 0; i < sizeof(T); i++)
ret = (ret << 8) | val[i];
return ret;
}
Int &operator=(T x) {
for (int i = 0; i < sizeof(T); i++)
val[sizeof(T) - 1 - i] = x >> (i * 8);
return *this;
}
Int &operator++() {
return *this = *this + 1;
}
Int operator++(int) {
T ret = *this;
*this = *this + 1;
return ret;
}
Int &operator--() {
return *this = *this - 1;
}
Int operator--(int) {
T ret = *this;
*this = *this - 1;
return ret;
}
Int &operator+=(T x) {
return *this = *this + x;
}
Int &operator&=(T x) {
return *this = *this & x;
}
Int &operator|=(T x) {
return *this = *this | x;
}
private:
uint8_t val[sizeof(T)];
};
class ibig16 : public Int<int16_t> { using Int::Int; };
class ibig32 : public Int<int32_t> { using Int::Int; };
class ibig64 : public Int<int64_t> { using Int::Int; };
class ubig16 : public Int<uint16_t> { using Int::Int; };
class ubig32 : public Int<uint32_t> { using Int::Int; };
class ubig64 : public Int<uint64_t> { using Int::Int; };
} // namespace mold