-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnan_union.hpp
173 lines (129 loc) · 4.44 KB
/
nan_union.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
#ifndef CPP_NAN_UNION_HPP
#define CPP_NAN_UNION_HPP
#include <bitset>
// pick the right index/constructor from a value
template<std::size_t size, class ... T> struct selector;
template<std::size_t size> struct selector<size> {
static void index() { }
static void construct() { }
};
template<std::size_t size, class H, class ... T>
struct selector<size, H, T...> : selector<size, T...> {
using selector<size, T...>::index;
static constexpr std::integral_constant<std::size_t,
size - (1 + sizeof...(T))>
index(const H& ) {
return {};
}
};
// a nan-tagged ieee754 double
union ieee754 {
double value;
using payload_type = std::uint64_t;
using tag_type = unsigned char;
struct bits_type {
payload_type payload : 48;
tag_type tag : 3;
unsigned int nan : 12;
bool flag : 1;
} bits;
static const unsigned int quiet_nan = (1 << 12) - 1;
ieee754() { }
std::bitset<64> debug;
};
template<class ... T>
class nan_union {
ieee754 data;
using selector_type = selector<sizeof...(T), T...>;
using payload_type = ieee754::payload_type;
public:
template<class U, unsigned index = decltype( selector_type::index( std::declval<U>() ) )::value>
static constexpr unsigned type_index() { return index; }
template<class U> static void destruct(payload_type self) {
reinterpret_cast<U&>(self).~U();
};
void release() {
using thunk_type = void (*) (payload_type);
static const thunk_type thunk[] = { destruct<T>... };
thunk[data.bits.tag](data.bits.payload);
}
template<class U, class ... Args, unsigned type = type_index<U>()>
void construct(Args&& ... args) {
using value_type = typename std::decay<U>::type;
data.bits.nan = ieee754::quiet_nan;
data.bits.tag = type;
data.bits.flag = !std::is_trivially_destructible< value_type >();
payload_type tmp;
new (&tmp) value_type(std::forward<Args>(args)...);
data.bits.payload = tmp;
}
template<class U>
static void copy_construct(nan_union& self, const nan_union& other) {
self.construct<U>(other.template get<U>());
}
template<class U>
static void move_construct(nan_union& self, nan_union&& other) {
self.construct<U>(std::move(other.template get<U>()));
}
public:
nan_union(const double& value) noexcept { data.value = value; }
template<class U, unsigned R = type_index<U>() >
nan_union(U&& value) { construct<U>( std::forward<U>(value)); }
template<class U, int type = type_index<U>() >
nan_union& operator=(U&& x) noexcept {
const bool same_type = data.bits.tag == type;
// need to release if assigning from different types and release
// bit is set
if(data.bits.nan && same_type && data.bits.flag) {
release();
}
if(same_type) {
payload_type tmp;
reinterpret_cast<U&>(tmp) = std::forward<U>(x);
data.bits.payload = tmp;
} else {
construct<U>(std::forward<U>(x));
}
return *this;
}
nan_union& operator=(const double& x) noexcept {
if(data.bits.nan && data.bits.flag) release();
data.value = x;
return *this;
}
~nan_union() noexcept {
if(data.bits.nan && data.bits.flag) release();
}
nan_union(const nan_union& other) noexcept {
static constexpr bool fast[] = { std::is_trivially_copy_constructible<T>::value... };
if(!other.data.bits.nan || fast[other.data.bits.type]) {
data.value = other.data.value;
return;
}
using thunk_type = void (*) (nan_union&, const nan_union& other);
static const thunk_type thunk[] = { copy_construct<T>... };
thunk[other.data.bits.type](*this, other);
}
nan_union(nan_union&& other) noexcept {
static constexpr bool fast[] = { std::is_trivially_move_constructible<T>::value... };
if(!other.data.bits.nan || fast[other.data.bits.type]) {
data.value = other.data.value;
return;
}
using thunk_type = void (*) (nan_union&, nan_union&& other);
static const thunk_type thunk[] = { move_construct<T>... };
thunk[other.data.bits.type](*this, std::move(other));
}
template<class U, int type = type_index<U>() >
U get() const {
if( std::is_same<U, double>::value && !data.bits.nan ) {
return reinterpret_cast<const U&>(data.value);
}
if( data.bits.nan && data.bits.tag == type ) {
const payload_type payload = data.bits.payload;
return reinterpret_cast<const U&>(payload);
}
throw std::bad_cast();
}
};
#endif