Commit 8243e467 authored by Robert Bradshaw's avatar Robert Bradshaw

CIntToPy functions.

parent 96fbd74f
......@@ -1566,12 +1566,19 @@ class CIntType(CNumericType):
self.get_from_py_type_conversion()
def create_from_py_utility_code(self, env):
self.from_py_function = "__Pyx_PyInt_As" + self.specialization_name()
self.from_py_function = "__Pyx_PyInt_to_py_" + self.specialization_name()
env.use_utility_code(TempitaUtilityCode.load(
"CIntFromPy", "TypeConversion.c",
context={"TYPE": self.declaration_code(''), "FROM_PY_FUNCTION": self.from_py_function}))
return True
def create_to_py_utility_code(self, env):
self.from_to_function = "__Pyx_PyInt_As" + self.specialization_name()
env.use_utility_code(TempitaUtilityCode.load(
"CIntToPy", "TypeConversion.c",
context={"TYPE": self.declaration_code(''), "TO_PY_FUNCTION": self.from_to_function}))
return True
def get_to_py_type_conversion(self):
if self.rank < list(rank_to_type_name).index('int'):
# This assumes sizeof(short) < sizeof(int)
......
......@@ -449,10 +449,40 @@ static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) {
return (Py_UNICODE)ival;
}
/////////////// CIntToPy.proto ///////////////
static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value);
/////////////// CIntToPy ///////////////
static CYTHON_INLINE PyObject* {{TO_PY_FUNCTION}}({{TYPE}} value) {
const {{TYPE}} neg_one = ({{TYPE}}) -1, const_zero = 0;
const int is_unsigned = neg_one > const_zero;
if (is_unsigned) {
if (sizeof({{TYPE}}) < sizeof(unsigned long)) {
return PyInt_FromLong(value);
} else if (sizeof({{TYPE}}) <= sizeof(unsigned long)) {
return PyLong_FromUnsignedLong(value);
} else if (sizeof({{TYPE}}) <= sizeof(unsigned long long)) {
return PyLong_FromUnsignedLongLong(value);
}
} else {
if (sizeof({{TYPE}}) <= sizeof(long)) {
return PyInt_FromLong(value);
} else if (sizeof({{TYPE}}) <= sizeof(long long)) {
return PyLong_FromLong(value);
}
}
{
int one = 1; int little = (int)*(unsigned char *)&one;
unsigned char *bytes = (unsigned char *)&value;
return _PyLong_FromByteArray(bytes, sizeof({{TYPE}}),
little, !is_unsigned);
}
}
/////////////// CIntFromPyVerify ///////////////
#define __PYX_VERIFY_RETURN_INT(type, value_type, func) \
......@@ -470,6 +500,7 @@ static CYTHON_INLINE Py_UNICODE __Pyx_PyObject_AsPy_UNICODE(PyObject* x) {
return (type) value; \
}
/////////////// CIntFromPy.proto ///////////////
static CYTHON_INLINE {{TYPE}} {{FROM_PY_FUNCTION}}(PyObject *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment