Ruby: ext/fiddle/libffi-3.2.1/testsuite/libffi.call/cls_align_longdouble_split.c Source File

Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)

cls_align_longdouble_split.c
Go to the documentation of this file.
1/* Area: ffi_call, closure_call
2 Purpose: Check structure alignment of long double.
3 Limitations: none.
4 PR: none.
5 Originator: <hos@tamanegi.org> 20031203 */
6
7/* { dg-excess-errors "no long double format" { xfail x86_64-*-mingw* x86_64-*-cygwin* } } */
8/* { dg-do run { xfail strongarm*-*-* xscale*-*-* } } */
9/* { dg-options -mlong-double-128 { target powerpc64*-*-linux* } } */
10/* { dg-output "" { xfail x86_64-*-mingw* x86_64-*-cygwin* } } */
11
12#include "ffitest.h"
13
14typedef struct cls_struct_align {
15 long double a;
16 long double b;
17 long double c;
18 long double d;
19 long double e;
20 long double f;
21 long double g;
22} cls_struct_align;
23
24 cls_struct_align cls_struct_align_fn(
25 cls_struct_align a1,
26 cls_struct_align a2)
27{
28 struct cls_struct_align r;
29
30 r.a = a1.a + a2.a;
31 r.b = a1.b + a2.b;
32 r.c = a1.c + a2.c;
33 r.d = a1.d + a2.d;
34 r.e = a1.e + a2.e;
35 r.f = a1.f + a2.f;
36 r.g = a1.g + a2.g;
37
38 printf("%Lg %Lg %Lg %Lg %Lg %Lg %Lg %Lg %Lg %Lg %Lg %Lg %Lg %Lg: "
39 "%Lg %Lg %Lg %Lg %Lg %Lg %Lg\n",
40 a1.a, a1.b, a1.c, a1.d, a1.e, a1.f, a1.g,
41 a2.a, a2.b, a2.c, a2.d, a2.e, a2.f, a2.g,
42 r.a, r.b, r.c, r.d, r.e, r.f, r.g);
43
44 return r;
45}
46
47 cls_struct_align cls_struct_align_fn2(
48 cls_struct_align a1)
49{
50 struct cls_struct_align r;
51
52 r.a = a1.a + 1;
53 r.b = a1.b + 1;
54 r.c = a1.c + 1;
55 r.d = a1.d + 1;
56 r.e = a1.e + 1;
57 r.f = a1.f + 1;
58 r.g = a1.g + 1;
59
60 printf("%Lg %Lg %Lg %Lg %Lg %Lg %Lg: "
61 "%Lg %Lg %Lg %Lg %Lg %Lg %Lg\n",
62 a1.a, a1.b, a1.c, a1.d, a1.e, a1.f, a1.g,
63 r.a, r.b, r.c, r.d, r.e, r.f, r.g);
64
65 return r;
66}
67
68static void
69cls_struct_align_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
70 void* userdata __UNUSED__)
71{
72 struct cls_struct_align a1, a2;
73
74 a1 = *(struct cls_struct_align*)(args[0]);
75 a2 = *(struct cls_struct_align*)(args[1]);
76
77 *(cls_struct_align*)resp = cls_struct_align_fn(a1, a2);
78}
79
80 int main (void)
81{
82 ffi_cif cif;
83 void *code;
84 ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
85 void* args_dbl[3];
86 ffi_type* cls_struct_fields[8];
87 ffi_type cls_struct_type;
88 ffi_type* dbl_arg_types[3];
89
90 struct cls_struct_align g_dbl = { 1, 2, 3, 4, 5, 6, 7 };
91 struct cls_struct_align f_dbl = { 8, 9, 10, 11, 12, 13, 14 };
92 struct cls_struct_align 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_longdouble;
100 cls_struct_fields[1] = &ffi_type_longdouble;
101 cls_struct_fields[2] = &ffi_type_longdouble;
102 cls_struct_fields[3] = &ffi_type_longdouble;
103 cls_struct_fields[4] = &ffi_type_longdouble;
104 cls_struct_fields[5] = &ffi_type_longdouble;
105 cls_struct_fields[6] = &ffi_type_longdouble;
106 cls_struct_fields[7] = NULL;
107
108 dbl_arg_types[0] = &cls_struct_type;
109 dbl_arg_types[1] = &cls_struct_type;
110 dbl_arg_types[2] = NULL;
111
112 CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
113 dbl_arg_types) == FFI_OK);
114
115 args_dbl[0] = &g_dbl;
116 args_dbl[1] = &f_dbl;
117 args_dbl[2] = NULL;
118
119 ffi_call(&cif, FFI_FN(cls_struct_align_fn), &res_dbl, args_dbl);
120 /* { dg-output "1 2 3 4 5 6 7 8 9 10 11 12 13 14: 9 11 13 15 17 19 21" } */
121 printf("res: %Lg %Lg %Lg %Lg %Lg %Lg %Lg\n", res_dbl.a, res_dbl.b,
122 res_dbl.c, res_dbl.d, res_dbl.e, res_dbl.f, res_dbl.g);
123 /* { dg-output "\nres: 9 11 13 15 17 19 21" } */
124
125 CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_align_gn, NULL, code) == FFI_OK);
126
127 res_dbl = ((cls_struct_align(*)(cls_struct_align, cls_struct_align))(code))(g_dbl, f_dbl);
128 /* { dg-output "\n1 2 3 4 5 6 7 8 9 10 11 12 13 14: 9 11 13 15 17 19 21" } */
129 printf("res: %Lg %Lg %Lg %Lg %Lg %Lg %Lg\n", res_dbl.a, res_dbl.b,
130 res_dbl.c, res_dbl.d, res_dbl.e, res_dbl.f, res_dbl.g);
131 /* { dg-output "\nres: 9 11 13 15 17 19 21" } */
132
133 exit(0);
134}
ffi_prep_closure_loc
ffi_status ffi_prep_closure_loc(ffi_closure *closure, ffi_cif *cif, void(*fun)(ffi_cif *, void *, void **, void *), void *user_data, void *codeloc)
Definition: ffi.c:928
ffi_call
void ffi_call(ffi_cif *cif, void(*fn)(void), void *rvalue, void **avalue)
Definition: ffi.c:813
FFI_DEFAULT_ABI
@ FFI_DEFAULT_ABI
Definition: ffitarget.h:38
cls_struct_align_fn
cls_struct_align cls_struct_align_fn(cls_struct_align a1, cls_struct_align a2)
Definition: cls_align_longdouble_split.c:24
cls_struct_align
struct cls_struct_align cls_struct_align
main
int main(void)
Definition: cls_align_longdouble_split.c:80
cls_struct_align_fn2
cls_struct_align cls_struct_align_fn2(cls_struct_align a1)
Definition: cls_align_longdouble_split.c:47
CHECK
#define CHECK(sub)
Definition: compile.c:429
ffitest.h
__UNUSED__
#define __UNUSED__
Definition: ffitest.h:23
ffi_prep_cif
ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs, ffi_type *rtype, ffi_type **atypes)
Definition: prep_cif.c:226
NULL
#define NULL
Definition: regenc.h:69
cls_struct_align
Definition: cls_align_double.c:12
cls_struct_align::c
long double c
Definition: cls_align_longdouble_split.c:17
cls_struct_align::b
double b
Definition: cls_align_double.c:14
cls_struct_align::a
long double a
Definition: cls_align_longdouble_split.c:15
cls_struct_align::f
long double f
Definition: cls_align_longdouble_split.c:20
cls_struct_align::e
long double e
Definition: cls_align_longdouble_split.c:19
cls_struct_align::a
unsigned char a
Definition: cls_align_double.c:13
cls_struct_align::g
long double g
Definition: cls_align_longdouble_split.c:21
cls_struct_align::c
unsigned char c
Definition: cls_align_double.c:15
cls_struct_align::d
long double d
Definition: cls_align_longdouble_split.c:18
code
Definition: inftree9.h:24