blob: df157a8f9846b71b125db838dce52eba7bc2b25d [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'
Tim Peters0973b992004-08-29 22:16:50 +000015 (at most (BASE-1)*(2*BASE+1) == MASK*(2*MASK+3)).
Guido van Rossumedcc38a1991-05-05 20:09:44 +000016 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.
Tim Peters47e52ee2004-08-30 02:44:38 +000018 And, at some places it is assumed that MASK fits in an int, as well.
19 long_pow() requires that SHIFT be divisible by 5. */
Guido van Rossumedcc38a1991-05-05 20:09:44 +000020
21typedef unsigned short digit;
Guido van Rossumbadb1161991-05-14 12:06:16 +000022typedef unsigned int wdigit; /* digit widened to parameter size */
Tim Peters7d3a5112000-07-08 04:17:21 +000023#define BASE_TWODIGITS_TYPE long
24typedef unsigned BASE_TWODIGITS_TYPE twodigits;
25typedef BASE_TWODIGITS_TYPE stwodigits; /* signed variant of twodigits */
Guido van Rossumedcc38a1991-05-05 20:09:44 +000026
Christian Heimes7f39c9f2008-01-25 12:18:43 +000027#define PyLong_SHIFT 15
28#define PyLong_BASE ((digit)1 << PyLong_SHIFT)
29#define PyLong_MASK ((int)(PyLong_BASE - 1))
Guido van Rossumedcc38a1991-05-05 20:09:44 +000030
Christian Heimesa3c8c102008-02-25 17:32:07 +000031/* b/w compatibility with Python 2.5 */
32#define SHIFT PyLong_SHIFT
33#define BASE PyLong_BASE
34#define MASK PyLong_MASK
35
Christian Heimes7f39c9f2008-01-25 12:18:43 +000036#if PyLong_SHIFT % 5 != 0
Christian Heimesa3c8c102008-02-25 17:32:07 +000037#error "longobject.c requires that PyLong_SHIFT be divisible by 5"
Tim Peters47e52ee2004-08-30 02:44:38 +000038#endif
39
Guido van Rossumedcc38a1991-05-05 20:09:44 +000040/* Long integer representation.
41 The absolute value of a number is equal to
42 SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i)
43 Negative numbers are represented with ob_size < 0;
44 zero is represented by ob_size == 0.
45 In a normalized number, ob_digit[abs(ob_size)-1] (the most significant
46 digit) is never zero. Also, in all cases, for all valid i,
47 0 <= ob_digit[i] <= MASK.
Thomas Wouters7e474022000-07-16 12:04:32 +000048 The allocation function takes care of allocating extra memory
Tim Peters03b18832002-03-02 04:33:09 +000049 so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available.
50
51 CAUTION: Generic code manipulating subtypes of PyVarObject has to
52 aware that longs abuse ob_size's sign bit.
53*/
Guido van Rossumedcc38a1991-05-05 20:09:44 +000054
Guido van Rossum3d095431992-01-19 16:30:12 +000055struct _longobject {
Tim Peters03b18832002-03-02 04:33:09 +000056 PyObject_VAR_HEAD
Guido van Rossumedcc38a1991-05-05 20:09:44 +000057 digit ob_digit[1];
Guido van Rossum3d095431992-01-19 16:30:12 +000058};
Guido van Rossumedcc38a1991-05-05 20:09:44 +000059
Martin v. Löwis18e16552006-02-15 17:27:45 +000060PyAPI_FUNC(PyLongObject *) _PyLong_New(Py_ssize_t);
Guido van Rossuma3309961993-07-28 09:05:47 +000061
Tim Peters64b5ce32001-09-10 20:52:51 +000062/* Return a copy of src. */
Mark Hammond91a681d2002-08-12 07:21:58 +000063PyAPI_FUNC(PyObject *) _PyLong_Copy(PyLongObject *src);
Tim Peters64b5ce32001-09-10 20:52:51 +000064
Guido van Rossuma3309961993-07-28 09:05:47 +000065#ifdef __cplusplus
66}
67#endif
68#endif /* !Py_LONGINTREPR_H */