X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/blobdiff_plain/c263b05cca879ab1bdca5823df0413604d39d4c6..e7d2e2e29042a980933261a173d96c71982514fa:/field.c diff --git a/field.c b/field.c index 9da6063..ba00d34 100644 --- a/field.c +++ b/field.c @@ -107,23 +107,18 @@ PyObject *fe_pywrap(PyObject *fobj, mp *x) return ((PyObject *)z); } -static mp *tofe(field *f, PyObject *o) +static mp *implicitfe(field *f, PyObject *o) { - mp *x = 0, *y = 0; - - if (FE_PYCHECK(o)) { - if (FE_F(o) != f && !field_samep(FE_F(o), f)) return (0); - y = MP_COPY(FE_X(o)); - } else if ((x = tomp(o)) != 0) { - if (MP_ZEROP(x)) - y = MP_COPY(f->zero); - else if (MP_EQ(x, MP_ONE)) - y = MP_COPY(f->one); - else - y = F_IN(f, MP_NEW, x); - MP_DROP(x); + mp *x; + + if (FE_PYCHECK(o) && FE_F(o) == f) return (MP_COPY(FE_X(o))); + switch (f->ops->ty) { + case FTY_PRIME: x = implicitmp(o); break; + case FTY_BINARY: x = implicitgf(o); break; + default: assert(!"huh?"); } - return (y); + if (!x) return (0); + return (F_IN(f, MP_NEW, x)); } /*----- Field elements ----------------------------------------------------*/ @@ -135,9 +130,9 @@ static int febinop(PyObject *x, PyObject *y, else if (FE_PYCHECK(y)) *fobj = FE_FOBJ(y); else return (-1); *f = FIELD_F(*fobj); - if ((*xx = tofe(*f, x)) == 0) + if ((*xx = implicitfe(*f, x)) == 0) return (-1); - if ((*yy = tofe(*f, y)) == 0) { + if ((*yy = implicitfe(*f, y)) == 0) { MP_DROP(*xx); return (-1); } @@ -179,7 +174,7 @@ static PyObject *fe_pyexp(PyObject *x, PyObject *y, PyObject *z) field *ff; mp *xx, *yy; - if (z != Py_None || !FE_PYCHECK(x) || (yy = tomp(y)) == 0) + if (z != Py_None || !FE_PYCHECK(x) || (yy = implicitmp(y)) == 0) RETURN_NOTIMPL; ff = FE_F(x); xx = FE_X(x); MP_COPY(xx); if (MP_NEGP(yy) && F_ZEROP(ff, xx)) ZDIVERR("division by zero"); @@ -218,9 +213,10 @@ end: return (rc); } -static long fe_pyhash(PyObject *me) +static Py_hash_t fe_pyhash(PyObject *me) { return (mphash(FE_X(me))); } +#ifdef PY2 static int fe_pycoerce(PyObject **x, PyObject **y) { mp *z; @@ -231,7 +227,7 @@ static int fe_pycoerce(PyObject **x, PyObject **y) Py_INCREF(*x); Py_INCREF(*y); return (0); } - if ((z = tofe(FE_F(*x), *y)) != 0) { + if ((z = implicitfe(FE_F(*x), *y)) != 0) { Py_INCREF(*x); *y = fe_pywrap(FE_FOBJ(*x), z); return (0); @@ -241,6 +237,7 @@ static int fe_pycoerce(PyObject **x, PyObject **y) end: return (-1); } +#endif static PyObject *fe_pyint(PyObject *x) { @@ -253,6 +250,7 @@ static PyObject *fe_pyint(PyObject *x) return (rc); } +#ifdef PY2 static PyObject *fe_pylong(PyObject *x) { mp *xx = F_OUT(FE_F(x), MP_NEW, FE_X(x)); @@ -260,6 +258,7 @@ static PyObject *fe_pylong(PyObject *x) MP_DROP(xx); return (rc); } +#endif #define BASEOP(name, radix, pre) \ static PyObject *fe_py##name(PyObject *x) { \ @@ -268,7 +267,9 @@ static PyObject *fe_pylong(PyObject *x) MP_DROP(xx); \ return (rc); \ } +#ifdef PY2 BASEOP(oct, 8, "0"); +#endif BASEOP(hex, 16, "0x"); #undef BASEOP @@ -322,7 +323,7 @@ static PyObject *feget__value(PyObject *me, void *hunoz) static const PyMemberDef fe_pymembers[] = { #define MEMBERSTRUCT fe_pyobj - MEMRNM(field, T_OBJECT, ob_type, READONLY, + MEMRNM(field, T_OBJECT, PY23(ob_type, ob_base.ob_type), READONLY, "X.field -> field containing X") #undef MEMBERSTRUCT { 0 } @@ -354,7 +355,9 @@ static const PyNumberMethods fe_pynumber = { fe_pyadd, /* @nb_add@ */ fe_pysub, /* @nb_subtract@ */ fe_pymul, /* @nb_multiply@ */ +#ifdef PY2 fe_pydiv, /* @nb_divide@ */ +#endif 0, /* @nb_remainder@ */ 0, /* @nb_divmod@ */ fe_pyexp, /* @nb_power@ */ @@ -368,17 +371,23 @@ static const PyNumberMethods fe_pynumber = { 0, /* @nb_and@ */ 0, /* @nb_xor@ */ 0, /* @nb_or@ */ +#ifdef PY2 fe_pycoerce, /* @nb_coerce@ */ +#endif fe_pyint, /* @nb_int@ */ - fe_pylong, /* @nb_long@ */ + PY23(fe_pylong, 0), /* @nb_long@ */ 0 /* meaningless */, /* @nb_float@ */ +#ifdef PY2 fe_pyoct, /* @nb_oct@ */ fe_pyhex, /* @nb_hex@ */ +#endif 0, /* @nb_inplace_add@ */ 0, /* @nb_inplace_subtract@ */ 0, /* @nb_inplace_multiply@ */ +#ifdef PY2 0, /* @nb_inplace_divide@ */ +#endif 0, /* @nb_inplace_remainder@ */ 0, /* @nb_inplace_power@ */ 0, /* @nb_inplace_lshift@ */ @@ -391,10 +400,12 @@ static const PyNumberMethods fe_pynumber = { fe_pydiv, /* @nb_true_divide@ */ 0, /* @nb_inplace_floor_divide@ */ 0, /* @nb_inplace_true_divide@ */ + + fe_pyint, /* @nb_index@ */ }; static const PyTypeObject fe_pytype_skel = { - PyObject_HEAD_INIT(0) 0, /* Header */ + PyVarObject_HEAD_INIT(0, 0) /* Header */ "FE", /* @tp_name@ */ sizeof(fe_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -512,7 +523,7 @@ static PyObject *fget_noctets(PyObject *me, void *hunoz) { return (PyInt_FromLong(FIELD_F(me)->noctets)); } static PyObject *fget_name(PyObject *me, void *hunoz) - { return (PyString_FromString(F_NAME(FIELD_F(me)))); } + { return (TEXT_FROMSTR(F_NAME(FIELD_F(me)))); } static PyObject *fget_type(PyObject *me, void *hunoz) { return (PyInt_FromLong(F_TYPE(FIELD_F(me)))); } @@ -540,7 +551,7 @@ static const PyMethodDef field_pymethods[] = { }; static const PyTypeObject field_pytype_skel = { - PyObject_HEAD_INIT(0) 0, /* Header */ + PyVarObject_HEAD_INIT(0, 0) /* Header */ "Field", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -618,7 +629,7 @@ static const PyGetSetDef primefield_pygetset[] = { }; static const PyTypeObject primefield_pytype_skel = { - PyObject_HEAD_INIT(0) 0, /* Header */ + PyVarObject_HEAD_INIT(0, 0) /* Header */ "PrimeField", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -646,7 +657,7 @@ static const PyTypeObject primefield_pytype_skel = { 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ - field_pyrichcompare, /* @tp_richcompare@ */ + 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ @@ -685,7 +696,7 @@ end: } static const PyTypeObject niceprimefield_pytype_skel = { - PyObject_HEAD_INIT(0) 0, /* Header */ + PyVarObject_HEAD_INIT(0, 0) /* Header */ "NicePrimeField", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -713,7 +724,7 @@ static const PyTypeObject niceprimefield_pytype_skel = { 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ - field_pyrichcompare, /* @tp_richcompare@ */ + 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ @@ -749,7 +760,7 @@ static const PyGetSetDef binfield_pygetset[] = { }; static const PyTypeObject binfield_pytype_skel = { - PyObject_HEAD_INIT(0) 0, /* Header */ + PyVarObject_HEAD_INIT(0, 0) /* Header */ "BinField", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -777,7 +788,7 @@ static const PyTypeObject binfield_pytype_skel = { 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ - field_pyrichcompare, /* @tp_richcompare@ */ + 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ @@ -815,7 +826,7 @@ end: } static const PyTypeObject binpolyfield_pytype_skel = { - PyObject_HEAD_INIT(0) 0, /* Header */ + PyVarObject_HEAD_INIT(0, 0) /* Header */ "BinPolyField", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -843,7 +854,7 @@ static const PyTypeObject binpolyfield_pytype_skel = { 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ - field_pyrichcompare, /* @tp_richcompare@ */ + 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ @@ -894,7 +905,7 @@ static const PyGetSetDef binnormfield_pygetset[] = { }; static const PyTypeObject binnormfield_pytype_skel = { - PyObject_HEAD_INIT(0) 0, /* Header */ + PyVarObject_HEAD_INIT(0, 0) /* Header */ "BinNormField", /* @tp_name@ */ sizeof(field_pyobj), /* @tp_basicsize@ */ 0, /* @tp_itemsize@ */ @@ -922,7 +933,7 @@ static const PyTypeObject binnormfield_pytype_skel = { 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ - field_pyrichcompare, /* @tp_richcompare@ */ + 0, /* @tp_richcompare@ */ 0, /* @tp_weaklistoffset@ */ 0, /* @tp_iter@ */ 0, /* @tp_iternext@ */ @@ -943,6 +954,11 @@ static const PyTypeObject binnormfield_pytype_skel = { /*----- Setup -------------------------------------------------------------*/ +static const struct nameval consts[] = { + CONST(FTY_PRIME), CONST(FTY_BINARY), + { 0 } +}; + void field_pyinit(void) { INITTYPE(fe, root); @@ -963,6 +979,7 @@ void field_pyinsert(PyObject *mod) INSERT("BinField", binfield_pytype); INSERT("BinPolyField", binpolyfield_pytype); INSERT("BinNormField", binnormfield_pytype); + setconstants(mod, consts); } /*----- That's all, folks -------------------------------------------------*/