2 /* Long (arbitrary precision) integer object implementation */
4 /* XXX The functional organization of this file is terrible */
7 #include "longintrepr.h"
12 #define ABS(x) ((x) < 0 ? -(x) : (x))
15 static PyLongObject
*long_normalize(PyLongObject
*);
16 static PyLongObject
*mul1(PyLongObject
*, wdigit
);
17 static PyLongObject
*muladd1(PyLongObject
*, wdigit
, wdigit
);
18 static PyLongObject
*divrem1(PyLongObject
*, wdigit
, digit
*);
19 static PyObject
*long_format(PyObject
*aa
, int base
, int addL
);
21 static int ticker
; /* XXX Could be shared with ceval? */
23 #define SIGCHECK(PyTryBlock) \
26 if (PyErr_CheckSignals()) { PyTryBlock; } \
29 /* Normalize (remove leading zeros from) a long int object.
30 Doesn't attempt to free the storage--in most cases, due to the nature
31 of the algorithms used, this could save at most be one word anyway. */
34 long_normalize(register PyLongObject
*v
)
36 int j
= ABS(v
->ob_size
);
39 while (i
> 0 && v
->ob_digit
[i
-1] == 0)
42 v
->ob_size
= (v
->ob_size
< 0) ? -(i
) : i
;
46 /* Allocate a new long int object with size digits.
47 Return NULL and set exception if we run out of memory. */
52 return PyObject_NEW_VAR(PyLongObject
, &PyLong_Type
, size
);
55 /* Create a new long int object from a C long int */
58 PyLong_FromLong(long ival
)
60 /* Assume a C long fits in at most 5 'digits' */
61 /* Works on both 32- and 64-bit machines */
62 PyLongObject
*v
= _PyLong_New(5);
64 unsigned long t
= ival
;
68 v
->ob_size
= -(v
->ob_size
);
70 for (i
= 0; i
< 5; i
++) {
71 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
74 v
= long_normalize(v
);
79 /* Create a new long int object from a C unsigned long int */
82 PyLong_FromUnsignedLong(unsigned long ival
)
84 /* Assume a C long fits in at most 5 'digits' */
85 /* Works on both 32- and 64-bit machines */
86 PyLongObject
*v
= _PyLong_New(5);
88 unsigned long t
= ival
;
90 for (i
= 0; i
< 5; i
++) {
91 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
94 v
= long_normalize(v
);
99 /* Create a new long int object from a C double */
102 PyLong_FromDouble(double dval
)
106 int i
, ndig
, expo
, neg
;
108 if (Py_IS_INFINITY(dval
)) {
109 PyErr_SetString(PyExc_OverflowError
,
110 "cannot convert float infinity to long");
117 frac
= frexp(dval
, &expo
); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
119 return PyLong_FromLong(0L);
120 ndig
= (expo
-1) / SHIFT
+ 1; /* Number of 'digits' in result */
121 v
= _PyLong_New(ndig
);
124 frac
= ldexp(frac
, (expo
-1) % SHIFT
+ 1);
125 for (i
= ndig
; --i
>= 0; ) {
126 long bits
= (long)frac
;
127 v
->ob_digit
[i
] = (digit
) bits
;
128 frac
= frac
- (double)bits
;
129 frac
= ldexp(frac
, SHIFT
);
132 v
->ob_size
= -(v
->ob_size
);
133 return (PyObject
*)v
;
136 /* Get a C long int from a long int object.
137 Returns -1 and sets an error condition if overflow occurs. */
140 PyLong_AsLong(PyObject
*vv
)
142 /* This version by Tim Peters */
143 register PyLongObject
*v
;
144 unsigned long x
, prev
;
147 if (vv
== NULL
|| !PyLong_Check(vv
)) {
148 PyErr_BadInternalCall();
151 v
= (PyLongObject
*)vv
;
161 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
162 if ((x
>> SHIFT
) != prev
)
165 /* Haven't lost any bits, but if the sign bit is set we're in
166 * trouble *unless* this is the min negative number. So,
167 * trouble iff sign bit set && (positive || some bit set other
168 * than the sign bit).
170 if ((long)x
< 0 && (sign
> 0 || (x
<< 1) != 0))
172 return (long)x
* sign
;
175 PyErr_SetString(PyExc_OverflowError
,
176 "long int too large to convert");
180 /* Get a C long int from a long int object.
181 Returns -1 and sets an error condition if overflow occurs. */
184 PyLong_AsUnsignedLong(PyObject
*vv
)
186 register PyLongObject
*v
;
187 unsigned long x
, prev
;
190 if (vv
== NULL
|| !PyLong_Check(vv
)) {
191 PyErr_BadInternalCall();
192 return (unsigned long) -1;
194 v
= (PyLongObject
*)vv
;
198 PyErr_SetString(PyExc_OverflowError
,
199 "can't convert negative value to unsigned long");
200 return (unsigned long) -1;
204 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
205 if ((x
>> SHIFT
) != prev
) {
206 PyErr_SetString(PyExc_OverflowError
,
207 "long int too large to convert");
208 return (unsigned long) -1;
214 /* Get a C double from a long int object. */
217 PyLong_AsDouble(PyObject
*vv
)
219 register PyLongObject
*v
;
221 double multiplier
= (double) (1L << SHIFT
);
224 if (vv
== NULL
|| !PyLong_Check(vv
)) {
225 PyErr_BadInternalCall();
228 v
= (PyLongObject
*)vv
;
237 x
= x
*multiplier
+ (double)v
->ob_digit
[i
];
242 /* Create a new long (or int) object from a C pointer */
245 PyLong_FromVoidPtr(void *p
)
247 #if SIZEOF_VOID_P == SIZEOF_LONG
248 return PyInt_FromLong((long)p
);
250 /* optimize null pointers */
252 return PyInt_FromLong(0);
254 /* we can assume that HAVE_LONG_LONG is true. if not, then the
255 configuration process should have bailed (having big pointers
256 without long longs seems non-sensical) */
257 return PyLong_FromLongLong((LONG_LONG
)p
);
258 #endif /* SIZEOF_VOID_P == SIZEOF_LONG */
261 /* Get a C pointer from a long object (or an int object in some cases) */
264 PyLong_AsVoidPtr(PyObject
*vv
)
266 /* This function will allow int or long objects. If vv is neither,
267 then the PyLong_AsLong*() functions will raise the exception:
268 PyExc_SystemError, "bad argument to internal function"
271 #if SIZEOF_VOID_P == SIZEOF_LONG
274 if ( PyInt_Check(vv
) )
275 x
= PyInt_AS_LONG(vv
);
277 x
= PyLong_AsLong(vv
);
279 /* we can assume that HAVE_LONG_LONG is true. if not, then the
280 configuration process should have bailed (having big pointers
281 without long longs seems non-sensical) */
284 if ( PyInt_Check(vv
) )
285 x
= PyInt_AS_LONG(vv
);
287 x
= PyLong_AsLongLong(vv
);
288 #endif /* SIZEOF_VOID_P == SIZEOF_LONG */
290 if (x
== -1 && PyErr_Occurred())
295 #ifdef HAVE_LONG_LONG
297 * LONG_LONG support by Chris Herborth (chrish@qnx.com)
299 * For better or worse :-), I tried to follow the coding style already
303 /* Create a new long int object from a C LONG_LONG int */
306 PyLong_FromLongLong(LONG_LONG ival
)
308 #if SIZEOF_LONG_LONG == SIZEOF_LONG
309 /* In case the compiler is faking it. */
310 return PyLong_FromLong( (long)ival
);
312 if ((LONG_LONG
)LONG_MIN
<= ival
&& ival
<= (LONG_LONG
)LONG_MAX
) {
313 return PyLong_FromLong( (long)ival
);
315 else if (0 <= ival
&& ival
<= (unsigned LONG_LONG
)ULONG_MAX
) {
316 return PyLong_FromUnsignedLong( (unsigned long)ival
);
319 /* Assume a C LONG_LONG fits in at most 10 'digits'.
320 * Should be OK if we're assuming long fits in 5.
322 PyLongObject
*v
= _PyLong_New(10);
325 unsigned LONG_LONG t
= ival
;
329 v
->ob_size
= -(v
->ob_size
);
332 for (i
= 0; i
< 10; i
++) {
333 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
337 v
= long_normalize(v
);
340 return (PyObject
*)v
;
345 /* Create a new long int object from a C unsigned LONG_LONG int */
347 PyLong_FromUnsignedLongLong(unsigned LONG_LONG ival
)
349 #if SIZEOF_LONG_LONG == SIZEOF_LONG
350 /* In case the compiler is faking it. */
351 return PyLong_FromUnsignedLong( (unsigned long)ival
);
353 if( ival
<= (unsigned LONG_LONG
)ULONG_MAX
) {
354 return PyLong_FromUnsignedLong( (unsigned long)ival
);
357 /* Assume a C long fits in at most 10 'digits'. */
358 PyLongObject
*v
= _PyLong_New(10);
361 unsigned LONG_LONG t
= ival
;
363 for (i
= 0; i
< 10; i
++) {
364 v
->ob_digit
[i
] = (digit
) (t
& MASK
);
368 v
= long_normalize(v
);
371 return (PyObject
*)v
;
376 /* Get a C LONG_LONG int from a long int object.
377 Returns -1 and sets an error condition if overflow occurs. */
380 PyLong_AsLongLong(PyObject
*vv
)
382 #if SIZEOF_LONG_LONG == SIZEOF_LONG
383 /* In case the compiler is faking it. */
384 return (LONG_LONG
)PyLong_AsLong( vv
);
386 register PyLongObject
*v
;
390 if (vv
== NULL
|| !PyLong_Check(vv
)) {
391 PyErr_BadInternalCall();
395 v
= (PyLongObject
*)vv
;
407 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
408 if ((x
>> SHIFT
) != prev
) {
409 PyErr_SetString(PyExc_OverflowError
,
410 "long int too long to convert");
420 PyLong_AsUnsignedLongLong(PyObject
*vv
)
422 #if SIZEOF_LONG_LONG == 4
423 /* In case the compiler is faking it. */
424 return (unsigned LONG_LONG
)PyLong_AsUnsignedLong( vv
);
426 register PyLongObject
*v
;
427 unsigned LONG_LONG x
, prev
;
430 if (vv
== NULL
|| !PyLong_Check(vv
)) {
431 PyErr_BadInternalCall();
432 return (unsigned LONG_LONG
) -1;
435 v
= (PyLongObject
*)vv
;
440 PyErr_SetString(PyExc_OverflowError
,
441 "can't convert negative value to unsigned long");
442 return (unsigned LONG_LONG
) -1;
447 x
= (x
<< SHIFT
) + v
->ob_digit
[i
];
448 if ((x
>> SHIFT
) != prev
) {
449 PyErr_SetString(PyExc_OverflowError
,
450 "long int too long to convert");
451 return (unsigned LONG_LONG
) -1;
458 #endif /* HAVE_LONG_LONG */
462 convert_binop(PyObject
*v
, PyObject
*w
, PyLongObject
**a
, PyLongObject
**b
) {
463 if (PyLong_Check(v
)) {
464 *a
= (PyLongObject
*) v
;
467 else if (PyInt_Check(v
)) {
468 *a
= (PyLongObject
*) PyLong_FromLong(PyInt_AS_LONG(v
));
473 if (PyLong_Check(w
)) {
474 *b
= (PyLongObject
*) w
;
477 else if (PyInt_Check(w
)) {
478 *b
= (PyLongObject
*) PyLong_FromLong(PyInt_AS_LONG(w
));
487 #define CONVERT_BINOP(v, w, a, b) \
488 if (!convert_binop(v, w, a, b)) { \
489 Py_INCREF(Py_NotImplemented); \
490 return Py_NotImplemented; \
494 /* Multiply by a single digit, ignoring the sign. */
496 static PyLongObject
*
497 mul1(PyLongObject
*a
, wdigit n
)
499 return muladd1(a
, n
, (digit
)0);
502 /* Multiply by a single digit and add a single digit, ignoring the sign. */
504 static PyLongObject
*
505 muladd1(PyLongObject
*a
, wdigit n
, wdigit extra
)
507 int size_a
= ABS(a
->ob_size
);
508 PyLongObject
*z
= _PyLong_New(size_a
+1);
509 twodigits carry
= extra
;
514 for (i
= 0; i
< size_a
; ++i
) {
515 carry
+= (twodigits
)a
->ob_digit
[i
] * n
;
516 z
->ob_digit
[i
] = (digit
) (carry
& MASK
);
519 z
->ob_digit
[i
] = (digit
) carry
;
520 return long_normalize(z
);
523 /* Divide a long integer by a digit, returning both the quotient
524 (as function result) and the remainder (through *prem).
525 The sign of a is ignored; n should not be zero. */
527 static PyLongObject
*
528 divrem1(PyLongObject
*a
, wdigit n
, digit
*prem
)
530 int size
= ABS(a
->ob_size
);
535 assert(n
> 0 && n
<= MASK
);
536 z
= _PyLong_New(size
);
539 for (i
= size
; --i
>= 0; ) {
540 rem
= (rem
<< SHIFT
) + a
->ob_digit
[i
];
541 z
->ob_digit
[i
] = (digit
) (rem
/n
);
545 return long_normalize(z
);
548 /* Convert a long int object to a string, using a given conversion base.
549 Return a string object.
550 If base is 8 or 16, add the proper prefix '0' or '0x'. */
553 long_format(PyObject
*aa
, int base
, int addL
)
555 register PyLongObject
*a
= (PyLongObject
*)aa
;
558 int size_a
= ABS(a
->ob_size
);
563 if (a
== NULL
|| !PyLong_Check(a
)) {
564 PyErr_BadInternalCall();
567 assert(base
>= 2 && base
<= 36);
569 /* Compute a rough upper bound for the length of the string */
576 i
= 5 + (addL
? 1 : 0) + (size_a
*SHIFT
+ bits
-1) / bits
;
577 str
= (PyStringObject
*) PyString_FromStringAndSize((char *)0, i
);
580 p
= PyString_AS_STRING(str
) + i
;
587 if (a
->ob_size
== 0) {
590 else if ((base
& (base
- 1)) == 0) {
591 /* JRH: special case for power-of-2 bases */
592 twodigits temp
= a
->ob_digit
[0];
593 int bitsleft
= SHIFT
;
595 int last
= abs(a
->ob_size
);
598 while ((i
>>= 1) > 1)
603 while (bitsleft
>= basebits
) {
604 if ((temp
== 0) && (i
>= last
- 1)) break;
605 rem
= temp
& (base
- 1);
610 assert(p
> PyString_AS_STRING(str
));
612 bitsleft
-= basebits
;
616 if (temp
== 0) break;
618 /* loop again to pick up final digits */
621 temp
= (a
->ob_digit
[i
] << bitsleft
) | temp
;
630 PyLongObject
*temp
= divrem1(a
, (digit
)base
, &rem
);
640 assert(p
> PyString_AS_STRING(str
));
649 } while (ABS(a
->ob_size
) != 0);
657 else if (base
== 16) {
661 else if (base
!= 10) {
663 *--p
= '0' + base
%10;
665 *--p
= '0' + base
/10;
669 if (p
!= PyString_AS_STRING(str
)) {
670 char *q
= PyString_AS_STRING(str
);
673 } while ((*q
++ = *p
++) != '\0');
675 _PyString_Resize((PyObject
**)&str
,
676 (int) (q
- PyString_AS_STRING(str
)));
678 return (PyObject
*)str
;
682 PyLong_FromString(char *str
, char **pend
, int base
)
685 char *start
, *orig_str
= str
;
688 if ((base
!= 0 && base
< 2) || base
> 36) {
689 PyErr_SetString(PyExc_ValueError
,
690 "long() arg 2 must be >= 2 and <= 36");
693 while (*str
!= '\0' && isspace(Py_CHARMASK(*str
)))
697 else if (*str
== '-') {
701 while (*str
!= '\0' && isspace(Py_CHARMASK(*str
)))
706 else if (str
[1] == 'x' || str
[1] == 'X')
711 if (base
== 16 && str
[0] == '0' && (str
[1] == 'x' || str
[1] == 'X'))
715 for ( ; z
!= NULL
; ++str
) {
721 else if (*str
>= 'a')
723 else if (*str
>= 'A')
725 if (k
< 0 || k
>= base
)
727 temp
= muladd1(z
, (digit
)base
, (digit
)k
);
735 if (sign
< 0 && z
!= NULL
&& z
->ob_size
!= 0)
736 z
->ob_size
= -(z
->ob_size
);
737 if (*str
== 'L' || *str
== 'l')
739 while (*str
&& isspace(Py_CHARMASK(*str
)))
745 return (PyObject
*) z
;
748 PyErr_Format(PyExc_ValueError
,
749 "invalid literal for long(): %.200s", orig_str
);
755 PyLong_FromUnicode(Py_UNICODE
*u
, int length
, int base
)
759 if (length
>= sizeof(buffer
)) {
760 PyErr_SetString(PyExc_ValueError
,
761 "long() literal too large to convert");
764 if (PyUnicode_EncodeDecimal(u
, length
, buffer
, NULL
))
767 return PyLong_FromString(buffer
, NULL
, base
);
771 static PyLongObject
*x_divrem
772 (PyLongObject
*, PyLongObject
*, PyLongObject
**);
773 static PyObject
*long_pos(PyLongObject
*);
774 static int long_divrem(PyLongObject
*, PyLongObject
*,
775 PyLongObject
**, PyLongObject
**);
777 /* Long division with remainder, top-level routine */
780 long_divrem(PyLongObject
*a
, PyLongObject
*b
,
781 PyLongObject
**pdiv
, PyLongObject
**prem
)
783 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
787 PyErr_SetString(PyExc_ZeroDivisionError
,
788 "long division or modulo by zero");
791 if (size_a
< size_b
||
793 a
->ob_digit
[size_a
-1] < b
->ob_digit
[size_b
-1])) {
795 *pdiv
= _PyLong_New(0);
797 *prem
= (PyLongObject
*) a
;
802 z
= divrem1(a
, b
->ob_digit
[0], &rem
);
805 *prem
= (PyLongObject
*) PyLong_FromLong((long)rem
);
808 z
= x_divrem(a
, b
, prem
);
813 The quotient z has the sign of a*b;
814 the remainder r has the sign of a,
816 if ((a
->ob_size
< 0) != (b
->ob_size
< 0))
817 z
->ob_size
= -(z
->ob_size
);
818 if (a
->ob_size
< 0 && (*prem
)->ob_size
!= 0)
819 (*prem
)->ob_size
= -((*prem
)->ob_size
);
824 /* Unsigned long division with remainder -- the algorithm */
826 static PyLongObject
*
827 x_divrem(PyLongObject
*v1
, PyLongObject
*w1
, PyLongObject
**prem
)
829 int size_v
= ABS(v1
->ob_size
), size_w
= ABS(w1
->ob_size
);
830 digit d
= (digit
) ((twodigits
)BASE
/ (w1
->ob_digit
[size_w
-1] + 1));
831 PyLongObject
*v
= mul1(v1
, d
);
832 PyLongObject
*w
= mul1(w1
, d
);
836 if (v
== NULL
|| w
== NULL
) {
842 assert(size_v
>= size_w
&& size_w
> 1); /* Assert checks by div() */
843 assert(v
->ob_refcnt
== 1); /* Since v will be used as accumulator! */
844 assert(size_w
== ABS(w
->ob_size
)); /* That's how d was calculated */
846 size_v
= ABS(v
->ob_size
);
847 a
= _PyLong_New(size_v
- size_w
+ 1);
849 for (j
= size_v
, k
= a
->ob_size
-1; a
!= NULL
&& k
>= 0; --j
, --k
) {
850 digit vj
= (j
>= size_v
) ? 0 : v
->ob_digit
[j
];
852 stwodigits carry
= 0;
860 if (vj
== w
->ob_digit
[size_w
-1])
863 q
= (((twodigits
)vj
<< SHIFT
) + v
->ob_digit
[j
-1]) /
864 w
->ob_digit
[size_w
-1];
866 while (w
->ob_digit
[size_w
-2]*q
>
868 ((twodigits
)vj
<< SHIFT
)
870 - q
*w
->ob_digit
[size_w
-1]
875 for (i
= 0; i
< size_w
&& i
+k
< size_v
; ++i
) {
876 twodigits z
= w
->ob_digit
[i
] * q
;
877 digit zz
= (digit
) (z
>> SHIFT
);
878 carry
+= v
->ob_digit
[i
+k
] - z
879 + ((twodigits
)zz
<< SHIFT
);
880 v
->ob_digit
[i
+k
] = carry
& MASK
;
881 carry
= Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE
,
887 carry
+= v
->ob_digit
[i
+k
];
888 v
->ob_digit
[i
+k
] = 0;
892 a
->ob_digit
[k
] = (digit
) q
;
895 a
->ob_digit
[k
] = (digit
) q
-1;
897 for (i
= 0; i
< size_w
&& i
+k
< size_v
; ++i
) {
898 carry
+= v
->ob_digit
[i
+k
] + w
->ob_digit
[i
];
899 v
->ob_digit
[i
+k
] = carry
& MASK
;
900 carry
= Py_ARITHMETIC_RIGHT_SHIFT(
910 a
= long_normalize(a
);
911 *prem
= divrem1(v
, d
, &d
);
912 /* d receives the (unused) remainder */
926 long_dealloc(PyObject
*v
)
932 long_repr(PyObject
*v
)
934 return long_format(v
, 10, 1);
938 long_str(PyObject
*v
)
940 return long_format(v
, 10, 0);
944 long_compare(PyLongObject
*a
, PyLongObject
*b
)
948 if (a
->ob_size
!= b
->ob_size
) {
949 if (ABS(a
->ob_size
) == 0 && ABS(b
->ob_size
) == 0)
952 sign
= a
->ob_size
- b
->ob_size
;
955 int i
= ABS(a
->ob_size
);
956 while (--i
>= 0 && a
->ob_digit
[i
] == b
->ob_digit
[i
])
961 sign
= (int)a
->ob_digit
[i
] - (int)b
->ob_digit
[i
];
966 return sign
< 0 ? -1 : sign
> 0 ? 1 : 0;
970 long_hash(PyLongObject
*v
)
975 /* This is designed so that Python ints and longs with the
976 same value hash to the same value, otherwise comparisons
977 of mapping keys will turn out weird */
986 /* Force a 32-bit circular shift */
987 x
= ((x
<< SHIFT
) & ~MASK
) | ((x
>> (32-SHIFT
)) & MASK
);
997 /* Add the absolute values of two long integers. */
999 static PyLongObject
*
1000 x_add(PyLongObject
*a
, PyLongObject
*b
)
1002 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
1007 /* Ensure a is the larger of the two: */
1008 if (size_a
< size_b
) {
1009 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1010 { int size_temp
= size_a
;
1012 size_b
= size_temp
; }
1014 z
= _PyLong_New(size_a
+1);
1017 for (i
= 0; i
< size_b
; ++i
) {
1018 carry
+= a
->ob_digit
[i
] + b
->ob_digit
[i
];
1019 z
->ob_digit
[i
] = carry
& MASK
;
1022 for (; i
< size_a
; ++i
) {
1023 carry
+= a
->ob_digit
[i
];
1024 z
->ob_digit
[i
] = carry
& MASK
;
1027 z
->ob_digit
[i
] = carry
;
1028 return long_normalize(z
);
1031 /* Subtract the absolute values of two integers. */
1033 static PyLongObject
*
1034 x_sub(PyLongObject
*a
, PyLongObject
*b
)
1036 int size_a
= ABS(a
->ob_size
), size_b
= ABS(b
->ob_size
);
1042 /* Ensure a is the larger of the two: */
1043 if (size_a
< size_b
) {
1045 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1046 { int size_temp
= size_a
;
1048 size_b
= size_temp
; }
1050 else if (size_a
== size_b
) {
1051 /* Find highest digit where a and b differ: */
1053 while (--i
>= 0 && a
->ob_digit
[i
] == b
->ob_digit
[i
])
1056 return _PyLong_New(0);
1057 if (a
->ob_digit
[i
] < b
->ob_digit
[i
]) {
1059 { PyLongObject
*temp
= a
; a
= b
; b
= temp
; }
1061 size_a
= size_b
= i
+1;
1063 z
= _PyLong_New(size_a
);
1066 for (i
= 0; i
< size_b
; ++i
) {
1067 /* The following assumes unsigned arithmetic
1068 works module 2**N for some N>SHIFT. */
1069 borrow
= a
->ob_digit
[i
] - b
->ob_digit
[i
] - borrow
;
1070 z
->ob_digit
[i
] = borrow
& MASK
;
1072 borrow
&= 1; /* Keep only one sign bit */
1074 for (; i
< size_a
; ++i
) {
1075 borrow
= a
->ob_digit
[i
] - borrow
;
1076 z
->ob_digit
[i
] = borrow
& MASK
;
1078 borrow
&= 1; /* Keep only one sign bit */
1080 assert(borrow
== 0);
1082 z
->ob_size
= -(z
->ob_size
);
1083 return long_normalize(z
);
1087 long_add(PyLongObject
*v
, PyLongObject
*w
)
1089 PyLongObject
*a
, *b
, *z
;
1091 CONVERT_BINOP((PyObject
*)v
, (PyObject
*)w
, &a
, &b
);
1093 if (a
->ob_size
< 0) {
1094 if (b
->ob_size
< 0) {
1096 if (z
!= NULL
&& z
->ob_size
!= 0)
1097 z
->ob_size
= -(z
->ob_size
);
1110 return (PyObject
*)z
;
1114 long_sub(PyLongObject
*v
, PyLongObject
*w
)
1116 PyLongObject
*a
, *b
, *z
;
1118 CONVERT_BINOP((PyObject
*)v
, (PyObject
*)w
, &a
, &b
);
1120 if (a
->ob_size
< 0) {
1125 if (z
!= NULL
&& z
->ob_size
!= 0)
1126 z
->ob_size
= -(z
->ob_size
);
1136 return (PyObject
*)z
;
1140 long_repeat(PyObject
*v
, PyLongObject
*w
)
1142 /* sequence * long */
1143 long n
= PyLong_AsLong((PyObject
*) w
);
1144 if (n
== -1 && PyErr_Occurred())
1147 return (*v
->ob_type
->tp_as_sequence
->sq_repeat
)(v
, n
);
1151 long_mul(PyLongObject
*v
, PyLongObject
*w
)
1153 PyLongObject
*a
, *b
, *z
;
1158 if (v
->ob_type
->tp_as_sequence
&&
1159 v
->ob_type
->tp_as_sequence
->sq_repeat
) {
1160 return long_repeat((PyObject
*)v
, w
);
1162 else if (w
->ob_type
->tp_as_sequence
&&
1163 w
->ob_type
->tp_as_sequence
->sq_repeat
) {
1164 return long_repeat((PyObject
*)w
, v
);
1167 CONVERT_BINOP((PyObject
*)v
, (PyObject
*)w
, &a
, &b
);
1169 size_a
= ABS(a
->ob_size
);
1170 size_b
= ABS(b
->ob_size
);
1171 if (size_a
> size_b
) {
1172 /* we are faster with the small object on the left */
1173 int hold_sa
= size_a
;
1174 PyLongObject
*hold_a
= a
;
1180 z
= _PyLong_New(size_a
+ size_b
);
1186 for (i
= 0; i
< z
->ob_size
; ++i
)
1188 for (i
= 0; i
< size_a
; ++i
) {
1189 twodigits carry
= 0;
1190 twodigits f
= a
->ob_digit
[i
];
1199 for (j
= 0; j
< size_b
; ++j
) {
1200 carry
+= z
->ob_digit
[i
+j
] + b
->ob_digit
[j
] * f
;
1201 z
->ob_digit
[i
+j
] = (digit
) (carry
& MASK
);
1204 for (; carry
!= 0; ++j
) {
1205 assert(i
+j
< z
->ob_size
);
1206 carry
+= z
->ob_digit
[i
+j
];
1207 z
->ob_digit
[i
+j
] = (digit
) (carry
& MASK
);
1212 z
->ob_size
= -(z
->ob_size
);
1214 z
->ob_size
= -(z
->ob_size
);
1217 return (PyObject
*) long_normalize(z
);
1220 /* The / and % operators are now defined in terms of divmod().
1221 The expression a mod b has the value a - b*floor(a/b).
1222 The long_divrem function gives the remainder after division of
1223 |a| by |b|, with the sign of a. This is also expressed
1224 as a - b*trunc(a/b), if trunc truncates towards zero.
1231 So, to get from rem to mod, we have to add b if a and b
1232 have different signs. We then subtract one from the 'div'
1233 part of the outcome to keep the invariant intact. */
1236 l_divmod(PyLongObject
*v
, PyLongObject
*w
,
1237 PyLongObject
**pdiv
, PyLongObject
**pmod
)
1239 PyLongObject
*div
, *mod
;
1241 if (long_divrem(v
, w
, &div
, &mod
) < 0)
1243 if ((mod
->ob_size
< 0 && w
->ob_size
> 0) ||
1244 (mod
->ob_size
> 0 && w
->ob_size
< 0)) {
1247 temp
= (PyLongObject
*) long_add(mod
, w
);
1254 one
= (PyLongObject
*) PyLong_FromLong(1L);
1256 (temp
= (PyLongObject
*) long_sub(div
, one
)) == NULL
) {
1272 long_div(PyObject
*v
, PyObject
*w
)
1274 PyLongObject
*a
, *b
, *div
, *mod
;
1276 CONVERT_BINOP(v
, w
, &a
, &b
);
1278 if (l_divmod(a
, b
, &div
, &mod
) < 0) {
1286 return (PyObject
*)div
;
1290 long_mod(PyObject
*v
, PyObject
*w
)
1292 PyLongObject
*a
, *b
, *div
, *mod
;
1294 CONVERT_BINOP(v
, w
, &a
, &b
);
1296 if (l_divmod(a
, b
, &div
, &mod
) < 0) {
1304 return (PyObject
*)mod
;
1308 long_divmod(PyObject
*v
, PyObject
*w
)
1310 PyLongObject
*a
, *b
, *div
, *mod
;
1313 CONVERT_BINOP(v
, w
, &a
, &b
);
1315 if (l_divmod(a
, b
, &div
, &mod
) < 0) {
1322 PyTuple_SetItem(z
, 0, (PyObject
*) div
);
1323 PyTuple_SetItem(z
, 1, (PyObject
*) mod
);
1335 long_pow(PyObject
*v
, PyObject
*w
, PyObject
*x
)
1337 PyLongObject
*a
, *b
;
1339 PyLongObject
*z
, *div
, *mod
;
1342 CONVERT_BINOP(v
, w
, &a
, &b
);
1343 if (PyLong_Check(x
) || Py_None
== x
) {
1347 else if (PyInt_Check(x
)) {
1348 c
= PyLong_FromLong(PyInt_AS_LONG(x
));
1353 Py_INCREF(Py_NotImplemented
);
1354 return Py_NotImplemented
;
1357 size_b
= b
->ob_size
;
1360 PyErr_SetString(PyExc_ValueError
,
1361 "long integer to a negative power");
1363 PyErr_SetString(PyExc_ZeroDivisionError
,
1364 "zero to a negative power");
1368 z
= (PyLongObject
*)PyLong_FromLong(1L);
1369 for (i
= 0; i
< size_b
; ++i
) {
1370 digit bi
= b
->ob_digit
[i
];
1373 for (j
= 0; j
< SHIFT
; ++j
) {
1377 temp
= (PyLongObject
*)long_mul(z
, a
);
1379 if (c
!=Py_None
&& temp
!=NULL
) {
1380 if (l_divmod(temp
,(PyLongObject
*)c
,
1395 if (bi
== 0 && i
+1 == size_b
)
1397 temp
= (PyLongObject
*)long_mul(a
, a
);
1399 if (c
!=Py_None
&& temp
!=NULL
) {
1400 if (l_divmod(temp
, (PyLongObject
*)c
, &div
,
1417 if (a
== NULL
|| z
== NULL
)
1420 if (c
!=Py_None
&& z
!=NULL
) {
1421 if (l_divmod(z
, (PyLongObject
*)c
, &div
, &mod
) < 0) {
1435 return (PyObject
*)z
;
1439 long_invert(PyLongObject
*v
)
1441 /* Implement ~x as -(x+1) */
1444 w
= (PyLongObject
*)PyLong_FromLong(1L);
1447 x
= (PyLongObject
*) long_add(v
, w
);
1451 if (x
->ob_size
!= 0)
1452 x
->ob_size
= -(x
->ob_size
);
1453 return (PyObject
*)x
;
1457 long_pos(PyLongObject
*v
)
1460 return (PyObject
*)v
;
1464 long_neg(PyLongObject
*v
)
1468 n
= ABS(v
->ob_size
);
1472 return (PyObject
*) v
;
1474 z
= _PyLong_New(ABS(n
));
1477 for (i
= 0; i
< n
; i
++)
1478 z
->ob_digit
[i
] = v
->ob_digit
[i
];
1479 z
->ob_size
= -(v
->ob_size
);
1480 return (PyObject
*)z
;
1484 long_abs(PyLongObject
*v
)
1490 return (PyObject
*)v
;
1495 long_nonzero(PyLongObject
*v
)
1497 return ABS(v
->ob_size
) != 0;
1501 long_rshift(PyLongObject
*v
, PyLongObject
*w
)
1503 PyLongObject
*a
, *b
;
1504 PyLongObject
*z
= NULL
;
1506 int newsize
, wordshift
, loshift
, hishift
, i
, j
;
1507 digit lomask
, himask
;
1509 CONVERT_BINOP((PyObject
*)v
, (PyObject
*)w
, &a
, &b
);
1511 if (a
->ob_size
< 0) {
1512 /* Right shifting negative numbers is harder */
1513 PyLongObject
*a1
, *a2
;
1514 a1
= (PyLongObject
*) long_invert(a
);
1517 a2
= (PyLongObject
*) long_rshift(a1
, b
);
1521 z
= (PyLongObject
*) long_invert(a2
);
1526 shiftby
= PyLong_AsLong((PyObject
*)b
);
1527 if (shiftby
== -1L && PyErr_Occurred())
1530 PyErr_SetString(PyExc_ValueError
,
1531 "negative shift count");
1534 wordshift
= shiftby
/ SHIFT
;
1535 newsize
= ABS(a
->ob_size
) - wordshift
;
1540 return (PyObject
*)z
;
1542 loshift
= shiftby
% SHIFT
;
1543 hishift
= SHIFT
- loshift
;
1544 lomask
= ((digit
)1 << hishift
) - 1;
1545 himask
= MASK
^ lomask
;
1546 z
= _PyLong_New(newsize
);
1550 z
->ob_size
= -(z
->ob_size
);
1551 for (i
= 0, j
= wordshift
; i
< newsize
; i
++, j
++) {
1552 z
->ob_digit
[i
] = (a
->ob_digit
[j
] >> loshift
) & lomask
;
1555 (a
->ob_digit
[j
+1] << hishift
) & himask
;
1557 z
= long_normalize(z
);
1562 return (PyObject
*) z
;
1567 long_lshift(PyObject
*v
, PyObject
*w
)
1569 /* This version due to Tim Peters */
1570 PyLongObject
*a
, *b
;
1571 PyLongObject
*z
= NULL
;
1573 int oldsize
, newsize
, wordshift
, remshift
, i
, j
;
1576 CONVERT_BINOP(v
, w
, &a
, &b
);
1578 shiftby
= PyLong_AsLong((PyObject
*)b
);
1579 if (shiftby
== -1L && PyErr_Occurred())
1582 PyErr_SetString(PyExc_ValueError
, "negative shift count");
1585 if ((long)(int)shiftby
!= shiftby
) {
1586 PyErr_SetString(PyExc_ValueError
,
1587 "outrageous left shift count");
1590 /* wordshift, remshift = divmod(shiftby, SHIFT) */
1591 wordshift
= (int)shiftby
/ SHIFT
;
1592 remshift
= (int)shiftby
- wordshift
* SHIFT
;
1594 oldsize
= ABS(a
->ob_size
);
1595 newsize
= oldsize
+ wordshift
;
1598 z
= _PyLong_New(newsize
);
1602 z
->ob_size
= -(z
->ob_size
);
1603 for (i
= 0; i
< wordshift
; i
++)
1606 for (i
= wordshift
, j
= 0; j
< oldsize
; i
++, j
++) {
1607 accum
|= a
->ob_digit
[j
] << remshift
;
1608 z
->ob_digit
[i
] = (digit
)(accum
& MASK
);
1612 z
->ob_digit
[newsize
-1] = (digit
)accum
;
1615 z
= long_normalize(z
);
1619 return (PyObject
*) z
;
1623 /* Bitwise and/xor/or operations */
1625 #define MAX(x, y) ((x) < (y) ? (y) : (x))
1626 #define MIN(x, y) ((x) > (y) ? (y) : (x))
1629 long_bitwise(PyLongObject
*a
,
1630 int op
, /* '&', '|', '^' */
1633 digit maska
, maskb
; /* 0 or MASK */
1635 int size_a
, size_b
, size_z
;
1641 if (a
->ob_size
< 0) {
1642 a
= (PyLongObject
*) long_invert(a
);
1649 if (b
->ob_size
< 0) {
1650 b
= (PyLongObject
*) long_invert(b
);
1661 if (maska
!= maskb
) {
1667 if (maska
&& maskb
) {
1675 if (maska
|| maskb
) {
1684 /* JRH: The original logic here was to allocate the result value (z)
1685 as the longer of the two operands. However, there are some cases
1686 where the result is guaranteed to be shorter than that: AND of two
1687 positives, OR of two negatives: use the shorter number. AND with
1688 mixed signs: use the positive number. OR with mixed signs: use the
1689 negative number. After the transformations above, op will be '&'
1690 iff one of these cases applies, and mask will be non-0 for operands
1691 whose length should be ignored.
1694 size_a
= a
->ob_size
;
1695 size_b
= b
->ob_size
;
1699 : (maskb
? size_a
: MIN(size_a
, size_b
)))
1700 : MAX(size_a
, size_b
);
1701 z
= _PyLong_New(size_z
);
1702 if (a
== NULL
|| b
== NULL
|| z
== NULL
) {
1709 for (i
= 0; i
< size_z
; ++i
) {
1710 diga
= (i
< size_a
? a
->ob_digit
[i
] : 0) ^ maska
;
1711 digb
= (i
< size_b
? b
->ob_digit
[i
] : 0) ^ maskb
;
1713 case '&': z
->ob_digit
[i
] = diga
& digb
; break;
1714 case '|': z
->ob_digit
[i
] = diga
| digb
; break;
1715 case '^': z
->ob_digit
[i
] = diga
^ digb
; break;
1721 z
= long_normalize(z
);
1723 return (PyObject
*) z
;
1730 long_and(PyObject
*v
, PyObject
*w
)
1732 PyLongObject
*a
, *b
;
1734 CONVERT_BINOP(v
, w
, &a
, &b
);
1735 c
= long_bitwise(a
, '&', b
);
1742 long_xor(PyObject
*v
, PyObject
*w
)
1744 PyLongObject
*a
, *b
;
1746 CONVERT_BINOP(v
, w
, &a
, &b
);
1747 c
= long_bitwise(a
, '^', b
);
1754 long_or(PyObject
*v
, PyObject
*w
)
1756 PyLongObject
*a
, *b
;
1758 CONVERT_BINOP(v
, w
, &a
, &b
);
1759 c
= long_bitwise(a
, '|', b
);
1766 long_coerce(PyObject
**pv
, PyObject
**pw
)
1768 if (PyInt_Check(*pw
)) {
1769 *pw
= PyLong_FromLong(PyInt_AS_LONG(*pw
));
1773 return 1; /* Can't do it */
1777 long_int(PyObject
*v
)
1780 x
= PyLong_AsLong(v
);
1781 if (PyErr_Occurred())
1783 return PyInt_FromLong(x
);
1787 long_long(PyObject
*v
)
1794 long_float(PyObject
*v
)
1797 PyFPE_START_PROTECT("long_float", return 0)
1798 result
= PyLong_AsDouble(v
);
1799 PyFPE_END_PROTECT(result
)
1800 return PyFloat_FromDouble(result
);
1804 long_oct(PyObject
*v
)
1806 return long_format(v
, 8, 1);
1810 long_hex(PyObject
*v
)
1812 return long_format(v
, 16, 1);
1815 static PyNumberMethods long_as_number
= {
1816 (binaryfunc
) long_add
, /*nb_add*/
1817 (binaryfunc
) long_sub
, /*nb_subtract*/
1818 (binaryfunc
) long_mul
, /*nb_multiply*/
1819 (binaryfunc
) long_div
, /*nb_divide*/
1820 (binaryfunc
) long_mod
, /*nb_remainder*/
1821 (binaryfunc
) long_divmod
, /*nb_divmod*/
1822 (ternaryfunc
) long_pow
, /*nb_power*/
1823 (unaryfunc
) long_neg
, /*nb_negative*/
1824 (unaryfunc
) long_pos
, /*tp_positive*/
1825 (unaryfunc
) long_abs
, /*tp_absolute*/
1826 (inquiry
) long_nonzero
, /*tp_nonzero*/
1827 (unaryfunc
) long_invert
, /*nb_invert*/
1828 (binaryfunc
) long_lshift
, /*nb_lshift*/
1829 (binaryfunc
) long_rshift
, /*nb_rshift*/
1830 (binaryfunc
) long_and
, /*nb_and*/
1831 (binaryfunc
) long_xor
, /*nb_xor*/
1832 (binaryfunc
) long_or
, /*nb_or*/
1833 (coercion
) long_coerce
, /*nb_coerce*/
1834 (unaryfunc
) long_int
, /*nb_int*/
1835 (unaryfunc
) long_long
, /*nb_long*/
1836 (unaryfunc
) long_float
, /*nb_float*/
1837 (unaryfunc
) long_oct
, /*nb_oct*/
1838 (unaryfunc
) long_hex
, /*nb_hex*/
1839 0, /*nb_inplace_add*/
1840 0, /*nb_inplace_subtract*/
1841 0, /*nb_inplace_multiply*/
1842 0, /*nb_inplace_divide*/
1843 0, /*nb_inplace_remainder*/
1844 0, /*nb_inplace_power*/
1845 0, /*nb_inplace_lshift*/
1846 0, /*nb_inplace_rshift*/
1847 0, /*nb_inplace_and*/
1848 0, /*nb_inplace_xor*/
1849 0, /*nb_inplace_or*/
1852 PyTypeObject PyLong_Type
= {
1853 PyObject_HEAD_INIT(&PyType_Type
)
1856 sizeof(PyLongObject
) - sizeof(digit
),
1858 (destructor
)long_dealloc
, /*tp_dealloc*/
1862 (cmpfunc
)long_compare
, /*tp_compare*/
1863 (reprfunc
)long_repr
, /*tp_repr*/
1864 &long_as_number
, /*tp_as_number*/
1865 0, /*tp_as_sequence*/
1866 0, /*tp_as_mapping*/
1867 (hashfunc
)long_hash
, /*tp_hash*/
1869 (reprfunc
)long_str
, /*tp_str*/
1873 Py_TPFLAGS_CHECKTYPES
/*tp_flags*/