3 * Pyke: the Python Kit for Extensions, mLib integration
5 * (c) 2019 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Pyke: the Python Kit for Extensions.
12 * Pyke is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
17 * Pyke is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with Pyke. If not, write to the Free Software Foundation, Inc.,
24 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
29 #include "pyke-mLib.h"
31 /* #undef HAVE_LONG_LONG */
33 /*----- Conversions -------------------------------------------------------*/
35 #ifndef HAVE_LONG_LONG
36 static PyObject *i32 = 0;
37 static int init_i32(void)
38 { if (!i32 && (i32 = PyInt_FromLong(32)) == 0) return (-1); return (0); }
41 PyObject *getk64(kludge64 u)
44 return (PyLong_FromUnsignedLongLong(GET64(unsigned PY_LONG_LONG, u)));
46 PyObject *i = 0, *j = 0, *t;
49 if (init_i32()) goto end;
50 if ((i = PyLong_FromUnsignedLong(HI64(u))) == 0) goto end;
51 if ((t = PyNumber_InPlaceLshift(i, i32)) == 0) goto end;
53 if ((j = PyLong_FromUnsignedLong(LO64(u))) == 0) goto end;
54 if ((t = PyNumber_InPlaceOr(i, j)) == 0) goto end;
57 if ((t = PyNumber_Int(i)) == 0) goto end;
68 # define CONVu64(n) do { \
71 if (!convk64(o, &k)) goto end; \
72 t = GET64(uint64, k); \
73 if (t > MASK##n) VALERR("out of range"); \
77 # define CONVu64(n) assert(!"shouldn't be possible")
81 int convu##n(PyObject *o, void *pp) \
86 if (MASK##n > ULONG_MAX) \
89 if (!convulong(o, &u)) goto end; \
90 if (u > MASK##n) VALERR("out of range"); \
99 int convk64(PyObject *o, void *pp)
104 unsigned PY_LONG_LONG t;
111 if ((i = PyNumber_Long(o)) == 0) goto end;
112 t = PyLong_AsUnsignedLongLong(i);
113 if (t == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) goto end;
114 ASSIGN64(*(kludge64 *)pp, t);
116 if (init_i32()) goto end;
117 if ((i = PyNumber_Int(o)) == 0) goto end;
118 lo = PyInt_AsUnsignedLongMask(i);
119 if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
121 hi = PyInt_AsUnsignedLongMask(i);
122 if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
124 if (PyObject_IsTrue(i)) VALERR("out of range");
125 SET64(*(kludge64 *)pp, hi, lo);
134 /*----- That's all, folks -------------------------------------------------*/