#include #include typedef intptr_t Py_ssize_t; #if UINTPTR_MAX == 0xffffffff typedef short sdigit; #elif UINTPTR_MAX == 0xffffffffffffffff typedef int32_t sdigit; #endif typedef struct PyObject { Py_ssize_t ob_refcnt; void *b; } PyObject; typedef struct PyLongObject { Py_ssize_t ob_refcnt; void *b, *c, *d; } PyLongObject; static inline void _Py_INCREF(PyObject *op) { op->ob_refcnt++; } #define Py_INCREF(op) _Py_INCREF((PyObject *)(op)) #ifndef NSMALLPOSINTS #define NSMALLPOSINTS 257 #endif #ifndef NSMALLNEGINTS #define NSMALLNEGINTS 5 #endif PyLongObject small_ints[NSMALLNEGINTS + NSMALLPOSINTS]; #define CHECK_SMALL_INT(ival) \ do if (-NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS) { \ return get_small_int((sdigit)ival); \ } while(0) static inline int is_small_int(long long ival) { return -NSMALLNEGINTS <= ival && ival < NSMALLPOSINTS; } static PyObject * get_small_int(sdigit ival) { PyObject *v; v = (PyObject *)&small_ints[ival + NSMALLNEGINTS]; Py_INCREF(v); return v; } PyObject *PyLong_FromSsize_t_rest(Py_ssize_t ival); // original code: use macro PyObject * PyLong_FromSsize_t_withmacro(Py_ssize_t ival) { CHECK_SMALL_INT(ival); return PyLong_FromSsize_t_rest(ival); } // use inlined function PyObject * PyLong_FromSsize_t_withfunc(Py_ssize_t ival) { if (is_small_int(ival)) { return get_small_int((sdigit)ival); } return PyLong_FromSsize_t_rest(ival); }