Skip to content

Commit 0782499

Browse files
authored
gh-113706: Update comment about long int representation (#113707)
1 parent 015b97d commit 0782499

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

Include/cpython/longintrepr.h

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,32 @@ typedef long stwodigits; /* signed variant of twodigits */
6262
#define PyLong_MASK ((digit)(PyLong_BASE - 1))
6363

6464
/* Long integer representation.
65+
66+
Long integers are made up of a number of 30- or 15-bit digits, depending on
67+
the platform. The number of digits (ndigits) is stored in the high bits of
68+
the lv_tag field (lvtag >> _PyLong_NON_SIZE_BITS).
69+
6570
The absolute value of a number is equal to
66-
SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i)
67-
Negative numbers are represented with ob_size < 0;
68-
zero is represented by ob_size == 0.
69-
In a normalized number, ob_digit[abs(ob_size)-1] (the most significant
71+
SUM(for i=0 through ndigits-1) ob_digit[i] * 2**(PyLong_SHIFT*i)
72+
73+
The sign of the value is stored in the lower 2 bits of lv_tag.
74+
75+
- 0: Positive
76+
- 1: Zero
77+
- 2: Negative
78+
79+
The third lowest bit of lv_tag is reserved for an immortality flag, but is
80+
not currently used.
81+
82+
In a normalized number, ob_digit[ndigits-1] (the most significant
7083
digit) is never zero. Also, in all cases, for all valid i,
71-
0 <= ob_digit[i] <= MASK.
84+
0 <= ob_digit[i] <= PyLong_MASK.
85+
7286
The allocation function takes care of allocating extra memory
73-
so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available.
87+
so that ob_digit[0] ... ob_digit[ndigits-1] are actually available.
7488
We always allocate memory for at least one digit, so accessing ob_digit[0]
75-
is always safe. However, in the case ob_size == 0, the contents of
89+
is always safe. However, in the case ndigits == 0, the contents of
7690
ob_digit[0] may be undefined.
77-
78-
CAUTION: Generic code manipulating subtypes of PyVarObject has to
79-
aware that ints abuse ob_size's sign bit.
8091
*/
8192

8293
typedef struct _PyLongValue {

0 commit comments

Comments
 (0)