1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Long (arbitrary precision) integer object implementation */
34 /* XXX The functional organization of this file is terrible */
37 #include "longintrepr.h"
43 #define ABS(x) ((x) < 0 ? -(x) : (x))
46 static PyLongObject
*long_normalize
Py_PROTO((PyLongObject
*));
47 static PyLongObject
*mul1
Py_PROTO((PyLongObject
*, wdigit
));
48 static PyLongObject
*muladd1
Py_PROTO((PyLongObject
*, wdigit
, wdigit
));
49 static PyLongObject
*divrem1
Py_PROTO((PyLongObject
*, wdigit
, digit
*));
50 static PyObject
*long_format
Py_PROTO((PyObject
*aa
, int base
, int addL
));
52 static int ticker
; /* XXX Could be shared with ceval? */
54 #define SIGCHECK(PyTryBlock) \
57 if (PyErr_CheckSignals()) { PyTryBlock; } \
60 /* Normalize (remove leading zeros from) a long int object.
61 Doesn't attempt to free the storage--in most cases, due to the nature
62 of the algorithms used, this could save at most be one word anyway. */
66 register PyLongObject
*v
;
68 int j
= ABS(v
->ob_size
);
71 while (i
> 0 && v
->ob_digit
[i
-1] == 0)
74 v
->ob_size
= (v
->ob_size
< 0) ? -(i
) : i
;
78 /* Allocate a new long int object with size digits.
79 Return NULL and set exception if we run out of memory. */
85 return PyObject_NEW_VAR(PyLongObject
, &PyLong_Type
, size
);
88 /* Create a new long int object from a C long int */
94 /* Assume a C long fits in at most 5 'digits' */
95 /* Works on both 32- and 64-bit machines */
96 PyLongObject
*v
= _PyLong_New(5);
98 unsigned long t
= ival
;
102 v
->ob_size
= -(v
->ob_size
);
104 for (i
= 0; i
< 5; i
++) {
105 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
108 v
= long_normalize(v
);
110 return (PyObject
*)v
;
113 /* Create a new long int object from a C unsigned long int */
116 PyLong_FromUnsignedLong(ival
)
119 /* Assume a C long fits in at most 5 'digits' */
120 /* Works on both 32- and 64-bit machines */
121 PyLongObject
*v
= _PyLong_New(5);
123 unsigned long t
= ival
;
125 for (i
= 0; i
< 5; i
++) {
126 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
129 v
= long_normalize(v
);
131 return (PyObject
*)v
;
134 /* Create a new long int object from a C double */
138 PyLong_FromDouble(double dval
)
140 PyLong_FromDouble(dval
)
146 int i
, ndig
, expo
, neg
;
148 if (dval
&& dval
* 0.5 == dval
) {
149 PyErr_SetString(PyExc_OverflowError
,
150 "cannot convert float infinity to long");
157 frac
= frexp(dval
, &expo
); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
159 return PyLong_FromLong(0L);
160 ndig
= (expo
-1) / SHIFT
+ 1; /* Number of 'digits' in result */
161 v
= _PyLong_New(ndig
);
164 frac
= ldexp(frac
, (expo
-1) % SHIFT
+ 1);
165 for (i
= ndig
; --i
>= 0; ) {
166 long bits
= (long)frac
;
167 v
->ob_digit
[i
] = (digit
) bits
;
168 frac
= frac
- (double)bits
;
169 frac
= ldexp(frac
, SHIFT
);
172 v
->ob_size
= -(v
->ob_size
);
173 return (PyObject
*)v
;
176 /* Get a C long int from a long int object.
177 Returns -1 and sets an error condition if overflow occurs. */
183 /* This version by Tim Peters */
184 register PyLongObject
*v
;
185 unsigned long x
, prev
;
188 if (vv
== NULL
|| !PyLong_Check(vv
)) {
189 PyErr_BadInternalCall();
192 v
= (PyLongObject
*)vv
;
202 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
203 if ((x
>> SHIFT
) != prev
)
206 /* Haven't lost any bits, but if the sign bit is set we're in
207 * trouble *unless* this is the min negative number. So,
208 * trouble iff sign bit set && (positive || some bit set other
209 * than the sign bit).
211 if ((long)x
< 0 && (sign
> 0 || (x
<< 1) != 0))
213 return (long)x
* sign
;
216 PyErr_SetString(PyExc_OverflowError
,
217 "long int too long to convert");
221 /* Get a C long int from a long int object.
222 Returns -1 and sets an error condition if overflow occurs. */
225 PyLong_AsUnsignedLong(vv
)
228 register PyLongObject
*v
;
229 unsigned long x
, prev
;
232 if (vv
== NULL
|| !PyLong_Check(vv
)) {
233 PyErr_BadInternalCall();
234 return (unsigned long) -1;
236 v
= (PyLongObject
*)vv
;
240 PyErr_SetString(PyExc_OverflowError
,
241 "can't convert negative value to unsigned long");
242 return (unsigned long) -1;
246 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
247 if ((x
>> SHIFT
) != prev
) {
248 PyErr_SetString(PyExc_OverflowError
,
249 "long int too long to convert");
250 return (unsigned long) -1;
256 /* Get a C double from a long int object. */
262 register PyLongObject
*v
;
264 double multiplier
= (double) (1L << SHIFT
);
267 if (vv
== NULL
|| !PyLong_Check(vv
)) {
268 PyErr_BadInternalCall();
271 v
= (PyLongObject
*)vv
;
280 x
= x
*multiplier
+ (double)v
->ob_digit
[i
];
285 /* Create a new long (or int) object from a C pointer */
288 PyLong_FromVoidPtr(p
)
291 #if SIZEOF_VOID_P == SIZEOF_LONG
292 return PyInt_FromLong((long)p
);
294 /* optimize null pointers */
296 return PyInt_FromLong(0);
298 /* we can assume that HAVE_LONG_LONG is true. if not, then the
299 configuration process should have bailed (having big pointers
300 without long longs seems non-sensical) */
301 return PyLong_FromLongLong((LONG_LONG
)p
);
302 #endif /* SIZEOF_VOID_P == SIZEOF_LONG */
305 /* Get a C pointer from a long object (or an int object in some cases) */
311 /* This function will allow int or long objects. If vv is neither,
312 then the PyLong_AsLong*() functions will raise the exception:
313 PyExc_SystemError, "bad argument to internal function"
316 #if SIZEOF_VOID_P == SIZEOF_LONG
319 if ( PyInt_Check(vv
) )
320 x
= PyInt_AS_LONG(vv
);
322 x
= PyLong_AsLong(vv
);
324 /* we can assume that HAVE_LONG_LONG is true. if not, then the
325 configuration process should have bailed (having big pointers
326 without long longs seems non-sensical) */
329 if ( PyInt_Check(vv
) )
330 x
= PyInt_AS_LONG(vv
);
332 x
= PyLong_AsLongLong(vv
);
333 #endif /* SIZEOF_VOID_P == SIZEOF_LONG */
335 if (x
== -1 && PyErr_Occurred())
340 #ifdef HAVE_LONG_LONG
342 * LONG_LONG support by Chris Herborth (chrish@qnx.com)
344 * For better or worse :-), I tried to follow the coding style already
348 /* Create a new long int object from a C LONG_LONG int */
351 PyLong_FromLongLong(ival
)
354 #if SIZEOF_LONG_LONG == SIZEOF_LONG
355 /* In case the compiler is faking it. */
356 return PyLong_FromLong( (long)ival
);
358 if( ival
<= (LONG_LONG
)LONG_MAX
) {
359 return PyLong_FromLong( (long)ival
);
361 else if( ival
<= (unsigned LONG_LONG
)ULONG_MAX
) {
362 return PyLong_FromUnsignedLong( (unsigned long)ival
);
365 /* Assume a C LONG_LONG fits in at most 10 'digits'.
366 * Should be OK if we're assuming long fits in 5.
368 PyLongObject
*v
= _PyLong_New(10);
371 unsigned LONG_LONG t
= ival
;
375 v
->ob_size
= -(v
->ob_size
);
378 for (i
= 0; i
< 10; i
++) {
379 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
383 v
= long_normalize(v
);
386 return (PyObject
*)v
;
391 /* Create a new long int object from a C unsigned LONG_LONG int */
393 PyLong_FromUnsignedLongLong(ival
)
394 unsigned LONG_LONG ival
;
396 #if SIZEOF_LONG_LONG == SIZEOF_LONG
397 /* In case the compiler is faking it. */
398 return PyLong_FromUnsignedLong( (unsigned long)ival
);
400 if( ival
<= (unsigned LONG_LONG
)ULONG_MAX
) {
401 return PyLong_FromUnsignedLong( (unsigned long)ival
);
404 /* Assume a C long fits in at most 10 'digits'. */
405 PyLongObject
*v
= _PyLong_New(10);
408 unsigned LONG_LONG t
= ival
;
410 for (i
= 0; i
< 10; i
++) {
411 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
415 v
= long_normalize(v
);
418 return (PyObject
*)v
;
423 /* Get a C LONG_LONG int from a long int object.
424 Returns -1 and sets an error condition if overflow occurs. */
427 PyLong_AsLongLong(vv
)
430 #if SIZEOF_LONG_LONG == SIZEOF_LONG
431 /* In case the compiler is faking it. */
432 return (LONG_LONG
)PyLong_AsLong( vv
);
434 register PyLongObject
*v
;
438 if (vv
== NULL
|| !PyLong_Check(vv
)) {
439 PyErr_BadInternalCall();
443 v
= (PyLongObject
*)vv
;
455 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
456 if ((x
>> SHIFT
) != prev
) {
457 PyErr_SetString(PyExc_OverflowError
,
458 "long int too long to convert");
468 PyLong_AsUnsignedLongLong(vv
)
471 #if SIZEOF_LONG_LONG == 4
472 /* In case the compiler is faking it. */
473 return (unsigned LONG_LONG
)PyLong_AsUnsignedLong( vv
);
475 register PyLongObject
*v
;
476 unsigned LONG_LONG x
, prev
;
479 if (vv
== NULL
|| !PyLong_Check(vv
)) {
480 PyErr_BadInternalCall();
481 return (unsigned LONG_LONG
) -1;
484 v
= (PyLongObject
*)vv
;
489 PyErr_SetString(PyExc_OverflowError
,
490 "can't convert negative value to unsigned long");
491 return (unsigned LONG_LONG
) -1;
496 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
497 if ((x
>> SHIFT
) != prev
) {
498 PyErr_SetString(PyExc_OverflowError
,
499 "long int too long to convert");
500 return (unsigned LONG_LONG
) -1;
507 #endif /* HAVE_LONG_LONG */
509 /* Multiply by a single digit, ignoring the sign. */
511 static PyLongObject
*
516 return muladd1(a
, n
, (digit
)0);
519 /* Multiply by a single digit and add a single digit, ignoring the sign. */
521 static PyLongObject
*
527 int size_a
= ABS(a
->ob_size
);
528 PyLongObject
*z
= _PyLong_New(size_a
+1);
529 twodigits carry
= extra
;
534 for (i
= 0; i
< size_a
; ++i
) {
535 carry
+= (twodigits
)a
->ob_digit
[i
] * n
;
536 z
->ob_digit
[i
] = (digit
) (carry
& MASK
);
539 z
->ob_digit
[i
] = (digit
) carry
;
540 return long_normalize(z
);
543 /* Divide a long integer by a digit, returning both the quotient
544 (as function result) and the remainder (through *prem).
545 The sign of a is ignored; n should not be zero. */
547 static PyLongObject
*
553 int size
= ABS(a
->ob_size
);
558 assert(n
> 0 && n
<= MASK
);
559 z
= _PyLong_New(size
);
562 for (i
= size
; --i
>= 0; ) {
563 rem
= (rem
<< SHIFT
) + a
->ob_digit
[i
];
564 z
->ob_digit
[i
] = (digit
) (rem
/n
);
568 return long_normalize(z
);
571 /* Convert a long int object to a string, using a given conversion base.
572 Return a string object.
573 If base is 8 or 16, add the proper prefix '0' or '0x'. */
576 long_format(aa
, base
, addL
)
581 register PyLongObject
*a
= (PyLongObject
*)aa
;
584 int size_a
= ABS(a
->ob_size
);
589 if (a
== NULL
|| !PyLong_Check(a
)) {
590 PyErr_BadInternalCall();
593 assert(base
>= 2 && base
<= 36);
595 /* Compute a rough upper bound for the length of the string */
602 i
= 5 + (addL
? 1 : 0) + (size_a
*SHIFT
+ bits
-1) / bits
;
603 str
= (PyStringObject
*) PyString_FromStringAndSize((char *)0, i
);
606 p
= PyString_AS_STRING(str
) + i
;
613 if (a
->ob_size
== 0) {
616 else if ((base
& (base
- 1)) == 0) {
617 /* JRH: special case for power-of-2 bases */
618 twodigits temp
= a
->ob_digit
[0];
619 int bitsleft
= SHIFT
;
621 int last
= abs(a
->ob_size
);
624 while ((i
>>= 1) > 1) ++basebits
;
628 while (bitsleft
>= basebits
) {
629 if ((temp
== 0) && (i
>= last
- 1)) break;
630 rem
= temp
& (base
- 1);
635 assert(p
> PyString_AS_STRING(str
));
637 bitsleft
-= basebits
;
641 if (temp
== 0) break;
643 /* loop again to pick up final digits */
646 temp
= (a
->ob_digit
[i
] << bitsleft
) | temp
;
655 PyLongObject
*temp
= divrem1(a
, (digit
)base
, &rem
);
665 assert(p
> PyString_AS_STRING(str
));
674 } while (ABS(a
->ob_size
) != 0);
682 else if (base
== 16) {
686 else if (base
!= 10) {
688 *--p
= '0' + base
%10;
690 *--p
= '0' + base
/10;
694 if (p
!= PyString_AS_STRING(str
)) {
695 char *q
= PyString_AS_STRING(str
);
698 } while ((*q
++ = *p
++) != '\0');
700 _PyString_Resize((PyObject
**)&str
,
701 (int) (q
- PyString_AS_STRING(str
)));
703 return (PyObject
*)str
;
707 /* Convert a string to a long int object, in a given base.
708 Base zero implies a default depending on the number.
709 External linkage: used in compile.c and stropmodule.c. */
716 return PyLong_FromString(str
, (char **)NULL
, base
);
721 PyLong_FromString(str
, pend
, base
)
730 if ((base
!= 0 && base
< 2) || base
> 36) {
731 PyErr_SetString(PyExc_ValueError
,
732 "invalid base for long literal");
735 while (*str
!= '\0' && isspace(Py_CHARMASK(*str
)))
739 else if (*str
== '-') {
743 while (*str
!= '\0' && isspace(Py_CHARMASK(*str
)))
748 else if (str
[1] == 'x' || str
[1] == 'X')
753 if (base
== 16 && str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
757 for ( ; z
!= NULL
; ++str
) {
763 else if (*str
>= 'a')
765 else if (*str
>= 'A')
767 if (k
< 0 || k
>= base
)
769 temp
= muladd1(z
, (digit
)base
, (digit
)k
);
776 PyErr_SetString(PyExc_ValueError
,
777 "no digits in long int constant");
781 if (sign
< 0 && z
!= NULL
&& z
->ob_size
!= 0)
782 z
->ob_size
= -(z
->ob_size
);
785 return (PyObject
*) z
;
788 static PyLongObject
*x_divrem
789 Py_PROTO((PyLongObject
*, PyLongObject
*, PyLongObject
**));
790 static PyObject
*long_pos
Py_PROTO((PyLongObject
*));
791 static int long_divrem
Py_PROTO((PyLongObject
*, PyLongObject
*,
792 PyLongObject
**, PyLongObject
**));
794 /* Long division with remainder, top-level routine */
797 long_divrem(a
, b
, pdiv
, prem
)
802 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
806 PyErr_SetString(PyExc_ZeroDivisionError
,
807 "long division or modulo");
810 if (size_a
< size_b
||
812 a
->ob_digit
[size_a
-1] < b
->ob_digit
[size_b
-1])) {
814 *pdiv
= _PyLong_New(0);
816 *prem
= (PyLongObject
*) a
;
821 z
= divrem1(a
, b
->ob_digit
[0], &rem
);
824 *prem
= (PyLongObject
*) PyLong_FromLong((long)rem
);
827 z
= x_divrem(a
, b
, prem
);
832 The quotient z has the sign of a*b;
833 the remainder r has the sign of a,
835 if ((a
->ob_size
< 0) != (b
->ob_size
< 0))
836 z
->ob_size
= -(z
->ob_size
);
837 if (a
->ob_size
< 0 && (*prem
)->ob_size
!= 0)
838 (*prem
)->ob_size
= -((*prem
)->ob_size
);
843 /* Unsigned long division with remainder -- the algorithm */
845 static PyLongObject
*
846 x_divrem(v1
, w1
, prem
)
847 PyLongObject
*v1
, *w1
;
850 int size_v
= ABS(v1
->ob_size
), size_w
= ABS(w1
->ob_size
);
851 digit d
= (digit
) ((twodigits
)BASE
/ (w1
->ob_digit
[size_w
-1] + 1));
852 PyLongObject
*v
= mul1(v1
, d
);
853 PyLongObject
*w
= mul1(w1
, d
);
857 if (v
== NULL
|| w
== NULL
) {
863 assert(size_v
>= size_w
&& size_w
> 1); /* Assert checks by div() */
864 assert(v
->ob_refcnt
== 1); /* Since v will be used as accumulator! */
865 assert(size_w
== ABS(w
->ob_size
)); /* That's how d was calculated */
867 size_v
= ABS(v
->ob_size
);
868 a
= _PyLong_New(size_v
- size_w
+ 1);
870 for (j
= size_v
, k
= a
->ob_size
-1; a
!= NULL
&& k
>= 0; --j
, --k
) {
871 digit vj
= (j
>= size_v
) ? 0 : v
->ob_digit
[j
];
873 stwodigits carry
= 0;
881 if (vj
== w
->ob_digit
[size_w
-1])
884 q
= (((twodigits
)vj
<< SHIFT
) + v
->ob_digit
[j
-1]) /
885 w
->ob_digit
[size_w
-1];
887 while (w
->ob_digit
[size_w
-2]*q
>
889 ((twodigits
)vj
<< SHIFT
)
891 - q
*w
->ob_digit
[size_w
-1]
896 for (i
= 0; i
< size_w
&& i
+k
< size_v
; ++i
) {
897 twodigits z
= w
->ob_digit
[i
] * q
;
898 digit zz
= (digit
) (z
>> SHIFT
);
899 carry
+= v
->ob_digit
[i
+k
] - z
900 + ((twodigits
)zz
<< SHIFT
);
901 v
->ob_digit
[i
+k
] = carry
& MASK
;
902 carry
= (carry
>> SHIFT
) - zz
;
906 carry
+= v
->ob_digit
[i
+k
];
907 v
->ob_digit
[i
+k
] = 0;
911 a
->ob_digit
[k
] = (digit
) q
;
914 a
->ob_digit
[k
] = (digit
) q
-1;
916 for (i
= 0; i
< size_w
&& i
+k
< size_v
; ++i
) {
917 carry
+= v
->ob_digit
[i
+k
] + w
->ob_digit
[i
];
918 v
->ob_digit
[i
+k
] = carry
& MASK
;
927 a
= long_normalize(a
);
928 *prem
= divrem1(v
, d
, &d
);
929 /* d receives the (unused) remainder */
943 static void long_dealloc
Py_PROTO((PyObject
*));
944 static PyObject
*long_repr
Py_PROTO((PyObject
*));
945 static int long_compare
Py_PROTO((PyLongObject
*, PyLongObject
*));
946 static long long_hash
Py_PROTO((PyLongObject
*));
948 static PyObject
*long_add
Py_PROTO((PyLongObject
*, PyLongObject
*));
949 static PyObject
*long_sub
Py_PROTO((PyLongObject
*, PyLongObject
*));
950 static PyObject
*long_mul
Py_PROTO((PyLongObject
*, PyLongObject
*));
951 static PyObject
*long_div
Py_PROTO((PyLongObject
*, PyLongObject
*));
952 static PyObject
*long_mod
Py_PROTO((PyLongObject
*, PyLongObject
*));
953 static PyObject
*long_divmod
Py_PROTO((PyLongObject
*, PyLongObject
*));
954 static PyObject
*long_pow
955 Py_PROTO((PyLongObject
*, PyLongObject
*, PyLongObject
*));
956 static PyObject
*long_neg
Py_PROTO((PyLongObject
*));
957 static PyObject
*long_pos
Py_PROTO((PyLongObject
*));
958 static PyObject
*long_abs
Py_PROTO((PyLongObject
*));
959 static int long_nonzero
Py_PROTO((PyLongObject
*));
960 static PyObject
*long_invert
Py_PROTO((PyLongObject
*));
961 static PyObject
*long_lshift
Py_PROTO((PyLongObject
*, PyLongObject
*));
962 static PyObject
*long_rshift
Py_PROTO((PyLongObject
*, PyLongObject
*));
963 static PyObject
*long_and
Py_PROTO((PyLongObject
*, PyLongObject
*));
964 static PyObject
*long_xor
Py_PROTO((PyLongObject
*, PyLongObject
*));
965 static PyObject
*long_or
Py_PROTO((PyLongObject
*, PyLongObject
*));
978 return long_format(v
, 10, 1);
985 return long_format(v
, 10, 0);
994 if (a
->ob_size
!= b
->ob_size
) {
995 if (ABS(a
->ob_size
) == 0 && ABS(b
->ob_size
) == 0)
998 sign
= a
->ob_size
- b
->ob_size
;
1001 int i
= ABS(a
->ob_size
);
1002 while (--i
>= 0 && a
->ob_digit
[i
] == b
->ob_digit
[i
])
1007 sign
= (int)a
->ob_digit
[i
] - (int)b
->ob_digit
[i
];
1012 return sign
< 0 ? -1 : sign
> 0 ? 1 : 0;
1022 /* This is designed so that Python ints and longs with the
1023 same value hash to the same value, otherwise comparisons
1024 of mapping keys will turn out weird */
1033 /* Force a 32-bit circular shift */
1034 x
= ((x
<< SHIFT
) & ~MASK
) | ((x
>> (32-SHIFT
)) & MASK
);
1035 x
+= v
->ob_digit
[i
];
1044 /* Add the absolute values of two long integers. */
1046 static PyLongObject
*x_add
Py_PROTO((PyLongObject
*, PyLongObject
*));
1047 static PyLongObject
*
1049 PyLongObject
*a
, *b
;
1051 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
1056 /* Ensure a is the larger of the two: */
1057 if (size_a
< size_b
) {
1058 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1059 { int size_temp
= size_a
;
1061 size_b
= size_temp
; }
1063 z
= _PyLong_New(size_a
+1);
1066 for (i
= 0; i
< size_b
; ++i
) {
1067 carry
+= a
->ob_digit
[i
] + b
->ob_digit
[i
];
1068 z
->ob_digit
[i
] = carry
& MASK
;
1069 /* The following assumes unsigned shifts don't
1070 propagate the sign bit. */
1073 for (; i
< size_a
; ++i
) {
1074 carry
+= a
->ob_digit
[i
];
1075 z
->ob_digit
[i
] = carry
& MASK
;
1078 z
->ob_digit
[i
] = carry
;
1079 return long_normalize(z
);
1082 /* Subtract the absolute values of two integers. */
1084 static PyLongObject
*x_sub
Py_PROTO((PyLongObject
*, PyLongObject
*));
1085 static PyLongObject
*
1087 PyLongObject
*a
, *b
;
1089 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
1095 /* Ensure a is the larger of the two: */
1096 if (size_a
< size_b
) {
1098 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1099 { int size_temp
= size_a
;
1101 size_b
= size_temp
; }
1103 else if (size_a
== size_b
) {
1104 /* Find highest digit where a and b differ: */
1106 while (--i
>= 0 && a
->ob_digit
[i
] == b
->ob_digit
[i
])
1109 return _PyLong_New(0);
1110 if (a
->ob_digit
[i
] < b
->ob_digit
[i
]) {
1112 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1114 size_a
= size_b
= i
+1;
1116 z
= _PyLong_New(size_a
);
1119 for (i
= 0; i
< size_b
; ++i
) {
1120 /* The following assumes unsigned arithmetic
1121 works module 2**N for some N>SHIFT. */
1122 borrow
= a
->ob_digit
[i
] - b
->ob_digit
[i
] - borrow
;
1123 z
->ob_digit
[i
] = borrow
& MASK
;
1125 borrow
&= 1; /* Keep only one sign bit */
1127 for (; i
< size_a
; ++i
) {
1128 borrow
= a
->ob_digit
[i
] - borrow
;
1129 z
->ob_digit
[i
] = borrow
& MASK
;
1132 assert(borrow
== 0);
1134 z
->ob_size
= -(z
->ob_size
);
1135 return long_normalize(z
);
1145 if (a
->ob_size
< 0) {
1146 if (b
->ob_size
< 0) {
1148 if (z
!= NULL
&& z
->ob_size
!= 0)
1149 z
->ob_size
= -(z
->ob_size
);
1160 return (PyObject
*)z
;
1170 if (a
->ob_size
< 0) {
1175 if (z
!= NULL
&& z
->ob_size
!= 0)
1176 z
->ob_size
= -(z
->ob_size
);
1184 return (PyObject
*)z
;
1197 size_a
= ABS(a
->ob_size
);
1198 size_b
= ABS(b
->ob_size
);
1199 z
= _PyLong_New(size_a
+ size_b
);
1202 for (i
= 0; i
< z
->ob_size
; ++i
)
1204 for (i
= 0; i
< size_a
; ++i
) {
1205 twodigits carry
= 0;
1206 twodigits f
= a
->ob_digit
[i
];
1213 for (j
= 0; j
< size_b
; ++j
) {
1214 carry
+= z
->ob_digit
[i
+j
] + b
->ob_digit
[j
] * f
;
1215 z
->ob_digit
[i
+j
] = (digit
) (carry
& MASK
);
1218 for (; carry
!= 0; ++j
) {
1219 assert(i
+j
< z
->ob_size
);
1220 carry
+= z
->ob_digit
[i
+j
];
1221 z
->ob_digit
[i
+j
] = (digit
) (carry
& MASK
);
1226 z
->ob_size
= -(z
->ob_size
);
1228 z
->ob_size
= -(z
->ob_size
);
1229 return (PyObject
*) long_normalize(z
);
1232 /* The / and % operators are now defined in terms of divmod().
1233 The expression a mod b has the value a - b*floor(a/b).
1234 The long_divrem function gives the remainder after division of
1235 |a| by |b|, with the sign of a. This is also expressed
1236 as a - b*trunc(a/b), if trunc truncates towards zero.
1243 So, to get from rem to mod, we have to add b if a and b
1244 have different signs. We then subtract one from the 'div'
1245 part of the outcome to keep the invariant intact. */
1247 static int l_divmod
Py_PROTO((PyLongObject
*, PyLongObject
*,
1248 PyLongObject
**, PyLongObject
**));
1250 l_divmod(v
, w
, pdiv
, pmod
)
1253 PyLongObject
**pdiv
;
1254 PyLongObject
**pmod
;
1256 PyLongObject
*div
, *mod
;
1258 if (long_divrem(v
, w
, &div
, &mod
) < 0)
1260 if ((mod
->ob_size
< 0 && w
->ob_size
> 0) ||
1261 (mod
->ob_size
> 0 && w
->ob_size
< 0)) {
1264 temp
= (PyLongObject
*) long_add(mod
, w
);
1271 one
= (PyLongObject
*) PyLong_FromLong(1L);
1273 (temp
= (PyLongObject
*) long_sub(div
, one
)) == NULL
) {
1293 PyLongObject
*div
, *mod
;
1294 if (l_divmod(v
, w
, &div
, &mod
) < 0)
1297 return (PyObject
*)div
;
1305 PyLongObject
*div
, *mod
;
1306 if (l_divmod(v
, w
, &div
, &mod
) < 0)
1309 return (PyObject
*)mod
;
1318 PyLongObject
*div
, *mod
;
1319 if (l_divmod(v
, w
, &div
, &mod
) < 0)
1323 PyTuple_SetItem(z
, 0, (PyObject
*) div
);
1324 PyTuple_SetItem(z
, 1, (PyObject
*) mod
);
1339 PyLongObject
*z
, *div
, *mod
;
1342 size_b
= b
->ob_size
;
1344 PyErr_SetString(PyExc_ValueError
,
1345 "long integer to the negative power");
1348 z
= (PyLongObject
*)PyLong_FromLong(1L);
1350 for (i
= 0; i
< size_b
; ++i
) {
1351 digit bi
= b
->ob_digit
[i
];
1354 for (j
= 0; j
< SHIFT
; ++j
) {
1358 temp
= (PyLongObject
*)long_mul(z
, a
);
1360 if ((PyObject
*)c
!=Py_None
&& temp
!=NULL
) {
1361 if (l_divmod(temp
,c
,&div
,&mod
) < 0) {
1375 if (bi
== 0 && i
+1 == size_b
)
1377 temp
= (PyLongObject
*)long_mul(a
, a
);
1379 if ((PyObject
*)c
!=Py_None
&& temp
!=NULL
) {
1380 if (l_divmod(temp
, c
, &div
, &mod
) < 0) {
1396 if (a
== NULL
|| z
== NULL
)
1400 if ((PyObject
*)c
!=Py_None
&& z
!=NULL
) {
1401 if (l_divmod(z
, c
, &div
, &mod
) < 0) {
1412 return (PyObject
*)z
;
1419 /* Implement ~x as -(x+1) */
1422 w
= (PyLongObject
*)PyLong_FromLong(1L);
1425 x
= (PyLongObject
*) long_add(v
, w
);
1429 if (x
->ob_size
!= 0)
1430 x
->ob_size
= -(x
->ob_size
);
1431 return (PyObject
*)x
;
1439 return (PyObject
*)v
;
1448 n
= ABS(v
->ob_size
);
1452 return (PyObject
*) v
;
1454 z
= _PyLong_New(ABS(n
));
1457 for (i
= 0; i
< n
; i
++)
1458 z
->ob_digit
[i
] = v
->ob_digit
[i
];
1459 z
->ob_size
= -(v
->ob_size
);
1460 return (PyObject
*)z
;
1471 return (PyObject
*)v
;
1479 return ABS(v
->ob_size
) != 0;
1489 int newsize
, wordshift
, loshift
, hishift
, i
, j
;
1490 digit lomask
, himask
;
1492 if (a
->ob_size
< 0) {
1493 /* Right shifting negative numbers is harder */
1494 PyLongObject
*a1
, *a2
, *a3
;
1495 a1
= (PyLongObject
*) long_invert(a
);
1496 if (a1
== NULL
) return NULL
;
1497 a2
= (PyLongObject
*) long_rshift(a1
, b
);
1499 if (a2
== NULL
) return NULL
;
1500 a3
= (PyLongObject
*) long_invert(a2
);
1502 return (PyObject
*) a3
;
1505 shiftby
= PyLong_AsLong((PyObject
*)b
);
1506 if (shiftby
== -1L && PyErr_Occurred())
1509 PyErr_SetString(PyExc_ValueError
, "negative shift count");
1512 wordshift
= shiftby
/ SHIFT
;
1513 newsize
= ABS(a
->ob_size
) - wordshift
;
1516 return (PyObject
*)z
;
1518 loshift
= shiftby
% SHIFT
;
1519 hishift
= SHIFT
- loshift
;
1520 lomask
= ((digit
)1 << hishift
) - 1;
1521 himask
= MASK
^ lomask
;
1522 z
= _PyLong_New(newsize
);
1526 z
->ob_size
= -(z
->ob_size
);
1527 for (i
= 0, j
= wordshift
; i
< newsize
; i
++, j
++) {
1528 z
->ob_digit
[i
] = (a
->ob_digit
[j
] >> loshift
) & lomask
;
1531 (a
->ob_digit
[j
+1] << hishift
) & himask
;
1533 return (PyObject
*) long_normalize(z
);
1541 /* This version due to Tim Peters */
1544 int oldsize
, newsize
, wordshift
, remshift
, i
, j
;
1547 shiftby
= PyLong_AsLong((PyObject
*)b
);
1548 if (shiftby
== -1L && PyErr_Occurred())
1551 PyErr_SetString(PyExc_ValueError
, "negative shift count");
1554 if ((long)(int)shiftby
!= shiftby
) {
1555 PyErr_SetString(PyExc_ValueError
,
1556 "outrageous left shift count");
1559 /* wordshift, remshift = divmod(shiftby, SHIFT) */
1560 wordshift
= (int)shiftby
/ SHIFT
;
1561 remshift
= (int)shiftby
- wordshift
* SHIFT
;
1563 oldsize
= ABS(a
->ob_size
);
1564 newsize
= oldsize
+ wordshift
;
1567 z
= _PyLong_New(newsize
);
1571 z
->ob_size
= -(z
->ob_size
);
1572 for (i
= 0; i
< wordshift
; i
++)
1575 for (i
= wordshift
, j
= 0; j
< oldsize
; i
++, j
++) {
1576 accum
|= a
->ob_digit
[j
] << remshift
;
1577 z
->ob_digit
[i
] = (digit
)(accum
& MASK
);
1581 z
->ob_digit
[newsize
-1] = (digit
)accum
;
1584 return (PyObject
*) long_normalize(z
);
1588 /* Bitwise and/xor/or operations */
1590 #define MAX(x, y) ((x) < (y) ? (y) : (x))
1591 #define MIN(x, y) ((x) > (y) ? (y) : (x))
1593 static PyObject
*long_bitwise
Py_PROTO((PyLongObject
*, int, PyLongObject
*));
1595 long_bitwise(a
, op
, b
)
1597 int op
; /* '&', '|', '^' */
1600 digit maska
, maskb
; /* 0 or MASK */
1602 int size_a
, size_b
, size_z
;
1608 if (a
->ob_size
< 0) {
1609 a
= (PyLongObject
*) long_invert(a
);
1616 if (b
->ob_size
< 0) {
1617 b
= (PyLongObject
*) long_invert(b
);
1628 if (maska
!= maskb
) {
1634 if (maska
&& maskb
) {
1642 if (maska
|| maskb
) {
1651 /* JRH: The original logic here was to allocate the result value (z)
1652 as the longer of the two operands. However, there are some cases
1653 where the result is guaranteed to be shorter than that: AND of two
1654 positives, OR of two negatives: use the shorter number. AND with
1655 mixed signs: use the positive number. OR with mixed signs: use the
1656 negative number. After the transformations above, op will be '&'
1657 iff one of these cases applies, and mask will be non-0 for operands
1658 whose length should be ignored.
1661 size_a
= a
->ob_size
;
1662 size_b
= b
->ob_size
;
1666 : (maskb
? size_a
: MIN(size_a
, size_b
)))
1667 : MAX(size_a
, size_b
);
1668 z
= _PyLong_New(size_z
);
1669 if (a
== NULL
|| b
== NULL
|| z
== NULL
) {
1676 for (i
= 0; i
< size_z
; ++i
) {
1677 diga
= (i
< size_a
? a
->ob_digit
[i
] : 0) ^ maska
;
1678 digb
= (i
< size_b
? b
->ob_digit
[i
] : 0) ^ maskb
;
1680 case '&': z
->ob_digit
[i
] = diga
& digb
; break;
1681 case '|': z
->ob_digit
[i
] = diga
| digb
; break;
1682 case '^': z
->ob_digit
[i
] = diga
^ digb
; break;
1688 z
= long_normalize(z
);
1690 return (PyObject
*) z
;
1701 return long_bitwise(a
, '&', b
);
1709 return long_bitwise(a
, '^', b
);
1717 return long_bitwise(a
, '|', b
);
1725 if (PyInt_Check(*pw
)) {
1726 *pw
= PyLong_FromLong(PyInt_AsLong(*pw
));
1730 return 1; /* Can't do it */
1738 x
= PyLong_AsLong(v
);
1739 if (PyErr_Occurred())
1741 return PyInt_FromLong(x
);
1757 PyFPE_START_PROTECT("long_float", return 0)
1758 result
= PyLong_AsDouble(v
);
1759 PyFPE_END_PROTECT(result
)
1760 return PyFloat_FromDouble(result
);
1767 return long_format(v
, 8, 1);
1774 return long_format(v
, 16, 1);
1778 #define UF (unaryfunc)
1779 #define BF (binaryfunc)
1780 #define TF (ternaryfunc)
1781 #define IF (inquiry)
1783 static PyNumberMethods long_as_number
= {
1784 BF long_add
, /*nb_add*/
1785 BF long_sub
, /*nb_subtract*/
1786 BF long_mul
, /*nb_multiply*/
1787 BF long_div
, /*nb_divide*/
1788 BF long_mod
, /*nb_remainder*/
1789 BF long_divmod
, /*nb_divmod*/
1790 TF long_pow
, /*nb_power*/
1791 UF long_neg
, /*nb_negative*/
1792 UF long_pos
, /*tp_positive*/
1793 UF long_abs
, /*tp_absolute*/
1794 IF long_nonzero
,/*tp_nonzero*/
1795 UF long_invert
, /*nb_invert*/
1796 BF long_lshift
, /*nb_lshift*/
1797 BF long_rshift
, /*nb_rshift*/
1798 BF long_and
, /*nb_and*/
1799 BF long_xor
, /*nb_xor*/
1800 BF long_or
, /*nb_or*/
1801 (int (*) Py_FPROTO((PyObject
**, PyObject
**)))
1802 (coercion
)long_coerce
, /*nb_coerce*/
1803 UF long_int
, /*nb_int*/
1804 UF long_long
, /*nb_long*/
1805 UF long_float
, /*nb_float*/
1806 UF long_oct
, /*nb_oct*/
1807 UF long_hex
, /*nb_hex*/
1810 PyTypeObject PyLong_Type
= {
1811 PyObject_HEAD_INIT(&PyType_Type
)
1814 sizeof(PyLongObject
) - sizeof(digit
),
1816 (destructor
)long_dealloc
, /*tp_dealloc*/
1820 (int (*) Py_FPROTO((PyObject
*, PyObject
*)))
1821 (cmpfunc
)long_compare
, /*tp_compare*/
1822 (reprfunc
)long_repr
, /*tp_repr*/
1823 &long_as_number
,/*tp_as_number*/
1824 0, /*tp_as_sequence*/
1825 0, /*tp_as_mapping*/
1826 (long (*) Py_FPROTO((PyObject
*)))
1827 (hashfunc
)long_hash
, /*tp_hash*/
1829 (reprfunc
)long_str
, /*tp_str*/