forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ccalltest.c
375 lines (312 loc) · 8.34 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
// This file is a part of Julia. License is MIT: http:https://julialang.org/license
#include <stdio.h>
#include <stdlib.h>
#include <complex.h>
#include <stdint.h>
#include <inttypes.h>
#include "../src/support/platform.h"
#include "../src/support/dtypes.h"
#ifdef _P64
#define jint int64_t
#define PRIjint PRId64
#else
#define jint int32_t
#define PRIjint PRId32
#endif
int verbose = 1;
//////////////////////////////////
// Test for proper argument register truncation
int xs[300] = {0,0,0,1,0};
//int testUcharX(unsigned char x);
#ifdef _COMPILER_MICROSOFT_
int __declspec(noinline)
#else
int __attribute__((noinline))
#endif
JL_DLLEXPORT testUcharX(unsigned char x) {
return xs[x];
}
//////////////////////////////////
// Tests for passing and returning Structs
// Complex-like data types
typedef struct {
jint real;
jint imag;
} complex_t;
JL_DLLEXPORT complex_t ctest(complex_t a) {
a.real += 1;
a.imag -= 2;
return a;
}
JL_DLLEXPORT 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;
}
JL_DLLEXPORT 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;
}
JL_DLLEXPORT 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;
}
JL_DLLEXPORT 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;
}
JL_DLLEXPORT complex_t *cptest(complex_t *a) {
//Unpack a ComplexPair{Int} struct pointer
if (verbose) fprintf(stderr,"%" PRIjint " + %" PRIjint " i\n", a->real, a->imag);
a->real += 1;
a->imag -= 2;
return a;
}
JL_DLLEXPORT complex_t *cptest_static(complex_t *a) {
complex_t *b = (complex_t*)malloc(sizeof(complex_t));
b->real = a->real;
b->imag = a->imag;
return b;
}
// 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 {
float x,y,z;
double a,b,c;
} struct16;
typedef struct {
jint x;
jint y;
char z;
} struct_big;
JL_DLLEXPORT struct1 test_1(struct1 a, float b) {
//Unpack a "small" struct { float, double }
if (verbose) fprintf(stderr,"%g + %g i & %g\n", a.x, a.y, b);
a.x += b * 1;
a.y -= b * 2;
return a;
}
JL_DLLEXPORT struct2a test_2a(struct2a a, int32_t b) {
//Unpack a ComplexPair{Int32} struct
if (verbose) fprintf(stderr,"%" PRId32 " + %" PRId32 " i & %" PRId32 "\n", a.x.x, a.y.y, b);
a.x.x += b*1;
a.y.y -= b*2;
return a;
}
JL_DLLEXPORT struct2b test_2b(struct2b a, int32_t b) {
//Unpack a ComplexPair{Int32} struct
if (verbose) fprintf(stderr,"%" PRId32 " + %" PRId32 " i & %" PRId32 "\n", a.x, a.y, b);
a.x += b*1;
a.y -= b*2;
return a;
}
JL_DLLEXPORT struct3a test_3a(struct3a a, int64_t b) {
//Unpack a ComplexPair{Int64} struct
if (verbose) fprintf(stderr,"%" PRId64 " + %" PRId64 " i & %" PRId64 "\n", a.x.x, a.y.y, b);
a.x.x += b*1;
a.y.y -= b*2;
return a;
}
JL_DLLEXPORT struct3b test_3b(struct3b a, int64_t b) {
//Unpack a ComplexPair{Int64} struct
if (verbose) fprintf(stderr,"%" PRId64 " + %" PRId64 " i & %" PRId64 "\n", a.x, a.y, b);
a.x += b*1;
a.y -= b*2;
return a;
}
JL_DLLEXPORT struct4 test_4(struct4 a, int32_t b) {
if (verbose) fprintf(stderr,"%" PRId32 ",%" PRId32 ",%" PRId32 " & %" PRId32 "\n", a.x, a.y, a.z, b);
a.x += b*1;
a.y -= b*2;
a.z += b*3;
return a;
}
JL_DLLEXPORT struct5 test_5(struct5 a, int32_t b) {
if (verbose) fprintf(stderr,"%" PRId32 ",%" PRId32 ",%" PRId32 ",%" PRId32 " & %" PRId32 "\n", a.x, a.y, a.z, a.a, b);
a.x += b*1;
a.y -= b*2;
a.z += b*3;
a.a -= b*4;
return a;
}
JL_DLLEXPORT struct6 test_6(struct6 a, int64_t b) {
if (verbose) fprintf(stderr,"%" PRId64 ",%" PRId64 ",%" PRId64 " & %" PRId64 "\n", a.x, a.y, a.z, b);
a.x += b*1;
a.y -= b*2;
a.z += b*3;
return a;
}
JL_DLLEXPORT struct7 test_7(struct7 a, int8_t b) {
if (verbose) fprintf(stderr,"%" PRId64 ",%" PRId8 " & %" PRId8 "\n", a.x, a.y, b);
a.x += b*1;
a.y -= b*2;
return a;
}
JL_DLLEXPORT struct8 test_8(struct8 a, int8_t b) {
if (verbose) fprintf(stderr,"%" PRId32 ",%" PRId8 " & %" PRId8 "\n", a.x, a.y, b);
a.x += b*1;
a.y -= b*2;
return a;
}
JL_DLLEXPORT struct9 test_9(struct9 a, int16_t b) {
if (verbose) fprintf(stderr,"%" PRId32 ",%" PRId16 " & %" PRId16 "\n", a.x, a.y, b);
a.x += b*1;
a.y -= b*2;
return a;
}
JL_DLLEXPORT struct10 test_10(struct10 a, int8_t b) {
if (verbose) fprintf(stderr,"%" PRId8 ",%" PRId8 ",%" PRId8 ",%" PRId8 " & %" PRId8 "\n", a.x, a.y, a.z, a.a, b);
a.x += b*1;
a.y -= b*2;
a.z += b*3;
a.a -= b*4;
return a;
}
JL_DLLEXPORT struct11 test_11(struct11 a, float b) {
//Unpack a nested ComplexPair{Float32} struct
if (verbose) fprintf(stderr,"%g + %g i & %g\n", creal(a.x), cimag(a.x), b);
a.x += b*1 - (b*2.0*I);
return a;
}
JL_DLLEXPORT struct12 test_12(struct12 a, float b) {
//Unpack two nested ComplexPair{Float32} structs
if (verbose) fprintf(stderr,"%g + %g i & %g + %g i & %g\n",
creal(a.x), cimag(a.x), creal(a.y), cimag(a.y), b);
a.x += b*1 - (b*2.0*I);
a.y += b*3 - (b*4.0*I);
return a;
}
JL_DLLEXPORT struct13 test_13(struct13 a, double b) {
//Unpack a nested ComplexPair{Float64} struct
if (verbose) fprintf(stderr,"%g + %g i & %g\n", creal(a.x), cimag(a.x), b);
a.x += b*1 - (b*2.0*I);
return a;
}
JL_DLLEXPORT struct14 test_14(struct14 a, float b) {
//The C equivalent of a ComplexPair{Float32} struct (but without special complex ABI)
if (verbose) fprintf(stderr,"%g + %g i & %g\n", a.x, a.y, b);
a.x += b*1;
a.y -= b*2;
return a;
}
JL_DLLEXPORT struct15 test_15(struct15 a, double b) {
//The C equivalent of a ComplexPair{Float64} struct (but without special complex ABI)
if (verbose) fprintf(stderr,"%g + %g i & %g\n", a.x, a.y, b);
a.x += b*1;
a.y -= b*2;
return a;
}
JL_DLLEXPORT struct16 test_16(struct16 a, float b) {
//Unpack a struct with non-obvious packing requirements
if (verbose) fprintf(stderr,"%g %g %g %g %g %g & %g\n", a.x, a.y, a.z, a.a, a.b, a.c, b);
a.x += b*1;
a.y -= b*2;
a.z += b*3;
a.a -= b*4;
a.b += b*5;
a.c -= b*6;
return a;
}
#define int128_t struct3b
JL_DLLEXPORT int128_t test_128(int128_t a, int64_t b) {
//Unpack a Int128
if (verbose) fprintf(stderr,"0x%016" PRIx64 "%016" PRIx64 " & %" PRId64 "\n", a.y, a.x, b);
a.x += b*1;
if (a.x == 0)
a.y += b*1;
return a;
}
JL_DLLEXPORT struct_big test_big(struct_big a) {
//Unpack a "big" struct { int, int, char }
if (verbose) fprintf(stderr,"%" PRIjint " %" PRIjint " %c\n", a.x, a.y, a.z);
a.x += 1;
a.y -= 2;
a.z -= 'A';
return a;
}
//////////////////////////////////
// Turn off verbose for automated tests, leave on for debugging
JL_DLLEXPORT void set_verbose(int level) {
verbose = level;
}
JL_DLLEXPORT void *test_echo_p(void *p) {
return p;
}