longintrepr.h 2.13 KB
Newer Older
1 2 3 4 5 6
#ifndef Py_LONGINTREPR_H
#define Py_LONGINTREPR_H
#ifdef __cplusplus
extern "C" {
#endif

Guido van Rossum's avatar
Guido van Rossum committed
7

8 9
/* This is published for the benefit of "friend" marshal.c only. */

Guido van Rossum's avatar
Guido van Rossum committed
10 11
/* Parameters of the long integer representation.
   These shouldn't have to be changed as C should guarantee that a short
12
   contains at least 16 bits, but it's made changeable anyway.
Guido van Rossum's avatar
Guido van Rossum committed
13 14
   Note: 'digit' should be able to hold 2*MASK+1, and 'twodigits'
   should be able to hold the intermediate results in 'mul'
15
   (at most (BASE-1)*(2*BASE+1) == MASK*(2*MASK+3)).
Guido van Rossum's avatar
Guido van Rossum committed
16 17
   Also, x_sub assumes that 'digit' is an unsigned type, and overflow
   is handled by taking the result mod 2**N for some N > SHIFT.
18 19
   And, at some places it is assumed that MASK fits in an int, as well.
   long_pow() requires that SHIFT be divisible by 5. */
Guido van Rossum's avatar
Guido van Rossum committed
20 21

typedef unsigned short digit;
22
typedef unsigned int wdigit; /* digit widened to parameter size */
23 24 25
#define BASE_TWODIGITS_TYPE long
typedef unsigned BASE_TWODIGITS_TYPE twodigits;
typedef BASE_TWODIGITS_TYPE stwodigits; /* signed variant of twodigits */
Guido van Rossum's avatar
Guido van Rossum committed
26

27 28 29
#define PyLong_SHIFT	15
#define PyLong_BASE	((digit)1 << PyLong_SHIFT)
#define PyLong_MASK	((int)(PyLong_BASE - 1))
Guido van Rossum's avatar
Guido van Rossum committed
30

31
#if PyLong_SHIFT % 5 != 0
32 33 34
#error "longobject.c requires that SHIFT be divisible by 5"
#endif

Guido van Rossum's avatar
Guido van Rossum committed
35 36 37 38 39 40 41 42
/* Long integer representation.
   The absolute value of a number is equal to
   	SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i)
   Negative numbers are represented with ob_size < 0;
   zero is represented by ob_size == 0.
   In a normalized number, ob_digit[abs(ob_size)-1] (the most significant
   digit) is never zero.  Also, in all cases, for all valid i,
   	0 <= ob_digit[i] <= MASK.
43
   The allocation function takes care of allocating extra memory
44 45 46 47 48
   so that ob_digit[0] ... ob_digit[abs(ob_size)-1] are actually available.

   CAUTION:  Generic code manipulating subtypes of PyVarObject has to
   aware that longs abuse  ob_size's sign bit.
*/
Guido van Rossum's avatar
Guido van Rossum committed
49

50
struct _longobject {
51
	PyObject_VAR_HEAD
Guido van Rossum's avatar
Guido van Rossum committed
52
	digit ob_digit[1];
53
};
Guido van Rossum's avatar
Guido van Rossum committed
54

Martin v. Löwis's avatar
Martin v. Löwis committed
55
PyAPI_FUNC(PyLongObject *) _PyLong_New(Py_ssize_t);
56

57
/* Return a copy of src. */
58
PyAPI_FUNC(PyObject *) _PyLong_Copy(PyLongObject *src);
59

60 61 62 63
#ifdef __cplusplus
}
#endif
#endif /* !Py_LONGINTREPR_H */