Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_LONGINTREPR_H |
| 2 | #define Py_LONGINTREPR_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 3d09543 | 1992-01-19 16:30:12 +0000 | [diff] [blame] | 8 | /* This is published for the benefit of "friend" marshal.c only. */ |
| 9 | |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 10 | /* Parameters of the long integer representation. |
| 11 | These shouldn't have to be changed as C should guarantee that a short |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 12 | contains at least 16 bits, but it's made changeable anyway. |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 13 | 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 | |
| 20 | typedef unsigned short digit; |
Guido van Rossum | badb116 | 1991-05-14 12:06:16 +0000 | [diff] [blame] | 21 | typedef unsigned int wdigit; /* digit widened to parameter size */ |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 22 | #define BASE_TWODIGITS_TYPE long |
| 23 | typedef unsigned BASE_TWODIGITS_TYPE twodigits; |
| 24 | typedef BASE_TWODIGITS_TYPE stwodigits; /* signed variant of twodigits */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 25 | |
| 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 Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 38 | The allocation function takes care of allocating extra memory |
Tim Peters | 03b1883 | 2002-03-02 04:33:09 +0000 | [diff] [blame] | 39 | so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available. |
| 40 | |
| 41 | CAUTION: Generic code manipulating subtypes of PyVarObject has to |
| 42 | aware that longs abuse ob_size's sign bit. |
| 43 | */ |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 3d09543 | 1992-01-19 16:30:12 +0000 | [diff] [blame] | 45 | struct _longobject { |
Tim Peters | 03b1883 | 2002-03-02 04:33:09 +0000 | [diff] [blame] | 46 | PyObject_VAR_HEAD |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 47 | digit ob_digit[1]; |
Guido van Rossum | 3d09543 | 1992-01-19 16:30:12 +0000 | [diff] [blame] | 48 | }; |
Guido van Rossum | edcc38a | 1991-05-05 20:09:44 +0000 | [diff] [blame] | 49 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 50 | PyAPI_FUNC(PyLongObject *) _PyLong_New(int); |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 51 | |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 52 | /* Return a copy of src. */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 53 | PyAPI_FUNC(PyObject *) _PyLong_Copy(PyLongObject *src); |
Tim Peters | 64b5ce3 | 2001-09-10 20:52:51 +0000 | [diff] [blame] | 54 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 55 | #ifdef __cplusplus |
| 56 | } |
| 57 | #endif |
| 58 | #endif /* !Py_LONGINTREPR_H */ |