Debian Codegraph @

testsuite/libffi.closures/cls_24byte.c

raw
1 /* Area: ffi_call, closure_call
2 Purpose: Check structure passing with different structure size.
3 Depending on the ABI. Check overlapping.
4 Limitations: none.
5 PR: none.
6 Originator: <[email protected]> 20030828 */
7
8 /* { dg-do run } */
9 #include "ffitest.h"
10
11 typedef struct cls_struct_24byte {
12 double a;
13 double b;
14 int c;
15 float d;
16 } cls_struct_24byte;
17
18 cls_struct_24byte cls_struct_24byte_fn(struct cls_struct_24byte b0,
19 struct cls_struct_24byte b1,
20 struct cls_struct_24byte b2,
21 struct cls_struct_24byte b3)
22 {
23 struct cls_struct_24byte result;
24
25 result.a = b0.a + b1.a + b2.a + b3.a;
26 result.b = b0.b + b1.b + b2.b + b3.b;
27 result.c = b0.c + b1.c + b2.c + b3.c;
28 result.d = b0.d + b1.d + b2.d + b3.d;
29
30 printf("%g %g %d %g %g %g %d %g %g %g %d %g %g %g %d %g: %g %g %d %g\n",
31 b0.a, b0.b, b0.c, b0.d,
32 b1.a, b1.b, b1.c, b1.d,
33 b2.a, b2.b, b2.c, b2.d,
34 b3.a, b3.b, b3.c, b3.d,
35 result.a, result.b, result.c, result.d);
36 CHECK_DOUBLE_EQ(b0.a, 9);
37 CHECK_DOUBLE_EQ(b0.b, 2);
38 CHECK(b0.c == 6);
39 CHECK_FLOAT_EQ(b0.d, 5);
40
41 CHECK_DOUBLE_EQ(b1.a, 1);
42 CHECK_DOUBLE_EQ(b1.b, 2);
43 CHECK(b1.c == 3);
44 CHECK_FLOAT_EQ(b1.d, 7);
45
46 CHECK_DOUBLE_EQ(b2.a, 4);
47 CHECK_DOUBLE_EQ(b2.b, 5);
48 CHECK(b2.c == 7);
49 CHECK_FLOAT_EQ(b2.d, 9);
50
51 CHECK_DOUBLE_EQ(b3.a, 8);
52 CHECK_DOUBLE_EQ(b3.b, 6);
53 CHECK(b3.c == 1);
54 CHECK_FLOAT_EQ(b3.d, 4);
55
56 CHECK_DOUBLE_EQ(result.a, 22);
57 CHECK_DOUBLE_EQ(result.b, 15);
58 CHECK(result.c == 17);
59 CHECK_FLOAT_EQ(result.d, 25);
60
61 return result;
62 }
63
64 static void
65 cls_struct_24byte_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
66 void* userdata __UNUSED__)
67 {
68 struct cls_struct_24byte b0, b1, b2, b3;
69
70 b0 = *(struct cls_struct_24byte*)(args[0]);
71 b1 = *(struct cls_struct_24byte*)(args[1]);
72 b2 = *(struct cls_struct_24byte*)(args[2]);
73 b3 = *(struct cls_struct_24byte*)(args[3]);
74
75 *(cls_struct_24byte*)resp = cls_struct_24byte_fn(b0, b1, b2, b3);
76 }
77
78 int main (void)
79 {
80 ffi_cif cif;
81 void *code;
82 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
83 void* args_dbl[5];
84 ffi_type* cls_struct_fields[5];
85 ffi_type cls_struct_type;
86 ffi_type* dbl_arg_types[5];
87
88 struct cls_struct_24byte e_dbl = { 9.0, 2.0, 6, 5.0 };
89 struct cls_struct_24byte f_dbl = { 1.0, 2.0, 3, 7.0 };
90 struct cls_struct_24byte g_dbl = { 4.0, 5.0, 7, 9.0 };
91 struct cls_struct_24byte h_dbl = { 8.0, 6.0, 1, 4.0 };
92 struct cls_struct_24byte res_dbl;
93
94 cls_struct_type.size = 0;
95 cls_struct_type.alignment = 0;
96 cls_struct_type.type = FFI_TYPE_STRUCT;
97 cls_struct_type.elements = cls_struct_fields;
98
99 cls_struct_fields[0] = &ffi_type_double;
100 cls_struct_fields[1] = &ffi_type_double;
101 cls_struct_fields[2] = &ffi_type_sint;
102 cls_struct_fields[3] = &ffi_type_float;
103 cls_struct_fields[4] = NULL;
104
105 dbl_arg_types[0] = &cls_struct_type;
106 dbl_arg_types[1] = &cls_struct_type;
107 dbl_arg_types[2] = &cls_struct_type;
108 dbl_arg_types[3] = &cls_struct_type;
109 dbl_arg_types[4] = NULL;
110
111 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 4, &cls_struct_type,
112 dbl_arg_types) == FFI_OK);
113
114 args_dbl[0] = &e_dbl;
115 args_dbl[1] = &f_dbl;
116 args_dbl[2] = &g_dbl;
117 args_dbl[3] = &h_dbl;
118 args_dbl[4] = NULL;
119
120 ffi_call(&cif, FFI_FN(cls_struct_24byte_fn), &res_dbl, args_dbl);
121 /* { dg-output "9 2 6 5 1 2 3 7 4 5 7 9 8 6 1 4: 22 15 17 25" } */
122 printf("res: %g %g %d %g\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
123 /* { dg-output "\nres: 22 15 17 25" } */
124 CHECK_DOUBLE_EQ(res_dbl.a, 22);
125 CHECK_DOUBLE_EQ(res_dbl.b, 15);
126 CHECK(res_dbl.c == 17);
127 CHECK_FLOAT_EQ(res_dbl.d, 25);
128
129 CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_24byte_gn, NULL, code) == FFI_OK);
130
131 res_dbl = ((cls_struct_24byte(*)(cls_struct_24byte,
132 cls_struct_24byte,
133 cls_struct_24byte,
134 cls_struct_24byte))
135 (code))(e_dbl, f_dbl, g_dbl, h_dbl);
136 /* { dg-output "\n9 2 6 5 1 2 3 7 4 5 7 9 8 6 1 4: 22 15 17 25" } */
137 printf("res: %g %g %d %g\n", res_dbl.a, res_dbl.b, res_dbl.c, res_dbl.d);
138 /* { dg-output "\nres: 22 15 17 25" } */
139 CHECK_DOUBLE_EQ(res_dbl.a, 22);
140 CHECK_DOUBLE_EQ(res_dbl.b, 15);
141 CHECK(res_dbl.c == 17);
142 CHECK_FLOAT_EQ(res_dbl.d, 25);
143
144 exit(0);
145 }