1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Long (arbitrary precision) integer object implementation */
13 /* XXX The functional organization of this file is terrible */
16 #include "longintrepr.h"
21 #define ABS(x) ((x) < 0 ? -(x) : (x))
24 static PyLongObject
*long_normalize(PyLongObject
*);
25 static PyLongObject
*mul1(PyLongObject
*, wdigit
);
26 static PyLongObject
*muladd1(PyLongObject
*, wdigit
, wdigit
);
27 static PyLongObject
*divrem1(PyLongObject
*, wdigit
, digit
*);
28 static PyObject
*long_format(PyObject
*aa
, int base
, int addL
);
30 static int ticker
; /* XXX Could be shared with ceval? */
32 #define SIGCHECK(PyTryBlock) \
35 if (PyErr_CheckSignals()) { PyTryBlock; } \
38 /* Normalize (remove leading zeros from) a long int object.
39 Doesn't attempt to free the storage--in most cases, due to the nature
40 of the algorithms used, this could save at most be one word anyway. */
43 long_normalize(register PyLongObject
*v
)
45 int j
= ABS(v
->ob_size
);
48 while (i
> 0 && v
->ob_digit
[i
-1] == 0)
51 v
->ob_size
= (v
->ob_size
< 0) ? -(i
) : i
;
55 /* Allocate a new long int object with size digits.
56 Return NULL and set exception if we run out of memory. */
61 return PyObject_NEW_VAR(PyLongObject
, &PyLong_Type
, size
);
64 /* Create a new long int object from a C long int */
67 PyLong_FromLong(long ival
)
69 /* Assume a C long fits in at most 5 'digits' */
70 /* Works on both 32- and 64-bit machines */
71 PyLongObject
*v
= _PyLong_New(5);
73 unsigned long t
= ival
;
77 v
->ob_size
= -(v
->ob_size
);
79 for (i
= 0; i
< 5; i
++) {
80 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
83 v
= long_normalize(v
);
88 /* Create a new long int object from a C unsigned long int */
91 PyLong_FromUnsignedLong(unsigned long ival
)
93 /* Assume a C long fits in at most 5 'digits' */
94 /* Works on both 32- and 64-bit machines */
95 PyLongObject
*v
= _PyLong_New(5);
97 unsigned long t
= ival
;
99 for (i
= 0; i
< 5; i
++) {
100 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
103 v
= long_normalize(v
);
105 return (PyObject
*)v
;
108 /* Create a new long int object from a C double */
111 PyLong_FromDouble(double dval
)
115 int i
, ndig
, expo
, neg
;
117 if (Py_IS_INFINITY(dval
)) {
118 PyErr_SetString(PyExc_OverflowError
,
119 "cannot convert float infinity to long");
126 frac
= frexp(dval
, &expo
); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
128 return PyLong_FromLong(0L);
129 ndig
= (expo
-1) / SHIFT
+ 1; /* Number of 'digits' in result */
130 v
= _PyLong_New(ndig
);
133 frac
= ldexp(frac
, (expo
-1) % SHIFT
+ 1);
134 for (i
= ndig
; --i
>= 0; ) {
135 long bits
= (long)frac
;
136 v
->ob_digit
[i
] = (digit
) bits
;
137 frac
= frac
- (double)bits
;
138 frac
= ldexp(frac
, SHIFT
);
141 v
->ob_size
= -(v
->ob_size
);
142 return (PyObject
*)v
;
145 /* Get a C long int from a long int object.
146 Returns -1 and sets an error condition if overflow occurs. */
149 PyLong_AsLong(PyObject
*vv
)
151 /* This version by Tim Peters */
152 register PyLongObject
*v
;
153 unsigned long x
, prev
;
156 if (vv
== NULL
|| !PyLong_Check(vv
)) {
157 PyErr_BadInternalCall();
160 v
= (PyLongObject
*)vv
;
170 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
171 if ((x
>> SHIFT
) != prev
)
174 /* Haven't lost any bits, but if the sign bit is set we're in
175 * trouble *unless* this is the min negative number. So,
176 * trouble iff sign bit set && (positive || some bit set other
177 * than the sign bit).
179 if ((long)x
< 0 && (sign
> 0 || (x
<< 1) != 0))
181 return (long)x
* sign
;
184 PyErr_SetString(PyExc_OverflowError
,
185 "long int too long to convert");
189 /* Get a C long int from a long int object.
190 Returns -1 and sets an error condition if overflow occurs. */
193 PyLong_AsUnsignedLong(PyObject
*vv
)
195 register PyLongObject
*v
;
196 unsigned long x
, prev
;
199 if (vv
== NULL
|| !PyLong_Check(vv
)) {
200 PyErr_BadInternalCall();
201 return (unsigned long) -1;
203 v
= (PyLongObject
*)vv
;
207 PyErr_SetString(PyExc_OverflowError
,
208 "can't convert negative value to unsigned long");
209 return (unsigned long) -1;
213 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
214 if ((x
>> SHIFT
) != prev
) {
215 PyErr_SetString(PyExc_OverflowError
,
216 "long int too long to convert");
217 return (unsigned long) -1;
223 /* Get a C double from a long int object. */
226 PyLong_AsDouble(PyObject
*vv
)
228 register PyLongObject
*v
;
230 double multiplier
= (double) (1L << SHIFT
);
233 if (vv
== NULL
|| !PyLong_Check(vv
)) {
234 PyErr_BadInternalCall();
237 v
= (PyLongObject
*)vv
;
246 x
= x
*multiplier
+ (double)v
->ob_digit
[i
];
251 /* Create a new long (or int) object from a C pointer */
254 PyLong_FromVoidPtr(void *p
)
256 #if SIZEOF_VOID_P == SIZEOF_LONG
257 return PyInt_FromLong((long)p
);
259 /* optimize null pointers */
261 return PyInt_FromLong(0);
263 /* we can assume that HAVE_LONG_LONG is true. if not, then the
264 configuration process should have bailed (having big pointers
265 without long longs seems non-sensical) */
266 return PyLong_FromLongLong((LONG_LONG
)p
);
267 #endif /* SIZEOF_VOID_P == SIZEOF_LONG */
270 /* Get a C pointer from a long object (or an int object in some cases) */
273 PyLong_AsVoidPtr(PyObject
*vv
)
275 /* This function will allow int or long objects. If vv is neither,
276 then the PyLong_AsLong*() functions will raise the exception:
277 PyExc_SystemError, "bad argument to internal function"
280 #if SIZEOF_VOID_P == SIZEOF_LONG
283 if ( PyInt_Check(vv
) )
284 x
= PyInt_AS_LONG(vv
);
286 x
= PyLong_AsLong(vv
);
288 /* we can assume that HAVE_LONG_LONG is true. if not, then the
289 configuration process should have bailed (having big pointers
290 without long longs seems non-sensical) */
293 if ( PyInt_Check(vv
) )
294 x
= PyInt_AS_LONG(vv
);
296 x
= PyLong_AsLongLong(vv
);
297 #endif /* SIZEOF_VOID_P == SIZEOF_LONG */
299 if (x
== -1 && PyErr_Occurred())
304 #ifdef HAVE_LONG_LONG
306 * LONG_LONG support by Chris Herborth (chrish@qnx.com)
308 * For better or worse :-), I tried to follow the coding style already
312 /* Create a new long int object from a C LONG_LONG int */
315 PyLong_FromLongLong(LONG_LONG ival
)
317 #if SIZEOF_LONG_LONG == SIZEOF_LONG
318 /* In case the compiler is faking it. */
319 return PyLong_FromLong( (long)ival
);
321 if ((LONG_LONG
)LONG_MIN
<= ival
&& ival
<= (LONG_LONG
)LONG_MAX
) {
322 return PyLong_FromLong( (long)ival
);
324 else if (0 <= ival
&& ival
<= (unsigned LONG_LONG
)ULONG_MAX
) {
325 return PyLong_FromUnsignedLong( (unsigned long)ival
);
328 /* Assume a C LONG_LONG fits in at most 10 'digits'.
329 * Should be OK if we're assuming long fits in 5.
331 PyLongObject
*v
= _PyLong_New(10);
334 unsigned LONG_LONG t
= ival
;
338 v
->ob_size
= -(v
->ob_size
);
341 for (i
= 0; i
< 10; i
++) {
342 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
346 v
= long_normalize(v
);
349 return (PyObject
*)v
;
354 /* Create a new long int object from a C unsigned LONG_LONG int */
356 PyLong_FromUnsignedLongLong(unsigned LONG_LONG ival
)
358 #if SIZEOF_LONG_LONG == SIZEOF_LONG
359 /* In case the compiler is faking it. */
360 return PyLong_FromUnsignedLong( (unsigned long)ival
);
362 if( ival
<= (unsigned LONG_LONG
)ULONG_MAX
) {
363 return PyLong_FromUnsignedLong( (unsigned long)ival
);
366 /* Assume a C long fits in at most 10 'digits'. */
367 PyLongObject
*v
= _PyLong_New(10);
370 unsigned LONG_LONG t
= ival
;
372 for (i
= 0; i
< 10; i
++) {
373 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
377 v
= long_normalize(v
);
380 return (PyObject
*)v
;
385 /* Get a C LONG_LONG int from a long int object.
386 Returns -1 and sets an error condition if overflow occurs. */
389 PyLong_AsLongLong(PyObject
*vv
)
391 #if SIZEOF_LONG_LONG == SIZEOF_LONG
392 /* In case the compiler is faking it. */
393 return (LONG_LONG
)PyLong_AsLong( vv
);
395 register PyLongObject
*v
;
399 if (vv
== NULL
|| !PyLong_Check(vv
)) {
400 PyErr_BadInternalCall();
404 v
= (PyLongObject
*)vv
;
416 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
417 if ((x
>> SHIFT
) != prev
) {
418 PyErr_SetString(PyExc_OverflowError
,
419 "long int too long to convert");
429 PyLong_AsUnsignedLongLong(PyObject
*vv
)
431 #if SIZEOF_LONG_LONG == 4
432 /* In case the compiler is faking it. */
433 return (unsigned LONG_LONG
)PyLong_AsUnsignedLong( vv
);
435 register PyLongObject
*v
;
436 unsigned LONG_LONG x
, prev
;
439 if (vv
== NULL
|| !PyLong_Check(vv
)) {
440 PyErr_BadInternalCall();
441 return (unsigned LONG_LONG
) -1;
444 v
= (PyLongObject
*)vv
;
449 PyErr_SetString(PyExc_OverflowError
,
450 "can't convert negative value to unsigned long");
451 return (unsigned LONG_LONG
) -1;
456 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
457 if ((x
>> SHIFT
) != prev
) {
458 PyErr_SetString(PyExc_OverflowError
,
459 "long int too long to convert");
460 return (unsigned LONG_LONG
) -1;
467 #endif /* HAVE_LONG_LONG */
469 /* Multiply by a single digit, ignoring the sign. */
471 static PyLongObject
*
472 mul1(PyLongObject
*a
, wdigit n
)
474 return muladd1(a
, n
, (digit
)0);
477 /* Multiply by a single digit and add a single digit, ignoring the sign. */
479 static PyLongObject
*
480 muladd1(PyLongObject
*a
, wdigit n
, wdigit extra
)
482 int size_a
= ABS(a
->ob_size
);
483 PyLongObject
*z
= _PyLong_New(size_a
+1);
484 twodigits carry
= extra
;
489 for (i
= 0; i
< size_a
; ++i
) {
490 carry
+= (twodigits
)a
->ob_digit
[i
] * n
;
491 z
->ob_digit
[i
] = (digit
) (carry
& MASK
);
494 z
->ob_digit
[i
] = (digit
) carry
;
495 return long_normalize(z
);
498 /* Divide a long integer by a digit, returning both the quotient
499 (as function result) and the remainder (through *prem).
500 The sign of a is ignored; n should not be zero. */
502 static PyLongObject
*
503 divrem1(PyLongObject
*a
, wdigit n
, digit
*prem
)
505 int size
= ABS(a
->ob_size
);
510 assert(n
> 0 && n
<= MASK
);
511 z
= _PyLong_New(size
);
514 for (i
= size
; --i
>= 0; ) {
515 rem
= (rem
<< SHIFT
) + a
->ob_digit
[i
];
516 z
->ob_digit
[i
] = (digit
) (rem
/n
);
520 return long_normalize(z
);
523 /* Convert a long int object to a string, using a given conversion base.
524 Return a string object.
525 If base is 8 or 16, add the proper prefix '0' or '0x'. */
528 long_format(PyObject
*aa
, int base
, int addL
)
530 register PyLongObject
*a
= (PyLongObject
*)aa
;
533 int size_a
= ABS(a
->ob_size
);
538 if (a
== NULL
|| !PyLong_Check(a
)) {
539 PyErr_BadInternalCall();
542 assert(base
>= 2 && base
<= 36);
544 /* Compute a rough upper bound for the length of the string */
551 i
= 5 + (addL
? 1 : 0) + (size_a
*SHIFT
+ bits
-1) / bits
;
552 str
= (PyStringObject
*) PyString_FromStringAndSize((char *)0, i
);
555 p
= PyString_AS_STRING(str
) + i
;
562 if (a
->ob_size
== 0) {
565 else if ((base
& (base
- 1)) == 0) {
566 /* JRH: special case for power-of-2 bases */
567 twodigits temp
= a
->ob_digit
[0];
568 int bitsleft
= SHIFT
;
570 int last
= abs(a
->ob_size
);
573 while ((i
>>= 1) > 1)
578 while (bitsleft
>= basebits
) {
579 if ((temp
== 0) && (i
>= last
- 1)) break;
580 rem
= temp
& (base
- 1);
585 assert(p
> PyString_AS_STRING(str
));
587 bitsleft
-= basebits
;
591 if (temp
== 0) break;
593 /* loop again to pick up final digits */
596 temp
= (a
->ob_digit
[i
] << bitsleft
) | temp
;
605 PyLongObject
*temp
= divrem1(a
, (digit
)base
, &rem
);
615 assert(p
> PyString_AS_STRING(str
));
624 } while (ABS(a
->ob_size
) != 0);
632 else if (base
== 16) {
636 else if (base
!= 10) {
638 *--p
= '0' + base
%10;
640 *--p
= '0' + base
/10;
644 if (p
!= PyString_AS_STRING(str
)) {
645 char *q
= PyString_AS_STRING(str
);
648 } while ((*q
++ = *p
++) != '\0');
650 _PyString_Resize((PyObject
**)&str
,
651 (int) (q
- PyString_AS_STRING(str
)));
653 return (PyObject
*)str
;
657 PyLong_FromString(char *str
, char **pend
, int base
)
660 char *start
, *orig_str
= str
;
663 if ((base
!= 0 && base
< 2) || base
> 36) {
664 PyErr_SetString(PyExc_ValueError
,
665 "invalid base for long literal");
668 while (*str
!= '\0' && isspace(Py_CHARMASK(*str
)))
672 else if (*str
== '-') {
676 while (*str
!= '\0' && isspace(Py_CHARMASK(*str
)))
681 else if (str
[1] == 'x' || str
[1] == 'X')
686 if (base
== 16 && str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
690 for ( ; z
!= NULL
; ++str
) {
696 else if (*str
>= 'a')
698 else if (*str
>= 'A')
700 if (k
< 0 || k
>= base
)
702 temp
= muladd1(z
, (digit
)base
, (digit
)k
);
710 if (sign
< 0 && z
!= NULL
&& z
->ob_size
!= 0)
711 z
->ob_size
= -(z
->ob_size
);
712 if (*str
== 'L' || *str
== 'l')
714 while (*str
&& isspace(Py_CHARMASK(*str
)))
720 return (PyObject
*) z
;
723 PyErr_Format(PyExc_ValueError
,
724 "invalid literal for long(): %.200s", orig_str
);
730 PyLong_FromUnicode(Py_UNICODE
*u
, int length
, int base
)
734 if (length
>= sizeof(buffer
)) {
735 PyErr_SetString(PyExc_ValueError
,
736 "long() literal too large to convert");
739 if (PyUnicode_EncodeDecimal(u
, length
, buffer
, NULL
))
742 return PyLong_FromString(buffer
, NULL
, base
);
746 static PyLongObject
*x_divrem
747 (PyLongObject
*, PyLongObject
*, PyLongObject
**);
748 static PyObject
*long_pos(PyLongObject
*);
749 static int long_divrem(PyLongObject
*, PyLongObject
*,
750 PyLongObject
**, PyLongObject
**);
752 /* Long division with remainder, top-level routine */
755 long_divrem(PyLongObject
*a
, PyLongObject
*b
,
756 PyLongObject
**pdiv
, PyLongObject
**prem
)
758 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
762 PyErr_SetString(PyExc_ZeroDivisionError
,
763 "long division or modulo");
766 if (size_a
< size_b
||
768 a
->ob_digit
[size_a
-1] < b
->ob_digit
[size_b
-1])) {
770 *pdiv
= _PyLong_New(0);
772 *prem
= (PyLongObject
*) a
;
777 z
= divrem1(a
, b
->ob_digit
[0], &rem
);
780 *prem
= (PyLongObject
*) PyLong_FromLong((long)rem
);
783 z
= x_divrem(a
, b
, prem
);
788 The quotient z has the sign of a*b;
789 the remainder r has the sign of a,
791 if ((a
->ob_size
< 0) != (b
->ob_size
< 0))
792 z
->ob_size
= -(z
->ob_size
);
793 if (a
->ob_size
< 0 && (*prem
)->ob_size
!= 0)
794 (*prem
)->ob_size
= -((*prem
)->ob_size
);
799 /* Unsigned long division with remainder -- the algorithm */
801 static PyLongObject
*
802 x_divrem(PyLongObject
*v1
, PyLongObject
*w1
, PyLongObject
**prem
)
804 int size_v
= ABS(v1
->ob_size
), size_w
= ABS(w1
->ob_size
);
805 digit d
= (digit
) ((twodigits
)BASE
/ (w1
->ob_digit
[size_w
-1] + 1));
806 PyLongObject
*v
= mul1(v1
, d
);
807 PyLongObject
*w
= mul1(w1
, d
);
811 if (v
== NULL
|| w
== NULL
) {
817 assert(size_v
>= size_w
&& size_w
> 1); /* Assert checks by div() */
818 assert(v
->ob_refcnt
== 1); /* Since v will be used as accumulator! */
819 assert(size_w
== ABS(w
->ob_size
)); /* That's how d was calculated */
821 size_v
= ABS(v
->ob_size
);
822 a
= _PyLong_New(size_v
- size_w
+ 1);
824 for (j
= size_v
, k
= a
->ob_size
-1; a
!= NULL
&& k
>= 0; --j
, --k
) {
825 digit vj
= (j
>= size_v
) ? 0 : v
->ob_digit
[j
];
827 stwodigits carry
= 0;
835 if (vj
== w
->ob_digit
[size_w
-1])
838 q
= (((twodigits
)vj
<< SHIFT
) + v
->ob_digit
[j
-1]) /
839 w
->ob_digit
[size_w
-1];
841 while (w
->ob_digit
[size_w
-2]*q
>
843 ((twodigits
)vj
<< SHIFT
)
845 - q
*w
->ob_digit
[size_w
-1]
850 for (i
= 0; i
< size_w
&& i
+k
< size_v
; ++i
) {
851 twodigits z
= w
->ob_digit
[i
] * q
;
852 digit zz
= (digit
) (z
>> SHIFT
);
853 carry
+= v
->ob_digit
[i
+k
] - z
854 + ((twodigits
)zz
<< SHIFT
);
855 v
->ob_digit
[i
+k
] = carry
& MASK
;
856 carry
= Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE
,
862 carry
+= v
->ob_digit
[i
+k
];
863 v
->ob_digit
[i
+k
] = 0;
867 a
->ob_digit
[k
] = (digit
) q
;
870 a
->ob_digit
[k
] = (digit
) q
-1;
872 for (i
= 0; i
< size_w
&& i
+k
< size_v
; ++i
) {
873 carry
+= v
->ob_digit
[i
+k
] + w
->ob_digit
[i
];
874 v
->ob_digit
[i
+k
] = carry
& MASK
;
875 carry
= Py_ARITHMETIC_RIGHT_SHIFT(
885 a
= long_normalize(a
);
886 *prem
= divrem1(v
, d
, &d
);
887 /* d receives the (unused) remainder */
901 long_dealloc(PyObject
*v
)
907 long_repr(PyObject
*v
)
909 return long_format(v
, 10, 1);
913 long_str(PyObject
*v
)
915 return long_format(v
, 10, 0);
919 long_compare(PyLongObject
*a
, PyLongObject
*b
)
923 if (a
->ob_size
!= b
->ob_size
) {
924 if (ABS(a
->ob_size
) == 0 && ABS(b
->ob_size
) == 0)
927 sign
= a
->ob_size
- b
->ob_size
;
930 int i
= ABS(a
->ob_size
);
931 while (--i
>= 0 && a
->ob_digit
[i
] == b
->ob_digit
[i
])
936 sign
= (int)a
->ob_digit
[i
] - (int)b
->ob_digit
[i
];
941 return sign
< 0 ? -1 : sign
> 0 ? 1 : 0;
945 long_hash(PyLongObject
*v
)
950 /* This is designed so that Python ints and longs with the
951 same value hash to the same value, otherwise comparisons
952 of mapping keys will turn out weird */
961 /* Force a 32-bit circular shift */
962 x
= ((x
<< SHIFT
) & ~MASK
) | ((x
>> (32-SHIFT
)) & MASK
);
972 /* Add the absolute values of two long integers. */
974 static PyLongObject
*
975 x_add(PyLongObject
*a
, PyLongObject
*b
)
977 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
982 /* Ensure a is the larger of the two: */
983 if (size_a
< size_b
) {
984 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
985 { int size_temp
= size_a
;
987 size_b
= size_temp
; }
989 z
= _PyLong_New(size_a
+1);
992 for (i
= 0; i
< size_b
; ++i
) {
993 carry
+= a
->ob_digit
[i
] + b
->ob_digit
[i
];
994 z
->ob_digit
[i
] = carry
& MASK
;
997 for (; i
< size_a
; ++i
) {
998 carry
+= a
->ob_digit
[i
];
999 z
->ob_digit
[i
] = carry
& MASK
;
1002 z
->ob_digit
[i
] = carry
;
1003 return long_normalize(z
);
1006 /* Subtract the absolute values of two integers. */
1008 static PyLongObject
*
1009 x_sub(PyLongObject
*a
, PyLongObject
*b
)
1011 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
1017 /* Ensure a is the larger of the two: */
1018 if (size_a
< size_b
) {
1020 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1021 { int size_temp
= size_a
;
1023 size_b
= size_temp
; }
1025 else if (size_a
== size_b
) {
1026 /* Find highest digit where a and b differ: */
1028 while (--i
>= 0 && a
->ob_digit
[i
] == b
->ob_digit
[i
])
1031 return _PyLong_New(0);
1032 if (a
->ob_digit
[i
] < b
->ob_digit
[i
]) {
1034 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1036 size_a
= size_b
= i
+1;
1038 z
= _PyLong_New(size_a
);
1041 for (i
= 0; i
< size_b
; ++i
) {
1042 /* The following assumes unsigned arithmetic
1043 works module 2**N for some N>SHIFT. */
1044 borrow
= a
->ob_digit
[i
] - b
->ob_digit
[i
] - borrow
;
1045 z
->ob_digit
[i
] = borrow
& MASK
;
1047 borrow
&= 1; /* Keep only one sign bit */
1049 for (; i
< size_a
; ++i
) {
1050 borrow
= a
->ob_digit
[i
] - borrow
;
1051 z
->ob_digit
[i
] = borrow
& MASK
;
1053 borrow
&= 1; /* Keep only one sign bit */
1055 assert(borrow
== 0);
1057 z
->ob_size
= -(z
->ob_size
);
1058 return long_normalize(z
);
1062 long_add(PyLongObject
*a
, PyLongObject
*b
)
1066 if (a
->ob_size
< 0) {
1067 if (b
->ob_size
< 0) {
1069 if (z
!= NULL
&& z
->ob_size
!= 0)
1070 z
->ob_size
= -(z
->ob_size
);
1081 return (PyObject
*)z
;
1085 long_sub(PyLongObject
*a
, PyLongObject
*b
)
1089 if (a
->ob_size
< 0) {
1094 if (z
!= NULL
&& z
->ob_size
!= 0)
1095 z
->ob_size
= -(z
->ob_size
);
1103 return (PyObject
*)z
;
1107 long_mul(PyLongObject
*a
, PyLongObject
*b
)
1114 size_a
= ABS(a
->ob_size
);
1115 size_b
= ABS(b
->ob_size
);
1116 if (size_a
> size_b
) {
1117 /* we are faster with the small object on the left */
1118 int hold_sa
= size_a
;
1119 PyLongObject
*hold_a
= a
;
1125 z
= _PyLong_New(size_a
+ size_b
);
1128 for (i
= 0; i
< z
->ob_size
; ++i
)
1130 for (i
= 0; i
< size_a
; ++i
) {
1131 twodigits carry
= 0;
1132 twodigits f
= a
->ob_digit
[i
];
1139 for (j
= 0; j
< size_b
; ++j
) {
1140 carry
+= z
->ob_digit
[i
+j
] + b
->ob_digit
[j
] * f
;
1141 z
->ob_digit
[i
+j
] = (digit
) (carry
& MASK
);
1144 for (; carry
!= 0; ++j
) {
1145 assert(i
+j
< z
->ob_size
);
1146 carry
+= z
->ob_digit
[i
+j
];
1147 z
->ob_digit
[i
+j
] = (digit
) (carry
& MASK
);
1152 z
->ob_size
= -(z
->ob_size
);
1154 z
->ob_size
= -(z
->ob_size
);
1155 return (PyObject
*) long_normalize(z
);
1158 /* The / and % operators are now defined in terms of divmod().
1159 The expression a mod b has the value a - b*floor(a/b).
1160 The long_divrem function gives the remainder after division of
1161 |a| by |b|, with the sign of a. This is also expressed
1162 as a - b*trunc(a/b), if trunc truncates towards zero.
1169 So, to get from rem to mod, we have to add b if a and b
1170 have different signs. We then subtract one from the 'div'
1171 part of the outcome to keep the invariant intact. */
1174 l_divmod(PyLongObject
*v
, PyLongObject
*w
,
1175 PyLongObject
**pdiv
, PyLongObject
**pmod
)
1177 PyLongObject
*div
, *mod
;
1179 if (long_divrem(v
, w
, &div
, &mod
) < 0)
1181 if ((mod
->ob_size
< 0 && w
->ob_size
> 0) ||
1182 (mod
->ob_size
> 0 && w
->ob_size
< 0)) {
1185 temp
= (PyLongObject
*) long_add(mod
, w
);
1192 one
= (PyLongObject
*) PyLong_FromLong(1L);
1194 (temp
= (PyLongObject
*) long_sub(div
, one
)) == NULL
) {
1210 long_div(PyLongObject
*v
, PyLongObject
*w
)
1212 PyLongObject
*div
, *mod
;
1213 if (l_divmod(v
, w
, &div
, &mod
) < 0)
1216 return (PyObject
*)div
;
1220 long_mod(PyLongObject
*v
, PyLongObject
*w
)
1222 PyLongObject
*div
, *mod
;
1223 if (l_divmod(v
, w
, &div
, &mod
) < 0)
1226 return (PyObject
*)mod
;
1230 long_divmod(PyLongObject
*v
, PyLongObject
*w
)
1233 PyLongObject
*div
, *mod
;
1234 if (l_divmod(v
, w
, &div
, &mod
) < 0)
1238 PyTuple_SetItem(z
, 0, (PyObject
*) div
);
1239 PyTuple_SetItem(z
, 1, (PyObject
*) mod
);
1249 long_pow(PyLongObject
*a
, PyLongObject
*b
, PyLongObject
*c
)
1251 PyLongObject
*z
, *div
, *mod
;
1254 size_b
= b
->ob_size
;
1256 PyErr_SetString(PyExc_ValueError
,
1257 "long integer to the negative power");
1260 z
= (PyLongObject
*)PyLong_FromLong(1L);
1262 for (i
= 0; i
< size_b
; ++i
) {
1263 digit bi
= b
->ob_digit
[i
];
1266 for (j
= 0; j
< SHIFT
; ++j
) {
1270 temp
= (PyLongObject
*)long_mul(z
, a
);
1272 if ((PyObject
*)c
!=Py_None
&& temp
!=NULL
) {
1273 if (l_divmod(temp
,c
,&div
,&mod
) < 0) {
1287 if (bi
== 0 && i
+1 == size_b
)
1289 temp
= (PyLongObject
*)long_mul(a
, a
);
1291 if ((PyObject
*)c
!=Py_None
&& temp
!=NULL
) {
1292 if (l_divmod(temp
, c
, &div
, &mod
) < 0) {
1308 if (a
== NULL
|| z
== NULL
)
1312 if ((PyObject
*)c
!=Py_None
&& z
!=NULL
) {
1313 if (l_divmod(z
, c
, &div
, &mod
) < 0) {
1324 return (PyObject
*)z
;
1328 long_invert(PyLongObject
*v
)
1330 /* Implement ~x as -(x+1) */
1333 w
= (PyLongObject
*)PyLong_FromLong(1L);
1336 x
= (PyLongObject
*) long_add(v
, w
);
1340 if (x
->ob_size
!= 0)
1341 x
->ob_size
= -(x
->ob_size
);
1342 return (PyObject
*)x
;
1346 long_pos(PyLongObject
*v
)
1349 return (PyObject
*)v
;
1353 long_neg(PyLongObject
*v
)
1357 n
= ABS(v
->ob_size
);
1361 return (PyObject
*) v
;
1363 z
= _PyLong_New(ABS(n
));
1366 for (i
= 0; i
< n
; i
++)
1367 z
->ob_digit
[i
] = v
->ob_digit
[i
];
1368 z
->ob_size
= -(v
->ob_size
);
1369 return (PyObject
*)z
;
1373 long_abs(PyLongObject
*v
)
1379 return (PyObject
*)v
;
1384 long_nonzero(PyLongObject
*v
)
1386 return ABS(v
->ob_size
) != 0;
1390 long_rshift(PyLongObject
*a
, PyLongObject
*b
)
1394 int newsize
, wordshift
, loshift
, hishift
, i
, j
;
1395 digit lomask
, himask
;
1397 if (a
->ob_size
< 0) {
1398 /* Right shifting negative numbers is harder */
1399 PyLongObject
*a1
, *a2
, *a3
;
1400 a1
= (PyLongObject
*) long_invert(a
);
1401 if (a1
== NULL
) return NULL
;
1402 a2
= (PyLongObject
*) long_rshift(a1
, b
);
1404 if (a2
== NULL
) return NULL
;
1405 a3
= (PyLongObject
*) long_invert(a2
);
1407 return (PyObject
*) a3
;
1410 shiftby
= PyLong_AsLong((PyObject
*)b
);
1411 if (shiftby
== -1L && PyErr_Occurred())
1414 PyErr_SetString(PyExc_ValueError
, "negative shift count");
1417 wordshift
= shiftby
/ SHIFT
;
1418 newsize
= ABS(a
->ob_size
) - wordshift
;
1421 return (PyObject
*)z
;
1423 loshift
= shiftby
% SHIFT
;
1424 hishift
= SHIFT
- loshift
;
1425 lomask
= ((digit
)1 << hishift
) - 1;
1426 himask
= MASK
^ lomask
;
1427 z
= _PyLong_New(newsize
);
1431 z
->ob_size
= -(z
->ob_size
);
1432 for (i
= 0, j
= wordshift
; i
< newsize
; i
++, j
++) {
1433 z
->ob_digit
[i
] = (a
->ob_digit
[j
] >> loshift
) & lomask
;
1436 (a
->ob_digit
[j
+1] << hishift
) & himask
;
1438 return (PyObject
*) long_normalize(z
);
1442 long_lshift(PyLongObject
*a
, PyLongObject
*b
)
1444 /* This version due to Tim Peters */
1447 int oldsize
, newsize
, wordshift
, remshift
, i
, j
;
1450 shiftby
= PyLong_AsLong((PyObject
*)b
);
1451 if (shiftby
== -1L && PyErr_Occurred())
1454 PyErr_SetString(PyExc_ValueError
, "negative shift count");
1457 if ((long)(int)shiftby
!= shiftby
) {
1458 PyErr_SetString(PyExc_ValueError
,
1459 "outrageous left shift count");
1462 /* wordshift, remshift = divmod(shiftby, SHIFT) */
1463 wordshift
= (int)shiftby
/ SHIFT
;
1464 remshift
= (int)shiftby
- wordshift
* SHIFT
;
1466 oldsize
= ABS(a
->ob_size
);
1467 newsize
= oldsize
+ wordshift
;
1470 z
= _PyLong_New(newsize
);
1474 z
->ob_size
= -(z
->ob_size
);
1475 for (i
= 0; i
< wordshift
; i
++)
1478 for (i
= wordshift
, j
= 0; j
< oldsize
; i
++, j
++) {
1479 accum
|= a
->ob_digit
[j
] << remshift
;
1480 z
->ob_digit
[i
] = (digit
)(accum
& MASK
);
1484 z
->ob_digit
[newsize
-1] = (digit
)accum
;
1487 return (PyObject
*) long_normalize(z
);
1491 /* Bitwise and/xor/or operations */
1493 #define MAX(x, y) ((x) < (y) ? (y) : (x))
1494 #define MIN(x, y) ((x) > (y) ? (y) : (x))
1497 long_bitwise(PyLongObject
*a
,
1498 int op
, /* '&', '|', '^' */
1501 digit maska
, maskb
; /* 0 or MASK */
1503 int size_a
, size_b
, size_z
;
1509 if (a
->ob_size
< 0) {
1510 a
= (PyLongObject
*) long_invert(a
);
1517 if (b
->ob_size
< 0) {
1518 b
= (PyLongObject
*) long_invert(b
);
1529 if (maska
!= maskb
) {
1535 if (maska
&& maskb
) {
1543 if (maska
|| maskb
) {
1552 /* JRH: The original logic here was to allocate the result value (z)
1553 as the longer of the two operands. However, there are some cases
1554 where the result is guaranteed to be shorter than that: AND of two
1555 positives, OR of two negatives: use the shorter number. AND with
1556 mixed signs: use the positive number. OR with mixed signs: use the
1557 negative number. After the transformations above, op will be '&'
1558 iff one of these cases applies, and mask will be non-0 for operands
1559 whose length should be ignored.
1562 size_a
= a
->ob_size
;
1563 size_b
= b
->ob_size
;
1567 : (maskb
? size_a
: MIN(size_a
, size_b
)))
1568 : MAX(size_a
, size_b
);
1569 z
= _PyLong_New(size_z
);
1570 if (a
== NULL
|| b
== NULL
|| z
== NULL
) {
1577 for (i
= 0; i
< size_z
; ++i
) {
1578 diga
= (i
< size_a
? a
->ob_digit
[i
] : 0) ^ maska
;
1579 digb
= (i
< size_b
? b
->ob_digit
[i
] : 0) ^ maskb
;
1581 case '&': z
->ob_digit
[i
] = diga
& digb
; break;
1582 case '|': z
->ob_digit
[i
] = diga
| digb
; break;
1583 case '^': z
->ob_digit
[i
] = diga
^ digb
; break;
1589 z
= long_normalize(z
);
1591 return (PyObject
*) z
;
1598 long_and(PyLongObject
*a
, PyLongObject
*b
)
1600 return long_bitwise(a
, '&', b
);
1604 long_xor(PyLongObject
*a
, PyLongObject
*b
)
1606 return long_bitwise(a
, '^', b
);
1610 long_or(PyLongObject
*a
, PyLongObject
*b
)
1612 return long_bitwise(a
, '|', b
);
1616 long_coerce(PyObject
**pv
, PyObject
**pw
)
1618 if (PyInt_Check(*pw
)) {
1619 *pw
= PyLong_FromLong(PyInt_AsLong(*pw
));
1623 return 1; /* Can't do it */
1627 long_int(PyObject
*v
)
1630 x
= PyLong_AsLong(v
);
1631 if (PyErr_Occurred())
1633 return PyInt_FromLong(x
);
1637 long_long(PyObject
*v
)
1644 long_float(PyObject
*v
)
1647 PyFPE_START_PROTECT("long_float", return 0)
1648 result
= PyLong_AsDouble(v
);
1649 PyFPE_END_PROTECT(result
)
1650 return PyFloat_FromDouble(result
);
1654 long_oct(PyObject
*v
)
1656 return long_format(v
, 8, 1);
1660 long_hex(PyObject
*v
)
1662 return long_format(v
, 16, 1);
1665 static PyNumberMethods long_as_number
= {
1666 (binaryfunc
) long_add
, /*nb_add*/
1667 (binaryfunc
) long_sub
, /*nb_subtract*/
1668 (binaryfunc
) long_mul
, /*nb_multiply*/
1669 (binaryfunc
) long_div
, /*nb_divide*/
1670 (binaryfunc
) long_mod
, /*nb_remainder*/
1671 (binaryfunc
) long_divmod
, /*nb_divmod*/
1672 (ternaryfunc
) long_pow
, /*nb_power*/
1673 (unaryfunc
) long_neg
, /*nb_negative*/
1674 (unaryfunc
) long_pos
, /*tp_positive*/
1675 (unaryfunc
) long_abs
, /*tp_absolute*/
1676 (inquiry
) long_nonzero
, /*tp_nonzero*/
1677 (unaryfunc
) long_invert
, /*nb_invert*/
1678 (binaryfunc
) long_lshift
, /*nb_lshift*/
1679 (binaryfunc
) long_rshift
, /*nb_rshift*/
1680 (binaryfunc
) long_and
, /*nb_and*/
1681 (binaryfunc
) long_xor
, /*nb_xor*/
1682 (binaryfunc
) long_or
, /*nb_or*/
1683 (coercion
) long_coerce
, /*nb_coerce*/
1684 (unaryfunc
) long_int
, /*nb_int*/
1685 (unaryfunc
) long_long
, /*nb_long*/
1686 (unaryfunc
) long_float
, /*nb_float*/
1687 (unaryfunc
) long_oct
, /*nb_oct*/
1688 (unaryfunc
) long_hex
, /*nb_hex*/
1691 PyTypeObject PyLong_Type
= {
1692 PyObject_HEAD_INIT(&PyType_Type
)
1695 sizeof(PyLongObject
) - sizeof(digit
),
1697 (destructor
)long_dealloc
, /*tp_dealloc*/
1701 (cmpfunc
)long_compare
, /*tp_compare*/
1702 (reprfunc
)long_repr
, /*tp_repr*/
1703 &long_as_number
, /*tp_as_number*/
1704 0, /*tp_as_sequence*/
1705 0, /*tp_as_mapping*/
1706 (hashfunc
)long_hash
, /*tp_hash*/
1708 (reprfunc
)long_str
, /*tp_str*/