cls_complex_va.inc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /* -*-c-*- */
  2. #include "ffitest.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <stdarg.h>
  6. #include <complex.h>
  7. static _Complex T_C_TYPE gComplexValue1 = 1 + 2 * I;
  8. static _Complex T_C_TYPE gComplexValue2 = 3 + 4 * I;
  9. static int cls_variadic(const char *format, ...)
  10. {
  11. va_list ap;
  12. _Complex T_C_TYPE p1, p2;
  13. va_start (ap, format);
  14. p1 = va_arg (ap, _Complex T_C_TYPE);
  15. p2 = va_arg (ap, _Complex T_C_TYPE);
  16. va_end (ap);
  17. return printf(format, T_CONV creal (p1), T_CONV cimag (p1),
  18. T_CONV creal (p2), T_CONV cimag (p2));
  19. }
  20. static void
  21. cls_complex_va_fn(ffi_cif* cif __UNUSED__, void* resp,
  22. void** args, void* userdata __UNUSED__)
  23. {
  24. char* format = *(char**)args[0];
  25. gComplexValue1 = *(_Complex T_C_TYPE*)args[1];
  26. gComplexValue2 = *(_Complex T_C_TYPE*)args[2];
  27. *(ffi_arg*)resp =
  28. printf(format,
  29. T_CONV creal (gComplexValue1), T_CONV cimag (gComplexValue1),
  30. T_CONV creal (gComplexValue2), T_CONV cimag (gComplexValue2));
  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[4];
  38. ffi_type* arg_types[4];
  39. char *format = "%.1f,%.1fi %.1f,%.1fi\n";
  40. _Complex T_C_TYPE complexArg1 = 1.0 + 22.0 *I;
  41. _Complex T_C_TYPE complexArg2 = 333.0 + 4444.0 *I;
  42. ffi_arg res = 0;
  43. arg_types[0] = &ffi_type_pointer;
  44. arg_types[1] = &T_FFI_TYPE;
  45. arg_types[2] = &T_FFI_TYPE;
  46. arg_types[3] = NULL;
  47. /* This printf call is variadic */
  48. CHECK(ffi_prep_cif_var(&cif, FFI_DEFAULT_ABI, 1, 3, &ffi_type_sint,
  49. arg_types) == FFI_OK);
  50. args[0] = &format;
  51. args[1] = &complexArg1;
  52. args[2] = &complexArg2;
  53. args[3] = NULL;
  54. ffi_call(&cif, FFI_FN(cls_variadic), &res, args);
  55. printf("res: %d\n", (int) res);
  56. CHECK (res == 24);
  57. CHECK(ffi_prep_closure_loc(pcl, &cif, cls_complex_va_fn, NULL, code)
  58. == FFI_OK);
  59. res = ((int(*)(char *, ...))(code))(format, complexArg1, complexArg2);
  60. CHECK (gComplexValue1 == complexArg1);
  61. CHECK (gComplexValue2 == complexArg2);
  62. printf("res: %d\n", (int) res);
  63. CHECK (res == 24);
  64. exit(0);
  65. }