Debian Codegraph @

testsuite/libffi.call/struct_by_value_4f.c

raw
1 /* Area: ffi_call
2 Purpose: Check structures.
3 Limitations: none.
4 PR: none.
5 Originator: From the original ffitest.c */
6
7 /* { dg-do run } */
8 #include "ffitest.h"
9
10 typedef struct
11 {
12 float f01;
13 float f02;
14 float f03;
15 float f04;
16 } test_structure_1;
17
18 static test_structure_1 ABI_ATTR struct1(test_structure_1 ts)
19 {
20 ts.f04++;
21
22 return ts;
23 }
24
25 int main (void)
26 {
27 ffi_cif cif;
28 ffi_type *args[MAX_ARGS];
29 void *values[MAX_ARGS];
30 ffi_type ts1_type;
31 ffi_type *ts1_type_elements[5];
32
33 test_structure_1 ts1_arg;
34
35 /* This is a hack to get a properly aligned result buffer */
36 test_structure_1 *ts1_result =
37 (test_structure_1 *) malloc (sizeof(test_structure_1));
38
39 ts1_type.size = 0;
40 ts1_type.alignment = 0;
41 ts1_type.type = FFI_TYPE_STRUCT;
42 ts1_type.elements = ts1_type_elements;
43 ts1_type_elements[0] = &ffi_type_float;
44 ts1_type_elements[1] = &ffi_type_float;
45 ts1_type_elements[2] = &ffi_type_float;
46 ts1_type_elements[3] = &ffi_type_float;
47 ts1_type_elements[4] = NULL;
48
49 args[0] = &ts1_type;
50 values[0] = &ts1_arg;
51
52 /* Initialize the cif */
53 CHECK(ffi_prep_cif(&cif, ABI_NUM, 1,
54 &ts1_type, args) == FFI_OK);
55
56 ts1_arg.f04 = 555.5;
57
58 ffi_call(&cif, FFI_FN(struct1), ts1_result, values);
59
60 CHECK(fabs(ts1_result->f04 - 556.5) < FLT_EPSILON);
61
62 /* This will fail if ffi_call isn't passing the struct by value. */
63 CHECK(fabs(ts1_arg.f04 - 555.5) < FLT_EPSILON);
64
65 free (ts1_result);
66 exit(0);
67 }