pycore_long.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #ifndef Py_INTERNAL_LONG_H
  2. #define Py_INTERNAL_LONG_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #ifndef Py_BUILD_CORE
  7. # error "this header requires Py_BUILD_CORE define"
  8. #endif
  9. #include "pycore_global_objects.h" // _PY_NSMALLNEGINTS
  10. #include "pycore_runtime.h" // _PyRuntime
  11. /*
  12. * Default int base conversion size limitation: Denial of Service prevention.
  13. *
  14. * Chosen such that this isn't wildly slow on modern hardware and so that
  15. * everyone's existing deployed numpy test suite passes before
  16. * https://github.com/numpy/numpy/issues/22098 is widely available.
  17. *
  18. * $ python -m timeit -s 's = "1"*4300' 'int(s)'
  19. * 2000 loops, best of 5: 125 usec per loop
  20. * $ python -m timeit -s 's = "1"*4300; v = int(s)' 'str(v)'
  21. * 1000 loops, best of 5: 311 usec per loop
  22. * (zen2 cloud VM)
  23. *
  24. * 4300 decimal digits fits a ~14284 bit number.
  25. */
  26. #define _PY_LONG_DEFAULT_MAX_STR_DIGITS 4300
  27. /*
  28. * Threshold for max digits check. For performance reasons int() and
  29. * int.__str__() don't checks values that are smaller than this
  30. * threshold. Acts as a guaranteed minimum size limit for bignums that
  31. * applications can expect from CPython.
  32. *
  33. * % python -m timeit -s 's = "1"*640; v = int(s)' 'str(int(s))'
  34. * 20000 loops, best of 5: 12 usec per loop
  35. *
  36. * "640 digits should be enough for anyone." - gps
  37. * fits a ~2126 bit decimal number.
  38. */
  39. #define _PY_LONG_MAX_STR_DIGITS_THRESHOLD 640
  40. #if ((_PY_LONG_DEFAULT_MAX_STR_DIGITS != 0) && \
  41. (_PY_LONG_DEFAULT_MAX_STR_DIGITS < _PY_LONG_MAX_STR_DIGITS_THRESHOLD))
  42. # error "_PY_LONG_DEFAULT_MAX_STR_DIGITS smaller than threshold."
  43. #endif
  44. /* runtime lifecycle */
  45. extern PyStatus _PyLong_InitTypes(PyInterpreterState *);
  46. extern void _PyLong_FiniTypes(PyInterpreterState *interp);
  47. /* other API */
  48. #define _PyLong_SMALL_INTS _Py_SINGLETON(small_ints)
  49. // _PyLong_GetZero() and _PyLong_GetOne() must always be available
  50. // _PyLong_FromUnsignedChar must always be available
  51. #if _PY_NSMALLPOSINTS < 257
  52. # error "_PY_NSMALLPOSINTS must be greater than or equal to 257"
  53. #endif
  54. // Return a borrowed reference to the zero singleton.
  55. // The function cannot return NULL.
  56. static inline PyObject* _PyLong_GetZero(void)
  57. { return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS]; }
  58. // Return a borrowed reference to the one singleton.
  59. // The function cannot return NULL.
  60. static inline PyObject* _PyLong_GetOne(void)
  61. { return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS+1]; }
  62. static inline PyObject* _PyLong_FromUnsignedChar(unsigned char i)
  63. {
  64. return Py_NewRef((PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS+i]);
  65. }
  66. PyObject *_PyLong_Add(PyLongObject *left, PyLongObject *right);
  67. PyObject *_PyLong_Multiply(PyLongObject *left, PyLongObject *right);
  68. PyObject *_PyLong_Subtract(PyLongObject *left, PyLongObject *right);
  69. /* Used by Python/mystrtoul.c, _PyBytes_FromHex(),
  70. _PyBytes_DecodeEscape(), etc. */
  71. PyAPI_DATA(unsigned char) _PyLong_DigitValue[256];
  72. /* Format the object based on the format_spec, as defined in PEP 3101
  73. (Advanced String Formatting). */
  74. PyAPI_FUNC(int) _PyLong_FormatAdvancedWriter(
  75. _PyUnicodeWriter *writer,
  76. PyObject *obj,
  77. PyObject *format_spec,
  78. Py_ssize_t start,
  79. Py_ssize_t end);
  80. PyAPI_FUNC(int) _PyLong_FormatWriter(
  81. _PyUnicodeWriter *writer,
  82. PyObject *obj,
  83. int base,
  84. int alternate);
  85. PyAPI_FUNC(char*) _PyLong_FormatBytesWriter(
  86. _PyBytesWriter *writer,
  87. char *str,
  88. PyObject *obj,
  89. int base,
  90. int alternate);
  91. /* Long value tag bits:
  92. * 0-1: Sign bits value = (1-sign), ie. negative=2, positive=0, zero=1.
  93. * 2: Reserved for immortality bit
  94. * 3+ Unsigned digit count
  95. */
  96. #define SIGN_MASK 3
  97. #define SIGN_ZERO 1
  98. #define SIGN_NEGATIVE 2
  99. #define NON_SIZE_BITS 3
  100. /* The functions _PyLong_IsCompact and _PyLong_CompactValue are defined
  101. * in Include/cpython/longobject.h, since they need to be inline.
  102. *
  103. * "Compact" values have at least one bit to spare,
  104. * so that addition and subtraction can be performed on the values
  105. * without risk of overflow.
  106. *
  107. * The inline functions need tag bits.
  108. * For readability, rather than do `#define SIGN_MASK _PyLong_SIGN_MASK`
  109. * we define them to the numbers in both places and then assert that
  110. * they're the same.
  111. */
  112. static_assert(SIGN_MASK == _PyLong_SIGN_MASK, "SIGN_MASK does not match _PyLong_SIGN_MASK");
  113. static_assert(NON_SIZE_BITS == _PyLong_NON_SIZE_BITS, "NON_SIZE_BITS does not match _PyLong_NON_SIZE_BITS");
  114. /* All *compact" values are guaranteed to fit into
  115. * a Py_ssize_t with at least one bit to spare.
  116. * In other words, for 64 bit machines, compact
  117. * will be signed 63 (or fewer) bit values
  118. */
  119. /* Return 1 if the argument is compact int */
  120. static inline int
  121. _PyLong_IsNonNegativeCompact(const PyLongObject* op) {
  122. assert(PyLong_Check(op));
  123. return op->long_value.lv_tag <= (1 << NON_SIZE_BITS);
  124. }
  125. static inline int
  126. _PyLong_BothAreCompact(const PyLongObject* a, const PyLongObject* b) {
  127. assert(PyLong_Check(a));
  128. assert(PyLong_Check(b));
  129. return (a->long_value.lv_tag | b->long_value.lv_tag) < (2 << NON_SIZE_BITS);
  130. }
  131. static inline bool
  132. _PyLong_IsZero(const PyLongObject *op)
  133. {
  134. return (op->long_value.lv_tag & SIGN_MASK) == SIGN_ZERO;
  135. }
  136. static inline bool
  137. _PyLong_IsNegative(const PyLongObject *op)
  138. {
  139. return (op->long_value.lv_tag & SIGN_MASK) == SIGN_NEGATIVE;
  140. }
  141. static inline bool
  142. _PyLong_IsPositive(const PyLongObject *op)
  143. {
  144. return (op->long_value.lv_tag & SIGN_MASK) == 0;
  145. }
  146. static inline Py_ssize_t
  147. _PyLong_DigitCount(const PyLongObject *op)
  148. {
  149. assert(PyLong_Check(op));
  150. return op->long_value.lv_tag >> NON_SIZE_BITS;
  151. }
  152. /* Equivalent to _PyLong_DigitCount(op) * _PyLong_NonCompactSign(op) */
  153. static inline Py_ssize_t
  154. _PyLong_SignedDigitCount(const PyLongObject *op)
  155. {
  156. assert(PyLong_Check(op));
  157. Py_ssize_t sign = 1 - (op->long_value.lv_tag & SIGN_MASK);
  158. return sign * (Py_ssize_t)(op->long_value.lv_tag >> NON_SIZE_BITS);
  159. }
  160. static inline int
  161. _PyLong_CompactSign(const PyLongObject *op)
  162. {
  163. assert(PyLong_Check(op));
  164. assert(_PyLong_IsCompact(op));
  165. return 1 - (op->long_value.lv_tag & SIGN_MASK);
  166. }
  167. static inline int
  168. _PyLong_NonCompactSign(const PyLongObject *op)
  169. {
  170. assert(PyLong_Check(op));
  171. assert(!_PyLong_IsCompact(op));
  172. return 1 - (op->long_value.lv_tag & SIGN_MASK);
  173. }
  174. /* Do a and b have the same sign? */
  175. static inline int
  176. _PyLong_SameSign(const PyLongObject *a, const PyLongObject *b)
  177. {
  178. return (a->long_value.lv_tag & SIGN_MASK) == (b->long_value.lv_tag & SIGN_MASK);
  179. }
  180. #define TAG_FROM_SIGN_AND_SIZE(sign, size) ((1 - (sign)) | ((size) << NON_SIZE_BITS))
  181. static inline void
  182. _PyLong_SetSignAndDigitCount(PyLongObject *op, int sign, Py_ssize_t size)
  183. {
  184. assert(size >= 0);
  185. assert(-1 <= sign && sign <= 1);
  186. assert(sign != 0 || size == 0);
  187. op->long_value.lv_tag = TAG_FROM_SIGN_AND_SIZE(sign, (size_t)size);
  188. }
  189. static inline void
  190. _PyLong_SetDigitCount(PyLongObject *op, Py_ssize_t size)
  191. {
  192. assert(size >= 0);
  193. op->long_value.lv_tag = (((size_t)size) << NON_SIZE_BITS) | (op->long_value.lv_tag & SIGN_MASK);
  194. }
  195. #define NON_SIZE_MASK ~((1 << NON_SIZE_BITS) - 1)
  196. static inline void
  197. _PyLong_FlipSign(PyLongObject *op) {
  198. unsigned int flipped_sign = 2 - (op->long_value.lv_tag & SIGN_MASK);
  199. op->long_value.lv_tag &= NON_SIZE_MASK;
  200. op->long_value.lv_tag |= flipped_sign;
  201. }
  202. #define _PyLong_DIGIT_INIT(val) \
  203. { \
  204. .ob_base = _PyObject_HEAD_INIT(&PyLong_Type) \
  205. .long_value = { \
  206. .lv_tag = TAG_FROM_SIGN_AND_SIZE( \
  207. (val) == 0 ? 0 : ((val) < 0 ? -1 : 1), \
  208. (val) == 0 ? 0 : 1), \
  209. { ((val) >= 0 ? (val) : -(val)) }, \
  210. } \
  211. }
  212. #define _PyLong_FALSE_TAG TAG_FROM_SIGN_AND_SIZE(0, 0)
  213. #define _PyLong_TRUE_TAG TAG_FROM_SIGN_AND_SIZE(1, 1)
  214. #ifdef __cplusplus
  215. }
  216. #endif
  217. #endif /* !Py_INTERNAL_LONG_H */