Debian Codegraph @

inc/probe/complex.c

raw
1 #include "ffi_platypus.h"
2
3 float
4 my_float_real(float complex c)
5 {
6 return crealf(c);
7 }
8
9 float
10 my_float_imag(float complex c)
11 {
12 return cimagf(c);
13 }
14
15 double
16 my_double_real(double complex c)
17 {
18 return creal(c);
19 }
20
21 double
22 my_double_imag(double complex c)
23 {
24 return cimag(c);
25 }
26
27 float complex
28 my_float_complex_ret(float r, float i)
29 {
30 return r + i*I;
31 }
32
33 double complex
34 my_double_complex_ret(double r, double i)
35 {
36 return r + i*I;
37 }
38
39 int
40 dlmain(int argc, char *argv[])
41 {
42 ffi_cif cif;
43 ffi_type *args[2];
44 void *values[2];
45
46 args[0] = &ffi_type_complex_float;
47
48 if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_float, args) == FFI_OK)
49 {
50 float answer;
51 float complex input;
52 input = 1.0 + 2.0 * I;
53 values[0] = &input;
54 ffi_call(&cif, (void*) my_float_real, &answer, values);
55 /* printf("answer = %g\n", answer); */
56 if(answer != 1.0)
57 return 2;
58 ffi_call(&cif, (void*) my_float_imag, &answer, values);
59 /* printf("answer = %g\n", answer); */
60 if(answer != 2.0)
61 return 2;
62 }
63 else
64 {
65 return 2;
66 }
67
68 args[0] = &ffi_type_complex_double;
69
70 if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_double, args) == FFI_OK)
71 {
72 double answer;
73 double complex input;
74 input = 1.0 + 2.0 * I;
75 values[0] = &input;
76 ffi_call(&cif, (void*) my_double_real, &answer, values);
77 /* printf("answer = %g\n", answer); */
78 if(answer != 1.0)
79 return 2;
80 ffi_call(&cif, (void*) my_double_imag, &answer, values);
81 /* printf("answer = %g\n", answer); */
82 if(answer != 2.0)
83 return 2;
84 }
85 else
86 {
87 return 2;
88 }
89
90 args[0] = &ffi_type_float;
91 args[1] = &ffi_type_float;
92
93 if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_complex_float, args) == FFI_OK)
94 {
95 float complex answer;
96 float r=1.0, i=2.0;
97 values[0] = &r;
98 values[1] = &i;
99 ffi_call(&cif, (void*) my_float_complex_ret, &answer, values);
100 if(creal(answer) != 1.0 || cimag(answer) != 2.0)
101 return 2;
102 }
103
104 args[0] = &ffi_type_double;
105 args[1] = &ffi_type_double;
106
107 if(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 2, &ffi_type_complex_double, args) == FFI_OK)
108 {
109 double complex answer;
110 double r=1.0, i=2.0;
111 values[0] = &r;
112 values[1] = &i;
113 ffi_call(&cif, (void*) my_double_complex_ret, &answer, values);
114 if(creal(answer) != 1.0 || cimag(answer) != 2.0)
115 return 2;
116 }
117
118 return 0;
119 }