Debian Codegraph @

xs/complex.c

raw
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "ppport.h"
5 #include "ffi_platypus.h"
6 #include "ffi_platypus_guts.h"
7
8 static double
9 decompose(SV *sv, int imag)
10 {
11 /* Re(z) */
12 dSP;
13
14 int count;
15 double result = 0.0;
16
17 ENTER;
18 SAVETMPS;
19 PUSHMARK(SP);
20 XPUSHs(sv);
21 PUTBACK;
22
23 count = call_pv(imag ? "Math::Complex::Im" : "Math::Complex::Re", G_ARRAY);
24
25 SPAGAIN;
26
27 if(count >= 1)
28 result = POPn;
29
30 PUTBACK;
31 FREETMPS;
32 LEAVE;
33
34 return result;
35 }
36
37 static void
38 set(SV *sv, SV *new_value, int imag)
39 {
40 dSP;
41
42 ENTER;
43 SAVETMPS;
44 PUSHMARK(SP);
45 XPUSHs(sv);
46 XPUSHs(new_value);
47 PUTBACK;
48
49 call_pv(imag ? "Math::Complex::Im" : "Math::Complex::Re", G_DISCARD);
50
51 FREETMPS;
52 LEAVE;
53 }
54
55 void
56 ffi_pl_perl_to_complex_float(SV *sv, float *ptr)
57 {
58 if(sv_isobject(sv) && sv_derived_from(sv, "Math::Complex"))
59 {
60 ptr[0] = decompose(sv, 0);
61 ptr[1] = decompose(sv, 1);
62 }
63 else if(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV)
64 {
65 AV *av = (AV*) SvRV(sv);
66 SV **real_sv, **imag_sv;
67 real_sv = av_fetch(av, 0, 0);
68 imag_sv = av_fetch(av, 1, 0);
69 ptr[0] = real_sv != NULL ? SvNV(*real_sv) : 0.0;
70 ptr[1]= imag_sv != NULL ? SvNV(*imag_sv) : 0.0;
71 }
72 else if(SvOK(sv))
73 {
74 ptr[0] = SvNV(sv);
75 ptr[1] = 0.0;
76 }
77 else
78 {
79 ptr[0] = 0.0;
80 ptr[1] = 0.0;
81 }
82 }
83
84 void
85 ffi_pl_complex_float_to_perl(SV *sv, float *ptr)
86 {
87 if(SvOK(sv) && sv_isobject(sv) && sv_derived_from(sv, "Math::Complex"))
88 {
89 /* the complex variable is a Math::Complex object */
90 set(sv, sv_2mortal(newSVnv(ptr[0])), 0);
91 set(sv, sv_2mortal(newSVnv(ptr[1])), 1);
92 }
93 else if(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV)
94 {
95 /* the compex variable is already an array */
96 AV *av = (AV*) SvRV(sv);
97 av_store(av, 0, newSVnv(ptr[0]));
98 av_store(av, 1, newSVnv(ptr[1]));
99 }
100 else
101 {
102 /* the complex variable is something else and an array needs to be created */
103 SV *values[2];
104 AV *av;
105 values[0] = newSVnv(ptr[0]);
106 values[1] = newSVnv(ptr[1]);
107 av = av_make(2, values);
108 sv_setsv(sv, newRV_noinc((SV*)av));
109 }
110 }
111
112 void
113 ffi_pl_perl_to_complex_double(SV *sv, double *ptr)
114 {
115 if(sv_isobject(sv) && sv_derived_from(sv, "Math::Complex"))
116 {
117 ptr[0] = decompose(sv, 0);
118 ptr[1] = decompose(sv, 1);
119 }
120 else if(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV)
121 {
122 AV *av = (AV*) SvRV(sv);
123 SV **real_sv, **imag_sv;
124 real_sv = av_fetch(av, 0, 0);
125 imag_sv = av_fetch(av, 1, 0);
126 ptr[0] = real_sv != NULL ? SvNV(*real_sv) : 0.0;
127 ptr[1]= imag_sv != NULL ? SvNV(*imag_sv) : 0.0;
128 }
129 else if(SvOK(sv))
130 {
131 ptr[0] = SvNV(sv);
132 ptr[1] = 0.0;
133 }
134 else
135 {
136 ptr[0] = 0.0;
137 ptr[1] = 0.0;
138 }
139 }
140
141 void
142 ffi_pl_complex_double_to_perl(SV *sv, double *ptr)
143 {
144 if(SvOK(sv) && sv_isobject(sv) && sv_derived_from(sv, "Math::Complex"))
145 {
146 /* the complex variable is a Math::Complex object */
147 set(sv, sv_2mortal(newSVnv(ptr[0])), 0);
148 set(sv, sv_2mortal(newSVnv(ptr[1])), 1);
149 }
150 else if(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV)
151 {
152 /* the compex variable is already an array */
153 AV *av = (AV*) SvRV(sv);
154 av_store(av, 0, newSVnv(ptr[0]));
155 av_store(av, 1, newSVnv(ptr[1]));
156 }
157 else
158 {
159 /* the complex variable is something else and an array needs to be created */
160 SV *values[2];
161 AV *av;
162 values[0] = newSVnv(ptr[0]);
163 values[1] = newSVnv(ptr[1]);
164 av = av_make(2, values);
165 sv_setsv(sv, newRV_noinc((SV*)av));
166 }
167 }
168