cls_complex_struct.inc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* -*-c-*- */
  2. #include "ffitest.h"
  3. #include <complex.h>
  4. typedef struct Cs {
  5. _Complex T_C_TYPE x;
  6. _Complex T_C_TYPE y;
  7. } Cs;
  8. Cs gc;
  9. void
  10. closure_test_fn(Cs p)
  11. {
  12. printf("%.1f,%.1fi %.1f,%.1fi\n",
  13. T_CONV creal (p.x), T_CONV cimag (p.x),
  14. T_CONV creal (p.y), T_CONV cimag (p.y));
  15. gc = p;
  16. }
  17. void
  18. closure_test_gn(ffi_cif* cif __UNUSED__, void* resp __UNUSED__,
  19. void** args, void* userdata __UNUSED__)
  20. {
  21. closure_test_fn(*(Cs*)args[0]);
  22. }
  23. int main(int argc __UNUSED__, char** argv __UNUSED__)
  24. {
  25. ffi_cif cif;
  26. void *code;
  27. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  28. ffi_type *cl_arg_types[1];
  29. ffi_type ts1_type;
  30. ffi_type* ts1_type_elements[4];
  31. Cs arg = { 1.0 + 11.0 * I, 2.0 + 22.0 * I};
  32. ts1_type.size = 0;
  33. ts1_type.alignment = 0;
  34. ts1_type.type = FFI_TYPE_STRUCT;
  35. ts1_type.elements = ts1_type_elements;
  36. ts1_type_elements[0] = &T_FFI_TYPE;
  37. ts1_type_elements[1] = &T_FFI_TYPE;
  38. ts1_type_elements[2] = NULL;
  39. cl_arg_types[0] = &ts1_type;
  40. /* Initialize the cif */
  41. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
  42. &ffi_type_void, cl_arg_types) == FFI_OK);
  43. CHECK(ffi_prep_closure_loc(pcl, &cif, closure_test_gn, NULL, code) == FFI_OK);
  44. gc.x = 0.0 + 0.0 * I;
  45. gc.y = 0.0 + 0.0 * I;
  46. ((void*(*)(Cs))(code))(arg);
  47. /* { dg-output "1.0,11.0i 2.0,22.0i\n" } */
  48. CHECK (gc.x == arg.x && gc.y == arg.y);
  49. gc.x = 0.0 + 0.0 * I;
  50. gc.y = 0.0 + 0.0 * I;
  51. closure_test_fn(arg);
  52. /* { dg-output "1.0,11.0i 2.0,22.0i\n" } */
  53. CHECK (gc.x == arg.x && gc.y == arg.y);
  54. return 0;
  55. }