cls_align_complex.inc 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* -*-c-*- */
  2. #include "ffitest.h"
  3. #include <complex.h>
  4. typedef struct cls_struct_align {
  5. unsigned char a;
  6. _Complex T_C_TYPE b;
  7. unsigned char c;
  8. } cls_struct_align;
  9. cls_struct_align cls_struct_align_fn(
  10. struct cls_struct_align a1, struct cls_struct_align a2)
  11. {
  12. struct cls_struct_align result;
  13. result.a = a1.a + a2.a;
  14. result.b = a1.b + a2.b;
  15. result.c = a1.c + a2.c;
  16. printf("%d %f,%fi %d %d %f,%fi %d: %d %f,%fi %d\n",
  17. a1.a, T_CONV creal (a1.b), T_CONV cimag (a1.b), a1.c,
  18. a2.a, T_CONV creal (a2.b), T_CONV cimag (a2.b), a2.c,
  19. result.a, T_CONV creal (result.b), T_CONV cimag (result.b), result.c);
  20. return result;
  21. }
  22. static void
  23. cls_struct_align_gn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  24. void* userdata __UNUSED__)
  25. {
  26. struct cls_struct_align a1, a2;
  27. a1 = *(struct cls_struct_align*)(args[0]);
  28. a2 = *(struct cls_struct_align*)(args[1]);
  29. *(cls_struct_align*)resp = cls_struct_align_fn(a1, a2);
  30. }
  31. int main (void)
  32. {
  33. ffi_cif cif;
  34. void *code;
  35. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  36. void* args_c[5];
  37. ffi_type* cls_struct_fields[4];
  38. ffi_type cls_struct_type;
  39. ffi_type* c_arg_types[5];
  40. struct cls_struct_align g_c = { 12, 4951 + 7 * I, 127 };
  41. struct cls_struct_align f_c = { 1, 9320 + 1 * I, 13 };
  42. struct cls_struct_align res_c;
  43. cls_struct_type.size = 0;
  44. cls_struct_type.alignment = 0;
  45. cls_struct_type.type = FFI_TYPE_STRUCT;
  46. cls_struct_type.elements = cls_struct_fields;
  47. cls_struct_fields[0] = &ffi_type_uchar;
  48. cls_struct_fields[1] = &T_FFI_TYPE;
  49. cls_struct_fields[2] = &ffi_type_uchar;
  50. cls_struct_fields[3] = NULL;
  51. c_arg_types[0] = &cls_struct_type;
  52. c_arg_types[1] = &cls_struct_type;
  53. c_arg_types[2] = NULL;
  54. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &cls_struct_type,
  55. c_arg_types) == FFI_OK);
  56. args_c[0] = &g_c;
  57. args_c[1] = &f_c;
  58. args_c[2] = NULL;
  59. ffi_call(&cif, FFI_FN(cls_struct_align_fn), &res_c, args_c);
  60. /* { dg-output "12 4951,7i 127 1 9320,1i 13: 13 14271,8i 140" } */
  61. printf("res: %d %f,%fi %d\n",
  62. res_c.a, T_CONV creal (res_c.b), T_CONV cimag (res_c.b), res_c.c);
  63. /* { dg-output "\nres: 13 14271,8i 140" } */
  64. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_struct_align_gn, NULL, code) == FFI_OK);
  65. res_c = ((cls_struct_align(*)(cls_struct_align, cls_struct_align))(code))(g_c, f_c);
  66. /* { dg-output "\n12 4951,7i 127 1 9320,1i 13: 13 14271,8i 140" } */
  67. printf("res: %d %f,%fi %d\n",
  68. res_c.a, T_CONV creal (res_c.b), T_CONV cimag (res_c.b), res_c.c);
  69. /* { dg-output "\nres: 13 14271,8i 140" } */
  70. exit(0);
  71. }