File: complex.c

package info (click to toggle)
libffi-platypus-perl 2.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,860 kB
  • sloc: perl: 7,388; ansic: 6,862; cpp: 53; sh: 19; makefile: 14
file content (119 lines) | stat: -rw-r--r-- 2,374 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "ffi_platypus.h"

float
my_float_real(float complex c)
{
  return crealf(c);
}

float
my_float_imag(float complex c)
{
  return cimagf(c);
}

double
my_double_real(double complex c)
{
  return creal(c);
}

double
my_double_imag(double complex c)
{
  return cimag(c);
}

float complex
my_float_complex_ret(float r, float i)
{
  return r + i*I;
}

double complex
my_double_complex_ret(double r, double i)
{
  return r + i*I;
}

int
dlmain(int argc, char *argv[])
{
  ffi_cif cif;
  ffi_type *args[2];
  void *values[2];

  args[0] = &ffi_type_complex_float;

  if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_float, args) == FFI_OK)
  {
    float answer;
    float complex input;
    input = 1.0 + 2.0 * I;
    values[0] = &input;
    ffi_call(&cif, (void*) my_float_real, &answer, values);
    /* printf("answer = %g\n", answer); */
    if(answer != 1.0)
      return 2;
    ffi_call(&cif, (void*) my_float_imag, &answer, values);
    /* printf("answer = %g\n", answer); */
    if(answer != 2.0)
      return 2;
  }
  else
  {
    return 2;
  }

  args[0] = &ffi_type_complex_double;

  if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_double, args) == FFI_OK)
  {
    double answer;
    double complex input;
    input = 1.0 + 2.0 * I;
    values[0] = &input;
    ffi_call(&cif, (void*) my_double_real, &answer, values);
    /* printf("answer = %g\n", answer); */
    if(answer != 1.0)
      return 2;
    ffi_call(&cif, (void*) my_double_imag, &answer, values);
    /* printf("answer = %g\n", answer); */
    if(answer != 2.0)
      return 2;
  }
  else
  {
    return 2;
  }

  args[0] = &ffi_type_float;
  args[1] = &ffi_type_float;

  if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_complex_float, args) == FFI_OK)
  {
    float complex answer;
    float r=1.0, i=2.0;
    values[0] = &r;
    values[1] = &i;
    ffi_call(&cif, (void*) my_float_complex_ret, &answer, values);
    if(creal(answer) != 1.0 || cimag(answer) != 2.0)
      return 2;
  }

  args[0] = &ffi_type_double;
  args[1] = &ffi_type_double;

  if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_complex_double, args) == FFI_OK)
  {
    double complex answer;
    double r=1.0, i=2.0;
    values[0] = &r;
    values[1] = &i;
    ffi_call(&cif, (void*) my_double_complex_ret, &answer, values);
    if(creal(answer) != 1.0 || cimag(answer) != 2.0)
      return 2;
  }

  return 0;
}