Debian Codegraph @

lib_pypy/hpy/devel/src/runtime/ctx_long.c

raw symbols SCIP
1 #include <Python.h>
2 #include "hpy.h"
3
4 #ifndef HPY_ABI_CPYTHON
5 // for _h2py and _py2h
6 # include "handles.h"
7 #endif
8
9 // just for better readability
10 #define SIZEOF_INT32 4
11 #define SIZEOF_INT64 8
12
13 HPyAPI_IMPL HPy ctx_Long_FromInt32_t(HPyContext *ctx, int32_t value) {
14 #if SIZEOF_LONG >= SIZEOF_INT32
15 return _py2h(PyLong_FromLong((long)value));
16 #else
17 #error "unsupported SIZEOF_LONG"
18 #endif
19 }
20
21 HPyAPI_IMPL HPy ctx_Long_FromUInt32_t(HPyContext *ctx, uint32_t value) {
22 #if SIZEOF_LONG >= SIZEOF_INT32
23 return _py2h(PyLong_FromUnsignedLong((unsigned long)value));
24 #else
25 #error "unsupported SIZEOF_LONG"
26 #endif
27 }
28
29 HPyAPI_IMPL HPy ctx_Long_FromInt64_t(HPyContext *ctx, int64_t v) {
30 #if SIZEOF_LONG >= SIZEOF_INT64
31 return _py2h(PyLong_FromLong((long)v));
32 #elif SIZEOF_LONG_LONG >= SIZEOF_INT64
33 return _py2h(PyLong_FromLongLong((long long)v));
34 #else
35 #error "unsupported SIZEOF_LONG_LONG"
36 #endif
37 }
38
39 HPyAPI_IMPL HPy ctx_Long_FromUInt64_t(HPyContext *ctx, uint64_t v) {
40 #if SIZEOF_LONG >= SIZEOF_INT64
41 return _py2h(PyLong_FromUnsignedLong((unsigned long)v));
42 #elif SIZEOF_LONG_LONG >= SIZEOF_INT64
43 return _py2h(PyLong_FromUnsignedLongLong((unsigned long long)v));
44 #else
45 #error "unsupported SIZEOF_LONG_LONG"
46 #endif
47 }
48
49 HPyAPI_IMPL int32_t ctx_Long_AsInt32_t(HPyContext *ctx, HPy h) {
50 long lres = PyLong_AsLong(_h2py(h));
51 #if SIZEOF_LONG == SIZEOF_INT32
52 return (int32_t) lres;
53 #elif SIZEOF_LONG >= SIZEOF_INT32
54 int32_t i32res = (int32_t) lres;
55 if (lres == (long) i32res)
56 return i32res;
57 PyErr_SetString(PyExc_OverflowError,
58 "Python int too large to convert to C int32_t");
59 return (int32_t) -1;
60 #else
61 #error "unsupported SIZEOF_LONG"
62 #endif
63 }
64
65 HPyAPI_IMPL uint32_t ctx_Long_AsUInt32_t(HPyContext *ctx, HPy h) {
66 unsigned long lres = PyLong_AsUnsignedLong(_h2py(h));
67 #if SIZEOF_LONG == SIZEOF_INT32
68 return (uint32_t) lres;
69 #elif SIZEOF_LONG >= SIZEOF_INT32
70 uint32_t ui32res = (uint32_t) lres;
71 if (lres == (unsigned long) ui32res)
72 return ui32res;
73 PyErr_SetString(PyExc_OverflowError,
74 "Python int too large to convert to C uint32_t");
75 return (uint32_t) -1;
76 #else
77 #error "unsupported SIZEOF_LONG"
78 #endif
79 }
80
81 HPyAPI_IMPL uint32_t ctx_Long_AsUInt32_tMask(HPyContext *ctx, HPy h) {
82 return (uint32_t) PyLong_AsUnsignedLongMask(_h2py(h));
83 }
84
85 HPyAPI_IMPL int64_t ctx_Long_AsInt64_t(HPyContext *ctx, HPy h) {
86 long long lres = PyLong_AsLongLong(_h2py(h));
87 #if SIZEOF_LONG_LONG == SIZEOF_INT64
88 return (int64_t) lres;
89 #elif SIZEOF_LONG_LONG >= SIZEOF_INT64
90 int64_t i64res = (int64_t) lres;
91 if (lres == (long long) i64res)
92 return i64res;
93 PyErr_SetString(PyExc_OverflowError,
94 "Python int too large to convert to C int64_t");
95 return (int64_t) -1;
96 #else
97 #error "unsupported SIZEOF_LONG_LONG"
98 #endif
99 }
100
101 HPyAPI_IMPL uint64_t ctx_Long_AsUInt64_t(HPyContext *ctx, HPy h) {
102 unsigned long long lres = PyLong_AsUnsignedLongLong(_h2py(h));
103 #if SIZEOF_LONG_LONG == SIZEOF_INT64
104 return (uint64_t) lres;
105 #elif SIZEOF_LONG_LONG >= SIZEOF_INT64
106 uint64_t ui64res = (uint64_t) lres;
107 if (lres == (unsigned long long) ui64res)
108 return ui64res;
109 PyErr_SetString(PyExc_OverflowError,
110 "Python int too large to convert to C uint64_t");
111 return (uint64_t) -1;
112 #else
113 #error "unsupported SIZEOF_LONG_LONG"
114 #endif
115 }
116
117 HPyAPI_IMPL uint64_t ctx_Long_AsUInt64_tMask(HPyContext *ctx, HPy h) {
118 return (uint64_t) PyLong_AsUnsignedLongLongMask(_h2py(h));
119 }