diff options
author | Pearu Peterson <pearu.peterson@gmail.com> | 2006-10-01 20:48:37 +0000 |
---|---|---|
committer | Pearu Peterson <pearu.peterson@gmail.com> | 2006-10-01 20:48:37 +0000 |
commit | bd4a480813a01fcd0e963d8b5d7cc1a579fa073f (patch) | |
tree | 8b2153ab5f19e04f45b45077d6e2c1cc1200566a /numpy/f2py | |
parent | c3c53e6805beb164581f041a69c9f51c2740d344 (diff) | |
download | numpy-bd4a480813a01fcd0e963d8b5d7cc1a579fa073f.tar.gz |
F2PY G3: fixed bugs, added float and complex scalars support.
Diffstat (limited to 'numpy/f2py')
-rw-r--r-- | numpy/f2py/lib/generate_pyobj_tofrom_funcs.py | 39 | ||||
-rw-r--r-- | numpy/f2py/lib/parser/typedecl_statements.py | 38 | ||||
-rw-r--r-- | numpy/f2py/lib/python_wrapper.py | 44 | ||||
-rw-r--r-- | numpy/f2py/lib/src/SCALARS_IN_BITS.cpp | 279 |
4 files changed, 340 insertions, 60 deletions
diff --git a/numpy/f2py/lib/generate_pyobj_tofrom_funcs.py b/numpy/f2py/lib/generate_pyobj_tofrom_funcs.py index b17d4a71e..188c35d34 100644 --- a/numpy/f2py/lib/generate_pyobj_tofrom_funcs.py +++ b/numpy/f2py/lib/generate_pyobj_tofrom_funcs.py @@ -9,12 +9,41 @@ __all__ = ['pyobj_to_npy_scalar','pyobj_to_f2py_string','pyobj_from_npy_scalar'] from parser.api import CHAR_BIT def pyobj_from_npy_int(ctype): - ctype_bits = int(ctype[7:]) - itemsize = ctype_bits/CHAR_BIT dtype = ctype.upper() + cls = 'Int'+ctype[7:] + return '''\ +/* depends: SCALARS_IN_BITS.cpp */ +static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) { + PyObject* obj = PyArrayScalar_New(%(cls)s); + if (obj==NULL) /* TODO: set exception */ return NULL; + PyArrayScalar_ASSIGN(obj,%(cls)s,*value); + return obj; +} +''' % (locals()) + +def pyobj_from_npy_float(ctype): + dtype = ctype.upper() + cls = 'Float'+ctype[9:] return '''\ +/* depends: SCALARS_IN_BITS.cpp */ static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) { - return PyArray_Return(PyArray_SimpleNewFromData(0, NULL, %(dtype)s, (char*)value)); + PyObject* obj = PyArrayScalar_New(%(cls)s); + if (obj==NULL) /* TODO: set exception */ return NULL; + PyArrayScalar_ASSIGN(obj,%(cls)s,*value); + return obj; +} +''' % (locals()) + +def pyobj_from_npy_complex(ctype): + dtype = ctype.upper() + cls = 'Complex'+ctype[11:] + return '''\ +/* depends: SCALARS_IN_BITS.cpp */ +static PyObject* pyobj_from_%(ctype)s(%(ctype)s* value) { + PyObject* obj = PyArrayScalar_New(%(cls)s); + if (obj==NULL) /* TODO: set exception */ return NULL; + PyArrayScalar_ASSIGN(obj,%(cls)s,*value); + return obj; } ''' % (locals()) @@ -125,5 +154,9 @@ static int pyobj_to_%(ctype)s(PyObject *obj, %(ctype)s* value) { def pyobj_from_npy_scalar(ctype): if ctype.startswith('npy_int'): return dict(c_code=pyobj_from_npy_int(ctype)) + elif ctype.startswith('npy_float'): + return dict(c_code=pyobj_from_npy_float(ctype)) + elif ctype.startswith('npy_complex'): + return dict(c_code=pyobj_from_npy_complex(ctype)) raise NotImplementedError,`ctype` diff --git a/numpy/f2py/lib/parser/typedecl_statements.py b/numpy/f2py/lib/parser/typedecl_statements.py index 2e5eea2bc..d656dafbf 100644 --- a/numpy/f2py/lib/parser/typedecl_statements.py +++ b/numpy/f2py/lib/parser/typedecl_statements.py @@ -341,11 +341,14 @@ class TypeDeclarationStatement(Statement): def get_length(self): return self.selector[0] or 1 - def get_bit_size(self): - return CHAR_BIT * int(self.get_kind()) - def get_byte_size(self): - return self.get_bit_size() / CHAR_BIT + length, kind = self.selector + if length: return int(length) + if kind: return int(kind) + return self.default_kind + + def get_bit_size(self): + return CHAR_BIT * int(self.get_byte_size()) def is_intrinsic(self): return not isinstance(self,(Type,Class)) def is_derived(self): return isinstance(self,Type) @@ -381,7 +384,10 @@ class Real(TypeDeclarationStatement): class DoublePrecision(TypeDeclarationStatement): match = re.compile(r'double\s*precision\b',re.I).match default_kind = 8 - + + def get_byte_size(self): + return self.default_kind + def get_zero_value(self): return '0.0D0' @@ -392,19 +398,11 @@ class Complex(TypeDeclarationStatement): match = re.compile(r'complex\b',re.I).match default_kind = 4 - def get_kind(self): + def get_byte_size(self): length, kind = self.selector - if kind: - return kind - if length: - return int(length)/2 - return self.default_kind - - def get_length(self): - return 2 * int(self.get_kind()) - - def get_bit_size(self): - return CHAR_BIT * self.get_length() + if length: return 2*int(length) + if kind: return 2*int(kind) + return 2*self.default_kind def get_zero_value(self): kind = self.get_kind() @@ -419,10 +417,8 @@ class DoubleComplex(TypeDeclarationStatement): match = re.compile(r'double\s*complex\b',re.I).match default_kind = 8 - def get_kind(self): return self.default_kind - def get_length(self): return 2 * self.get_kind() - def get_bit_size(self): - return CHAR_BIT * self.get_length() + def get_byte_size(self): + return 2 * self.default_kind def get_zero_value(self): return '(0.0D0,0.0D0)' diff --git a/numpy/f2py/lib/python_wrapper.py b/numpy/f2py/lib/python_wrapper.py index 35f3147fc..432f96c8f 100644 --- a/numpy/f2py/lib/python_wrapper.py +++ b/numpy/f2py/lib/python_wrapper.py @@ -23,6 +23,7 @@ extern \"C\" { #define PY_ARRAY_UNIQUE_SYMBOL PyArray_API #include "numpy/arrayobject.h" +#include "numpy/arrayscalars.h" %(header_list)s @@ -140,13 +141,7 @@ class PythonCAPIIntrinsicType(WrapperBase): self.ctype = ctype = typedecl.get_c_type() if ctype.startswith('npy_'): - from generate_pyobj_tofrom_funcs import pyobj_to_npy_scalar - d = pyobj_to_npy_scalar(ctype) - for v in d.values(): - self.resolve_dependencies(parent, v) - for k,v in d.items(): - l = getattr(parent, k+'_list') - l.append(v) + WrapperCCode(parent, 'pyobj_from_%s' % (ctype)) return if not ctype.startswith('f2py_type_'): @@ -598,38 +593,14 @@ initialize_%(typename)s_interface(initialize_%(typename)s_interface_c);\ if __name__ == '__main__': - #from utils import str2stmt, get_char_bit - - stmt = parse(""" - module rat - integer :: i - type info - integer flag - end type info - type rational - integer n - integer d - type(info) i - end type rational - end module rat - subroutine foo(a) - use rat - type(rational) a - end - """) - #stmt = stmt.content[-1].content[1] - #print stmt - #wrapgen = TypeWrapper(stmt) - #print wrapgen.fortran_code() - #print wrapgen.c_code() foo_code = """! -*- f90 -*- module rat type info - integer flag + complex flag end type info type rational - integer n,d + !integer n,d type(info) i end type rational end module rat @@ -685,14 +656,15 @@ if __name__ == '__main__': #print foo.info.__doc__ #print foo.rational.__doc__ #print dir(foo.rational) - i = foo.info(7) + i = foo.info(70+3j) + print 'i=',i #print i #,i.as_tuple() #print 'i.flag=',i.flag - r = foo.rational(2,3,i) + r = foo.rational(i) print r j = r.i print 'r.i.flag=',(r.i).flag - print 'j.flag=',j.flag + print 'j.flag=',type(j.flag) #print 'r=',r sys.exit() n,d,ii = r.as_tuple() diff --git a/numpy/f2py/lib/src/SCALARS_IN_BITS.cpp b/numpy/f2py/lib/src/SCALARS_IN_BITS.cpp new file mode 100644 index 000000000..78db836d0 --- /dev/null +++ b/numpy/f2py/lib/src/SCALARS_IN_BITS.cpp @@ -0,0 +1,279 @@ +#if NPY_BITSOF_LONG == 8 +# ifndef PyInt8ScalarObject +# define PyInt8ScalarObject PyLongScalarObject +# define PyInt8ArrType_Type PyLongArrType_Type +# endif +#elif NPY_BITSOF_LONG == 16 +# ifndef PyInt16ScalarObject +# define PyInt16ScalarObject PyLongScalarObject +# define PyInt16ArrType_Type PyLongArrType_Type +# endif +#elif NPY_BITSOF_LONG == 32 +# ifndef PyInt32ScalarObject +# define PyInt32ScalarObject PyLongScalarObject +# define PyInt32ArrType_Type PyLongArrType_Type +# endif +#elif NPY_BITSOF_LONG == 64 +# ifndef PyInt64ScalarObject +# define PyInt64ScalarObject PyLongScalarObject +# define PyInt64ArrType_Type PyLongArrType_Type +# endif +#elif NPY_BITSOF_LONG == 128 +# ifndef PyInt128ScalarObject +# define PyInt128ScalarObject PyLongScalarObject +# define PyInt128ArrType_Type PyLongArrType_Type +# endif +#endif + +#if NPY_BITSOF_LONGLONG == 8 +# ifndef PyInt8ScalarObject +# define PyInt8ScalarObject PyLongLongScalarObject +# define PyInt8ArrType_Type PyLongLongArrType_Type +# endif +#elif NPY_BITSOF_LONGLONG == 16 +# ifndef PyInt16ScalarObject +# define PyInt16ScalarObject PyLongLongScalarObject +# define PyInt16ArrType_Type PyLongLongArrType_Type +# endif +#elif NPY_BITSOF_LONGLONG == 32 +# ifndef PyInt32ScalarObject +# define PyInt32ScalarObject PyLongLongScalarObject +# define PyInt32ArrType_Type PyLongLongArrType_Type +# endif +#elif NPY_BITSOF_LONGLONG == 64 +# ifndef PyInt64ScalarObject +# define PyInt64ScalarObject PyLongLongScalarObject +# define PyInt64ArrType_Type PyLongLongArrType_Type +# endif +#elif NPY_BITSOF_LONGLONG == 128 +# ifndef PyInt128ScalarObject +# define PyInt128ScalarObject PyLongLongScalarObject +# define PyInt128ArrType_Type PyLongLongArrType_Type +# endif +#elif NPY_BITSOF_LONGLONG == 256 +# ifndef PyInt256ScalarObject +# define PyInt256ScalarObject PyLongLongScalarObject +# define PyInt256ArrType_Type PyLongLongArrType_Type +# endif +#endif + +#if NPY_BITSOF_INT == 8 +# ifndef PyInt8ScalarObject +# define PyInt8ScalarObject PyIntScalarObject +# define PyInt8ArrType_Type PyIntArrType_Type +# endif +#elif NPY_BITSOF_INT == 16 +# ifndef PyInt16ScalarObject +# define PyInt16ScalarObject PyIntScalarObject +# define PyInt16ArrType_Type PyIntArrType_Type +# endif +#elif NPY_BITSOF_INT == 32 +# ifndef PyInt32ScalarObject +# define PyInt32ScalarObject PyIntScalarObject +# define PyInt32ArrType_Type PyIntArrType_Type +# endif +#elif NPY_BITSOF_INT == 64 +# ifndef PyInt64ScalarObject +# define PyInt64ScalarObject PyIntScalarObject +# define PyInt64ArrType_Type PyIntArrType_Type +# endif +#elif NPY_BITSOF_INT == 128 +# ifndef PyInt128ScalarObject +# define PyInt128ScalarObject PyIntScalarObject +# define PyInt128ArrType_Type PyIntArrType_Type +# endif +#endif + +#if NPY_BITSOF_SHORT == 8 +# ifndef PyInt8ScalarObject +# define PyInt8ScalarObject PyShortScalarObject +# define PyInt8ArrType_Type PyShortArrType_Type +# endif +#elif NPY_BITSOF_SHORT == 16 +# ifndef PyInt16ScalarObject +# define PyInt16ScalarObject PyShortScalarObject +# define PyInt16ArrType_Type PyShortArrType_Type +# endif +#elif NPY_BITSOF_SHORT == 32 +# ifndef PyInt32ScalarObject +# define PyInt32ScalarObject PyShortScalarObject +# define PyInt32ArrType_Type PyShortArrType_Type +# endif +#elif NPY_BITSOF_SHORT == 64 +# ifndef PyInt64ScalarObject +# define PyInt64ScalarObject PyShortScalarObject +# define PyInt64ArrType_Type PyShortArrType_Type +# endif +#elif NPY_BITSOF_SHORT == 128 +# ifndef PyInt128ScalarObject +# define PyInt128ScalarObject PyShortScalarObject +# define PyInt128ArrType_Type PyShortArrType_Type +# endif +#endif + +#if NPY_BITSOF_CHAR == 8 +# ifndef PyInt8ScalarObject +# define PyInt8ScalarObject PyByteScalarObject +# define PyInt8ArrType_Type PyByteArrType_Type +# endif +#elif NPY_BITSOF_CHAR == 16 +# ifndef PyInt16ScalarObject +# define PyInt16ScalarObject PyByteScalarObject +# define PyInt16ArrType_Type PyByteArrType_Type +# endif +#elif NPY_BITSOF_CHAR == 32 +# ifndef PyInt32ScalarObject +# define PyInt32ScalarObject PyByteScalarObject +# define PyInt32ArrType_Type PyByteArrType_Type +# endif +#elif NPY_BITSOF_CHAR == 64 +# ifndef PyInt64ScalarObject +# define PyInt64ScalarObject PyByteScalarObject +# define PyInt64ArrType_Type PyByteArrType_Type +# endif +#elif NPY_BITSOF_CHAR == 128 +# ifndef PyInt128ScalarObject +# define PyInt128ScalarObject PyByteScalarObject +# define PyInt128ArrType_Type PyByteArrType_Type +# endif +#endif + +#if NPY_BITSOF_DOUBLE == 16 +# ifndef PyFloat16ScalarObject +# define PyFloat16ScalarObject PyDoubleScalarObject +# define PyComplex32ScalarObject PyCDoubleScalarObject +# define PyFloat16ArrType_Type PyDoubleArrType_Type +# define PyComplex32ArrType_Type PyCDoubleArrType_Type +# endif +#elif NPY_BITSOF_DOUBLE == 32 +# ifndef PyFloat32ScalarObject +# define PyFloat32ScalarObject PyDoubleScalarObject +# define PyComplex64ScalarObject PyCDoubleScalarObject +# define PyFloat32ArrType_Type PyDoubleArrType_Type +# define PyComplex64ArrType_Type PyCDoubleArrType_Type +# endif +#elif NPY_BITSOF_DOUBLE == 64 +# ifndef PyFloat64ScalarObject +# define PyFloat64ScalarObject PyDoubleScalarObject +# define PyComplex128ScalarObject PyCDoubleScalarObject +# define PyFloat64ArrType_Type PyDoubleArrType_Type +# define PyComplex128ArrType_Type PyCDoubleArrType_Type +# endif +#elif NPY_BITSOF_DOUBLE == 80 +# ifndef PyFloat80ScalarObject +# define PyFloat80ScalarObject PyDoubleScalarObject +# define PyComplex160ScalarObject PyCDoubleScalarObject +# define PyFloat80ArrType_Type PyDoubleArrType_Type +# define PyComplex160ArrType_Type PyCDoubleArrType_Type +# endif +#elif NPY_BITSOF_DOUBLE == 96 +# ifndef PyFloat96ScalarObject +# define PyFloat96ScalarObject PyDoubleScalarObject +# define PyComplex192ScalarObject PyCDoubleScalarObject +# define PyFloat96ArrType_Type PyDoubleArrType_Type +# define PyComplex192ArrType_Type PyCDoubleArrType_Type +# endif +#elif NPY_BITSOF_DOUBLE == 128 +# ifndef PyFloat128ScalarObject +# define PyFloat128ScalarObject PyDoubleScalarObject +# define PyComplex256ScalarObject PyCDoubleScalarObject +# define PyFloat128ArrType_Type PyDoubleArrType_Type +# define PyComplex256ArrType_Type PyCDoubleArrType_Type +# endif +#endif + +#if NPY_BITSOF_FLOAT == 16 +# ifndef PyFloat16ScalarObject +# define PyFloat16ScalarObject PyFloatScalarObject +# define PyComplex32ScalarObject PyCFloatScalarObject +# define PyFloat16ArrType_Type PyFloatArrType_Type +# define PyComplex32ArrType_Type PyCFloatArrType_Type +# endif +#elif NPY_BITSOF_FLOAT == 32 +# ifndef PyFloat32ScalarObject +# define PyFloat32ScalarObject PyFloatScalarObject +# define PyComplex64ScalarObject PyCFloatScalarObject +# define PyFloat32ArrType_Type PyFloatArrType_Type +# define PyComplex64ArrType_Type PyCFloatArrType_Type +# endif +#elif NPY_BITSOF_FLOAT == 64 +# ifndef PyFloat64ScalarObject +# define PyFloat64ScalarObject PyFloatScalarObject +# define PyComplex128ScalarObject PyCFloatScalarObject +# define PyFloat64ArrType_Type PyFloatArrType_Type +# define PyComplex128ArrType_Type PyCFloatArrType_Type +# endif +#elif NPY_BITSOF_FLOAT == 80 +# ifndef PyFloat80ScalarObject +# define PyFloat80ScalarObject PyFloatScalarObject +# define PyComplex160ScalarObject PyCFloatScalarObject +# define PyFloat80ArrType_Type PyFloatArrType_Type +# define PyComplex160ArrType_Type PyCFloatArrType_Type +# endif +#elif NPY_BITSOF_FLOAT == 96 +# ifndef PyFloat96ScalarObject +# define PyFloat96ScalarObject PyFloatScalarObject +# define PyComplex192ScalarObject PyCFloatScalarObject +# define PyFloat96ArrType_Type PyFloatArrType_Type +# define PyComplex192ArrType_Type PyCFloatArrType_Type +# endif +#elif NPY_BITSOF_FLOAT == 128 +# ifndef PyFloat128ScalarObject +# define PyFloat128ScalarObject PyFloatScalarObject +# define PyComplex256ScalarObject PyCFloatScalarObject +# define PyFloat128ArrType_Type PyFloatArrType_Type +# define PyComplex256ArrType_Type PyCFloatArrType_Type +# endif +#endif + +#if NPY_BITSOF_LONGDOUBLE == 16 +# ifndef PyFloat16ScalarObject +# define PyFloat16ScalarObject PyLongDoubleScalarObject +# define PyComplex32ScalarObject PyCLongDoubleScalarObject +# define PyFloat16ArrType_Type PyLongDoubleArrType_Type +# define PyComplex32ArrType_Type PyCLongDoubleArrType_Type +# endif +#elif NPY_BITSOF_LONGDOUBLE == 32 +# ifndef PyFloat32ScalarObject +# define PyFloat32ScalarObject PyLongDoubleScalarObject +# define PyComplex64ScalarObject PyCLongDoubleScalarObject +# define PyFloat32ArrType_Type PyLongDoubleArrType_Type +# define PyComplex64ArrType_Type PyCLongDoubleArrType_Type +# endif +#elif NPY_BITSOF_LONGDOUBLE == 64 +# ifndef PyFloat64ScalarObject +# define PyFloat64ScalarObject PyLongDoubleScalarObject +# define PyComplex128ScalarObject PyCLongDoubleScalarObject +# define PyFloat64ArrType_Type PyLongDoubleArrType_Type +# define PyComplex128ArrType_Type PyCLongDoubleArrType_Type +# endif +#elif NPY_BITSOF_LONGDOUBLE == 80 +# ifndef PyFloat80ScalarObject +# define PyFloat80ScalarObject PyLongDoubleScalarObject +# define PyComplex160ScalarObject PyCLongDoubleScalarObject +# define PyFloat80ArrType_Type PyLongDoubleArrType_Type +# define PyComplex160ArrType_Type PyCLongDoubleArrType_Type +# endif +#elif NPY_BITSOF_LONGDOUBLE == 96 +# ifndef PyFloat96ScalarObject +# define PyFloat96ScalarObject PyLongDoubleScalarObject +# define PyComplex192ScalarObject PyCLongDoubleScalarObject +# define PyFloat96ArrType_Type PyLongDoubleArrType_Type +# define PyComplex192ArrType_Type PyCLongDoubleArrType_Type +# endif +#elif NPY_BITSOF_LONGDOUBLE == 128 +# ifndef PyFloat128ScalarObject +# define PyFloat128ScalarObject PyLongDoubleScalarObject +# define PyComplex256ScalarObject PyCLongDoubleScalarObject +# define PyFloat128ArrType_Type PyLongDoubleArrType_Type +# define PyComplex256ArrType_Type PyCLongDoubleArrType_Type +# endif +#elif NPY_BITSOF_LONGDOUBLE == 256 +# ifndef PyFloat256ScalarObject +# define PyFloat256ScalarObject PyLongDoubleScalarObject +# define PyComplex512ScalarObject PyCLongDoubleScalarObject +# define PyFloat256ArrType_Type PyLongDoubleArrType_Type +# define PyComplex512ArrType_Type PyCLongDoubleArrType_Type +# endif +#endif + |