17db96d56Sopenharmony_ci#ifndef Py_LIMITED_API
27db96d56Sopenharmony_ci#ifndef Py_LONGINTREPR_H
37db96d56Sopenharmony_ci#define Py_LONGINTREPR_H
47db96d56Sopenharmony_ci#ifdef __cplusplus
57db96d56Sopenharmony_ciextern "C" {
67db96d56Sopenharmony_ci#endif
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ci/* This is published for the benefit of "friends" marshal.c and _decimal.c. */
107db96d56Sopenharmony_ci
117db96d56Sopenharmony_ci/* Parameters of the integer representation.  There are two different
127db96d56Sopenharmony_ci   sets of parameters: one set for 30-bit digits, stored in an unsigned 32-bit
137db96d56Sopenharmony_ci   integer type, and one set for 15-bit digits with each digit stored in an
147db96d56Sopenharmony_ci   unsigned short.  The value of PYLONG_BITS_IN_DIGIT, defined either at
157db96d56Sopenharmony_ci   configure time or in pyport.h, is used to decide which digit size to use.
167db96d56Sopenharmony_ci
177db96d56Sopenharmony_ci   Type 'digit' should be able to hold 2*PyLong_BASE-1, and type 'twodigits'
187db96d56Sopenharmony_ci   should be an unsigned integer type able to hold all integers up to
197db96d56Sopenharmony_ci   PyLong_BASE*PyLong_BASE-1.  x_sub assumes that 'digit' is an unsigned type,
207db96d56Sopenharmony_ci   and that overflow is handled by taking the result modulo 2**N for some N >
217db96d56Sopenharmony_ci   PyLong_SHIFT.  The majority of the code doesn't care about the precise
227db96d56Sopenharmony_ci   value of PyLong_SHIFT, but there are some notable exceptions:
237db96d56Sopenharmony_ci
247db96d56Sopenharmony_ci   - PyLong_{As,From}ByteArray require that PyLong_SHIFT be at least 8
257db96d56Sopenharmony_ci
267db96d56Sopenharmony_ci   - long_hash() requires that PyLong_SHIFT is *strictly* less than the number
277db96d56Sopenharmony_ci     of bits in an unsigned long, as do the PyLong <-> long (or unsigned long)
287db96d56Sopenharmony_ci     conversion functions
297db96d56Sopenharmony_ci
307db96d56Sopenharmony_ci   - the Python int <-> size_t/Py_ssize_t conversion functions expect that
317db96d56Sopenharmony_ci     PyLong_SHIFT is strictly less than the number of bits in a size_t
327db96d56Sopenharmony_ci
337db96d56Sopenharmony_ci   - the marshal code currently expects that PyLong_SHIFT is a multiple of 15
347db96d56Sopenharmony_ci
357db96d56Sopenharmony_ci   - NSMALLNEGINTS and NSMALLPOSINTS should be small enough to fit in a single
367db96d56Sopenharmony_ci     digit; with the current values this forces PyLong_SHIFT >= 9
377db96d56Sopenharmony_ci
387db96d56Sopenharmony_ci  The values 15 and 30 should fit all of the above requirements, on any
397db96d56Sopenharmony_ci  platform.
407db96d56Sopenharmony_ci*/
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci#if PYLONG_BITS_IN_DIGIT == 30
437db96d56Sopenharmony_citypedef uint32_t digit;
447db96d56Sopenharmony_citypedef int32_t sdigit; /* signed variant of digit */
457db96d56Sopenharmony_citypedef uint64_t twodigits;
467db96d56Sopenharmony_citypedef int64_t stwodigits; /* signed variant of twodigits */
477db96d56Sopenharmony_ci#define PyLong_SHIFT    30
487db96d56Sopenharmony_ci#define _PyLong_DECIMAL_SHIFT   9 /* max(e such that 10**e fits in a digit) */
497db96d56Sopenharmony_ci#define _PyLong_DECIMAL_BASE    ((digit)1000000000) /* 10 ** DECIMAL_SHIFT */
507db96d56Sopenharmony_ci#elif PYLONG_BITS_IN_DIGIT == 15
517db96d56Sopenharmony_citypedef unsigned short digit;
527db96d56Sopenharmony_citypedef short sdigit; /* signed variant of digit */
537db96d56Sopenharmony_citypedef unsigned long twodigits;
547db96d56Sopenharmony_citypedef long stwodigits; /* signed variant of twodigits */
557db96d56Sopenharmony_ci#define PyLong_SHIFT    15
567db96d56Sopenharmony_ci#define _PyLong_DECIMAL_SHIFT   4 /* max(e such that 10**e fits in a digit) */
577db96d56Sopenharmony_ci#define _PyLong_DECIMAL_BASE    ((digit)10000) /* 10 ** DECIMAL_SHIFT */
587db96d56Sopenharmony_ci#else
597db96d56Sopenharmony_ci#error "PYLONG_BITS_IN_DIGIT should be 15 or 30"
607db96d56Sopenharmony_ci#endif
617db96d56Sopenharmony_ci#define PyLong_BASE     ((digit)1 << PyLong_SHIFT)
627db96d56Sopenharmony_ci#define PyLong_MASK     ((digit)(PyLong_BASE - 1))
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ci/* Long integer representation.
657db96d56Sopenharmony_ci   The absolute value of a number is equal to
667db96d56Sopenharmony_ci        SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i)
677db96d56Sopenharmony_ci   Negative numbers are represented with ob_size < 0;
687db96d56Sopenharmony_ci   zero is represented by ob_size == 0.
697db96d56Sopenharmony_ci   In a normalized number, ob_digit[abs(ob_size)-1] (the most significant
707db96d56Sopenharmony_ci   digit) is never zero.  Also, in all cases, for all valid i,
717db96d56Sopenharmony_ci        0 <= ob_digit[i] <= MASK.
727db96d56Sopenharmony_ci   The allocation function takes care of allocating extra memory
737db96d56Sopenharmony_ci   so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available.
747db96d56Sopenharmony_ci   We always allocate memory for at least one digit, so accessing ob_digit[0]
757db96d56Sopenharmony_ci   is always safe. However, in the case ob_size == 0, the contents of
767db96d56Sopenharmony_ci   ob_digit[0] may be undefined.
777db96d56Sopenharmony_ci
787db96d56Sopenharmony_ci   CAUTION:  Generic code manipulating subtypes of PyVarObject has to
797db96d56Sopenharmony_ci   aware that ints abuse  ob_size's sign bit.
807db96d56Sopenharmony_ci*/
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_cistruct _longobject {
837db96d56Sopenharmony_ci    PyObject_VAR_HEAD
847db96d56Sopenharmony_ci    digit ob_digit[1];
857db96d56Sopenharmony_ci};
867db96d56Sopenharmony_ci
877db96d56Sopenharmony_ciPyAPI_FUNC(PyLongObject *) _PyLong_New(Py_ssize_t);
887db96d56Sopenharmony_ci
897db96d56Sopenharmony_ci/* Return a copy of src. */
907db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) _PyLong_Copy(PyLongObject *src);
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ci#ifdef __cplusplus
937db96d56Sopenharmony_ci}
947db96d56Sopenharmony_ci#endif
957db96d56Sopenharmony_ci#endif /* !Py_LONGINTREPR_H */
967db96d56Sopenharmony_ci#endif /* Py_LIMITED_API */
97