a29f34cb52326848ce90be73098772f54d694292
[pyke] / pyke-mLib.c
1 /* -*-c-*-
2 *
3 * Pyke: the Python Kit for Extensions, mLib integration
4 *
5 * (c) 2019 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Pyke: the Python Kit for Extensions.
11 *
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.
16 *
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
20 * for more details.
21 *
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.
25 */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "pyke-mLib.h"
30
31 /* #undef HAVE_LONG_LONG */
32
33 /*----- Conversions -------------------------------------------------------*/
34
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); }
39 #endif
40
41 PyObject *getk64(kludge64 u)
42 {
43 #ifdef HAVE_LONG_LONG
44 return (PyLong_FromUnsignedLongLong(GET64(unsigned PY_LONG_LONG, u)));
45 #else
46 PyObject *i = 0, *j = 0, *t;
47 PyObject *rc = 0;
48
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;
52 Py_DECREF(i); i = t;
53 if ((j = PyLong_FromUnsignedLong(LO64(u))) == 0) goto end;
54 if ((t = PyNumber_InPlaceOr(i, j)) == 0) goto end;
55 Py_DECREF(i); i = t;
56 if ((rc = PyNumber_Int(i)) == 0) goto end;
57 end:
58 Py_XDECREF(i);
59 Py_XDECREF(j);
60 return (rc);
61 #endif
62 }
63
64 #ifdef HAVE_UINT64
65 # define CONVu64(n) do { \
66 kludge64 k; \
67 uint64 t; \
68 if (!convk64(o, &k)) goto end; \
69 t = GET64(uint64, k); \
70 if (t > MASK##n) VALERR("out of range"); \
71 *p = t; \
72 } while (0)
73 #else
74 # define CONVu64(n) assert(!"shouldn't be possible")
75 #endif
76
77 #define CONVU_(n) \
78 int convu##n(PyObject *o, void *pp) \
79 { \
80 unsigned long u; \
81 uint##n *p = pp; \
82 \
83 if (MASK##n > ULONG_MAX) \
84 CONVu64(n); \
85 else { \
86 if (!convulong(o, &u)) goto end; \
87 if (u > MASK##n) VALERR("out of range"); \
88 *p = u; \
89 } \
90 return (1); \
91 end: \
92 return (0); \
93 }
94 DOUINTSZ(CONVU_)
95
96 int convk64(PyObject *o, void *pp)
97 {
98 PyObject *i = 0;
99 int rc = 0;
100 #if HAVE_LONG_LONG
101 unsigned PY_LONG_LONG t;
102 #else
103 PyObject *t;
104 uint32 lo, hi;
105 #endif
106
107 #if HAVE_LONG_LONG
108 if ((i = PyNumber_Long(o)) == 0) goto end;
109 t = PyLong_AsUnsignedLongLong(i);
110 if (t == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) goto end;
111 ASSIGN64(*(kludge64 *)pp, t);
112 #else
113 if (init_i32()) goto end;
114 if ((i = PyNumber_Int(o)) == 0) goto end;
115 lo = PyInt_AsUnsignedLongMask(i);
116 if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
117 Py_DECREF(i); i = t;
118 hi = PyInt_AsUnsignedLongMask(i);
119 if ((t = PyNumber_InPlaceRshift(i, i32)) == 0) goto end;
120 Py_DECREF(i); i = t;
121 if (PyObject_IsTrue(i)) VALERR("out of range");
122 SET64(*(kludge64 *)pp, hi, lo);
123 #endif
124 rc = 1;
125
126 end:
127 Py_XDECREF(i);
128 return (rc);
129 }
130
131 /*----- That's all, folks -------------------------------------------------*/