forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccalltest.c
433 lines (368 loc) · 9.5 KB
/
ccalltest.c
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <stdio.h>
#include <complex.h>
#include <stdint.h>
#include <inttypes.h>
int verbose = 1;
/*******************************************************************************
* * Compiler *
* *******************************************************************************/
/*
* * Notes:
* *
* * 1. Checking for Intel's compiler should be done before checking for
* * Microsoft's. On Windows Intel's compiler also defines _MSC_VER as the
* * acknoledgement of the fact that it is integrated with Visual Studio.
* *
* * 2. Checking for MinGW should be done before checking for GCC as MinGW
* * pretends to be GCC.
* */
#if defined(__clang__)
#define _COMPILER_CLANG_
#elif defined(__INTEL_COMPILER) || defined(__ICC)
#define _COMPILER_INTEL_
#elif defined(__MINGW32__)
#define _COMPILER_MINGW_
#elif defined(_MSC_VER)
#define _COMPILER_MICROSOFT_
#elif defined(__GNUC__)
#define _COMPILER_GCC_
#endif
/*******************************************************************************
* * OS *
* *******************************************************************************/
#if defined(__FreeBSD__)
#define _OS_FREEBSD__
#elif defined(__linux__)
#define _OS_LINUX_
#elif defined(_WIN32) || defined(_WIN64)
#define _OS_WINDOWS_
#elif defined(__APPLE__) && defined(__MACH__)
#define _OS_DARWIN_
#endif
/*******************************************************************************
* * Architecture *
* *******************************************************************************/
#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
#define _CPU_X86_64_
#elif defined(i386) || defined(__i386) || defined(__i386__) || defined(_M_IX86) || defined(_X86_)
#define _CPU_X86_
#elif defined(__arm__) || defined(_M_ARM)
#define _CPU_ARM_
#endif
#if defined(_CPU_X86_64_)
# define _P64
#elif defined(_CPU_X86_)
# define _P32
#elif defined(_OS_WINDOWS_)
/* Not sure how to determine pointer size on Windows running ARM. */
# if _WIN64
# define _P64
# else
# define _P32
# endif
#elif defined(_COMPILER_GCC_)
# if __x86_64__ || __ppc64__
# define _P64
# else
# define _P32
# endif
#else
# error pointer size not known for your platform / compiler
#endif
#ifdef _P64
#define jint int64_t
#else
#define jint int32_t
#endif
//////////////////////////////////
// Test for proper argument register truncation
int xs[300] = {0,0,0,1,0};
//int testUcharX(unsigned char x);
int __attribute__((noinline)) testUcharX(unsigned char x) {
return xs[x];
}
#define xstr(s) str(s)
#define str(s) #s
volatile int (*fptr)(unsigned char x);
volatile int a;
volatile int b;
//////////////////////////////////
// Tests for passing and returning Structs
// Complex-like data types
typedef struct {
jint real;
jint imag;
} complex_t;
complex_t ctest(complex_t a) {
a.real += 1;
a.imag -= 2;
return a;
}
complex double cgtest(complex double a) {
//Unpack a ComplexPair{Float64} struct
if (verbose) fprintf(stderr,"%g + %g i\n", creal(a), cimag(a));
a += 1 - (2.0*I);
return a;
}
complex double* cgptest(complex double *a) {
//Unpack a ComplexPair{Float64} struct
if (verbose) fprintf(stderr,"%g + %g i\n", creal(*a), cimag(*a));
*a += 1 - (2.0*I);
return a;
}
complex float cftest(complex float a) {
//Unpack a ComplexPair{Float32} struct
if (verbose) fprintf(stderr,"%g + %g i\n", creal(a), cimag(a));
a += 1 - (2.0*I);
return a;
}
complex float* cfptest(complex float *a) {
//Unpack a ComplexPair{Float64} struct
if (verbose) fprintf(stderr,"%g + %g i\n", creal(*a), cimag(*a));
*a += 1 - (2.0*I);
return a;
}
complex_t* cptest(complex_t *a) {
//Unpack a ComplexPair{Int} struct pointer
if (verbose) fprintf(stderr,"%lld + %lld i\n", (long long)a->real, (long long)a->imag);
a->real += 1;
a->imag -= 2;
return a;
}
// Native-like data types
char* stest(char *x) {
//Print a character Array
if (verbose) fprintf(stderr,"%s\n", x);
return x;
}
struct jl_asciistring_t { struct { void* type; char* data; } *data; };
char* sptest(struct jl_asciistring_t str) {
//Unpack an ASCIIString
return stest(str.data->data);
}
// Various sized data types
typedef struct {
float x;
double y;
} struct1;
typedef struct {
struct { int32_t x; } x;
struct { int32_t y; } y;
} struct2a;
typedef struct {
int32_t x;
int32_t y;
} struct2b;
typedef struct {
struct { int64_t x; } x;
struct { int64_t y; } y;
} struct3a;
typedef struct {
int64_t x;
int64_t y;
} struct3b;
typedef struct {
int32_t x;
int32_t y;
int32_t z;
} struct4;
typedef struct {
int32_t x;
int32_t y;
int32_t z;
int32_t a;
} struct5;
typedef struct {
int64_t x;
int64_t y;
int64_t z;
} struct6;
typedef struct {
int64_t x;
char y;
} struct7;
typedef struct {
int32_t x;
char y;
} struct8;
typedef struct {
int32_t x;
int16_t y;
} struct9;
typedef struct {
char x;
char y;
char z;
char a;
} struct10;
typedef struct {
complex float x;
} struct11;
typedef struct {
complex float x;
complex float y;
} struct12;
typedef struct {
complex double x;
} struct13;
typedef struct {
float x;
float y;
} struct14;
typedef struct {
double x;
double y;
} struct15;
typedef struct {
jint x;
jint y;
char z;
} struct_big;
struct1 test_1(struct1 a) {
//Unpack a "small" struct { float, double }
if (verbose) fprintf(stderr,"%g + %g i\n", a.x, a.y);
a.x += 1;
a.y -= 2;
return a;
}
struct1 add_1(struct1 a, struct1 b) {
// Two small structs
struct1 c;
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}
struct2a test_2a(struct2a a) {
//Unpack a ComplexPair{Int32} struct
if (verbose) fprintf(stderr,"%" PRId32 " + %" PRId32 " i\n", a.x.x, a.y.y);
a.x.x += 1;
a.y.y -= 2;
return a;
}
struct2b test_2b(struct2b a) {
//Unpack a ComplexPair{Int32} struct
if (verbose) fprintf(stderr,"%" PRId32 " + %" PRId32 " i\n", a.x, a.y);
a.x += 1;
a.y -= 2;
return a;
}
struct3a test_3a(struct3a a) {
//Unpack a ComplexPair{Int64} struct
if (verbose) fprintf(stderr,"%" PRId64 " + %" PRId64 " i\n", a.x.x, a.y.y);
a.x.x += 1;
a.y.y -= 2;
return a;
}
struct3b test_3b(struct3b a) {
//Unpack a ComplexPair{Int64} struct
if (verbose) fprintf(stderr,"%" PRId64 " + %" PRId64 " i\n", a.x, a.y);
a.x += 1;
a.y -= 2;
return a;
}
struct4 test_4(struct4 a)
{
if (verbose) fprintf(stderr,"(%" PRId32 ",%" PRId32 ",%" PRId32 ")\n", a.x, a.y, a.z);
a.x += 1;
a.y -= 2;
a.z += 3;
return a;
}
struct5 test_5(struct5 a)
{
if (verbose) fprintf(stderr,"(%" PRId32 ",%" PRId32 ",%" PRId32 ",%" PRId32 ")\n", a.x, a.y, a.z, a.a);
a.x += 1;
a.y -= 2;
a.z += 3;
a.a -= 4;
return a;
}
struct6 test_6(struct6 a)
{
if (verbose) fprintf(stderr,"(%" PRId64 ",%" PRId64 ",%" PRId64 ")\n", a.x, a.y, a.z);
a.x += 1;
a.y -= 2;
a.z += 3;
return a;
}
struct7 test_7(struct7 a)
{
if (verbose) fprintf(stderr,"(%" PRId64 ",%" PRId8 ")\n", a.x, a.y);
a.x += 1;
a.y -= 2;
return a;
}
struct8 test_8(struct8 a)
{
if (verbose) fprintf(stderr,"(%" PRId32 ",%" PRId8 ")\n", a.x, a.y);
a.x += 1;
a.y -= 2;
return a;
}
struct9 test_9(struct9 a)
{
if (verbose) fprintf(stderr,"(%" PRId32 ",%" PRId16 ")\n", a.x, a.y);
a.x += 1;
a.y -= 2;
return a;
}
struct10 test_10(struct10 a)
{
if (verbose) fprintf(stderr,"(%" PRId8 ",%" PRId8 ",%" PRId8 ",%" PRId8 ")\n", a.x, a.y, a.z, a.a);
a.x += 1;
a.y -= 2;
a.z += 3;
a.a -= 4;
return a;
}
struct14 test_14(struct14 a) {
//The C equivalent of a ComplexPair{Float32} struct (but without special complex ABI)
if (verbose) fprintf(stderr,"%g + %g i\n", a.x, a.y);
a.x += 1;
a.y -= 2;
return a;
}
struct15 test_15(struct15 a) {
//The C equivalent of a ComplexPair{Float32} struct (but without special complex ABI)
if (verbose) fprintf(stderr,"%g + %g i\n", a.x, a.y);
a.x += 1;
a.y -= 2;
return a;
}
#define int128_t struct3b
int128_t test_128(int128_t a) {
//Unpack a Int128
if (verbose) fprintf(stderr,"0x%016" PRIx64 "%016" PRIx64 "\n", a.y, a.x);
a.x += 1;
if (a.x == 0)
a.y += 1;
return a;
}
struct_big test_big(struct_big a) {
//Unpack a "big" struct { int, int, char }
if (verbose) fprintf(stderr,"%lld %lld %c\n", (long long)a.x, (long long)a.y, a.z);
a.x += 1;
a.y -= 2;
a.z -= 'A';
return a;
}
int main() {
fprintf(stderr,"all of the following should be 1 except xs[259] = 0\n");
a = 3;
b = 259;
fptr = (volatile int (*)(unsigned char x))&testUcharX;
if ((((size_t)fptr)&((size_t)1)) == 1) fptr = NULL;
fprintf(stderr,"compiled with: '%s'\nxs[3] = %d\nxs[259] = %d\ntestUcharX(3) = %d\ntestUcharX(%d) = %d\nfptr(3) = %d\nfptr(259) = %d\n",
xstr(CC), xs[a], xs[b], testUcharX(a), b, testUcharX((unsigned char)b), fptr(a), fptr(b));
fprintf(stderr,"misc tests:\n");
struct1 a = {352.39422e23, 19.287577};
a = test_1(a);
}
//////////////////////////////////
// Turn off verbose for automated tests, leave on for debugging
void set_verbose(int level) {
verbose = level;
}
void *test_echo_p(void *p) {
return p;
}