1 /*	Area:			ffi_call, closure_call
2 	Purpose:		Check structure alignment of long double.
3 	Limitations:	none.
4 	PR:				none.
5 	Originator:		Blake Chaffin	6/18/2007
6 */
7 
8 /* { dg-do run { xfail strongarm*-*-* } } */
9 /* { dg-options -mlong-double-128 { target powerpc64*-*-linux* } } */
10 
11 #include "ffitest.h"
12 
13 typedef struct cls_struct_align {
14   long double a;
15   long double b;
16   long double c;
17   long double d;
18   long double e;
19   double f;
20   long double g;
21 } cls_struct_align;
22 
cls_struct_align_fn(cls_struct_align a1,cls_struct_align a2)23 cls_struct_align cls_struct_align_fn(
24 	cls_struct_align	a1,
25 	cls_struct_align	a2)
26 {
27 	struct cls_struct_align r;
28 
29 	r.a = a1.a + a2.a;
30 	r.b = a1.b + a2.b;
31 	r.c = a1.c + a2.c;
32 	r.d = a1.d + a2.d;
33 	r.e = a1.e + a2.e;
34 	r.f = a1.f + a2.f;
35 	r.g = a1.g + a2.g;
36 
37 	printf("%Lg %Lg %Lg %Lg %Lg %g %Lg %Lg %Lg %Lg %Lg %Lg %g %Lg: "
38 		"%Lg %Lg %Lg %Lg %Lg %g %Lg\n",
39 		a1.a, a1.b, a1.c, a1.d, a1.e, a1.f, a1.g,
40 		a2.a, a2.b, a2.c, a2.d, a2.e, a2.f, a2.g,
41 		r.a, r.b, r.c, r.d, r.e, r.f, r.g);
42 
43 	return r;
44 }
45 
46 static void
cls_struct_align_gn(ffi_cif * cif __UNUSED__,void * resp,void ** args,void * userdata __UNUSED__)47 cls_struct_align_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
48 		    void* userdata __UNUSED__)
49 {
50 	struct cls_struct_align a1, a2;
51 
52 	a1 = *(struct cls_struct_align*)(args[0]);
53 	a2 = *(struct cls_struct_align*)(args[1]);
54 
55 	*(cls_struct_align*)resp = cls_struct_align_fn(a1, a2);
56 }
57 
main(void)58 int main (void)
59 {
60 	ffi_cif cif;
61         void *code;
62 	ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
63 	void* args_dbl[3];
64 	ffi_type* cls_struct_fields[8];
65 	ffi_type cls_struct_type;
66 	ffi_type* dbl_arg_types[3];
67 
68 	struct cls_struct_align g_dbl = { 1, 2, 3, 4, 5, 6, 7 };
69 	struct cls_struct_align f_dbl = { 8, 9, 10, 11, 12, 13, 14 };
70 	struct cls_struct_align res_dbl;
71 
72 	cls_struct_type.size = 0;
73 	cls_struct_type.alignment = 0;
74 	cls_struct_type.type = FFI_TYPE_STRUCT;
75 	cls_struct_type.elements = cls_struct_fields;
76 
77 	cls_struct_fields[0] = &ffi_type_longdouble;
78 	cls_struct_fields[1] = &ffi_type_longdouble;
79 	cls_struct_fields[2] = &ffi_type_longdouble;
80 	cls_struct_fields[3] = &ffi_type_longdouble;
81 	cls_struct_fields[4] = &ffi_type_longdouble;
82 	cls_struct_fields[5] = &ffi_type_double;
83 	cls_struct_fields[6] = &ffi_type_longdouble;
84 	cls_struct_fields[7] = NULL;
85 
86 	dbl_arg_types[0] = &cls_struct_type;
87 	dbl_arg_types[1] = &cls_struct_type;
88 	dbl_arg_types[2] = NULL;
89 
90 	CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
91 		dbl_arg_types) == FFI_OK);
92 
93 	args_dbl[0] = &g_dbl;
94 	args_dbl[1] = &f_dbl;
95 	args_dbl[2] = NULL;
96 
97 	ffi_call(&cif, FFI_FN(cls_struct_align_fn), &res_dbl, args_dbl);
98 	/* { dg-output "1 2 3 4 5 6 7 8 9 10 11 12 13 14: 9 11 13 15 17 19 21" } */
99 	printf("res: %Lg %Lg %Lg %Lg %Lg %g %Lg\n", res_dbl.a, res_dbl.b,
100 		res_dbl.c, res_dbl.d, res_dbl.e, res_dbl.f, res_dbl.g);
101 	/* { dg-output "\nres: 9 11 13 15 17 19 21" } */
102 
103 	CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_align_gn, NULL, code) == FFI_OK);
104 
105 	res_dbl = ((cls_struct_align(*)(cls_struct_align, cls_struct_align))(code))(g_dbl, f_dbl);
106 	/* { dg-output "\n1 2 3 4 5 6 7 8 9 10 11 12 13 14: 9 11 13 15 17 19 21" } */
107 	printf("res: %Lg %Lg %Lg %Lg %Lg %g %Lg\n", res_dbl.a, res_dbl.b,
108 		res_dbl.c, res_dbl.d, res_dbl.e, res_dbl.f, res_dbl.g);
109 	/* { dg-output "\nres: 9 11 13 15 17 19 21" } */
110 
111   exit(0);
112 }
113 
114 
115 
116