cls_complex.inc 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* -*-c-*- */
  2. #include "ffitest.h"
  3. #include <complex.h>
  4. static void cls_ret_complex_fn(ffi_cif* cif __UNUSED__, void* resp, void** args,
  5. void* userdata __UNUSED__)
  6. {
  7. _Complex T_C_TYPE *pa;
  8. _Complex T_C_TYPE *pr;
  9. pa = (_Complex T_C_TYPE *)args[0];
  10. pr = (_Complex T_C_TYPE *)resp;
  11. *pr = *pa;
  12. printf("%.6f,%.6fi: %.6f,%.6fi\n",
  13. T_CONV creal (*pa), T_CONV cimag (*pa),
  14. T_CONV creal (*pr), T_CONV cimag (*pr));
  15. }
  16. typedef _Complex T_C_TYPE (*cls_ret_complex)(_Complex T_C_TYPE);
  17. int main (void)
  18. {
  19. ffi_cif cif;
  20. void *code;
  21. ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
  22. ffi_type * cl_arg_types[2];
  23. _Complex T_C_TYPE res;
  24. cl_arg_types[0] = &T_FFI_TYPE;
  25. cl_arg_types[1] = NULL;
  26. /* Initialize the cif */
  27. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
  28. &T_FFI_TYPE, cl_arg_types) == FFI_OK);
  29. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_ret_complex_fn, NULL, code) == FFI_OK);
  30. res = (*((cls_ret_complex)code))(0.125 + 128.0 * I);
  31. printf("res: %.6f,%.6fi\n", T_CONV creal (res), T_CONV cimag (res));
  32. CHECK (res == (0.125 + 128.0 * I));
  33. exit(0);
  34. }