complex_int.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* Area: ffi_call
  2. Purpose: Check non-standard complex types.
  3. Limitations: none.
  4. PR: none.
  5. Originator: <vogt@linux.vnet.ibm.com>. */
  6. /* { dg-do run } */
  7. #include "ffitest.h"
  8. #include "ffi.h"
  9. #include <complex.h>
  10. _Complex int f_complex(_Complex int c, int x, int *py)
  11. {
  12. __real__ c = -2 * __real__ c;
  13. __imag__ c = __imag__ c + 1;
  14. *py += x;
  15. return c;
  16. }
  17. /*
  18. * This macro can be used to define new complex type descriptors
  19. * in a platform independent way.
  20. *
  21. * name: Name of the new descriptor is ffi_type_complex_<name>.
  22. * type: The C base type of the complex type.
  23. */
  24. #define FFI_COMPLEX_TYPEDEF(name, type, ffitype) \
  25. static ffi_type *ffi_elements_complex_##name [2] = { \
  26. (ffi_type *)(&ffitype), NULL \
  27. }; \
  28. struct struct_align_complex_##name { \
  29. char c; \
  30. _Complex type x; \
  31. }; \
  32. ffi_type ffi_type_complex_##name = { \
  33. sizeof(_Complex type), \
  34. offsetof(struct struct_align_complex_##name, x), \
  35. FFI_TYPE_COMPLEX, \
  36. (ffi_type **)ffi_elements_complex_##name \
  37. }
  38. /* Define new complex type descriptors using the macro: */
  39. /* ffi_type_complex_sint */
  40. FFI_COMPLEX_TYPEDEF(sint, int, ffi_type_sint);
  41. /* ffi_type_complex_uchar */
  42. FFI_COMPLEX_TYPEDEF(uchar, unsigned char, ffi_type_uint8);
  43. int main (void)
  44. {
  45. ffi_cif cif;
  46. ffi_type *args[MAX_ARGS];
  47. void *values[MAX_ARGS];
  48. _Complex int tc_arg;
  49. _Complex int tc_result;
  50. int tc_int_arg_x;
  51. int tc_y;
  52. int *tc_ptr_arg_y = &tc_y;
  53. args[0] = &ffi_type_complex_sint;
  54. args[1] = &ffi_type_sint;
  55. args[2] = &ffi_type_pointer;
  56. values[0] = &tc_arg;
  57. values[1] = &tc_int_arg_x;
  58. values[2] = &tc_ptr_arg_y;
  59. /* Initialize the cif */
  60. CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 3, &ffi_type_complex_sint, args)
  61. == FFI_OK);
  62. tc_arg = 1 + 7 * I;
  63. tc_int_arg_x = 1234;
  64. tc_y = 9876;
  65. ffi_call(&cif, FFI_FN(f_complex), &tc_result, values);
  66. printf ("%d,%di %d,%di, x %d 1234, y %d 11110\n",
  67. (int)tc_result, (int)(tc_result * -I), 2, 8, tc_int_arg_x, tc_y);
  68. /* dg-output "-2,8i 2,8i, x 1234 1234, y 11110 11110" */
  69. CHECK (creal (tc_result) == -2);
  70. CHECK (cimag (tc_result) == 8);
  71. CHECK (tc_int_arg_x == 1234);
  72. CHECK (*tc_ptr_arg_y == 11110);
  73. exit(0);
  74. }