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
));
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
;
152 frac
= frexp(dval
, &expo
); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
154 return PyLong_FromLong(0L);
155 ndig
= (expo
-1) / SHIFT
+ 1; /* Number of 'digits' in result */
156 v
= _PyLong_New(ndig
);
159 frac
= ldexp(frac
, (expo
-1) % SHIFT
+ 1);
160 for (i
= ndig
; --i
>= 0; ) {
161 long bits
= (long)frac
;
162 v
->ob_digit
[i
] = (digit
) bits
;
163 frac
= frac
- (double)bits
;
164 frac
= ldexp(frac
, SHIFT
);
167 v
->ob_size
= -(v
->ob_size
);
168 return (PyObject
*)v
;
171 /* Get a C long int from a long int object.
172 Returns -1 and sets an error condition if overflow occurs. */
178 /* This version by Tim Peters */
179 register PyLongObject
*v
;
180 unsigned long x
, prev
;
183 if (vv
== NULL
|| !PyLong_Check(vv
)) {
184 PyErr_BadInternalCall();
187 v
= (PyLongObject
*)vv
;
197 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
198 if ((x
>> SHIFT
) != prev
)
201 /* Haven't lost any bits, but if the sign bit is set we're in
202 * trouble *unless* this is the min negative number. So,
203 * trouble iff sign bit set && (positive || some bit set other
204 * than the sign bit).
206 if ((long)x
< 0 && (sign
> 0 || (x
<< 1) != 0))
208 return (long)x
* sign
;
211 PyErr_SetString(PyExc_OverflowError
,
212 "long int too long to convert");
216 /* Get a C long int from a long int object.
217 Returns -1 and sets an error condition if overflow occurs. */
220 PyLong_AsUnsignedLong(vv
)
223 register PyLongObject
*v
;
224 unsigned long x
, prev
;
227 if (vv
== NULL
|| !PyLong_Check(vv
)) {
228 PyErr_BadInternalCall();
229 return (unsigned long) -1;
231 v
= (PyLongObject
*)vv
;
235 PyErr_SetString(PyExc_OverflowError
,
236 "can't convert negative value to unsigned long");
237 return (unsigned long) -1;
241 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
242 if ((x
>> SHIFT
) != prev
) {
243 PyErr_SetString(PyExc_OverflowError
,
244 "long int too long to convert");
245 return (unsigned long) -1;
251 /* Get a C double from a long int object. */
257 register PyLongObject
*v
;
259 double multiplier
= (double) (1L << SHIFT
);
262 if (vv
== NULL
|| !PyLong_Check(vv
)) {
263 PyErr_BadInternalCall();
266 v
= (PyLongObject
*)vv
;
275 x
= x
*multiplier
+ (double)v
->ob_digit
[i
];
280 #ifdef HAVE_LONG_LONG
282 * long long support by Chris Herborth (chrish@qnx.com)
284 * For better or worse :-), I tried to follow the coding style already
292 /* Hopefully this is portable... */
294 #define LONG_MAX 2147483647L
297 #define ULONG_MAX 4294967295U
300 #define LONGLONG_MAX 9223372036854775807LL
302 #ifndef ULONGLONG_MAX
303 #define ULONGLONG_MAX 0xffffffffffffffffULL
306 /* Create a new long int object from a C long long int */
309 PyLong_FromLongLong(ival
)
312 #if SIZEOF_LONG_LONG == SIZEOF_LONG
313 /* In case the compiler is faking it. */
314 return PyLong_FromLong( (long)ival
);
316 if( ival
<= (long long)LONG_MAX
) {
317 return PyLong_FromLong( (long)ival
);
319 else if( ival
<= (unsigned long long)ULONG_MAX
) {
320 return PyLong_FromUnsignedLong( (unsigned long)ival
);
323 /* Assume a C long long fits in at most 10 'digits'.
324 * Should be OK if we're assuming long fits in 5.
326 PyLongObject
*v
= _PyLong_New(10);
329 unsigned long long t
= ival
;
333 v
->ob_size
= -(v
->ob_size
);
336 for (i
= 0; i
< 10; i
++) {
337 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
341 v
= long_normalize(v
);
344 return (PyObject
*)v
;
347 /* If we got here, we're confused... */
348 PyErr_SetString( PyExc_ArithmeticError
, "invalid long integer" );
353 /* Create a new long int object from a C unsigned long long int */
355 PyLong_FromUnsignedLongLong(ival
)
356 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
;
383 /* If we got here, we're confused... */
384 PyErr_SetString( PyExc_ArithmeticError
, "invalid unsigned long integer" );
389 /* Get a C long long int from a long int object.
390 Returns -1 and sets an error condition if overflow occurs. */
393 PyLong_AsLongLong(vv
)
396 #if SIZEOF_LONG_LONG == SIZEOF_LONG
397 /* In case the compiler is faking it. */
398 return (long long)PyLong_AsLong( vv
);
400 register PyLongObject
*v
;
404 if (vv
== NULL
|| !PyLong_Check(vv
)) {
405 PyErr_BadInternalCall();
409 v
= (PyLongObject
*)vv
;
421 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
422 if ((x
>> SHIFT
) != prev
) {
423 PyErr_SetString(PyExc_OverflowError
,
424 "long int too long to convert");
434 PyLong_AsUnsignedLongLong(vv
)
437 #if SIZEOF_LONG_LONG == 4
438 /* In case the compiler is faking it. */
439 return (unsigned long long)PyLong_AsUnsignedLong( vv
);
441 register PyLongObject
*v
;
442 unsigned long long x
, prev
;
445 if (vv
== NULL
|| !PyLong_Check(vv
)) {
446 PyErr_BadInternalCall();
447 return (unsigned long long) -1;
450 v
= (PyLongObject
*)vv
;
455 PyErr_SetString(PyExc_OverflowError
,
456 "can't convert negative value to unsigned long");
457 return (unsigned long long) -1;
462 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
463 if ((x
>> SHIFT
) != prev
) {
464 PyErr_SetString(PyExc_OverflowError
,
465 "long int too long to convert");
466 return (unsigned long long) -1;
473 #endif /* HAVE_LONG_LONG */
475 /* Multiply by a single digit, ignoring the sign. */
477 static PyLongObject
*
482 return muladd1(a
, n
, (digit
)0);
485 /* Multiply by a single digit and add a single digit, ignoring the sign. */
487 static PyLongObject
*
493 int size_a
= ABS(a
->ob_size
);
494 PyLongObject
*z
= _PyLong_New(size_a
+1);
495 twodigits carry
= extra
;
500 for (i
= 0; i
< size_a
; ++i
) {
501 carry
+= (twodigits
)a
->ob_digit
[i
] * n
;
502 z
->ob_digit
[i
] = (digit
) (carry
& MASK
);
505 z
->ob_digit
[i
] = (digit
) carry
;
506 return long_normalize(z
);
509 /* Divide a long integer by a digit, returning both the quotient
510 (as function result) and the remainder (through *prem).
511 The sign of a is ignored; n should not be zero. */
513 static PyLongObject
*
519 int size
= ABS(a
->ob_size
);
524 assert(n
> 0 && n
<= MASK
);
525 z
= _PyLong_New(size
);
528 for (i
= size
; --i
>= 0; ) {
529 rem
= (rem
<< SHIFT
) + a
->ob_digit
[i
];
530 z
->ob_digit
[i
] = (digit
) (rem
/n
);
534 return long_normalize(z
);
537 /* Convert a long int object to a string, using a given conversion base.
538 Return a string object.
539 If base is 8 or 16, add the proper prefix '0' or '0x'.
540 External linkage: used in bltinmodule.c by hex() and oct(). */
543 long_format(aa
, base
)
547 register PyLongObject
*a
= (PyLongObject
*)aa
;
550 int size_a
= ABS(a
->ob_size
);
555 if (a
== NULL
|| !PyLong_Check(a
)) {
556 PyErr_BadInternalCall();
559 assert(base
>= 2 && base
<= 36);
561 /* Compute a rough upper bound for the length of the string */
568 i
= 6 + (size_a
*SHIFT
+ bits
-1) / bits
;
569 str
= (PyStringObject
*) PyString_FromStringAndSize((char *)0, i
);
572 p
= PyString_AS_STRING(str
) + i
;
578 if (a
->ob_size
== 0) {
581 else if ((base
& (base
- 1)) == 0) {
582 /* JRH: special case for power-of-2 bases */
583 twodigits temp
= a
->ob_digit
[0];
584 int bitsleft
= SHIFT
;
586 int last
= abs(a
->ob_size
);
589 while ((i
>>= 1) > 1) ++basebits
;
593 while (bitsleft
>= basebits
) {
594 if ((temp
== 0) && (i
>= last
- 1)) break;
595 rem
= temp
& (base
- 1);
600 assert(p
> PyString_AS_STRING(str
));
602 bitsleft
-= basebits
;
606 if (temp
== 0) break;
608 /* loop again to pick up final digits */
611 temp
= (a
->ob_digit
[i
] << bitsleft
) | temp
;
620 PyLongObject
*temp
= divrem1(a
, (digit
)base
, &rem
);
630 assert(p
> PyString_AS_STRING(str
));
639 } while (ABS(a
->ob_size
) != 0);
647 else if (base
== 16) {
651 else if (base
!= 10) {
653 *--p
= '0' + base
%10;
655 *--p
= '0' + base
/10;
659 if (p
!= PyString_AS_STRING(str
)) {
660 char *q
= PyString_AS_STRING(str
);
663 } while ((*q
++ = *p
++) != '\0');
665 _PyString_Resize((PyObject
**)&str
,
666 (int) (q
- PyString_AS_STRING(str
)));
668 return (PyObject
*)str
;
672 /* Convert a string to a long int object, in a given base.
673 Base zero implies a default depending on the number.
674 External linkage: used in compile.c and stropmodule.c. */
681 return PyLong_FromString(str
, (char **)NULL
, base
);
686 PyLong_FromString(str
, pend
, base
)
695 if ((base
!= 0 && base
< 2) || base
> 36) {
696 PyErr_SetString(PyExc_ValueError
,
697 "invalid base for long literal");
700 while (*str
!= '\0' && isspace(Py_CHARMASK(*str
)))
704 else if (*str
== '-') {
708 while (*str
!= '\0' && isspace(Py_CHARMASK(*str
)))
713 else if (str
[1] == 'x' || str
[1] == 'X')
718 if (base
== 16 && str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
722 for ( ; z
!= NULL
; ++str
) {
728 else if (*str
>= 'a')
730 else if (*str
>= 'A')
732 if (k
< 0 || k
>= base
)
734 temp
= muladd1(z
, (digit
)base
, (digit
)k
);
741 PyErr_SetString(PyExc_ValueError
,
742 "no digits in long int constant");
745 if (sign
< 0 && z
!= NULL
&& z
->ob_size
!= 0)
746 z
->ob_size
= -(z
->ob_size
);
749 return (PyObject
*) z
;
752 static PyLongObject
*x_divrem
753 Py_PROTO((PyLongObject
*, PyLongObject
*, PyLongObject
**));
754 static PyObject
*long_pos
Py_PROTO((PyLongObject
*));
755 static int long_divrem
Py_PROTO((PyLongObject
*, PyLongObject
*,
756 PyLongObject
**, PyLongObject
**));
758 /* Long division with remainder, top-level routine */
761 long_divrem(a
, b
, pdiv
, prem
)
766 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
770 PyErr_SetString(PyExc_ZeroDivisionError
,
771 "long division or modulo");
774 if (size_a
< size_b
||
776 a
->ob_digit
[size_a
-1] < b
->ob_digit
[size_b
-1])) {
778 *pdiv
= _PyLong_New(0);
780 *prem
= (PyLongObject
*) a
;
785 z
= divrem1(a
, b
->ob_digit
[0], &rem
);
788 *prem
= (PyLongObject
*) PyLong_FromLong((long)rem
);
791 z
= x_divrem(a
, b
, prem
);
796 The quotient z has the sign of a*b;
797 the remainder r has the sign of a,
799 if ((a
->ob_size
< 0) != (b
->ob_size
< 0))
800 z
->ob_size
= -(z
->ob_size
);
801 if (a
->ob_size
< 0 && (*prem
)->ob_size
!= 0)
802 (*prem
)->ob_size
= -((*prem
)->ob_size
);
807 /* Unsigned long division with remainder -- the algorithm */
809 static PyLongObject
*
810 x_divrem(v1
, w1
, prem
)
811 PyLongObject
*v1
, *w1
;
814 int size_v
= ABS(v1
->ob_size
), size_w
= ABS(w1
->ob_size
);
815 digit d
= (digit
) ((twodigits
)BASE
/ (w1
->ob_digit
[size_w
-1] + 1));
816 PyLongObject
*v
= mul1(v1
, d
);
817 PyLongObject
*w
= mul1(w1
, d
);
821 if (v
== NULL
|| w
== NULL
) {
827 assert(size_v
>= size_w
&& size_w
> 1); /* Assert checks by div() */
828 assert(v
->ob_refcnt
== 1); /* Since v will be used as accumulator! */
829 assert(size_w
== ABS(w
->ob_size
)); /* That's how d was calculated */
831 size_v
= ABS(v
->ob_size
);
832 a
= _PyLong_New(size_v
- size_w
+ 1);
834 for (j
= size_v
, k
= a
->ob_size
-1; a
!= NULL
&& k
>= 0; --j
, --k
) {
835 digit vj
= (j
>= size_v
) ? 0 : v
->ob_digit
[j
];
837 stwodigits carry
= 0;
845 if (vj
== w
->ob_digit
[size_w
-1])
848 q
= (((twodigits
)vj
<< SHIFT
) + v
->ob_digit
[j
-1]) /
849 w
->ob_digit
[size_w
-1];
851 while (w
->ob_digit
[size_w
-2]*q
>
853 ((twodigits
)vj
<< SHIFT
)
855 - q
*w
->ob_digit
[size_w
-1]
860 for (i
= 0; i
< size_w
&& i
+k
< size_v
; ++i
) {
861 twodigits z
= w
->ob_digit
[i
] * q
;
862 digit zz
= (digit
) (z
>> SHIFT
);
863 carry
+= v
->ob_digit
[i
+k
] - z
864 + ((twodigits
)zz
<< SHIFT
);
865 v
->ob_digit
[i
+k
] = carry
& MASK
;
866 carry
= (carry
>> SHIFT
) - zz
;
870 carry
+= v
->ob_digit
[i
+k
];
871 v
->ob_digit
[i
+k
] = 0;
875 a
->ob_digit
[k
] = (digit
) q
;
878 a
->ob_digit
[k
] = (digit
) q
-1;
880 for (i
= 0; i
< size_w
&& i
+k
< size_v
; ++i
) {
881 carry
+= v
->ob_digit
[i
+k
] + w
->ob_digit
[i
];
882 v
->ob_digit
[i
+k
] = carry
& MASK
;
891 a
= long_normalize(a
);
892 *prem
= divrem1(v
, d
, &d
);
893 /* d receives the (unused) remainder */
907 static void long_dealloc
Py_PROTO((PyObject
*));
908 static PyObject
*long_repr
Py_PROTO((PyObject
*));
909 static int long_compare
Py_PROTO((PyLongObject
*, PyLongObject
*));
910 static long long_hash
Py_PROTO((PyLongObject
*));
912 static PyObject
*long_add
Py_PROTO((PyLongObject
*, PyLongObject
*));
913 static PyObject
*long_sub
Py_PROTO((PyLongObject
*, PyLongObject
*));
914 static PyObject
*long_mul
Py_PROTO((PyLongObject
*, PyLongObject
*));
915 static PyObject
*long_div
Py_PROTO((PyLongObject
*, PyLongObject
*));
916 static PyObject
*long_mod
Py_PROTO((PyLongObject
*, PyLongObject
*));
917 static PyObject
*long_divmod
Py_PROTO((PyLongObject
*, PyLongObject
*));
918 static PyObject
*long_pow
919 Py_PROTO((PyLongObject
*, PyLongObject
*, PyLongObject
*));
920 static PyObject
*long_neg
Py_PROTO((PyLongObject
*));
921 static PyObject
*long_pos
Py_PROTO((PyLongObject
*));
922 static PyObject
*long_abs
Py_PROTO((PyLongObject
*));
923 static int long_nonzero
Py_PROTO((PyLongObject
*));
924 static PyObject
*long_invert
Py_PROTO((PyLongObject
*));
925 static PyObject
*long_lshift
Py_PROTO((PyLongObject
*, PyLongObject
*));
926 static PyObject
*long_rshift
Py_PROTO((PyLongObject
*, PyLongObject
*));
927 static PyObject
*long_and
Py_PROTO((PyLongObject
*, PyLongObject
*));
928 static PyObject
*long_xor
Py_PROTO((PyLongObject
*, PyLongObject
*));
929 static PyObject
*long_or
Py_PROTO((PyLongObject
*, PyLongObject
*));
942 return long_format(v
, 10);
951 if (a
->ob_size
!= b
->ob_size
) {
952 if (ABS(a
->ob_size
) == 0 && ABS(b
->ob_size
) == 0)
955 sign
= a
->ob_size
- b
->ob_size
;
958 int i
= ABS(a
->ob_size
);
959 while (--i
>= 0 && a
->ob_digit
[i
] == b
->ob_digit
[i
])
964 sign
= (int)a
->ob_digit
[i
] - (int)b
->ob_digit
[i
];
969 return sign
< 0 ? -1 : sign
> 0 ? 1 : 0;
979 /* This is designed so that Python ints and longs with the
980 same value hash to the same value, otherwise comparisons
981 of mapping keys will turn out weird */
990 /* Force a 32-bit circular shift */
991 x
= ((x
<< SHIFT
) & ~MASK
) | ((x
>> (32-SHIFT
)) & MASK
);
1001 /* Add the absolute values of two long integers. */
1003 static PyLongObject
*x_add
Py_PROTO((PyLongObject
*, PyLongObject
*));
1004 static PyLongObject
*
1006 PyLongObject
*a
, *b
;
1008 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
1013 /* Ensure a is the larger of the two: */
1014 if (size_a
< size_b
) {
1015 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1016 { int size_temp
= size_a
;
1018 size_b
= size_temp
; }
1020 z
= _PyLong_New(size_a
+1);
1023 for (i
= 0; i
< size_b
; ++i
) {
1024 carry
+= a
->ob_digit
[i
] + b
->ob_digit
[i
];
1025 z
->ob_digit
[i
] = carry
& MASK
;
1026 /* The following assumes unsigned shifts don't
1027 propagate the sign bit. */
1030 for (; i
< size_a
; ++i
) {
1031 carry
+= a
->ob_digit
[i
];
1032 z
->ob_digit
[i
] = carry
& MASK
;
1035 z
->ob_digit
[i
] = carry
;
1036 return long_normalize(z
);
1039 /* Subtract the absolute values of two integers. */
1041 static PyLongObject
*x_sub
Py_PROTO((PyLongObject
*, PyLongObject
*));
1042 static PyLongObject
*
1044 PyLongObject
*a
, *b
;
1046 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
1052 /* Ensure a is the larger of the two: */
1053 if (size_a
< size_b
) {
1055 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1056 { int size_temp
= size_a
;
1058 size_b
= size_temp
; }
1060 else if (size_a
== size_b
) {
1061 /* Find highest digit where a and b differ: */
1063 while (--i
>= 0 && a
->ob_digit
[i
] == b
->ob_digit
[i
])
1066 return _PyLong_New(0);
1067 if (a
->ob_digit
[i
] < b
->ob_digit
[i
]) {
1069 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1071 size_a
= size_b
= i
+1;
1073 z
= _PyLong_New(size_a
);
1076 for (i
= 0; i
< size_b
; ++i
) {
1077 /* The following assumes unsigned arithmetic
1078 works module 2**N for some N>SHIFT. */
1079 borrow
= a
->ob_digit
[i
] - b
->ob_digit
[i
] - borrow
;
1080 z
->ob_digit
[i
] = borrow
& MASK
;
1082 borrow
&= 1; /* Keep only one sign bit */
1084 for (; i
< size_a
; ++i
) {
1085 borrow
= a
->ob_digit
[i
] - borrow
;
1086 z
->ob_digit
[i
] = borrow
& MASK
;
1089 assert(borrow
== 0);
1091 z
->ob_size
= -(z
->ob_size
);
1092 return long_normalize(z
);
1102 if (a
->ob_size
< 0) {
1103 if (b
->ob_size
< 0) {
1105 if (z
!= NULL
&& z
->ob_size
!= 0)
1106 z
->ob_size
= -(z
->ob_size
);
1117 return (PyObject
*)z
;
1127 if (a
->ob_size
< 0) {
1132 if (z
!= NULL
&& z
->ob_size
!= 0)
1133 z
->ob_size
= -(z
->ob_size
);
1141 return (PyObject
*)z
;
1154 size_a
= ABS(a
->ob_size
);
1155 size_b
= ABS(b
->ob_size
);
1156 z
= _PyLong_New(size_a
+ size_b
);
1159 for (i
= 0; i
< z
->ob_size
; ++i
)
1161 for (i
= 0; i
< size_a
; ++i
) {
1162 twodigits carry
= 0;
1163 twodigits f
= a
->ob_digit
[i
];
1170 for (j
= 0; j
< size_b
; ++j
) {
1171 carry
+= z
->ob_digit
[i
+j
] + b
->ob_digit
[j
] * f
;
1172 z
->ob_digit
[i
+j
] = (digit
) (carry
& MASK
);
1175 for (; carry
!= 0; ++j
) {
1176 assert(i
+j
< z
->ob_size
);
1177 carry
+= z
->ob_digit
[i
+j
];
1178 z
->ob_digit
[i
+j
] = (digit
) (carry
& MASK
);
1183 z
->ob_size
= -(z
->ob_size
);
1185 z
->ob_size
= -(z
->ob_size
);
1186 return (PyObject
*) long_normalize(z
);
1189 /* The / and % operators are now defined in terms of divmod().
1190 The expression a mod b has the value a - b*floor(a/b).
1191 The long_divrem function gives the remainder after division of
1192 |a| by |b|, with the sign of a. This is also expressed
1193 as a - b*trunc(a/b), if trunc truncates towards zero.
1200 So, to get from rem to mod, we have to add b if a and b
1201 have different signs. We then subtract one from the 'div'
1202 part of the outcome to keep the invariant intact. */
1204 static int l_divmod
Py_PROTO((PyLongObject
*, PyLongObject
*,
1205 PyLongObject
**, PyLongObject
**));
1207 l_divmod(v
, w
, pdiv
, pmod
)
1210 PyLongObject
**pdiv
;
1211 PyLongObject
**pmod
;
1213 PyLongObject
*div
, *mod
;
1215 if (long_divrem(v
, w
, &div
, &mod
) < 0)
1217 if ((mod
->ob_size
< 0 && w
->ob_size
> 0) ||
1218 (mod
->ob_size
> 0 && w
->ob_size
< 0)) {
1221 temp
= (PyLongObject
*) long_add(mod
, w
);
1228 one
= (PyLongObject
*) PyLong_FromLong(1L);
1230 (temp
= (PyLongObject
*) long_sub(div
, one
)) == NULL
) {
1250 PyLongObject
*div
, *mod
;
1251 if (l_divmod(v
, w
, &div
, &mod
) < 0)
1254 return (PyObject
*)div
;
1262 PyLongObject
*div
, *mod
;
1263 if (l_divmod(v
, w
, &div
, &mod
) < 0)
1266 return (PyObject
*)mod
;
1275 PyLongObject
*div
, *mod
;
1276 if (l_divmod(v
, w
, &div
, &mod
) < 0)
1280 PyTuple_SetItem(z
, 0, (PyObject
*) div
);
1281 PyTuple_SetItem(z
, 1, (PyObject
*) mod
);
1296 PyLongObject
*z
, *div
, *mod
;
1299 size_b
= b
->ob_size
;
1301 PyErr_SetString(PyExc_ValueError
,
1302 "long integer to the negative power");
1305 z
= (PyLongObject
*)PyLong_FromLong(1L);
1307 for (i
= 0; i
< size_b
; ++i
) {
1308 digit bi
= b
->ob_digit
[i
];
1311 for (j
= 0; j
< SHIFT
; ++j
) {
1315 temp
= (PyLongObject
*)long_mul(z
, a
);
1317 if ((PyObject
*)c
!=Py_None
&& temp
!=NULL
) {
1318 l_divmod(temp
, c
, &div
, &mod
);
1328 if (bi
== 0 && i
+1 == size_b
)
1330 temp
= (PyLongObject
*)long_mul(a
, a
);
1332 if ((PyObject
*)c
!=Py_None
&& temp
!=NULL
) {
1333 l_divmod(temp
, c
, &div
, &mod
);
1345 if (a
== NULL
|| z
== NULL
)
1349 if ((PyObject
*)c
!=Py_None
&& z
!=NULL
) {
1350 l_divmod(z
, c
, &div
, &mod
);
1355 return (PyObject
*)z
;
1362 /* Implement ~x as -(x+1) */
1365 w
= (PyLongObject
*)PyLong_FromLong(1L);
1368 x
= (PyLongObject
*) long_add(v
, w
);
1372 if (x
->ob_size
!= 0)
1373 x
->ob_size
= -(x
->ob_size
);
1374 return (PyObject
*)x
;
1382 return (PyObject
*)v
;
1391 n
= ABS(v
->ob_size
);
1395 return (PyObject
*) v
;
1397 z
= _PyLong_New(ABS(n
));
1400 for (i
= 0; i
< n
; i
++)
1401 z
->ob_digit
[i
] = v
->ob_digit
[i
];
1402 z
->ob_size
= -(v
->ob_size
);
1403 return (PyObject
*)z
;
1414 return (PyObject
*)v
;
1422 return ABS(v
->ob_size
) != 0;
1432 int newsize
, wordshift
, loshift
, hishift
, i
, j
;
1433 digit lomask
, himask
;
1435 if (a
->ob_size
< 0) {
1436 /* Right shifting negative numbers is harder */
1437 PyLongObject
*a1
, *a2
, *a3
;
1438 a1
= (PyLongObject
*) long_invert(a
);
1439 if (a1
== NULL
) return NULL
;
1440 a2
= (PyLongObject
*) long_rshift(a1
, b
);
1442 if (a2
== NULL
) return NULL
;
1443 a3
= (PyLongObject
*) long_invert(a2
);
1445 return (PyObject
*) a3
;
1448 shiftby
= PyLong_AsLong((PyObject
*)b
);
1449 if (shiftby
== -1L && PyErr_Occurred())
1452 PyErr_SetString(PyExc_ValueError
, "negative shift count");
1455 wordshift
= shiftby
/ SHIFT
;
1456 newsize
= ABS(a
->ob_size
) - wordshift
;
1459 return (PyObject
*)z
;
1461 loshift
= shiftby
% SHIFT
;
1462 hishift
= SHIFT
- loshift
;
1463 lomask
= ((digit
)1 << hishift
) - 1;
1464 himask
= MASK
^ lomask
;
1465 z
= _PyLong_New(newsize
);
1469 z
->ob_size
= -(z
->ob_size
);
1470 for (i
= 0, j
= wordshift
; i
< newsize
; i
++, j
++) {
1471 z
->ob_digit
[i
] = (a
->ob_digit
[j
] >> loshift
) & lomask
;
1474 (a
->ob_digit
[j
+1] << hishift
) & himask
;
1476 return (PyObject
*) long_normalize(z
);
1484 /* This version due to Tim Peters */
1487 int oldsize
, newsize
, wordshift
, remshift
, i
, j
;
1490 shiftby
= PyLong_AsLong((PyObject
*)b
);
1491 if (shiftby
== -1L && PyErr_Occurred())
1494 PyErr_SetString(PyExc_ValueError
, "negative shift count");
1497 if ((long)(int)shiftby
!= shiftby
) {
1498 PyErr_SetString(PyExc_ValueError
,
1499 "outrageous left shift count");
1502 /* wordshift, remshift = divmod(shiftby, SHIFT) */
1503 wordshift
= (int)shiftby
/ SHIFT
;
1504 remshift
= (int)shiftby
- wordshift
* SHIFT
;
1506 oldsize
= ABS(a
->ob_size
);
1507 newsize
= oldsize
+ wordshift
;
1510 z
= _PyLong_New(newsize
);
1514 z
->ob_size
= -(z
->ob_size
);
1515 for (i
= 0; i
< wordshift
; i
++)
1518 for (i
= wordshift
, j
= 0; j
< oldsize
; i
++, j
++) {
1519 accum
|= a
->ob_digit
[j
] << remshift
;
1520 z
->ob_digit
[i
] = (digit
)(accum
& MASK
);
1524 z
->ob_digit
[newsize
-1] = (digit
)accum
;
1527 return (PyObject
*) long_normalize(z
);
1531 /* Bitwise and/xor/or operations */
1533 #define MAX(x, y) ((x) < (y) ? (y) : (x))
1534 #define MIN(x, y) ((x) > (y) ? (y) : (x))
1536 static PyObject
*long_bitwise
Py_PROTO((PyLongObject
*, int, PyLongObject
*));
1538 long_bitwise(a
, op
, b
)
1540 int op
; /* '&', '|', '^' */
1543 digit maska
, maskb
; /* 0 or MASK */
1545 int size_a
, size_b
, size_z
;
1551 if (a
->ob_size
< 0) {
1552 a
= (PyLongObject
*) long_invert(a
);
1559 if (b
->ob_size
< 0) {
1560 b
= (PyLongObject
*) long_invert(b
);
1571 if (maska
!= maskb
) {
1577 if (maska
&& maskb
) {
1585 if (maska
|| maskb
) {
1594 /* JRH: The original logic here was to allocate the result value (z)
1595 as the longer of the two operands. However, there are some cases
1596 where the result is guaranteed to be shorter than that: AND of two
1597 positives, OR of two negatives: use the shorter number. AND with
1598 mixed signs: use the positive number. OR with mixed signs: use the
1599 negative number. After the transformations above, op will be '&'
1600 iff one of these cases applies, and mask will be non-0 for operands
1601 whose length should be ignored.
1604 size_a
= a
->ob_size
;
1605 size_b
= b
->ob_size
;
1609 : (maskb
? size_a
: MIN(size_a
, size_b
)))
1610 : MAX(size_a
, size_b
);
1611 z
= _PyLong_New(size_z
);
1612 if (a
== NULL
|| b
== NULL
|| z
== NULL
) {
1619 for (i
= 0; i
< size_z
; ++i
) {
1620 diga
= (i
< size_a
? a
->ob_digit
[i
] : 0) ^ maska
;
1621 digb
= (i
< size_b
? b
->ob_digit
[i
] : 0) ^ maskb
;
1623 case '&': z
->ob_digit
[i
] = diga
& digb
; break;
1624 case '|': z
->ob_digit
[i
] = diga
| digb
; break;
1625 case '^': z
->ob_digit
[i
] = diga
^ digb
; break;
1631 z
= long_normalize(z
);
1633 return (PyObject
*) z
;
1644 return long_bitwise(a
, '&', b
);
1652 return long_bitwise(a
, '^', b
);
1660 return long_bitwise(a
, '|', b
);
1668 if (PyInt_Check(*pw
)) {
1669 *pw
= PyLong_FromLong(PyInt_AsLong(*pw
));
1673 return 1; /* Can't do it */
1681 x
= PyLong_AsLong(v
);
1682 if (PyErr_Occurred())
1684 return PyInt_FromLong(x
);
1700 PyFPE_START_PROTECT("long_float", return 0)
1701 result
= PyLong_AsDouble(v
);
1702 PyFPE_END_PROTECT(result
)
1703 return PyFloat_FromDouble(result
);
1710 return long_format(v
, 8);
1717 return long_format(v
, 16);
1721 #define UF (unaryfunc)
1722 #define BF (binaryfunc)
1723 #define TF (ternaryfunc)
1724 #define IF (inquiry)
1726 static PyNumberMethods long_as_number
= {
1727 BF long_add
, /*nb_add*/
1728 BF long_sub
, /*nb_subtract*/
1729 BF long_mul
, /*nb_multiply*/
1730 BF long_div
, /*nb_divide*/
1731 BF long_mod
, /*nb_remainder*/
1732 BF long_divmod
, /*nb_divmod*/
1733 TF long_pow
, /*nb_power*/
1734 UF long_neg
, /*nb_negative*/
1735 UF long_pos
, /*tp_positive*/
1736 UF long_abs
, /*tp_absolute*/
1737 IF long_nonzero
,/*tp_nonzero*/
1738 UF long_invert
, /*nb_invert*/
1739 BF long_lshift
, /*nb_lshift*/
1740 BF long_rshift
, /*nb_rshift*/
1741 BF long_and
, /*nb_and*/
1742 BF long_xor
, /*nb_xor*/
1743 BF long_or
, /*nb_or*/
1744 (int (*) Py_FPROTO((PyObject
**, PyObject
**)))
1745 (coercion
)long_coerce
, /*nb_coerce*/
1746 UF long_int
, /*nb_int*/
1747 UF long_long
, /*nb_long*/
1748 UF long_float
, /*nb_float*/
1749 UF long_oct
, /*nb_oct*/
1750 UF long_hex
, /*nb_hex*/
1753 PyTypeObject PyLong_Type
= {
1754 PyObject_HEAD_INIT(&PyType_Type
)
1757 sizeof(PyLongObject
) - sizeof(digit
),
1759 (destructor
)long_dealloc
, /*tp_dealloc*/
1763 (int (*) Py_FPROTO((PyObject
*, PyObject
*)))
1764 (cmpfunc
)long_compare
, /*tp_compare*/
1765 (reprfunc
)long_repr
, /*tp_repr*/
1766 &long_as_number
,/*tp_as_number*/
1767 0, /*tp_as_sequence*/
1768 0, /*tp_as_mapping*/
1769 (long (*) Py_FPROTO((PyObject
*)))
1770 (hashfunc
)long_hash
, /*tp_hash*/