]> git.lizzy.rs Git - rust.git/blob - src/test/auxiliary/rust_test_helpers.c
Merge pull request #1 from rust-lang/master
[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 quad
172 rust_dbg_abi_1(struct quad q) {
173     struct quad qq = { q.c + 1,
174                        q.d - 1,
175                        q.a + 1,
176                        q.b - 1 };
177     return qq;
178 }
179
180 struct floats
181 rust_dbg_abi_2(struct floats f) {
182     struct floats ff = { f.c + 1.0,
183                          0xff,
184                          f.a - 1.0 };
185     return ff;
186 }
187
188 int
189 rust_dbg_static_mut = 3;
190
191 void
192 rust_dbg_static_mut_check_four() {
193     assert(rust_dbg_static_mut == 4);
194 }
195
196 struct S {
197     uint64_t x;
198     uint64_t y;
199     uint64_t z;
200 };
201
202 uint64_t get_x(struct S s) {
203     return s.x;
204 }
205
206 uint64_t get_y(struct S s) {
207     return s.y;
208 }
209
210 uint64_t get_z(struct S s) {
211     return s.z;
212 }
213
214 uint64_t get_c_many_params(void *a, void *b, void *c, void *d, struct quad f) {
215     return f.c;
216 }
217
218 struct quad_floats {
219     float a;
220     float b;
221     float c;
222     float d;
223 };
224
225 float get_c_exhaust_sysv64_ints(
226     void *a,
227     void *b,
228     void *c,
229     void *d,
230     void *e,
231     void *f,
232     // `f` used the last integer register, so `g` goes on the stack.
233     // It also used to bring the "count of available integer registers" down to
234     // `-1` which broke the next SSE-only aggregate argument (`h`) - see #62350.
235     void *g,
236     struct quad_floats h
237 ) {
238     return h.c;
239 }
240
241 // Calculates the average of `(x + y) / n` where x: i64, y: f64. There must be exactly n pairs
242 // passed as variadic arguments. There are two versions of this function: the
243 // variadic one, and the one that takes a `va_list`.
244 double rust_valist_interesting_average(uint64_t n, va_list pairs) {
245     double sum = 0.0;
246     int i;
247     for(i = 0; i < n; i += 1) {
248         sum += (double)va_arg(pairs, int64_t);
249         sum += va_arg(pairs, double);
250     }
251     return sum / n;
252 }
253
254 double rust_interesting_average(uint64_t n, ...) {
255     double sum;
256     va_list pairs;
257     va_start(pairs, n);
258     sum = rust_valist_interesting_average(n, pairs);
259     va_end(pairs);
260     return sum;
261 }
262
263 int32_t rust_int8_to_int32(int8_t x) {
264     return (int32_t)x;
265 }
266
267 typedef union LARGE_INTEGER {
268   struct {
269     uint32_t LowPart;
270     uint32_t HighPart;
271   };
272   struct {
273     uint32_t LowPart;
274     uint32_t HighPart;
275   } u;
276   uint64_t QuadPart;
277 } LARGE_INTEGER;
278
279 LARGE_INTEGER increment_all_parts(LARGE_INTEGER li) {
280     li.LowPart += 1;
281     li.HighPart += 1;
282     li.u.LowPart += 1;
283     li.u.HighPart += 1;
284     li.QuadPart += 1;
285     return li;
286 }
287
288 #if __SIZEOF_INT128__ == 16
289
290 unsigned __int128 identity(unsigned __int128 a) {
291     return a;
292 }
293
294 __int128 square(__int128 a) {
295     return a * a;
296 }
297
298 __int128 sub(__int128 a, __int128 b) {
299     return a - b;
300 }
301
302 #endif