github.com/prattmic/llgo-embedded@v0.0.0-20150820070356-41cfecea0e1e/third_party/gofrontend/libffi/testsuite/libffi.complex/cls_complex_struct.inc (about)

     1  /* -*-c-*- */
     2  #include "ffitest.h"
     3  #include <complex.h>
     4  
     5  typedef struct Cs {
     6    _Complex T_C_TYPE x;
     7    _Complex T_C_TYPE y;
     8  } Cs;
     9  
    10  Cs gc;
    11  
    12  void
    13  closure_test_fn(Cs p)
    14  {
    15    printf("%.1f,%.1fi %.1f,%.1fi\n",
    16  	 T_CONV creal (p.x), T_CONV cimag (p.x),
    17  	 T_CONV creal (p.y), T_CONV cimag (p.y));
    18    gc = p;
    19  }
    20  
    21  void
    22  closure_test_gn(ffi_cif* cif __UNUSED__, void* resp __UNUSED__,
    23  		void** args, void* userdata __UNUSED__)
    24  {
    25    closure_test_fn(*(Cs*)args[0]);
    26  }
    27  
    28  int main(int argc __UNUSED__, char** argv __UNUSED__)
    29  {
    30    ffi_cif cif;
    31  
    32    void *code;
    33    ffi_closure *pcl = ffi_closure_alloc(sizeof(ffi_closure), &code);
    34    ffi_type *cl_arg_types[1];
    35  
    36    ffi_type ts1_type;
    37    ffi_type* ts1_type_elements[4];
    38  
    39    Cs arg = { 1.0 + 11.0 * I, 2.0 + 22.0 * I};
    40  
    41    ts1_type.size = 0;
    42    ts1_type.alignment = 0;
    43    ts1_type.type = FFI_TYPE_STRUCT;
    44    ts1_type.elements = ts1_type_elements;
    45  
    46    ts1_type_elements[0] = &T_FFI_TYPE;
    47    ts1_type_elements[1] = &T_FFI_TYPE;
    48    ts1_type_elements[2] = NULL;
    49  
    50    cl_arg_types[0] = &ts1_type;
    51  
    52    /* Initialize the cif */
    53    CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
    54  		     &ffi_type_void, cl_arg_types) == FFI_OK);
    55  
    56    CHECK(ffi_prep_closure_loc(pcl, &cif, closure_test_gn, NULL, code) == FFI_OK);
    57  
    58    gc.x = 0.0 + 0.0 * I;
    59    gc.y = 0.0 + 0.0 * I;
    60    ((void*(*)(Cs))(code))(arg);
    61    /* { dg-output "1.0,11.0i 2.0,22.0i\n" } */
    62    CHECK (gc.x == arg.x && gc.y == arg.y);
    63  
    64    gc.x = 0.0 + 0.0 * I;
    65    gc.y = 0.0 + 0.0 * I;
    66    closure_test_fn(arg);
    67    /* { dg-output "1.0,11.0i 2.0,22.0i\n" } */
    68    CHECK (gc.x == arg.x && gc.y == arg.y);
    69  
    70    return 0;
    71  }