cls_3float.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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: <compnerd@compnerd.org> 20171026 */
  7. /* { dg-do run } */
  8. #include "ffitest.h"
  9. typedef struct cls_struct_3float {
  10. float f;
  11. float g;
  12. float h;
  13. } cls_struct_3float;
  14. cls_struct_3float cls_struct_3float_fn(struct cls_struct_3float a1,
  15. struct cls_struct_3float a2)
  16. {
  17. struct cls_struct_3float result;
  18. result.f = a1.f + a2.f;
  19. result.g = a1.g + a2.g;
  20. result.h = a1.h + a2.h;
  21. printf("%g %g %g %g %g %g: %g %g %g\n", a1.f, a1.g, a1.h,
  22. a2.f, a2.g, a2.h, result.f, result.g, result.h);
  23. return result;
  24. }
  25. static void
  26. cls_struct_3float_gn(ffi_cif *cif __UNUSED__, void* resp, void **args,
  27. void* userdata __UNUSED__)
  28. {
  29. struct cls_struct_3float a1, a2;
  30. a1 = *(struct cls_struct_3float*)(args[0]);
  31. a2 = *(struct cls_struct_3float*)(args[1]);
  32. *(cls_struct_3float*)resp = cls_struct_3float_fn(a1, a2);
  33. }
  34. int main (void)
  35. {
  36. ffi_cif cif;
  37. void *code;
  38. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  39. void *args_dbl[3];
  40. ffi_type* cls_struct_fields[4];
  41. ffi_type cls_struct_type;
  42. ffi_type* dbl_arg_types[3];
  43. struct cls_struct_3float g_dbl = { 1.0f, 2.0f, 3.0f };
  44. struct cls_struct_3float f_dbl = { 1.0f, 2.0f, 3.0f };
  45. struct cls_struct_3float res_dbl;
  46. cls_struct_fields[0] = &ffi_type_float;
  47. cls_struct_fields[1] = &ffi_type_float;
  48. cls_struct_fields[2] = &ffi_type_float;
  49. cls_struct_fields[3] = NULL;
  50. cls_struct_type.size = 0;
  51. cls_struct_type.alignment = 0;
  52. cls_struct_type.type = FFI_TYPE_STRUCT;
  53. cls_struct_type.elements = cls_struct_fields;
  54. dbl_arg_types[0] = &cls_struct_type;
  55. dbl_arg_types[1] = &cls_struct_type;
  56. dbl_arg_types[2] = NULL;
  57. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  58. dbl_arg_types) == FFI_OK);
  59. args_dbl[0] = &g_dbl;
  60. args_dbl[1] = &f_dbl;
  61. args_dbl[2] = NULL;
  62. ffi_call(&cif, FFI_FN(cls_struct_3float_fn), &res_dbl, args_dbl);
  63. /* { dg-output "1 2 3 1 2 3: 2 4 6" } */
  64. printf("res: %g %g %g\n", res_dbl.f, res_dbl.g, res_dbl.h);
  65. /* { dg-output "\nres: 2 4 6" } */
  66. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_3float_gn, NULL, code) ==
  67. FFI_OK);
  68. res_dbl = ((cls_struct_3float(*)(cls_struct_3float,
  69. cls_struct_3float))(code))(g_dbl, f_dbl);
  70. /* { dg-output "\n1 2 3 1 2 3: 2 4 6" } */
  71. printf("res: %g %g %g\n", res_dbl.f, res_dbl.g, res_dbl.h);
  72. /* { dg-output "\nres: 2 4 6" } */
  73. exit(0);
  74. }