cls_align_longdouble.c 2.5 KB

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