|
|
|||
File indexing completed on 2025-11-19 09:50:41
0001 #ifndef Py_LIMITED_API 0002 #ifndef Py_LONGINTREPR_H 0003 #define Py_LONGINTREPR_H 0004 #ifdef __cplusplus 0005 extern "C" { 0006 #endif 0007 0008 0009 /* This is published for the benefit of "friends" marshal.c and _decimal.c. */ 0010 0011 /* Parameters of the integer representation. There are two different 0012 sets of parameters: one set for 30-bit digits, stored in an unsigned 32-bit 0013 integer type, and one set for 15-bit digits with each digit stored in an 0014 unsigned short. The value of PYLONG_BITS_IN_DIGIT, defined either at 0015 configure time or in pyport.h, is used to decide which digit size to use. 0016 0017 Type 'digit' should be able to hold 2*PyLong_BASE-1, and type 'twodigits' 0018 should be an unsigned integer type able to hold all integers up to 0019 PyLong_BASE*PyLong_BASE-1. x_sub assumes that 'digit' is an unsigned type, 0020 and that overflow is handled by taking the result modulo 2**N for some N > 0021 PyLong_SHIFT. The majority of the code doesn't care about the precise 0022 value of PyLong_SHIFT, but there are some notable exceptions: 0023 0024 - PyLong_{As,From}ByteArray require that PyLong_SHIFT be at least 8 0025 0026 - long_hash() requires that PyLong_SHIFT is *strictly* less than the number 0027 of bits in an unsigned long, as do the PyLong <-> long (or unsigned long) 0028 conversion functions 0029 0030 - the Python int <-> size_t/Py_ssize_t conversion functions expect that 0031 PyLong_SHIFT is strictly less than the number of bits in a size_t 0032 0033 - the marshal code currently expects that PyLong_SHIFT is a multiple of 15 0034 0035 - NSMALLNEGINTS and NSMALLPOSINTS should be small enough to fit in a single 0036 digit; with the current values this forces PyLong_SHIFT >= 9 0037 0038 The values 15 and 30 should fit all of the above requirements, on any 0039 platform. 0040 */ 0041 0042 #if PYLONG_BITS_IN_DIGIT == 30 0043 typedef uint32_t digit; 0044 typedef int32_t sdigit; /* signed variant of digit */ 0045 typedef uint64_t twodigits; 0046 typedef int64_t stwodigits; /* signed variant of twodigits */ 0047 #define PyLong_SHIFT 30 0048 #define _PyLong_DECIMAL_SHIFT 9 /* max(e such that 10**e fits in a digit) */ 0049 #define _PyLong_DECIMAL_BASE ((digit)1000000000) /* 10 ** DECIMAL_SHIFT */ 0050 #elif PYLONG_BITS_IN_DIGIT == 15 0051 typedef unsigned short digit; 0052 typedef short sdigit; /* signed variant of digit */ 0053 typedef unsigned long twodigits; 0054 typedef long stwodigits; /* signed variant of twodigits */ 0055 #define PyLong_SHIFT 15 0056 #define _PyLong_DECIMAL_SHIFT 4 /* max(e such that 10**e fits in a digit) */ 0057 #define _PyLong_DECIMAL_BASE ((digit)10000) /* 10 ** DECIMAL_SHIFT */ 0058 #else 0059 #error "PYLONG_BITS_IN_DIGIT should be 15 or 30" 0060 #endif 0061 #define PyLong_BASE ((digit)1 << PyLong_SHIFT) 0062 #define PyLong_MASK ((digit)(PyLong_BASE - 1)) 0063 0064 /* Long integer representation. 0065 0066 Long integers are made up of a number of 30- or 15-bit digits, depending on 0067 the platform. The number of digits (ndigits) is stored in the high bits of 0068 the lv_tag field (lvtag >> _PyLong_NON_SIZE_BITS). 0069 0070 The absolute value of a number is equal to 0071 SUM(for i=0 through ndigits-1) ob_digit[i] * 2**(PyLong_SHIFT*i) 0072 0073 The sign of the value is stored in the lower 2 bits of lv_tag. 0074 0075 - 0: Positive 0076 - 1: Zero 0077 - 2: Negative 0078 0079 The third lowest bit of lv_tag is reserved for an immortality flag, but is 0080 not currently used. 0081 0082 In a normalized number, ob_digit[ndigits-1] (the most significant 0083 digit) is never zero. Also, in all cases, for all valid i, 0084 0 <= ob_digit[i] <= PyLong_MASK. 0085 0086 The allocation function takes care of allocating extra memory 0087 so that ob_digit[0] ... ob_digit[ndigits-1] are actually available. 0088 We always allocate memory for at least one digit, so accessing ob_digit[0] 0089 is always safe. However, in the case ndigits == 0, the contents of 0090 ob_digit[0] may be undefined. 0091 */ 0092 0093 typedef struct _PyLongValue { 0094 uintptr_t lv_tag; /* Number of digits, sign and flags */ 0095 digit ob_digit[1]; 0096 } _PyLongValue; 0097 0098 struct _longobject { 0099 PyObject_HEAD 0100 _PyLongValue long_value; 0101 }; 0102 0103 PyAPI_FUNC(PyLongObject*) _PyLong_New(Py_ssize_t); 0104 0105 // Return a copy of src. 0106 PyAPI_FUNC(PyObject*) _PyLong_Copy(PyLongObject *src); 0107 0108 PyAPI_FUNC(PyLongObject*) _PyLong_FromDigits( 0109 int negative, 0110 Py_ssize_t digit_count, 0111 digit *digits); 0112 0113 0114 /* Inline some internals for speed. These should be in pycore_long.h 0115 * if user code didn't need them inlined. */ 0116 0117 #define _PyLong_SIGN_MASK 3 0118 #define _PyLong_NON_SIZE_BITS 3 0119 0120 0121 static inline int 0122 _PyLong_IsCompact(const PyLongObject* op) { 0123 assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS)); 0124 return op->long_value.lv_tag < (2 << _PyLong_NON_SIZE_BITS); 0125 } 0126 0127 #define PyUnstable_Long_IsCompact _PyLong_IsCompact 0128 0129 static inline Py_ssize_t 0130 _PyLong_CompactValue(const PyLongObject *op) 0131 { 0132 Py_ssize_t sign; 0133 assert(PyType_HasFeature((op)->ob_base.ob_type, Py_TPFLAGS_LONG_SUBCLASS)); 0134 assert(PyUnstable_Long_IsCompact(op)); 0135 sign = 1 - (op->long_value.lv_tag & _PyLong_SIGN_MASK); 0136 return sign * (Py_ssize_t)op->long_value.ob_digit[0]; 0137 } 0138 0139 #define PyUnstable_Long_CompactValue _PyLong_CompactValue 0140 0141 0142 #ifdef __cplusplus 0143 } 0144 #endif 0145 #endif /* !Py_LONGINTREPR_H */ 0146 #endif /* Py_LIMITED_API */
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|