• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef Py_INTERNAL_LONG_H
2 #define Py_INTERNAL_LONG_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #ifndef Py_BUILD_CORE
8 #  error "this header requires Py_BUILD_CORE define"
9 #endif
10 
11 #include "pycore_bytesobject.h"   // _PyBytesWriter
12 #include "pycore_global_objects.h"// _PY_NSMALLNEGINTS
13 #include "pycore_runtime.h"       // _PyRuntime
14 
15 /*
16  * Default int base conversion size limitation: Denial of Service prevention.
17  *
18  * Chosen such that this isn't wildly slow on modern hardware and so that
19  * everyone's existing deployed numpy test suite passes before
20  * https://github.com/numpy/numpy/issues/22098 is widely available.
21  *
22  * $ python -m timeit -s 's = "1"*4300' 'int(s)'
23  * 2000 loops, best of 5: 125 usec per loop
24  * $ python -m timeit -s 's = "1"*4300; v = int(s)' 'str(v)'
25  * 1000 loops, best of 5: 311 usec per loop
26  * (zen2 cloud VM)
27  *
28  * 4300 decimal digits fits a ~14284 bit number.
29  */
30 #define _PY_LONG_DEFAULT_MAX_STR_DIGITS 4300
31 /*
32  * Threshold for max digits check.  For performance reasons int() and
33  * int.__str__() don't checks values that are smaller than this
34  * threshold.  Acts as a guaranteed minimum size limit for bignums that
35  * applications can expect from CPython.
36  *
37  * % python -m timeit -s 's = "1"*640; v = int(s)' 'str(int(s))'
38  * 20000 loops, best of 5: 12 usec per loop
39  *
40  * "640 digits should be enough for anyone." - gps
41  * fits a ~2126 bit decimal number.
42  */
43 #define _PY_LONG_MAX_STR_DIGITS_THRESHOLD 640
44 
45 #if ((_PY_LONG_DEFAULT_MAX_STR_DIGITS != 0) && \
46    (_PY_LONG_DEFAULT_MAX_STR_DIGITS < _PY_LONG_MAX_STR_DIGITS_THRESHOLD))
47 # error "_PY_LONG_DEFAULT_MAX_STR_DIGITS smaller than threshold."
48 #endif
49 
50 /* runtime lifecycle */
51 
52 extern PyStatus _PyLong_InitTypes(PyInterpreterState *);
53 extern void _PyLong_FiniTypes(PyInterpreterState *interp);
54 
55 
56 /* other API */
57 
58 #define _PyLong_SMALL_INTS _Py_SINGLETON(small_ints)
59 
60 // _PyLong_GetZero() and _PyLong_GetOne() must always be available
61 // _PyLong_FromUnsignedChar must always be available
62 #if _PY_NSMALLPOSINTS < 257
63 #  error "_PY_NSMALLPOSINTS must be greater than or equal to 257"
64 #endif
65 
66 // Return a reference to the immortal zero singleton.
67 // The function cannot return NULL.
_PyLong_GetZero(void)68 static inline PyObject* _PyLong_GetZero(void)
69 { return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS]; }
70 
71 // Return a reference to the immortal one singleton.
72 // The function cannot return NULL.
_PyLong_GetOne(void)73 static inline PyObject* _PyLong_GetOne(void)
74 { return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS+1]; }
75 
_PyLong_FromUnsignedChar(unsigned char i)76 static inline PyObject* _PyLong_FromUnsignedChar(unsigned char i)
77 {
78     return (PyObject *)&_PyLong_SMALL_INTS[_PY_NSMALLNEGINTS+i];
79 }
80 
81 // _PyLong_Frexp returns a double x and an exponent e such that the
82 // true value is approximately equal to x * 2**e.  e is >= 0.  x is
83 // 0.0 if and only if the input is 0 (in which case, e and x are both
84 // zeroes); otherwise, 0.5 <= abs(x) < 1.0.  On overflow, which is
85 // possible if the number of bits doesn't fit into a Py_ssize_t, sets
86 // OverflowError and returns -1.0 for x, 0 for e.
87 //
88 // Export for 'math' shared extension
89 PyAPI_DATA(double) _PyLong_Frexp(PyLongObject *a, Py_ssize_t *e);
90 
91 extern PyObject* _PyLong_FromBytes(const char *, Py_ssize_t, int);
92 
93 // _PyLong_DivmodNear.  Given integers a and b, compute the nearest
94 // integer q to the exact quotient a / b, rounding to the nearest even integer
95 // in the case of a tie.  Return (q, r), where r = a - q*b.  The remainder r
96 // will satisfy abs(r) <= abs(b)/2, with equality possible only if q is
97 // even.
98 //
99 // Export for '_datetime' shared extension.
100 PyAPI_DATA(PyObject*) _PyLong_DivmodNear(PyObject *, PyObject *);
101 
102 // _PyLong_Format: Convert the long to a string object with given base,
103 // appending a base prefix of 0[box] if base is 2, 8 or 16.
104 // Export for '_tkinter' shared extension.
105 PyAPI_DATA(PyObject*) _PyLong_Format(PyObject *obj, int base);
106 
107 // Export for 'math' shared extension
108 PyAPI_DATA(PyObject*) _PyLong_Rshift(PyObject *, size_t);
109 
110 // Export for 'math' shared extension
111 PyAPI_DATA(PyObject*) _PyLong_Lshift(PyObject *, size_t);
112 
113 PyAPI_FUNC(PyObject*) _PyLong_Add(PyLongObject *left, PyLongObject *right);
114 PyAPI_FUNC(PyObject*) _PyLong_Multiply(PyLongObject *left, PyLongObject *right);
115 PyAPI_FUNC(PyObject*) _PyLong_Subtract(PyLongObject *left, PyLongObject *right);
116 
117 // Export for 'binascii' shared extension.
118 PyAPI_DATA(unsigned char) _PyLong_DigitValue[256];
119 
120 /* Format the object based on the format_spec, as defined in PEP 3101
121    (Advanced String Formatting). */
122 extern int _PyLong_FormatAdvancedWriter(
123     _PyUnicodeWriter *writer,
124     PyObject *obj,
125     PyObject *format_spec,
126     Py_ssize_t start,
127     Py_ssize_t end);
128 
129 extern int _PyLong_FormatWriter(
130     _PyUnicodeWriter *writer,
131     PyObject *obj,
132     int base,
133     int alternate);
134 
135 extern char* _PyLong_FormatBytesWriter(
136     _PyBytesWriter *writer,
137     char *str,
138     PyObject *obj,
139     int base,
140     int alternate);
141 
142 // Argument converters used by Argument Clinic
143 
144 // Export for 'select' shared extension (Argument Clinic code)
145 PyAPI_FUNC(int) _PyLong_UnsignedShort_Converter(PyObject *, void *);
146 
147 // Export for '_testclinic' shared extension (Argument Clinic code)
148 PyAPI_FUNC(int) _PyLong_UnsignedInt_Converter(PyObject *, void *);
149 
150 // Export for '_blake2' shared extension (Argument Clinic code)
151 PyAPI_FUNC(int) _PyLong_UnsignedLong_Converter(PyObject *, void *);
152 
153 // Export for '_blake2' shared extension (Argument Clinic code)
154 PyAPI_FUNC(int) _PyLong_UnsignedLongLong_Converter(PyObject *, void *);
155 
156 // Export for '_testclinic' shared extension (Argument Clinic code)
157 PyAPI_FUNC(int) _PyLong_Size_t_Converter(PyObject *, void *);
158 
159 /* Long value tag bits:
160  * 0-1: Sign bits value = (1-sign), ie. negative=2, positive=0, zero=1.
161  * 2: Reserved for immortality bit
162  * 3+ Unsigned digit count
163  */
164 #define SIGN_MASK 3
165 #define SIGN_ZERO 1
166 #define SIGN_NEGATIVE 2
167 #define NON_SIZE_BITS 3
168 
169 /* The functions _PyLong_IsCompact and _PyLong_CompactValue are defined
170  * in Include/cpython/longobject.h, since they need to be inline.
171  *
172  * "Compact" values have at least one bit to spare,
173  * so that addition and subtraction can be performed on the values
174  * without risk of overflow.
175  *
176  * The inline functions need tag bits.
177  * For readability, rather than do `#define SIGN_MASK _PyLong_SIGN_MASK`
178  * we define them to the numbers in both places and then assert that
179  * they're the same.
180  */
181 #if SIGN_MASK != _PyLong_SIGN_MASK
182 #  error "SIGN_MASK does not match _PyLong_SIGN_MASK"
183 #endif
184 #if NON_SIZE_BITS != _PyLong_NON_SIZE_BITS
185 #  error "NON_SIZE_BITS does not match _PyLong_NON_SIZE_BITS"
186 #endif
187 
188 /* All *compact" values are guaranteed to fit into
189  * a Py_ssize_t with at least one bit to spare.
190  * In other words, for 64 bit machines, compact
191  * will be signed 63 (or fewer) bit values
192  */
193 
194 /* Return 1 if the argument is compact int */
195 static inline int
_PyLong_IsNonNegativeCompact(const PyLongObject * op)196 _PyLong_IsNonNegativeCompact(const PyLongObject* op) {
197     assert(PyLong_Check(op));
198     return op->long_value.lv_tag <= (1 << NON_SIZE_BITS);
199 }
200 
201 
202 static inline int
_PyLong_BothAreCompact(const PyLongObject * a,const PyLongObject * b)203 _PyLong_BothAreCompact(const PyLongObject* a, const PyLongObject* b) {
204     assert(PyLong_Check(a));
205     assert(PyLong_Check(b));
206     return (a->long_value.lv_tag | b->long_value.lv_tag) < (2 << NON_SIZE_BITS);
207 }
208 
209 static inline bool
_PyLong_IsZero(const PyLongObject * op)210 _PyLong_IsZero(const PyLongObject *op)
211 {
212     return (op->long_value.lv_tag & SIGN_MASK) == SIGN_ZERO;
213 }
214 
215 static inline bool
_PyLong_IsNegative(const PyLongObject * op)216 _PyLong_IsNegative(const PyLongObject *op)
217 {
218     return (op->long_value.lv_tag & SIGN_MASK) == SIGN_NEGATIVE;
219 }
220 
221 static inline bool
_PyLong_IsPositive(const PyLongObject * op)222 _PyLong_IsPositive(const PyLongObject *op)
223 {
224     return (op->long_value.lv_tag & SIGN_MASK) == 0;
225 }
226 
227 static inline Py_ssize_t
_PyLong_DigitCount(const PyLongObject * op)228 _PyLong_DigitCount(const PyLongObject *op)
229 {
230     assert(PyLong_Check(op));
231     return op->long_value.lv_tag >> NON_SIZE_BITS;
232 }
233 
234 /* Equivalent to _PyLong_DigitCount(op) * _PyLong_NonCompactSign(op) */
235 static inline Py_ssize_t
_PyLong_SignedDigitCount(const PyLongObject * op)236 _PyLong_SignedDigitCount(const PyLongObject *op)
237 {
238     assert(PyLong_Check(op));
239     Py_ssize_t sign = 1 - (op->long_value.lv_tag & SIGN_MASK);
240     return sign * (Py_ssize_t)(op->long_value.lv_tag >> NON_SIZE_BITS);
241 }
242 
243 static inline int
_PyLong_CompactSign(const PyLongObject * op)244 _PyLong_CompactSign(const PyLongObject *op)
245 {
246     assert(PyLong_Check(op));
247     assert(_PyLong_IsCompact(op));
248     return 1 - (op->long_value.lv_tag & SIGN_MASK);
249 }
250 
251 static inline int
_PyLong_NonCompactSign(const PyLongObject * op)252 _PyLong_NonCompactSign(const PyLongObject *op)
253 {
254     assert(PyLong_Check(op));
255     assert(!_PyLong_IsCompact(op));
256     return 1 - (op->long_value.lv_tag & SIGN_MASK);
257 }
258 
259 /* Do a and b have the same sign? */
260 static inline int
_PyLong_SameSign(const PyLongObject * a,const PyLongObject * b)261 _PyLong_SameSign(const PyLongObject *a, const PyLongObject *b)
262 {
263     return (a->long_value.lv_tag & SIGN_MASK) == (b->long_value.lv_tag & SIGN_MASK);
264 }
265 
266 #define TAG_FROM_SIGN_AND_SIZE(sign, size) ((1 - (sign)) | ((size) << NON_SIZE_BITS))
267 
268 static inline void
_PyLong_SetSignAndDigitCount(PyLongObject * op,int sign,Py_ssize_t size)269 _PyLong_SetSignAndDigitCount(PyLongObject *op, int sign, Py_ssize_t size)
270 {
271     assert(size >= 0);
272     assert(-1 <= sign && sign <= 1);
273     assert(sign != 0 || size == 0);
274     op->long_value.lv_tag = TAG_FROM_SIGN_AND_SIZE(sign, (size_t)size);
275 }
276 
277 static inline void
_PyLong_SetDigitCount(PyLongObject * op,Py_ssize_t size)278 _PyLong_SetDigitCount(PyLongObject *op, Py_ssize_t size)
279 {
280     assert(size >= 0);
281     op->long_value.lv_tag = (((size_t)size) << NON_SIZE_BITS) | (op->long_value.lv_tag & SIGN_MASK);
282 }
283 
284 #define NON_SIZE_MASK ~((1 << NON_SIZE_BITS) - 1)
285 
286 static inline void
_PyLong_FlipSign(PyLongObject * op)287 _PyLong_FlipSign(PyLongObject *op) {
288     unsigned int flipped_sign = 2 - (op->long_value.lv_tag & SIGN_MASK);
289     op->long_value.lv_tag &= NON_SIZE_MASK;
290     op->long_value.lv_tag |= flipped_sign;
291 }
292 
293 #define _PyLong_DIGIT_INIT(val) \
294     { \
295         .ob_base = _PyObject_HEAD_INIT(&PyLong_Type), \
296         .long_value  = { \
297             .lv_tag = TAG_FROM_SIGN_AND_SIZE( \
298                 (val) == 0 ? 0 : ((val) < 0 ? -1 : 1), \
299                 (val) == 0 ? 0 : 1), \
300             { ((val) >= 0 ? (val) : -(val)) }, \
301         } \
302     }
303 
304 #define _PyLong_FALSE_TAG TAG_FROM_SIGN_AND_SIZE(0, 0)
305 #define _PyLong_TRUE_TAG TAG_FROM_SIGN_AND_SIZE(1, 1)
306 
307 #ifdef __cplusplus
308 }
309 #endif
310 #endif /* !Py_INTERNAL_LONG_H */
311