unsigned_long.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "unsigned_long.h"
  2. #include "helpers.h"
  3. #include <util/generic/va_args.h>
  4. #include <util/system/compiler.h>
  5. #if (PY_MAJOR_VERSION == 2)
  6. #include <longintrepr.h>
  7. #endif
  8. #if (PY_MAJOR_VERSION >= 3) && defined(Py_LIMITED_API)
  9. #error "limited API for Python3 not supported yet"
  10. #endif
  11. #if PY_VERSION_HEX >= 0x030800b4 && PY_VERSION_HEX < 0x03090000
  12. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  13. #endif
  14. #ifndef Py_TPFLAGS_CHECKTYPES
  15. #define Py_TPFLAGS_CHECKTYPES 0
  16. #endif
  17. #if PY_VERSION_HEX < 0x030c0000
  18. #define GetSize(obj) Py_SIZE(obj)
  19. #define IsNegative(obj) GetSize(obj) < 0
  20. #define IsPositive(obj) GetSize(obj) >= 0
  21. #else
  22. #define GetSize(obj) (((PyLongObject*)obj)->long_value.lv_tag >> _PyLong_NON_SIZE_BITS)
  23. #define IsNegative(obj) ((((PyLongObject*)obj)->long_value.lv_tag & _PyLong_SIGN_MASK) & 2)
  24. #define IsPositive(obj) !IsNegative(obj)
  25. #endif
  26. namespace NCYson {
  27. static NPrivate::TPyObjectPtr PyUnsignedLong_ReprPtr;
  28. static void SetNegativeValueError(PyObject* obj, PyTypeObject* type) {
  29. assert(PyLong_Check(obj));
  30. assert(IsNegative(obj));
  31. PyObject* repr = ConvertPyLongToPyBytes(obj);
  32. PyErr_Format(
  33. PyExc_OverflowError,
  34. "cannot convert negative value (%s) to %s",
  35. Y_LIKELY(repr) ? PyBytes_AS_STRING(repr) : "???",
  36. type->tp_name);
  37. Py_XDECREF(repr);
  38. }
  39. PyObject* PreparePyUIntType(PyObject* reprfunc) {
  40. if (Y_UNLIKELY(PyType_Ready(&PyUnsignedLong_Type) < 0)) {
  41. return nullptr;
  42. }
  43. PyUnsignedLong_ReprPtr.Reset(reprfunc);
  44. Py_INCREF((PyObject*)&PyUnsignedLong_Type);
  45. return (PyObject*)&PyUnsignedLong_Type;
  46. }
  47. PyObject* ConstructPyUIntFromPyLong(PyLongObject* obj) {
  48. PyObject *result;
  49. Py_ssize_t index;
  50. Py_ssize_t size;
  51. assert(PyLong_Check(obj));
  52. if (IsNegative(obj)) {
  53. SetNegativeValueError((PyObject*)obj, &PyUnsignedLong_Type);
  54. return nullptr;
  55. }
  56. size = GetSize(obj);
  57. result = PyLong_Type.tp_alloc(&PyUnsignedLong_Type, size);
  58. if (Y_UNLIKELY(!result)) {
  59. return nullptr;
  60. }
  61. assert(IsExactPyUInt(result));
  62. Py_SET_SIZE(result, size);
  63. #if PY_VERSION_HEX >= 0x030c0000
  64. ((PyLongObject*)result)->long_value.lv_tag = obj->long_value.lv_tag;
  65. #endif
  66. for (index = 0; index < size; ++index) {
  67. #if PY_VERSION_HEX < 0x030c0000
  68. ((PyLongObject*)result)->ob_digit[index] = obj->ob_digit[index];
  69. #else
  70. ((PyLongObject*)result)->long_value.ob_digit[index] = obj->long_value.ob_digit[index];
  71. #endif
  72. }
  73. return result;
  74. }
  75. PyObject* ConstructPyUIntFromUint(uint64_t n) {
  76. PyObject* result;
  77. PyObject* tmp;
  78. tmp = PyLong_FromUnsignedLong(n);
  79. if (Y_UNLIKELY(!tmp)) {
  80. return nullptr;
  81. }
  82. result = ConstructPyUIntFromPyLong((PyLongObject*)tmp);
  83. Py_DECREF(tmp);
  84. return result;
  85. }
  86. static PyObject* unsigned_long_new(PyTypeObject *type, PyObject *args, PyObject* kws) {
  87. PyObject* result;
  88. result = PyLong_Type.tp_new(type, args, kws);
  89. if (Y_UNLIKELY(!result)) {
  90. return nullptr;
  91. }
  92. assert(IsExactPyUInt(result));
  93. if (IsNegative(result)) {
  94. SetNegativeValueError(result, type);
  95. Py_DECREF(result);
  96. return nullptr;
  97. }
  98. return result;
  99. }
  100. static PyObject* unsigned_long_repr(PyObject* self) {
  101. PyObject* result;
  102. PyObject* callable = PyUnsignedLong_ReprPtr.GetBorrowed();
  103. if (callable) {
  104. result = PyObject_CallFunctionObjArgs(callable, self, nullptr);
  105. } else {
  106. result = PyObject_Repr(self);
  107. }
  108. return result;
  109. }
  110. #define PYOBJECT_ARG(o) PyObject* o,
  111. #define PYOBJECT_ARG_LAST(o) PyObject* o
  112. #define UNSIGNED_LONG_OPERATION(SLOT, ...) \
  113. static PyObject* unsigned_long_##SLOT(Y_MAP_ARGS_WITH_LAST(PYOBJECT_ARG, PYOBJECT_ARG_LAST, __VA_ARGS__)) { \
  114. PyObject* result = PyLong_Type.tp_as_number->nb_##SLOT(__VA_ARGS__); \
  115. if (result && PyLong_CheckExact(result) && (IsPositive(result))) { \
  116. PyObject* tmp = result; \
  117. result = ConstructPyUIntFromPyLong((PyLongObject*)tmp); \
  118. Py_DECREF(tmp); \
  119. } \
  120. return result; \
  121. }
  122. UNSIGNED_LONG_OPERATION(add, x, y);
  123. UNSIGNED_LONG_OPERATION(subtract, x, y);
  124. UNSIGNED_LONG_OPERATION(multiply, x, y);
  125. #if PY_MAJOR_VERSION < 3
  126. UNSIGNED_LONG_OPERATION(divide, x, y);
  127. #endif
  128. UNSIGNED_LONG_OPERATION(remainder, x, y);
  129. UNSIGNED_LONG_OPERATION(power, x, y, z);
  130. UNSIGNED_LONG_OPERATION(lshift, x, y);
  131. UNSIGNED_LONG_OPERATION(rshift, x, y);
  132. UNSIGNED_LONG_OPERATION(and, x, y);
  133. UNSIGNED_LONG_OPERATION(xor, x, y);
  134. UNSIGNED_LONG_OPERATION(or, x, y);
  135. UNSIGNED_LONG_OPERATION(floor_divide, x, y);
  136. UNSIGNED_LONG_OPERATION(true_divide, x, y);
  137. #undef UNSIGNED_LONG_OPERATION
  138. #undef PYOBJECT_ARG_LAST
  139. #undef PYOBJECT_ARG
  140. static PyNumberMethods unsigned_long_as_number = {
  141. unsigned_long_add, /*nb_add*/
  142. unsigned_long_subtract, /*nb_subtract*/
  143. unsigned_long_multiply, /*nb_multiply*/
  144. #if PY_MAJOR_VERSION < 3
  145. unsigned_long_divide, /*nb_divide*/
  146. #endif
  147. unsigned_long_remainder, /*nb_remainder*/
  148. 0, /*nb_divmod*/
  149. unsigned_long_power, /*nb_power*/
  150. 0, /*nb_negative*/
  151. GetSelf, /*nb_positive*/
  152. GetSelf, /*nb_absolute*/
  153. 0, /*nb_nonzero*/
  154. 0, /*nb_invert*/
  155. unsigned_long_lshift, /*nb_lshift*/
  156. unsigned_long_rshift, /*nb_rshift*/
  157. unsigned_long_and, /*nb_and*/
  158. unsigned_long_xor, /*nb_xor*/
  159. unsigned_long_or, /*nb_or*/
  160. #if PY_MAJOR_VERSION < 3
  161. 0, /*nb_coerce*/
  162. #endif
  163. 0, /*nb_int*/
  164. #if PY_MAJOR_VERSION < 3
  165. 0, /*nb_long*/
  166. #else
  167. 0, /*reserved*/
  168. #endif
  169. 0, /*nb_float*/
  170. #if PY_MAJOR_VERSION < 3
  171. 0, /*nb_oct*/
  172. 0, /*nb_hex*/
  173. #endif
  174. 0, /*nb_inplace_add*/
  175. 0, /*nb_inplace_subtract*/
  176. 0, /*nb_inplace_multiply*/
  177. #if PY_MAJOR_VERSION < 3
  178. 0, /*nb_inplace_divide*/
  179. #endif
  180. 0, /*nb_inplace_remainder*/
  181. 0, /*nb_inplace_power*/
  182. 0, /*nb_inplace_lshift*/
  183. 0, /*nb_inplace_rshift*/
  184. 0, /*nb_inplace_and*/
  185. 0, /*nb_inplace_xor*/
  186. 0, /*nb_inplace_or*/
  187. unsigned_long_floor_divide, /*nb_floor_divide*/
  188. unsigned_long_true_divide, /*nb_true_divide*/
  189. 0, /*nb_inplace_floor_divide*/
  190. 0, /*nb_inplace_true_divide*/
  191. 0, /*nb_index*/
  192. #if PY_VERSION_HEX >= 0x03050000
  193. 0, /*nb_matrix_multiply*/
  194. 0, /*nb_inplace_matrix_multiply*/
  195. #endif
  196. };
  197. PyTypeObject PyUnsignedLong_Type = {
  198. PyVarObject_HEAD_INIT(0, 0)
  199. "cyson._cyson.UInt", /*tp_name*/
  200. PyLong_Type.tp_basicsize, /*tp_basicsize*/
  201. PyLong_Type.tp_itemsize, /*tp_itemsize*/
  202. PyLong_Type.tp_dealloc, /*tp_dealloc*/
  203. #if PY_VERSION_HEX < 0x030800b4
  204. 0, /*tp_print*/
  205. #endif
  206. #if PY_VERSION_HEX >= 0x030800b4
  207. 0, /*tp_vectorcall_offset*/
  208. #endif
  209. 0, /*tp_getattr*/
  210. 0, /*tp_setattr*/
  211. #if PY_MAJOR_VERSION < 3
  212. 0, /*tp_compare*/
  213. #endif
  214. #if PY_MAJOR_VERSION >= 3
  215. 0, /*tp_as_async*/
  216. #endif
  217. unsigned_long_repr, /*tp_repr*/
  218. &unsigned_long_as_number, /*tp_as_number*/
  219. 0, /*tp_as_sequence*/
  220. 0, /*tp_as_mapping*/
  221. 0, /*tp_hash*/
  222. 0, /*tp_call*/
  223. 0, /*tp_str*/
  224. 0, /*tp_getattro*/
  225. 0, /*tp_setattro*/
  226. 0, /*tp_as_buffer*/
  227. Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_LONG_SUBCLASS, /*tp_flags*/
  228. "UInt(0) -> UInt\nUInt(x, base=10) -> UInt", /*tp_doc*/
  229. 0, /*tp_traverse*/
  230. 0, /*tp_clear*/
  231. 0, /*tp_richcompare*/
  232. 0, /*tp_weaklistoffset*/
  233. 0, /*tp_iter*/
  234. 0, /*tp_iternext*/
  235. 0, /*tp_methods*/
  236. 0, /*tp_members*/
  237. 0, /*tp_getset*/
  238. &PyLong_Type, /*tp_base*/
  239. 0, /*tp_dict*/
  240. 0, /*tp_descr_get*/
  241. 0, /*tp_descr_set*/
  242. 0, /*tp_dictoffset*/
  243. 0, /*tp_init*/
  244. 0, /*tp_alloc*/
  245. unsigned_long_new, /*tp_new*/
  246. 0, /*tp_free*/
  247. 0, /*tp_is_gc*/
  248. 0, /*tp_bases*/
  249. 0, /*tp_mro*/
  250. 0, /*tp_cache*/
  251. 0, /*tp_subclasses*/
  252. 0, /*tp_weaklist*/
  253. 0, /*tp_del*/
  254. 0, /*tp_version_tag*/
  255. #if PY_VERSION_HEX >= 0x030400a1
  256. 0, /*tp_finalize*/
  257. #endif
  258. #if PY_VERSION_HEX >= 0x030800b1
  259. 0, /*tp_vectorcall*/
  260. #endif
  261. #if PY_VERSION_HEX >= 0x030800b4 && PY_VERSION_HEX < 0x03090000
  262. 0, /*tp_print*/
  263. #endif
  264. #if PY_VERSION_HEX >= 0x030c0000
  265. 0, /*tp_watched*/
  266. #endif
  267. };
  268. }