forked from qicosmos/cosmos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nan-boxing.hpp
175 lines (148 loc) · 3.38 KB
/
Nan-boxing.hpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#pragma once
#include <cstddef>
#include <assert.h>
#include <cstdint>
using namespace std;
enum TypeTag {
BOOL,
INT32,
UINT32,
UINT64,
INT64,
DOUBLE,
CHAR_ARRAY,
STRING,
NIL
};
#define PAYLOAD_MASK 0x00007FFFFFFFFFFFULL
#define NAN_MASK 0x7FF8000000000000ULL
#define TAG_MASK 0xF
#define TAG_SHIFT 47
union Value {
uint64_t ival;
double fval;
Value(double x) : fval(x)
{
}
Value(int v)
{
ival = NAN_MASK | ((uint64_t)INT32 << TAG_SHIFT) | (uint64_t)v;
}
Value(uint32_t v)
{
ival = NAN_MASK | ((uint64_t)UINT32 << TAG_SHIFT) | (uint64_t)v;
}
Value(int64_t v)
{
ival = static_cast<uint64_t>(v);
}
Value(uint64_t v)
{
ival = v;
}
Value(bool v)
{
ival = NAN_MASK | ((uint64_t)BOOL << TAG_SHIFT) | (uint64_t)v;
}
Value(const char* v)
{
ival = NAN_MASK | ((uint64_t)CHAR_ARRAY << TAG_SHIFT) | (uint64_t)v;
}
Value(const string& v)
{
ival = NAN_MASK | ((uint64_t)STRING << TAG_SHIFT) | (uint64_t)&v;
}
Value(TypeTag tag = NIL, void *payload = nullptr) {
assert((uint64_t)payload <= PAYLOAD_MASK);
ival = NAN_MASK | ((uint64_t)tag << TAG_SHIFT) | (uint64_t)payload;
}
int toInt() const
{
assert(getTag() == INT32);
return (int)getPayload();
}
int64_t toInt64() const
{
return (int64_t)ival;
}
uint32_t toUInt() const
{
assert(getTag() == UINT32);
return (int)getPayload();
}
uint64_t toUInt64() const
{
return ival;
}
bool toBool() const
{
assert(getTag() == BOOL);
return getPayload() != 0;
}
double toDouble() const
{
assert(getTag() == DOUBLE);
return fval;
}
char *toCharArray() const
{
assert(getTag() == CHAR_ARRAY);
return (char *)getPayload();
}
string& toString() const
{
assert(getTag() == STRING);
return *(string *)getPayload();
}
TypeTag getTag() const
{
return isPayload() ? DOUBLE : TypeTag((ival >> TAG_SHIFT) & TAG_MASK);
}
uint64_t getPayload() const
{
assert(!isPayload());
return ival & PAYLOAD_MASK;
}
bool operator<(const Value& other) const
{
return hash()<other.hash();
}
bool operator==(const Value& other) const
{
return hash() == other.hash();
}
private:
bool isPayload() const
{
return (int64_t)ival <= (int64_t)NAN_MASK;
}
uint64_t toHashUInt64() const
{
assert(getTag() < INT64);
if (getTag() == UINT64)
return ival;
return (uint64_t)getPayload();
}
int64_t toHashInt64() const
{
assert(getTag() == INT64);
return toInt64();
}
std::size_t hash() const {
switch (getTag()) {
case UINT64:
case INT32:
case UINT32:
case BOOL:
return std::hash<uint64_t>()(toHashUInt64());
case INT64:
return std::hash<int64_t>()(toHashInt64());
case DOUBLE:
return std::hash<double>()(toDouble());
case STRING:
return std::hash<std::string>()(toString());
default:
throw std::invalid_argument("can not find this type");
}
}
};