blob: 89ba5867e06f4a4ec3c6f2f0413ac30cd61eff19 [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_LONGINTREPR_H
2#define Py_LONGINTREPR_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Guido van Rossumedcc38a1991-05-05 20:09:44 +00007
Guido van Rossum3d095431992-01-19 16:30:12 +00008/* This is published for the benefit of "friend" marshal.c only. */
9
Guido van Rossumedcc38a1991-05-05 20:09:44 +000010/* Parameters of the long integer representation.
11 These shouldn't have to be changed as C should guarantee that a short
Tim Peters7d3a5112000-07-08 04:17:21 +000012 contains at least 16 bits, but it's made changeable anyway.
Guido van Rossumedcc38a1991-05-05 20:09:44 +000013 Note: 'digit' should be able to hold 2*MASK+1, and 'twodigits'
14 should be able to hold the intermediate results in 'mul'
15 (at most MASK << SHIFT).
16 Also, x_sub assumes that 'digit' is an unsigned type, and overflow
17 is handled by taking the result mod 2**N for some N > SHIFT.
18 And, at some places it is assumed that MASK fits in an int, as well. */
19
20typedef unsigned short digit;
Guido van Rossumbadb1161991-05-14 12:06:16 +000021typedef unsigned int wdigit; /* digit widened to parameter size */
Tim Peters7d3a5112000-07-08 04:17:21 +000022#define BASE_TWODIGITS_TYPE long
23typedef unsigned BASE_TWODIGITS_TYPE twodigits;
24typedef BASE_TWODIGITS_TYPE stwodigits; /* signed variant of twodigits */
Guido van Rossumedcc38a1991-05-05 20:09:44 +000025
26#define SHIFT 15
27#define BASE ((digit)1 << SHIFT)
28#define MASK ((int)(BASE - 1))
29
30/* Long integer representation.
31 The absolute value of a number is equal to
32 SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i)
33 Negative numbers are represented with ob_size < 0;
34 zero is represented by ob_size == 0.
35 In a normalized number, ob_digit[abs(ob_size)-1] (the most significant
36 digit) is never zero. Also, in all cases, for all valid i,
37 0 <= ob_digit[i] <= MASK.
Thomas Wouters7e474022000-07-16 12:04:32 +000038 The allocation function takes care of allocating extra memory
Guido van Rossumedcc38a1991-05-05 20:09:44 +000039 so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available. */
40
Guido van Rossum3d095431992-01-19 16:30:12 +000041struct _longobject {
Guido van Rossumcaa63801995-01-12 11:45:45 +000042 PyObject_HEAD
Guido van Rossumb7d3d4e1995-02-10 16:55:33 +000043 int ob_size;
Guido van Rossumedcc38a1991-05-05 20:09:44 +000044 digit ob_digit[1];
Guido van Rossum3d095431992-01-19 16:30:12 +000045};
Guido van Rossumedcc38a1991-05-05 20:09:44 +000046
Tim Peters9f688bf2000-07-07 15:53:28 +000047DL_IMPORT(PyLongObject *) _PyLong_New(int);
Guido van Rossuma3309961993-07-28 09:05:47 +000048
Tim Peters64b5ce32001-09-10 20:52:51 +000049/* Return a copy of src. */
50DL_IMPORT(PyObject *) _PyLong_Copy(PyLongObject *src);
51
Guido van Rossuma3309961993-07-28 09:05:47 +000052#ifdef __cplusplus
53}
54#endif
55#endif /* !Py_LONGINTREPR_H */