-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_sanity_typecasts.cpp
342 lines (272 loc) · 10.3 KB
/
test_sanity_typecasts.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "gtest/gtest.h"
#include "pochivm.h"
#include "test_util_helper.h"
using namespace PochiVM;
namespace {
void TestEachInterestingIntParam(const std::function<void(uint64_t)>& testFn)
{
const static uint64_t choice[4] = {
static_cast<uint64_t>(123),
static_cast<uint64_t>(-123),
static_cast<uint64_t>(0),
static_cast<uint64_t>(233)
};
for (int bitmask = 0; bitmask < (4 << 8); bitmask++)
{
uint64_t value = 0;
int x = bitmask;
for (int i = 0; i < 8; i++)
{
value += (choice[x % 4]) * (static_cast<uint64_t>(1) << (i * 8));
x /= 4;
}
testFn(value);
}
}
template<typename T, typename U>
std::function<U(T)> GetStaticCastFn()
{
using FnPrototype = U(*)(T);
auto [fn, val] = NewFunction<FnPrototype>("MyFn");
fn.SetBody(Return(StaticCast<U>(val)));
ReleaseAssert(thread_pochiVMContext->m_curModule->Validate());
thread_pochiVMContext->m_curModule->PrepareForDebugInterp();
thread_pochiVMContext->m_curModule->PrepareForFastInterp();
thread_pochiVMContext->m_curModule->EmitIR();
thread_pochiVMContext->m_curModule->OptimizeIRIfNotDebugMode(2 /*optLevel*/);
// TODO: this leaks. Fix later
//
SimpleJIT* jit = new SimpleJIT();
jit->SetModule(thread_pochiVMContext->m_curModule);
FnPrototype jitFn = jit->GetFunction<FnPrototype>("MyFn");
auto interpFn = thread_pochiVMContext->m_curModule->
GetDebugInterpGeneratedFunction<FnPrototype>("MyFn");
ReleaseAssert(interpFn);
FastInterpFunction<FnPrototype> fastInterpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("MyFn");
return [jitFn, interpFn, fastInterpFn](T value) -> U {
U out1 = interpFn(value);
U out2 = jitFn(value);
U out3 = fastInterpFn(value);
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfloat-equal"
// float equal is safe here: static cast is deterministic
//
ReleaseAssert(out1 == out2);
ReleaseAssert(out1 == out3);
#pragma clang diagnostic pop
return out1;
};
}
template<typename T, typename U>
void TestStaticCastBetweenIntTypes()
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
std::function<U(T)> fn = GetStaticCastFn<T, U>();
auto testFn = [&](uint64_t _v)
{
T value = static_cast<T>(_v);
ReleaseAssert(fn(value) == static_cast<U>(value));
};
TestEachInterestingIntParam(testFn);
}
template<typename T, typename U>
void TestStaticCastBetweenFloatTypes()
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
std::function<U(T)> fn = GetStaticCastFn<T, U>();
// float equal is safe here: static cast is deterministic
//
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfloat-equal"
auto testFn = [&](T value)
{
ReleaseAssert(fn(value) == static_cast<U>(value));
};
#pragma clang diagnostic pop
testFn(static_cast<T>(0));
testFn(static_cast<T>(123.45));
testFn(static_cast<T>(-123.45));
testFn(static_cast<T>(987.65));
testFn(static_cast<T>(-987.65));
}
template<typename T, typename U>
void TestStaticCastIntToFloat()
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
std::function<U(T)> fn = GetStaticCastFn<T, U>();
auto testFn = [&](uint64_t _v)
{
T value = static_cast<T>(_v);
U result = fn(value);
double diff = fabs(static_cast<double>(value) - static_cast<double>(result));
double tol = (std::is_same<U, double>::value) ? 1e-14 : 1e-6;
// If absolute diff < tol, just return. 'value' may be 0...
if (diff < tol) { return; }
double relDiff = diff / fabs(static_cast<double>(value));
ReleaseAssert(relDiff < tol);
};
TestEachInterestingIntParam(testFn);
}
void TestStaticCastPointerTypes()
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
// currently the only possible one is casting to void*
// TODO: add more when we support CPP classes
//
std::function<void*(int*)> fn = GetStaticCastFn<int*, void*>();
int x = 233;
int* px = &x;
ReleaseAssert(static_cast<void*>(px) == fn(px));
}
} // anonymous namespace
TEST(Sanity, StaticCast)
{
// Static cast between all primitive integer types
//
#define F(type) TestStaticCastBetweenIntTypes<bool, type>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
#define F(type) TestStaticCastBetweenIntTypes<int8_t, type>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
#define F(type) TestStaticCastBetweenIntTypes<uint8_t, type>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
#define F(type) TestStaticCastBetweenIntTypes<int16_t, type>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
#define F(type) TestStaticCastBetweenIntTypes<uint16_t, type>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
#define F(type) TestStaticCastBetweenIntTypes<int32_t, type>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
#define F(type) TestStaticCastBetweenIntTypes<uint32_t, type>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
#define F(type) TestStaticCastBetweenIntTypes<int64_t, type>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
#define F(type) TestStaticCastBetweenIntTypes<uint64_t, type>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
// Static cast between floating point types
//
TestStaticCastBetweenFloatTypes<float, float>();
TestStaticCastBetweenFloatTypes<float, double>();
TestStaticCastBetweenFloatTypes<double, float>();
TestStaticCastBetweenFloatTypes<double, double>();
// Static cast from int to floating point types
//
#define F(type) TestStaticCastIntToFloat<type, float>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
#define F(type) TestStaticCastIntToFloat<type, double>();
FOR_EACH_PRIMITIVE_INT_TYPE
#undef F
// Static cast on pointer types
//
TestStaticCastPointerTypes();
}
TEST(SanityError, StaticCastNullptrDisallowed)
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
std::function<void*(int*)> fn = GetStaticCastFn<int*, void*>();
#ifdef TESTBUILD
// static_cast on NULL (0) fires a TestAssert
// Google test somehow triggers a bunch of warnings...
//
printf("Expecting a TestAssert being fired...\n");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wused-but-marked-unused"
#pragma clang diagnostic ignored "-Wcovered-switch-default"
// TODO: This is not good enough, since we want to make sure both interpFn and generatedFn fires assert,
// and the death is caused by the assert we expected, not some random other failures.
// And furthermore, currently generatedFn does NOT fire the assert. Fix later.
//
ASSERT_DEATH(fn(reinterpret_cast<int*>(0)), "");
#pragma clang diagnostic pop
#endif
std::ignore = fn;
}
TEST(Sanity, ReinterpretCast_1)
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
double x = 123.4;
using FnPrototype = uint64_t*(*)(double*);
auto [fn, val] = NewFunction<FnPrototype>("MyFn");
fn.SetBody(Return(ReinterpretCast<uint64_t*>(val)));
ReleaseAssert(thread_pochiVMContext->m_curModule->Validate());
thread_pochiVMContext->m_curModule->PrepareForDebugInterp();
thread_pochiVMContext->m_curModule->PrepareForFastInterp();
auto interpFn = thread_pochiVMContext->m_curModule->
GetDebugInterpGeneratedFunction<FnPrototype>("MyFn");
FastInterpFunction<FnPrototype> fastinterpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("MyFn");
union {
double vd;
uint64_t vi;
} u;
u.vd = x;
ReleaseAssert(*interpFn(&x) == u.vi);
ReleaseAssert(*fastinterpFn(&x) == u.vi);
}
TEST(Sanity, ReinterpretCast_2)
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
double x = 123.4;
using FnPrototype = uint64_t(*)(double*);
auto [fn, val] = NewFunction<FnPrototype>("MyFn");
fn.SetBody(Return(ReinterpretCast<uint64_t>(val)));
ReleaseAssert(thread_pochiVMContext->m_curModule->Validate());
thread_pochiVMContext->m_curModule->PrepareForDebugInterp();
thread_pochiVMContext->m_curModule->PrepareForFastInterp();
auto interpFn = thread_pochiVMContext->m_curModule->
GetDebugInterpGeneratedFunction<FnPrototype>("MyFn");
FastInterpFunction<FnPrototype> fastinterpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("MyFn");
ReleaseAssert(interpFn(&x) == reinterpret_cast<uintptr_t>(&x));
ReleaseAssert(fastinterpFn(&x) == reinterpret_cast<uintptr_t>(&x));
}
TEST(Sanity, ReinterpretCast_3)
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
double x = 123.4;
using FnPrototype = double*(*)(uint64_t);
auto [fn, val] = NewFunction<FnPrototype>("MyFn");
fn.SetBody(Return(ReinterpretCast<double*>(val)));
ReleaseAssert(thread_pochiVMContext->m_curModule->Validate());
thread_pochiVMContext->m_curModule->PrepareForDebugInterp();
thread_pochiVMContext->m_curModule->PrepareForFastInterp();
auto interpFn = thread_pochiVMContext->m_curModule->
GetDebugInterpGeneratedFunction<FnPrototype>("MyFn");
FastInterpFunction<FnPrototype> fastinterpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("MyFn");
ReleaseAssert(interpFn(reinterpret_cast<uintptr_t>(&x)) == &x);
ReleaseAssert(fastinterpFn(reinterpret_cast<uintptr_t>(&x)) == &x);
}