]> git.lizzy.rs Git - rust.git/blob - src/test/auxiliary/rust_test_helpers.c
Rollup merge of #71843 - sfackler:cas-loop-cleanup, r=dtolnay
[rust.git] / src / test / auxiliary / rust_test_helpers.c
1 // Helper functions used only in tests
2
3 #include <stdint.h>
4 #include <assert.h>
5 #include <stdarg.h>
6
7 // These functions are used in the unit tests for C ABI calls.
8
9 uint32_t
10 rust_dbg_extern_identity_u32(uint32_t u) {
11     return u;
12 }
13
14 uint64_t
15 rust_dbg_extern_identity_u64(uint64_t u) {
16     return u;
17 }
18
19 double
20 rust_dbg_extern_identity_double(double u) {
21     return u;
22 }
23
24 char
25 rust_dbg_extern_identity_u8(char u) {
26     return u;
27 }
28
29 typedef void *(*dbg_callback)(void*);
30
31 void *
32 rust_dbg_call(dbg_callback cb, void *data) {
33     return cb(data);
34 }
35
36 void rust_dbg_do_nothing() { }
37
38 struct TwoU8s {
39     uint8_t one;
40     uint8_t two;
41 };
42
43 struct TwoU8s
44 rust_dbg_extern_return_TwoU8s() {
45     struct TwoU8s s;
46     s.one = 10;
47     s.two = 20;
48     return s;
49 }
50
51 struct TwoU8s
52 rust_dbg_extern_identity_TwoU8s(struct TwoU8s u) {
53     return u;
54 }
55
56 struct TwoU16s {
57     uint16_t one;
58     uint16_t two;
59 };
60
61 struct TwoU16s
62 rust_dbg_extern_return_TwoU16s() {
63     struct TwoU16s s;
64     s.one = 10;
65     s.two = 20;
66     return s;
67 }
68
69 struct TwoU16s
70 rust_dbg_extern_identity_TwoU16s(struct TwoU16s u) {
71     return u;
72 }
73
74 struct TwoU32s {
75     uint32_t one;
76     uint32_t two;
77 };
78
79 struct TwoU32s
80 rust_dbg_extern_return_TwoU32s() {
81     struct TwoU32s s;
82     s.one = 10;
83     s.two = 20;
84     return s;
85 }
86
87 struct TwoU32s
88 rust_dbg_extern_identity_TwoU32s(struct TwoU32s u) {
89     return u;
90 }
91
92 struct TwoU64s {
93     uint64_t one;
94     uint64_t two;
95 };
96
97 struct TwoU64s
98 rust_dbg_extern_return_TwoU64s() {
99     struct TwoU64s s;
100     s.one = 10;
101     s.two = 20;
102     return s;
103 }
104
105 struct TwoU64s
106 rust_dbg_extern_identity_TwoU64s(struct TwoU64s u) {
107     return u;
108 }
109
110 struct TwoDoubles {
111     double one;
112     double two;
113 };
114
115 struct TwoDoubles
116 rust_dbg_extern_identity_TwoDoubles(struct TwoDoubles u) {
117     return u;
118 }
119
120 struct ManyInts {
121     int8_t arg1;
122     int16_t arg2;
123     int32_t arg3;
124     int16_t arg4;
125     int8_t arg5;
126     struct TwoU8s arg6;
127 };
128
129 // MSVC doesn't allow empty structs or unions
130 #ifndef _MSC_VER
131 struct Empty {
132 };
133
134 void
135 rust_dbg_extern_empty_struct(struct ManyInts v1, struct Empty e, struct ManyInts v2) {
136     assert(v1.arg1 == v2.arg1 + 1);
137     assert(v1.arg2 == v2.arg2 + 1);
138     assert(v1.arg3 == v2.arg3 + 1);
139     assert(v1.arg4 == v2.arg4 + 1);
140     assert(v1.arg5 == v2.arg5 + 1);
141     assert(v1.arg6.one == v2.arg6.one + 1);
142     assert(v1.arg6.two == v2.arg6.two + 1);
143 }
144 #endif
145
146 intptr_t
147 rust_get_test_int() {
148   return 1;
149 }
150
151 char *
152 rust_get_null_ptr() {
153     return 0;
154 }
155
156 // Debug helpers strictly to verify ABI conformance.
157
158 struct quad {
159     uint64_t a;
160     uint64_t b;
161     uint64_t c;
162     uint64_t d;
163 };
164
165 struct floats {
166     double a;
167     uint8_t b;
168     double c;
169 };
170
171 struct char_char_double {
172     uint8_t a;
173     uint8_t b;
174     double c;
175 };
176
177 struct char_char_float {
178     uint8_t a;
179     uint8_t b;
180     float c;
181 };
182
183 struct quad
184 rust_dbg_abi_1(struct quad q) {
185     struct quad qq = { q.c + 1,
186                        q.d - 1,
187                        q.a + 1,
188                        q.b - 1 };
189     return qq;
190 }
191
192 struct floats
193 rust_dbg_abi_2(struct floats f) {
194     struct floats ff = { f.c + 1.0,
195                          0xff,
196                          f.a - 1.0 };
197     return ff;
198 }
199
200 struct char_char_double
201 rust_dbg_abi_3(struct char_char_double a) {
202     struct char_char_double ccd = { a.a + 1,
203                                     a.b - 1,
204                                     a.c + 1.0 };
205     return ccd;
206 }
207
208 struct char_char_float
209 rust_dbg_abi_4(struct char_char_float a) {
210     struct char_char_float ccd = { a.a + 1,
211                                    a.b - 1,
212                                    a.c + 1.0 };
213     return ccd;
214 }
215
216
217 int
218 rust_dbg_static_mut = 3;
219
220 void
221 rust_dbg_static_mut_check_four() {
222     assert(rust_dbg_static_mut == 4);
223 }
224
225 struct S {
226     uint64_t x;
227     uint64_t y;
228     uint64_t z;
229 };
230
231 uint64_t get_x(struct S s) {
232     return s.x;
233 }
234
235 uint64_t get_y(struct S s) {
236     return s.y;
237 }
238
239 uint64_t get_z(struct S s) {
240     return s.z;
241 }
242
243 uint64_t get_c_many_params(void *a, void *b, void *c, void *d, struct quad f) {
244     return f.c;
245 }
246
247 struct quad_floats {
248     float a;
249     float b;
250     float c;
251     float d;
252 };
253
254 float get_c_exhaust_sysv64_ints(
255     void *a,
256     void *b,
257     void *c,
258     void *d,
259     void *e,
260     void *f,
261     // `f` used the last integer register, so `g` goes on the stack.
262     // It also used to bring the "count of available integer registers" down to
263     // `-1` which broke the next SSE-only aggregate argument (`h`) - see #62350.
264     void *g,
265     struct quad_floats h
266 ) {
267     return h.c;
268 }
269
270 // Calculates the average of `(x + y) / n` where x: i64, y: f64. There must be exactly n pairs
271 // passed as variadic arguments. There are two versions of this function: the
272 // variadic one, and the one that takes a `va_list`.
273 double rust_valist_interesting_average(uint64_t n, va_list pairs) {
274     double sum = 0.0;
275     int i;
276     for(i = 0; i < n; i += 1) {
277         sum += (double)va_arg(pairs, int64_t);
278         sum += va_arg(pairs, double);
279     }
280     return sum / n;
281 }
282
283 double rust_interesting_average(uint64_t n, ...) {
284     double sum;
285     va_list pairs;
286     va_start(pairs, n);
287     sum = rust_valist_interesting_average(n, pairs);
288     va_end(pairs);
289     return sum;
290 }
291
292 int32_t rust_int8_to_int32(int8_t x) {
293     return (int32_t)x;
294 }
295
296 typedef union LARGE_INTEGER {
297   struct {
298     uint32_t LowPart;
299     uint32_t HighPart;
300   };
301   struct {
302     uint32_t LowPart;
303     uint32_t HighPart;
304   } u;
305   uint64_t QuadPart;
306 } LARGE_INTEGER;
307
308 LARGE_INTEGER increment_all_parts(LARGE_INTEGER li) {
309     li.LowPart += 1;
310     li.HighPart += 1;
311     li.u.LowPart += 1;
312     li.u.HighPart += 1;
313     li.QuadPart += 1;
314     return li;
315 }
316
317 #if __SIZEOF_INT128__ == 16
318
319 unsigned __int128 identity(unsigned __int128 a) {
320     return a;
321 }
322
323 __int128 square(__int128 a) {
324     return a * a;
325 }
326
327 __int128 sub(__int128 a, __int128 b) {
328     return a - b;
329 }
330
331 #endif
332
333 #define OPTION_TAG_NONE (0)
334 #define OPTION_TAG_SOME (1)
335
336 struct U8TaggedEnumOptionU64 {
337     uint8_t tag;
338     union {
339         uint64_t some;
340     };
341 };
342
343 struct U8TaggedEnumOptionU64
344 rust_dbg_new_some_u64(uint64_t some) {
345     struct U8TaggedEnumOptionU64 r = {
346         .tag = OPTION_TAG_SOME,
347         .some = some,
348     };
349     return r;
350 }
351
352 struct U8TaggedEnumOptionU64
353 rust_dbg_new_none_u64(void) {
354     struct U8TaggedEnumOptionU64 r = {
355         .tag = OPTION_TAG_NONE,
356     };
357     return r;
358 }
359
360 int32_t
361 rust_dbg_unpack_option_u64(struct U8TaggedEnumOptionU64 o, uint64_t *into) {
362     assert(into);
363     switch (o.tag) {
364     case OPTION_TAG_SOME:
365         *into = o.some;
366         return 1;
367     case OPTION_TAG_NONE:
368         return 0;
369     default:
370         assert(0 && "unexpected tag");
371         return 0;
372     }
373 }
374
375 struct U8TaggedEnumOptionU64U64 {
376     uint8_t tag;
377     union {
378         struct {
379             uint64_t a;
380             uint64_t b;
381         } some;
382     };
383 };
384
385 struct U8TaggedEnumOptionU64U64
386 rust_dbg_new_some_u64u64(uint64_t a, uint64_t b) {
387     struct U8TaggedEnumOptionU64U64 r = {
388         .tag = OPTION_TAG_SOME,
389         .some = { .a = a, .b = b },
390     };
391     return r;
392 }
393
394 struct U8TaggedEnumOptionU64U64
395 rust_dbg_new_none_u64u64(void) {
396     struct U8TaggedEnumOptionU64U64 r = {
397         .tag = OPTION_TAG_NONE,
398     };
399     return r;
400 }
401
402 int32_t
403 rust_dbg_unpack_option_u64u64(struct U8TaggedEnumOptionU64U64 o, uint64_t *a, uint64_t *b) {
404     assert(a);
405     assert(b);
406     switch (o.tag) {
407     case OPTION_TAG_SOME:
408         *a = o.some.a;
409         *b = o.some.b;
410         return 1;
411     case OPTION_TAG_NONE:
412         return 0;
413     default:
414         assert(0 && "unexpected tag");
415         return 0;
416     }
417 }