Commit 9dbcaa45 authored by Boxiang Sun's avatar Boxiang Sun

copy complex implementation from CPython

parent 92666536
// This file is originally from CPython 2.7, with modifications for Pyston
/* Complex number structure */
#ifndef Py_COMPLEXOBJECT_H
......@@ -23,13 +21,13 @@ typedef struct {
#define c_pow _Py_c_pow
#define c_abs _Py_c_abs
PyAPI_FUNC(Py_complex) c_sum(Py_complex, Py_complex) PYSTON_NOEXCEPT;
PyAPI_FUNC(Py_complex) c_diff(Py_complex, Py_complex) PYSTON_NOEXCEPT;
PyAPI_FUNC(Py_complex) c_neg(Py_complex) PYSTON_NOEXCEPT;
PyAPI_FUNC(Py_complex) c_prod(Py_complex, Py_complex) PYSTON_NOEXCEPT;
PyAPI_FUNC(Py_complex) c_quot(Py_complex, Py_complex) PYSTON_NOEXCEPT;
PyAPI_FUNC(Py_complex) c_pow(Py_complex, Py_complex) PYSTON_NOEXCEPT;
PyAPI_FUNC(double) c_abs(Py_complex) PYSTON_NOEXCEPT;
PyAPI_FUNC(Py_complex) c_sum(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) c_diff(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) c_neg(Py_complex);
PyAPI_FUNC(Py_complex) c_prod(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) c_quot(Py_complex, Py_complex);
PyAPI_FUNC(Py_complex) c_pow(Py_complex, Py_complex);
PyAPI_FUNC(double) c_abs(Py_complex);
/* Complex object interface */
......@@ -39,35 +37,28 @@ PyComplexObject represents a complex number with double-precision
real and imaginary parts.
*/
// Pyston change: this is not our object format
#if 0
typedef struct {
PyObject_HEAD
Py_complex cval;
} PyComplexObject;
#endif
typedef struct _PyComplexObject PyComplexObject;
// Pyston change: this is not a static object any more
// PyAPI_DATA(PyTypeObject) PyComplex_Type;
PyAPI_DATA(PyTypeObject*) complex_cls;
#define PyComplex_Type (*complex_cls)
PyAPI_DATA(PyTypeObject) PyComplex_Type;
#define PyComplex_Check(op) PyObject_TypeCheck(op, &PyComplex_Type)
#define PyComplex_CheckExact(op) (Py_TYPE(op) == &PyComplex_Type)
PyAPI_FUNC(PyObject *) PyComplex_FromCComplex(Py_complex) PYSTON_NOEXCEPT;
PyAPI_FUNC(PyObject *) PyComplex_FromDoubles(double real, double imag) PYSTON_NOEXCEPT;
PyAPI_FUNC(PyObject *) PyComplex_FromCComplex(Py_complex);
PyAPI_FUNC(PyObject *) PyComplex_FromDoubles(double real, double imag);
PyAPI_FUNC(double) PyComplex_RealAsDouble(PyObject *op) PYSTON_NOEXCEPT;
PyAPI_FUNC(double) PyComplex_ImagAsDouble(PyObject *op) PYSTON_NOEXCEPT;
PyAPI_FUNC(Py_complex) PyComplex_AsCComplex(PyObject *op) PYSTON_NOEXCEPT;
PyAPI_FUNC(double) PyComplex_RealAsDouble(PyObject *op);
PyAPI_FUNC(double) PyComplex_ImagAsDouble(PyObject *op);
PyAPI_FUNC(Py_complex) PyComplex_AsCComplex(PyObject *op);
/* Format the object based on the format_spec, as defined in PEP 3101
(Advanced String Formatting). */
PyAPI_FUNC(PyObject *) _PyComplex_FormatAdvanced(PyObject *obj,
char *format_spec,
Py_ssize_t format_spec_len) PYSTON_NOEXCEPT;
Py_ssize_t format_spec_len);
#ifdef __cplusplus
}
......
......@@ -109,7 +109,7 @@ c_quot(Py_complex a, Py_complex b)
r.imag = (a.imag - a.real * ratio) / denom;
}
}
else {
else if (abs_bimag >= abs_breal) {
/* divide tops and bottom by b.imag */
const double ratio = b.real / b.imag;
const double denom = b.real * ratio + b.imag;
......@@ -117,6 +117,10 @@ c_quot(Py_complex a, Py_complex b)
r.real = (a.real * ratio + a.imag) / denom;
r.imag = (a.imag * ratio - a.real) / denom;
}
else {
/* At least one of b.real or b.imag is a NaN */
r.real = r.imag = Py_NAN;
}
return r;
}
......@@ -215,8 +219,6 @@ c_abs(Py_complex z)
return result;
}
// Pyston changes: comment this out
#if 0
static PyObject *
complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
{
......@@ -479,12 +481,10 @@ complex_hash(PyComplexObject *v)
return combined;
}
#endif
/* This macro may return! */
#define TO_COMPLEX(obj, c) \
if (PyComplex_Check(obj)) \
c = PyComplex_AsCComplex(obj); \
c = ((PyComplexObject *)(obj))->cval; \
else if (to_complex(&(obj), &(c)) < 0) \
return (obj)
......@@ -515,8 +515,7 @@ to_complex(PyObject **pobj, Py_complex *pc)
return -1;
}
// Pyston change: comment this out
#if 0
static PyObject *
complex_add(PyObject *v, PyObject *w)
{
......@@ -650,10 +649,8 @@ complex_divmod(PyObject *v, PyObject *w)
Py_XDECREF(m);
return z;
}
#endif
// Pyston change: make no static
PyObject *
static PyObject *
complex_pow(PyObject *v, PyObject *w, PyObject *z)
{
Py_complex p;
......@@ -690,8 +687,6 @@ complex_pow(PyObject *v, PyObject *w, PyObject *z)
return PyComplex_FromCComplex(p);
}
// Pyston changes: comment this out
#if 0
static PyObject *
complex_int_div(PyObject *v, PyObject *w)
{
......@@ -1317,7 +1312,8 @@ static PyNumberMethods complex_as_number = {
};
PyTypeObject PyComplex_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
// Pyston change, was &Py_Type_Type:
PyVarObject_HEAD_INIT(NULL, 0)
"complex",
sizeof(PyComplexObject),
0,
......@@ -1358,6 +1354,5 @@ PyTypeObject PyComplex_Type = {
complex_new, /* tp_new */
PyObject_Del, /* tp_free */
};
#endif
#endif
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