mirror of https://github.com/go-python/gopy.git
cffi: Support complex number types.
* Generating complex64 / complex128 types from a CFFI engine is supported. * Passing complex types as an argument is supported. * Create a new test for complex operations.
This commit is contained in:
parent
fddd338960
commit
02e3537b5f
|
@ -16,3 +16,11 @@ func Add(i, j int) int {
|
|||
func Bool(b bool) bool {
|
||||
return b
|
||||
}
|
||||
|
||||
func Comp64Add(i, j complex64) complex64 {
|
||||
return i + j
|
||||
}
|
||||
|
||||
func Comp128Add(i, j complex128) complex128 {
|
||||
return i + j
|
||||
}
|
||||
|
|
|
@ -18,3 +18,7 @@ fct()
|
|||
print("pkg.Add(1,2)= %s" % (pkg.Add(1,2),))
|
||||
print("pkg.Bool(True)= %s" % pkg.Bool(True))
|
||||
print("pkg.Bool(False)= %s" % pkg.Bool(False))
|
||||
a = 3+4j
|
||||
b = 2+5j
|
||||
print("pkg.Comp64Add(%s, %s) = %s" % (a, b, pkg.Comp128Add(a, b)))
|
||||
print("pkg.Comp128Add(%s, %s) = %s" % (a, b, pkg.Comp128Add(a, b)))
|
||||
|
|
|
@ -39,7 +39,11 @@ typedef void *GoMap;
|
|||
typedef void *GoChan;
|
||||
typedef struct { void *t; void *v; } GoInterface;
|
||||
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
|
||||
typedef struct { GoFloat32 real; GoFloat32 imag; } GoComplex64;
|
||||
typedef struct { GoFloat64 real; GoFloat64 imag; } GoComplex128;
|
||||
|
||||
extern GoComplex64 _cgopy_GoComplex64(GoFloat32 p0, GoFloat32 p1);
|
||||
extern GoComplex128 _cgopy_GoComplex128(GoFloat64 p0, GoFloat64 p1);
|
||||
extern GoString _cgopy_GoString(char* p0);
|
||||
extern char* _cgopy_CString(GoString p0);
|
||||
extern void _cgopy_FreeCString(char* p0);
|
||||
|
@ -62,6 +66,20 @@ class _cffi_helper(object):
|
|||
def cffi_cgopy_cnv_py2c_bool(o):
|
||||
return ffi.cast('_Bool', o)
|
||||
|
||||
@staticmethod
|
||||
def cffi_cgopy_cnv_py2c_complex64(o):
|
||||
real = o.real
|
||||
imag = o.imag
|
||||
complex64 = _cffi_helper.lib._cgopy_GoComplex64(real, imag)
|
||||
return complex64
|
||||
|
||||
@staticmethod
|
||||
def cffi_cgopy_cnv_py2c_complex128(o):
|
||||
real = o.real
|
||||
imag = o.imag
|
||||
complex128 = _cffi_helper.lib._cgopy_GoComplex128(real, imag)
|
||||
return complex128
|
||||
|
||||
@staticmethod
|
||||
def cffi_cgopy_cnv_py2c_string(o):
|
||||
s = ffi.new("char[]", o)
|
||||
|
@ -87,6 +105,14 @@ class _cffi_helper(object):
|
|||
def cffi_cgopy_cnv_c2py_bool(c):
|
||||
return bool(c)
|
||||
|
||||
@staticmethod
|
||||
def cffi_cgopy_cnv_c2py_complex64(c):
|
||||
return complex(c.real, c.imag)
|
||||
|
||||
@staticmethod
|
||||
def cffi_cgopy_cnv_c2py_complex128(c):
|
||||
return complex(c.real, c.imag)
|
||||
|
||||
@staticmethod
|
||||
def cffi_cgopy_cnv_c2py_string(c):
|
||||
s = _cffi_helper.lib._cgopy_CString(c)
|
||||
|
|
|
@ -94,6 +94,16 @@ func _cgopy_FreeCString(cs *C.char) {
|
|||
C.free(unsafe.Pointer(cs))
|
||||
}
|
||||
|
||||
//export _cgopy_GoComplex64
|
||||
func _cgopy_GoComplex64(real, imag float32) complex64 {
|
||||
return complex(real, imag)
|
||||
}
|
||||
|
||||
//export _cgopy_GoComplex128
|
||||
func _cgopy_GoComplex128(real, imag float64) complex128 {
|
||||
return complex(real, imag)
|
||||
}
|
||||
|
||||
// --- end cgo helpers ---
|
||||
|
||||
// --- begin cref helpers ---
|
||||
|
|
|
@ -963,7 +963,7 @@ func init() {
|
|||
goname: "complex64",
|
||||
cpyname: "float complex",
|
||||
cgoname: "GoComplex64",
|
||||
pyfmt: "D",
|
||||
pyfmt: "O&",
|
||||
pybuf: "ff",
|
||||
pysig: "complex",
|
||||
c2py: "cgopy_cnv_c2py_complex64",
|
||||
|
@ -979,7 +979,7 @@ func init() {
|
|||
goname: "complex128",
|
||||
cpyname: "double complex",
|
||||
cgoname: "GoComplex128",
|
||||
pyfmt: "D",
|
||||
pyfmt: "O&",
|
||||
pybuf: "dd",
|
||||
pysig: "complex",
|
||||
c2py: "cgopy_cnv_c2py_complex128",
|
||||
|
|
|
@ -378,6 +378,8 @@ fct()...
|
|||
pkg.Add(1,2)= 3
|
||||
pkg.Bool(True)= True
|
||||
pkg.Bool(False)= False
|
||||
pkg.Comp64Add((3+4j), (2+5j)) = (5+9j)
|
||||
pkg.Comp128Add((3+4j), (2+5j)) = (5+9j)
|
||||
`),
|
||||
})
|
||||
|
||||
|
@ -391,6 +393,8 @@ fct()...
|
|||
pkg.Add(1,2)= 3
|
||||
pkg.Bool(True)= True
|
||||
pkg.Bool(False)= False
|
||||
pkg.Comp64Add((3+4j), (2+5j)) = (5+9j)
|
||||
pkg.Comp128Add((3+4j), (2+5j)) = (5+9j)
|
||||
`),
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue