PageRenderTime 816ms CodeModel.GetById 52ms app.highlight 609ms RepoModel.GetById 15ms app.codeStats 6ms

/Objects/longobject.c

http://unladen-swallow.googlecode.com/
C | 3590 lines | 2711 code | 340 blank | 539 comment | 728 complexity | 251be7320969b50e535e9d86a3bcbd95 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1
   2
   3/* Long (arbitrary precision) integer object implementation */
   4
   5/* XXX The functional organization of this file is terrible */
   6
   7#include "Python.h"
   8#include "longintrepr.h"
   9
  10#include <ctype.h>
  11
  12/* For long multiplication, use the O(N**2) school algorithm unless
  13 * both operands contain more than KARATSUBA_CUTOFF digits (this
  14 * being an internal Python long digit, in base PyLong_BASE).
  15 */
  16#define KARATSUBA_CUTOFF 70
  17#define KARATSUBA_SQUARE_CUTOFF (2 * KARATSUBA_CUTOFF)
  18
  19/* For exponentiation, use the binary left-to-right algorithm
  20 * unless the exponent contains more than FIVEARY_CUTOFF digits.
  21 * In that case, do 5 bits at a time.  The potential drawback is that
  22 * a table of 2**5 intermediate results is computed.
  23 */
  24#define FIVEARY_CUTOFF 8
  25
  26#define ABS(x) ((x) < 0 ? -(x) : (x))
  27
  28#undef MIN
  29#undef MAX
  30#define MAX(x, y) ((x) < (y) ? (y) : (x))
  31#define MIN(x, y) ((x) > (y) ? (y) : (x))
  32
  33/* Forward */
  34static PyLongObject *long_normalize(PyLongObject *);
  35static PyLongObject *mul1(PyLongObject *, wdigit);
  36static PyLongObject *muladd1(PyLongObject *, wdigit, wdigit);
  37static PyLongObject *divrem1(PyLongObject *, digit, digit *);
  38
  39#define SIGCHECK(PyTryBlock) \
  40	if (--_Py_Ticker < 0) { \
  41		_Py_Ticker = _Py_CheckInterval; \
  42		if (PyErr_CheckSignals()) PyTryBlock \
  43	}
  44
  45/* Normalize (remove leading zeros from) a long int object.
  46   Doesn't attempt to free the storage--in most cases, due to the nature
  47   of the algorithms used, this could save at most be one word anyway. */
  48
  49static PyLongObject *
  50long_normalize(register PyLongObject *v)
  51{
  52	Py_ssize_t j = ABS(Py_SIZE(v));
  53	Py_ssize_t i = j;
  54
  55	while (i > 0 && v->ob_digit[i-1] == 0)
  56		--i;
  57	if (i != j)
  58		Py_SIZE(v) = (Py_SIZE(v) < 0) ? -(i) : i;
  59	return v;
  60}
  61
  62/* Allocate a new long int object with size digits.
  63   Return NULL and set exception if we run out of memory. */
  64
  65PyLongObject *
  66_PyLong_New(Py_ssize_t size)
  67{
  68	if (size > PY_SSIZE_T_MAX) {
  69		PyErr_NoMemory();
  70		return NULL;
  71	}
  72	/* coverity[ampersand_in_size] */
  73	/* XXX(nnorwitz): This can overflow --
  74           PyObject_NEW_VAR / _PyObject_VAR_SIZE need to detect overflow */
  75	return PyObject_NEW_VAR(PyLongObject, &PyLong_Type, size);
  76}
  77
  78PyObject *
  79_PyLong_Copy(PyLongObject *src)
  80{
  81	PyLongObject *result;
  82	Py_ssize_t i;
  83
  84	assert(src != NULL);
  85	i = src->ob_size;
  86	if (i < 0)
  87		i = -(i);
  88	result = _PyLong_New(i);
  89	if (result != NULL) {
  90		result->ob_size = src->ob_size;
  91		while (--i >= 0)
  92			result->ob_digit[i] = src->ob_digit[i];
  93	}
  94	return (PyObject *)result;
  95}
  96
  97/* Create a new long int object from a C long int */
  98
  99PyObject *
 100PyLong_FromLong(long ival)
 101{
 102	PyLongObject *v;
 103        unsigned long abs_ival;
 104	unsigned long t;  /* unsigned so >> doesn't propagate sign bit */
 105	int ndigits = 0;
 106	int negative = 0;
 107
 108	if (ival < 0) {
 109		/* if LONG_MIN == -LONG_MAX-1 (true on most platforms) then
 110		   ANSI C says that the result of -ival is undefined when ival
 111		   == LONG_MIN.  Hence the following workaround. */
 112		abs_ival = (unsigned long)(-1-ival) + 1;
 113		negative = 1;
 114	}
 115	else {
 116		abs_ival = (unsigned long)ival;
 117	}
 118
 119	/* Count the number of Python digits.
 120	   We used to pick 5 ("big enough for anything"), but that's a
 121	   waste of time and space given that 5*15 = 75 bits are rarely
 122	   needed. */
 123	t = abs_ival;
 124	while (t) {
 125		++ndigits;
 126		t >>= PyLong_SHIFT;
 127	}
 128	v = _PyLong_New(ndigits);
 129	if (v != NULL) {
 130		digit *p = v->ob_digit;
 131		v->ob_size = negative ? -ndigits : ndigits;
 132		t = abs_ival;
 133		while (t) {
 134			*p++ = (digit)(t & PyLong_MASK);
 135			t >>= PyLong_SHIFT;
 136		}
 137	}
 138	return (PyObject *)v;
 139}
 140
 141/* Create a new long int object from a C unsigned long int */
 142
 143PyObject *
 144PyLong_FromUnsignedLong(unsigned long ival)
 145{
 146	PyLongObject *v;
 147	unsigned long t;
 148	int ndigits = 0;
 149
 150	/* Count the number of Python digits. */
 151	t = (unsigned long)ival;
 152	while (t) {
 153		++ndigits;
 154		t >>= PyLong_SHIFT;
 155	}
 156	v = _PyLong_New(ndigits);
 157	if (v != NULL) {
 158		digit *p = v->ob_digit;
 159		Py_SIZE(v) = ndigits;
 160		while (ival) {
 161			*p++ = (digit)(ival & PyLong_MASK);
 162			ival >>= PyLong_SHIFT;
 163		}
 164	}
 165	return (PyObject *)v;
 166}
 167
 168/* Create a new long int object from a C double */
 169
 170PyObject *
 171PyLong_FromDouble(double dval)
 172{
 173	PyLongObject *v;
 174	double frac;
 175	int i, ndig, expo, neg;
 176	neg = 0;
 177	if (Py_IS_INFINITY(dval)) {
 178		PyErr_SetString(PyExc_OverflowError,
 179			"cannot convert float infinity to integer");
 180		return NULL;
 181	}
 182	if (Py_IS_NAN(dval)) {
 183		PyErr_SetString(PyExc_ValueError,
 184			"cannot convert float NaN to integer");
 185		return NULL;
 186	}
 187	if (dval < 0.0) {
 188		neg = 1;
 189		dval = -dval;
 190	}
 191	frac = frexp(dval, &expo); /* dval = frac*2**expo; 0.0 <= frac < 1.0 */
 192	if (expo <= 0)
 193		return PyLong_FromLong(0L);
 194	ndig = (expo-1) / PyLong_SHIFT + 1; /* Number of 'digits' in result */
 195	v = _PyLong_New(ndig);
 196	if (v == NULL)
 197		return NULL;
 198	frac = ldexp(frac, (expo-1) % PyLong_SHIFT + 1);
 199	for (i = ndig; --i >= 0; ) {
 200		long bits = (long)frac;
 201		v->ob_digit[i] = (digit) bits;
 202		frac = frac - (double)bits;
 203		frac = ldexp(frac, PyLong_SHIFT);
 204	}
 205	if (neg)
 206		Py_SIZE(v) = -(Py_SIZE(v));
 207	return (PyObject *)v;
 208}
 209
 210/* Checking for overflow in PyLong_AsLong is a PITA since C doesn't define
 211 * anything about what happens when a signed integer operation overflows,
 212 * and some compilers think they're doing you a favor by being "clever"
 213 * then.  The bit pattern for the largest postive signed long is
 214 * (unsigned long)LONG_MAX, and for the smallest negative signed long
 215 * it is abs(LONG_MIN), which we could write -(unsigned long)LONG_MIN.
 216 * However, some other compilers warn about applying unary minus to an
 217 * unsigned operand.  Hence the weird "0-".
 218 */
 219#define PY_ABS_LONG_MIN		(0-(unsigned long)LONG_MIN)
 220#define PY_ABS_SSIZE_T_MIN	(0-(size_t)PY_SSIZE_T_MIN)
 221
 222/* Get a C long int from a long int object.
 223   Returns -1 and sets an error condition if overflow occurs. */
 224
 225long
 226PyLong_AsLong(PyObject *vv)
 227{
 228	/* This version by Tim Peters */
 229	register PyLongObject *v;
 230	unsigned long x, prev;
 231	Py_ssize_t i;
 232	int sign;
 233
 234	if (vv == NULL || !PyLong_Check(vv)) {
 235		if (vv != NULL && PyInt_Check(vv))
 236			return PyInt_AsLong(vv);
 237		PyErr_BadInternalCall();
 238		return -1;
 239	}
 240	v = (PyLongObject *)vv;
 241	i = v->ob_size;
 242	sign = 1;
 243	x = 0;
 244	if (i < 0) {
 245		sign = -1;
 246		i = -(i);
 247	}
 248	while (--i >= 0) {
 249		prev = x;
 250		x = (x << PyLong_SHIFT) + v->ob_digit[i];
 251		if ((x >> PyLong_SHIFT) != prev)
 252			goto overflow;
 253	}
 254	/* Haven't lost any bits, but casting to long requires extra care
 255	 * (see comment above).
 256         */
 257	if (x <= (unsigned long)LONG_MAX) {
 258		return (long)x * sign;
 259	}
 260	else if (sign < 0 && x == PY_ABS_LONG_MIN) {
 261		return LONG_MIN;
 262	}
 263	/* else overflow */
 264
 265 overflow:
 266	PyErr_SetString(PyExc_OverflowError,
 267			"long int too large to convert to int");
 268	return -1;
 269}
 270
 271/* Get a Py_ssize_t from a long int object.
 272   Returns -1 and sets an error condition if overflow occurs. */
 273
 274Py_ssize_t
 275PyLong_AsSsize_t(PyObject *vv) {
 276	register PyLongObject *v;
 277	size_t x, prev;
 278	Py_ssize_t i;
 279	int sign;
 280
 281	if (vv == NULL || !PyLong_Check(vv)) {
 282		PyErr_BadInternalCall();
 283		return -1;
 284	}
 285	v = (PyLongObject *)vv;
 286	i = v->ob_size;
 287	sign = 1;
 288	x = 0;
 289	if (i < 0) {
 290		sign = -1;
 291		i = -(i);
 292	}
 293	while (--i >= 0) {
 294		prev = x;
 295		x = (x << PyLong_SHIFT) + v->ob_digit[i];
 296		if ((x >> PyLong_SHIFT) != prev)
 297			goto overflow;
 298	}
 299	/* Haven't lost any bits, but casting to a signed type requires
 300	 * extra care (see comment above).
 301	 */
 302	if (x <= (size_t)PY_SSIZE_T_MAX) {
 303		return (Py_ssize_t)x * sign;
 304	}
 305	else if (sign < 0 && x == PY_ABS_SSIZE_T_MIN) {
 306		return PY_SSIZE_T_MIN;
 307	}
 308	/* else overflow */
 309
 310 overflow:
 311	PyErr_SetString(PyExc_OverflowError,
 312			"long int too large to convert to int");
 313	return -1;
 314}
 315
 316/* Get a C unsigned long int from a long int object.
 317   Returns -1 and sets an error condition if overflow occurs. */
 318
 319unsigned long
 320PyLong_AsUnsignedLong(PyObject *vv)
 321{
 322	register PyLongObject *v;
 323	unsigned long x, prev;
 324	Py_ssize_t i;
 325
 326	if (vv == NULL || !PyLong_Check(vv)) {
 327		if (vv != NULL && PyInt_Check(vv)) {
 328			long val = PyInt_AsLong(vv);
 329			if (val < 0) {
 330				PyErr_SetString(PyExc_OverflowError,
 331				"can't convert negative value to unsigned long");
 332				return (unsigned long) -1;
 333			}
 334			return val;
 335		}
 336		PyErr_BadInternalCall();
 337		return (unsigned long) -1;
 338	}
 339	v = (PyLongObject *)vv;
 340	i = Py_SIZE(v);
 341	x = 0;
 342	if (i < 0) {
 343		PyErr_SetString(PyExc_OverflowError,
 344			   "can't convert negative value to unsigned long");
 345		return (unsigned long) -1;
 346	}
 347	while (--i >= 0) {
 348		prev = x;
 349		x = (x << PyLong_SHIFT) + v->ob_digit[i];
 350		if ((x >> PyLong_SHIFT) != prev) {
 351			PyErr_SetString(PyExc_OverflowError,
 352				"long int too large to convert");
 353			return (unsigned long) -1;
 354		}
 355	}
 356	return x;
 357}
 358
 359/* Get a C unsigned long int from a long int object, ignoring the high bits.
 360   Returns -1 and sets an error condition if an error occurs. */
 361
 362unsigned long
 363PyLong_AsUnsignedLongMask(PyObject *vv)
 364{
 365	register PyLongObject *v;
 366	unsigned long x;
 367	Py_ssize_t i;
 368	int sign;
 369
 370	if (vv == NULL || !PyLong_Check(vv)) {
 371		if (vv != NULL && PyInt_Check(vv))
 372			return PyInt_AsUnsignedLongMask(vv);
 373		PyErr_BadInternalCall();
 374		return (unsigned long) -1;
 375	}
 376	v = (PyLongObject *)vv;
 377	i = v->ob_size;
 378	sign = 1;
 379	x = 0;
 380	if (i < 0) {
 381		sign = -1;
 382		i = -i;
 383	}
 384	while (--i >= 0) {
 385		x = (x << PyLong_SHIFT) + v->ob_digit[i];
 386	}
 387	return x * sign;
 388}
 389
 390int
 391_PyLong_Sign(PyObject *vv)
 392{
 393	PyLongObject *v = (PyLongObject *)vv;
 394
 395	assert(v != NULL);
 396	assert(PyLong_Check(v));
 397
 398	return Py_SIZE(v) == 0 ? 0 : (Py_SIZE(v) < 0 ? -1 : 1);
 399}
 400
 401size_t
 402_PyLong_NumBits(PyObject *vv)
 403{
 404	PyLongObject *v = (PyLongObject *)vv;
 405	size_t result = 0;
 406	Py_ssize_t ndigits;
 407
 408	assert(v != NULL);
 409	assert(PyLong_Check(v));
 410	ndigits = ABS(Py_SIZE(v));
 411	assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
 412	if (ndigits > 0) {
 413		digit msd = v->ob_digit[ndigits - 1];
 414
 415		result = (ndigits - 1) * PyLong_SHIFT;
 416		if (result / PyLong_SHIFT != (size_t)(ndigits - 1))
 417			goto Overflow;
 418		do {
 419			++result;
 420			if (result == 0)
 421				goto Overflow;
 422			msd >>= 1;
 423		} while (msd);
 424	}
 425	return result;
 426
 427Overflow:
 428	PyErr_SetString(PyExc_OverflowError, "long has too many bits "
 429			"to express in a platform size_t");
 430	return (size_t)-1;
 431}
 432
 433PyObject *
 434_PyLong_FromByteArray(const unsigned char* bytes, size_t n,
 435		      int little_endian, int is_signed)
 436{
 437	const unsigned char* pstartbyte;/* LSB of bytes */
 438	int incr;			/* direction to move pstartbyte */
 439	const unsigned char* pendbyte;	/* MSB of bytes */
 440	size_t numsignificantbytes;	/* number of bytes that matter */
 441	size_t ndigits;			/* number of Python long digits */
 442	PyLongObject* v;		/* result */
 443	int idigit = 0;  		/* next free index in v->ob_digit */
 444
 445	if (n == 0)
 446		return PyLong_FromLong(0L);
 447
 448	if (little_endian) {
 449		pstartbyte = bytes;
 450		pendbyte = bytes + n - 1;
 451		incr = 1;
 452	}
 453	else {
 454		pstartbyte = bytes + n - 1;
 455		pendbyte = bytes;
 456		incr = -1;
 457	}
 458
 459	if (is_signed)
 460		is_signed = *pendbyte >= 0x80;
 461
 462	/* Compute numsignificantbytes.  This consists of finding the most
 463	   significant byte.  Leading 0 bytes are insignficant if the number
 464	   is positive, and leading 0xff bytes if negative. */
 465	{
 466		size_t i;
 467		const unsigned char* p = pendbyte;
 468		const int pincr = -incr;  /* search MSB to LSB */
 469		const unsigned char insignficant = is_signed ? 0xff : 0x00;
 470
 471		for (i = 0; i < n; ++i, p += pincr) {
 472			if (*p != insignficant)
 473				break;
 474		}
 475		numsignificantbytes = n - i;
 476		/* 2's-comp is a bit tricky here, e.g. 0xff00 == -0x0100, so
 477		   actually has 2 significant bytes.  OTOH, 0xff0001 ==
 478		   -0x00ffff, so we wouldn't *need* to bump it there; but we
 479		   do for 0xffff = -0x0001.  To be safe without bothering to
 480		   check every case, bump it regardless. */
 481		if (is_signed && numsignificantbytes < n)
 482			++numsignificantbytes;
 483	}
 484
 485	/* How many Python long digits do we need?  We have
 486	   8*numsignificantbytes bits, and each Python long digit has PyLong_SHIFT
 487	   bits, so it's the ceiling of the quotient. */
 488	ndigits = (numsignificantbytes * 8 + PyLong_SHIFT - 1) / PyLong_SHIFT;
 489	if (ndigits > (size_t)INT_MAX)
 490		return PyErr_NoMemory();
 491	v = _PyLong_New((int)ndigits);
 492	if (v == NULL)
 493		return NULL;
 494
 495	/* Copy the bits over.  The tricky parts are computing 2's-comp on
 496	   the fly for signed numbers, and dealing with the mismatch between
 497	   8-bit bytes and (probably) 15-bit Python digits.*/
 498	{
 499		size_t i;
 500		twodigits carry = 1;		/* for 2's-comp calculation */
 501		twodigits accum = 0;		/* sliding register */
 502		unsigned int accumbits = 0; 	/* number of bits in accum */
 503		const unsigned char* p = pstartbyte;
 504
 505		for (i = 0; i < numsignificantbytes; ++i, p += incr) {
 506			twodigits thisbyte = *p;
 507			/* Compute correction for 2's comp, if needed. */
 508			if (is_signed) {
 509				thisbyte = (0xff ^ thisbyte) + carry;
 510				carry = thisbyte >> 8;
 511				thisbyte &= 0xff;
 512			}
 513			/* Because we're going LSB to MSB, thisbyte is
 514			   more significant than what's already in accum,
 515			   so needs to be prepended to accum. */
 516			accum |= (twodigits)thisbyte << accumbits;
 517			accumbits += 8;
 518			if (accumbits >= PyLong_SHIFT) {
 519				/* There's enough to fill a Python digit. */
 520				assert(idigit < (int)ndigits);
 521				v->ob_digit[idigit] = (digit)(accum & PyLong_MASK);
 522				++idigit;
 523				accum >>= PyLong_SHIFT;
 524				accumbits -= PyLong_SHIFT;
 525				assert(accumbits < PyLong_SHIFT);
 526			}
 527		}
 528		assert(accumbits < PyLong_SHIFT);
 529		if (accumbits) {
 530			assert(idigit < (int)ndigits);
 531			v->ob_digit[idigit] = (digit)accum;
 532			++idigit;
 533		}
 534	}
 535
 536	Py_SIZE(v) = is_signed ? -idigit : idigit;
 537	return (PyObject *)long_normalize(v);
 538}
 539
 540int
 541_PyLong_AsByteArray(PyLongObject* v,
 542		    unsigned char* bytes, size_t n,
 543		    int little_endian, int is_signed)
 544{
 545	Py_ssize_t i;		/* index into v->ob_digit */
 546	Py_ssize_t ndigits;		/* |v->ob_size| */
 547	twodigits accum;	/* sliding register */
 548	unsigned int accumbits; /* # bits in accum */
 549	int do_twos_comp;	/* store 2's-comp?  is_signed and v < 0 */
 550	digit carry;		/* for computing 2's-comp */
 551	size_t j;		/* # bytes filled */
 552	unsigned char* p;	/* pointer to next byte in bytes */
 553	int pincr;		/* direction to move p */
 554
 555	assert(v != NULL && PyLong_Check(v));
 556
 557	if (Py_SIZE(v) < 0) {
 558		ndigits = -(Py_SIZE(v));
 559		if (!is_signed) {
 560			PyErr_SetString(PyExc_TypeError,
 561				"can't convert negative long to unsigned");
 562			return -1;
 563		}
 564		do_twos_comp = 1;
 565	}
 566	else {
 567		ndigits = Py_SIZE(v);
 568		do_twos_comp = 0;
 569	}
 570
 571	if (little_endian) {
 572		p = bytes;
 573		pincr = 1;
 574	}
 575	else {
 576		p = bytes + n - 1;
 577		pincr = -1;
 578	}
 579
 580	/* Copy over all the Python digits.
 581	   It's crucial that every Python digit except for the MSD contribute
 582	   exactly PyLong_SHIFT bits to the total, so first assert that the long is
 583	   normalized. */
 584	assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
 585	j = 0;
 586	accum = 0;
 587	accumbits = 0;
 588	carry = do_twos_comp ? 1 : 0;
 589	for (i = 0; i < ndigits; ++i) {
 590		digit thisdigit = v->ob_digit[i];
 591		if (do_twos_comp) {
 592			thisdigit = (thisdigit ^ PyLong_MASK) + carry;
 593			carry = thisdigit >> PyLong_SHIFT;
 594			thisdigit &= PyLong_MASK;
 595		}
 596		/* Because we're going LSB to MSB, thisdigit is more
 597		   significant than what's already in accum, so needs to be
 598		   prepended to accum. */
 599		accum |= (twodigits)thisdigit << accumbits;
 600
 601		/* The most-significant digit may be (probably is) at least
 602		   partly empty. */
 603		if (i == ndigits - 1) {
 604			/* Count # of sign bits -- they needn't be stored,
 605			 * although for signed conversion we need later to
 606			 * make sure at least one sign bit gets stored. */
 607			digit s = do_twos_comp ? thisdigit ^ PyLong_MASK :
 608				                thisdigit;
 609			while (s != 0) {
 610				s >>= 1;
 611				accumbits++;
 612			}
 613		}
 614		else
 615			accumbits += PyLong_SHIFT;
 616
 617		/* Store as many bytes as possible. */
 618		while (accumbits >= 8) {
 619			if (j >= n)
 620				goto Overflow;
 621			++j;
 622			*p = (unsigned char)(accum & 0xff);
 623			p += pincr;
 624			accumbits -= 8;
 625			accum >>= 8;
 626		}
 627	}
 628
 629	/* Store the straggler (if any). */
 630	assert(accumbits < 8);
 631	assert(carry == 0);  /* else do_twos_comp and *every* digit was 0 */
 632	if (accumbits > 0) {
 633		if (j >= n)
 634			goto Overflow;
 635		++j;
 636		if (do_twos_comp) {
 637			/* Fill leading bits of the byte with sign bits
 638			   (appropriately pretending that the long had an
 639			   infinite supply of sign bits). */
 640			accum |= (~(twodigits)0) << accumbits;
 641		}
 642		*p = (unsigned char)(accum & 0xff);
 643		p += pincr;
 644	}
 645	else if (j == n && n > 0 && is_signed) {
 646		/* The main loop filled the byte array exactly, so the code
 647		   just above didn't get to ensure there's a sign bit, and the
 648		   loop below wouldn't add one either.  Make sure a sign bit
 649		   exists. */
 650		unsigned char msb = *(p - pincr);
 651		int sign_bit_set = msb >= 0x80;
 652		assert(accumbits == 0);
 653		if (sign_bit_set == do_twos_comp)
 654			return 0;
 655		else
 656			goto Overflow;
 657	}
 658
 659	/* Fill remaining bytes with copies of the sign bit. */
 660	{
 661		unsigned char signbyte = do_twos_comp ? 0xffU : 0U;
 662		for ( ; j < n; ++j, p += pincr)
 663			*p = signbyte;
 664	}
 665
 666	return 0;
 667
 668Overflow:
 669	PyErr_SetString(PyExc_OverflowError, "long too big to convert");
 670	return -1;
 671
 672}
 673
 674double
 675_PyLong_AsScaledDouble(PyObject *vv, int *exponent)
 676{
 677/* NBITS_WANTED should be > the number of bits in a double's precision,
 678   but small enough so that 2**NBITS_WANTED is within the normal double
 679   range.  nbitsneeded is set to 1 less than that because the most-significant
 680   Python digit contains at least 1 significant bit, but we don't want to
 681   bother counting them (catering to the worst case cheaply).
 682
 683   57 is one more than VAX-D double precision; I (Tim) don't know of a double
 684   format with more precision than that; it's 1 larger so that we add in at
 685   least one round bit to stand in for the ignored least-significant bits.
 686*/
 687#define NBITS_WANTED 57
 688	PyLongObject *v;
 689	double x;
 690	const double multiplier = (double)(1L << PyLong_SHIFT);
 691	Py_ssize_t i;
 692	int sign;
 693	int nbitsneeded;
 694
 695	if (vv == NULL || !PyLong_Check(vv)) {
 696		PyErr_BadInternalCall();
 697		return -1;
 698	}
 699	v = (PyLongObject *)vv;
 700	i = Py_SIZE(v);
 701	sign = 1;
 702	if (i < 0) {
 703		sign = -1;
 704		i = -(i);
 705	}
 706	else if (i == 0) {
 707		*exponent = 0;
 708		return 0.0;
 709	}
 710	--i;
 711	x = (double)v->ob_digit[i];
 712	nbitsneeded = NBITS_WANTED - 1;
 713	/* Invariant:  i Python digits remain unaccounted for. */
 714	while (i > 0 && nbitsneeded > 0) {
 715		--i;
 716		x = x * multiplier + (double)v->ob_digit[i];
 717		nbitsneeded -= PyLong_SHIFT;
 718	}
 719	/* There are i digits we didn't shift in.  Pretending they're all
 720	   zeroes, the true value is x * 2**(i*PyLong_SHIFT). */
 721	*exponent = i;
 722	assert(x > 0.0);
 723	return x * sign;
 724#undef NBITS_WANTED
 725}
 726
 727/* Get a C double from a long int object. */
 728
 729double
 730PyLong_AsDouble(PyObject *vv)
 731{
 732	int e = -1;
 733	double x;
 734
 735	if (vv == NULL || !PyLong_Check(vv)) {
 736		PyErr_BadInternalCall();
 737		return -1;
 738	}
 739	x = _PyLong_AsScaledDouble(vv, &e);
 740	if (x == -1.0 && PyErr_Occurred())
 741		return -1.0;
 742	/* 'e' initialized to -1 to silence gcc-4.0.x, but it should be
 743	   set correctly after a successful _PyLong_AsScaledDouble() call */
 744	assert(e >= 0);
 745	if (e > INT_MAX / PyLong_SHIFT)
 746		goto overflow;
 747	errno = 0;
 748	x = ldexp(x, e * PyLong_SHIFT);
 749	if (Py_OVERFLOWED(x))
 750		goto overflow;
 751	return x;
 752
 753overflow:
 754	PyErr_SetString(PyExc_OverflowError,
 755		"long int too large to convert to float");
 756	return -1.0;
 757}
 758
 759/* Create a new long (or int) object from a C pointer */
 760
 761PyObject *
 762PyLong_FromVoidPtr(void *p)
 763{
 764#if SIZEOF_VOID_P <= SIZEOF_LONG
 765	if ((long)p < 0)
 766		return PyLong_FromUnsignedLong((unsigned long)p);
 767	return PyInt_FromLong((long)p);
 768#else
 769
 770#ifndef HAVE_LONG_LONG
 771#   error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"
 772#endif
 773#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
 774#   error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
 775#endif
 776	/* optimize null pointers */
 777	if (p == NULL)
 778		return PyInt_FromLong(0);
 779	return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p);
 780
 781#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
 782}
 783
 784/* Get a C pointer from a long object (or an int object in some cases) */
 785
 786void *
 787PyLong_AsVoidPtr(PyObject *vv)
 788{
 789	/* This function will allow int or long objects. If vv is neither,
 790	   then the PyLong_AsLong*() functions will raise the exception:
 791	   PyExc_SystemError, "bad argument to internal function"
 792	*/
 793#if SIZEOF_VOID_P <= SIZEOF_LONG
 794	long x;
 795
 796	if (PyInt_Check(vv))
 797		x = PyInt_AS_LONG(vv);
 798	else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
 799		x = PyLong_AsLong(vv);
 800	else
 801		x = PyLong_AsUnsignedLong(vv);
 802#else
 803
 804#ifndef HAVE_LONG_LONG
 805#   error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"
 806#endif
 807#if SIZEOF_LONG_LONG < SIZEOF_VOID_P
 808#   error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"
 809#endif
 810	PY_LONG_LONG x;
 811
 812	if (PyInt_Check(vv))
 813		x = PyInt_AS_LONG(vv);
 814	else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
 815		x = PyLong_AsLongLong(vv);
 816	else
 817		x = PyLong_AsUnsignedLongLong(vv);
 818
 819#endif /* SIZEOF_VOID_P <= SIZEOF_LONG */
 820
 821	if (x == -1 && PyErr_Occurred())
 822		return NULL;
 823	return (void *)x;
 824}
 825
 826#ifdef HAVE_LONG_LONG
 827
 828/* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later
 829 * rewritten to use the newer PyLong_{As,From}ByteArray API.
 830 */
 831
 832#define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one
 833
 834/* Create a new long int object from a C PY_LONG_LONG int. */
 835
 836PyObject *
 837PyLong_FromLongLong(PY_LONG_LONG ival)
 838{
 839	PyLongObject *v;
 840	unsigned PY_LONG_LONG abs_ival;
 841	unsigned PY_LONG_LONG t;  /* unsigned so >> doesn't propagate sign bit */
 842	int ndigits = 0;
 843	int negative = 0;
 844
 845	if (ival < 0) {
 846		/* avoid signed overflow on negation;  see comments
 847		   in PyLong_FromLong above. */
 848		abs_ival = (unsigned PY_LONG_LONG)(-1-ival) + 1;
 849		negative = 1;
 850	}
 851	else {
 852		abs_ival = (unsigned PY_LONG_LONG)ival;
 853	}
 854
 855	/* Count the number of Python digits.
 856	   We used to pick 5 ("big enough for anything"), but that's a
 857	   waste of time and space given that 5*15 = 75 bits are rarely
 858	   needed. */
 859	t = abs_ival;
 860	while (t) {
 861		++ndigits;
 862		t >>= PyLong_SHIFT;
 863	}
 864	v = _PyLong_New(ndigits);
 865	if (v != NULL) {
 866		digit *p = v->ob_digit;
 867		Py_SIZE(v) = negative ? -ndigits : ndigits;
 868		t = abs_ival;
 869		while (t) {
 870			*p++ = (digit)(t & PyLong_MASK);
 871			t >>= PyLong_SHIFT;
 872		}
 873	}
 874	return (PyObject *)v;
 875}
 876
 877/* Create a new long int object from a C unsigned PY_LONG_LONG int. */
 878
 879PyObject *
 880PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival)
 881{
 882	PyLongObject *v;
 883	unsigned PY_LONG_LONG t;
 884	int ndigits = 0;
 885
 886	/* Count the number of Python digits. */
 887	t = (unsigned PY_LONG_LONG)ival;
 888	while (t) {
 889		++ndigits;
 890		t >>= PyLong_SHIFT;
 891	}
 892	v = _PyLong_New(ndigits);
 893	if (v != NULL) {
 894		digit *p = v->ob_digit;
 895		Py_SIZE(v) = ndigits;
 896		while (ival) {
 897			*p++ = (digit)(ival & PyLong_MASK);
 898			ival >>= PyLong_SHIFT;
 899		}
 900	}
 901	return (PyObject *)v;
 902}
 903
 904/* Create a new long int object from a C Py_ssize_t. */
 905
 906PyObject *
 907PyLong_FromSsize_t(Py_ssize_t ival)
 908{
 909	Py_ssize_t bytes = ival;
 910	int one = 1;
 911	return _PyLong_FromByteArray(
 912			(unsigned char *)&bytes,
 913			SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 1);
 914}
 915
 916/* Create a new long int object from a C size_t. */
 917
 918PyObject *
 919PyLong_FromSize_t(size_t ival)
 920{
 921	size_t bytes = ival;
 922	int one = 1;
 923	return _PyLong_FromByteArray(
 924			(unsigned char *)&bytes,
 925			SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);
 926}
 927
 928/* Get a C PY_LONG_LONG int from a long int object.
 929   Return -1 and set an error if overflow occurs. */
 930
 931PY_LONG_LONG
 932PyLong_AsLongLong(PyObject *vv)
 933{
 934	PY_LONG_LONG bytes;
 935	int one = 1;
 936	int res;
 937
 938	if (vv == NULL) {
 939		PyErr_BadInternalCall();
 940		return -1;
 941	}
 942	if (!PyLong_Check(vv)) {
 943		PyNumberMethods *nb;
 944		PyObject *io;
 945		if (PyInt_Check(vv))
 946			return (PY_LONG_LONG)PyInt_AsLong(vv);
 947		if ((nb = vv->ob_type->tp_as_number) == NULL ||
 948		    nb->nb_int == NULL) {
 949			PyErr_SetString(PyExc_TypeError, "an integer is required");
 950			return -1;
 951		}
 952		io = (*nb->nb_int) (vv);
 953		if (io == NULL)
 954			return -1;
 955		if (PyInt_Check(io)) {
 956			bytes = PyInt_AsLong(io);
 957			Py_DECREF(io);
 958			return bytes;
 959		}
 960		if (PyLong_Check(io)) {
 961			bytes = PyLong_AsLongLong(io);
 962			Py_DECREF(io);
 963			return bytes;
 964		}
 965		Py_DECREF(io);
 966		PyErr_SetString(PyExc_TypeError, "integer conversion failed");
 967		return -1;
 968	}
 969
 970	res = _PyLong_AsByteArray(
 971			(PyLongObject *)vv, (unsigned char *)&bytes,
 972			SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1);
 973
 974	/* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
 975	if (res < 0)
 976		return (PY_LONG_LONG)-1;
 977	else
 978		return bytes;
 979}
 980
 981/* Get a C unsigned PY_LONG_LONG int from a long int object.
 982   Return -1 and set an error if overflow occurs. */
 983
 984unsigned PY_LONG_LONG
 985PyLong_AsUnsignedLongLong(PyObject *vv)
 986{
 987	unsigned PY_LONG_LONG bytes;
 988	int one = 1;
 989	int res;
 990
 991	if (vv == NULL || !PyLong_Check(vv)) {
 992		PyErr_BadInternalCall();
 993		return (unsigned PY_LONG_LONG)-1;
 994	}
 995
 996	res = _PyLong_AsByteArray(
 997			(PyLongObject *)vv, (unsigned char *)&bytes,
 998			SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0);
 999
1000	/* Plan 9 can't handle PY_LONG_LONG in ? : expressions */
1001	if (res < 0)
1002		return (unsigned PY_LONG_LONG)res;
1003	else
1004		return bytes;
1005}
1006
1007/* Get a C unsigned long int from a long int object, ignoring the high bits.
1008   Returns -1 and sets an error condition if an error occurs. */
1009
1010unsigned PY_LONG_LONG
1011PyLong_AsUnsignedLongLongMask(PyObject *vv)
1012{
1013	register PyLongObject *v;
1014	unsigned PY_LONG_LONG x;
1015	Py_ssize_t i;
1016	int sign;
1017
1018	if (vv == NULL || !PyLong_Check(vv)) {
1019		PyErr_BadInternalCall();
1020		return (unsigned long) -1;
1021	}
1022	v = (PyLongObject *)vv;
1023	i = v->ob_size;
1024	sign = 1;
1025	x = 0;
1026	if (i < 0) {
1027		sign = -1;
1028		i = -i;
1029	}
1030	while (--i >= 0) {
1031		x = (x << PyLong_SHIFT) + v->ob_digit[i];
1032	}
1033	return x * sign;
1034}
1035#undef IS_LITTLE_ENDIAN
1036
1037#endif /* HAVE_LONG_LONG */
1038
1039
1040static int
1041convert_binop(PyObject *v, PyObject *w, PyLongObject **a, PyLongObject **b) {
1042	if (PyLong_Check(v)) {
1043		*a = (PyLongObject *) v;
1044		Py_INCREF(v);
1045	}
1046	else if (PyInt_Check(v)) {
1047		*a = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(v));
1048	}
1049	else {
1050		return 0;
1051	}
1052	if (PyLong_Check(w)) {
1053		*b = (PyLongObject *) w;
1054		Py_INCREF(w);
1055	}
1056	else if (PyInt_Check(w)) {
1057		*b = (PyLongObject *) PyLong_FromLong(PyInt_AS_LONG(w));
1058	}
1059	else {
1060		Py_DECREF(*a);
1061		return 0;
1062	}
1063	return 1;
1064}
1065
1066#define CONVERT_BINOP(v, w, a, b) \
1067	if (!convert_binop(v, w, a, b)) { \
1068		Py_INCREF(Py_NotImplemented); \
1069		return Py_NotImplemented; \
1070	}
1071
1072/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required.  x[0:n]
1073 * is modified in place, by adding y to it.  Carries are propagated as far as
1074 * x[m-1], and the remaining carry (0 or 1) is returned.
1075 */
1076static digit
1077v_iadd(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
1078{
1079	Py_ssize_t i;
1080	digit carry = 0;
1081
1082	assert(m >= n);
1083	for (i = 0; i < n; ++i) {
1084		carry += x[i] + y[i];
1085		x[i] = carry & PyLong_MASK;
1086		carry >>= PyLong_SHIFT;
1087		assert((carry & 1) == carry);
1088	}
1089	for (; carry && i < m; ++i) {
1090		carry += x[i];
1091		x[i] = carry & PyLong_MASK;
1092		carry >>= PyLong_SHIFT;
1093		assert((carry & 1) == carry);
1094	}
1095	return carry;
1096}
1097
1098/* x[0:m] and y[0:n] are digit vectors, LSD first, m >= n required.  x[0:n]
1099 * is modified in place, by subtracting y from it.  Borrows are propagated as
1100 * far as x[m-1], and the remaining borrow (0 or 1) is returned.
1101 */
1102static digit
1103v_isub(digit *x, Py_ssize_t m, digit *y, Py_ssize_t n)
1104{
1105	Py_ssize_t i;
1106	digit borrow = 0;
1107
1108	assert(m >= n);
1109	for (i = 0; i < n; ++i) {
1110		borrow = x[i] - y[i] - borrow;
1111		x[i] = borrow & PyLong_MASK;
1112		borrow >>= PyLong_SHIFT;
1113		borrow &= 1;	/* keep only 1 sign bit */
1114	}
1115	for (; borrow && i < m; ++i) {
1116		borrow = x[i] - borrow;
1117		x[i] = borrow & PyLong_MASK;
1118		borrow >>= PyLong_SHIFT;
1119		borrow &= 1;
1120	}
1121	return borrow;
1122}
1123
1124/* Multiply by a single digit, ignoring the sign. */
1125
1126static PyLongObject *
1127mul1(PyLongObject *a, wdigit n)
1128{
1129	return muladd1(a, n, (digit)0);
1130}
1131
1132/* Multiply by a single digit and add a single digit, ignoring the sign. */
1133
1134static PyLongObject *
1135muladd1(PyLongObject *a, wdigit n, wdigit extra)
1136{
1137	Py_ssize_t size_a = ABS(Py_SIZE(a));
1138	PyLongObject *z = _PyLong_New(size_a+1);
1139	twodigits carry = extra;
1140	Py_ssize_t i;
1141
1142	if (z == NULL)
1143		return NULL;
1144	for (i = 0; i < size_a; ++i) {
1145		carry += (twodigits)a->ob_digit[i] * n;
1146		z->ob_digit[i] = (digit) (carry & PyLong_MASK);
1147		carry >>= PyLong_SHIFT;
1148	}
1149	z->ob_digit[i] = (digit) carry;
1150	return long_normalize(z);
1151}
1152
1153/* Divide long pin, w/ size digits, by non-zero digit n, storing quotient
1154   in pout, and returning the remainder.  pin and pout point at the LSD.
1155   It's OK for pin == pout on entry, which saves oodles of mallocs/frees in
1156   _PyLong_Format, but that should be done with great care since longs are
1157   immutable. */
1158
1159static digit
1160inplace_divrem1(digit *pout, digit *pin, Py_ssize_t size, digit n)
1161{
1162	twodigits rem = 0;
1163
1164	assert(n > 0 && n <= PyLong_MASK);
1165	pin += size;
1166	pout += size;
1167	while (--size >= 0) {
1168		digit hi;
1169		rem = (rem << PyLong_SHIFT) + *--pin;
1170		*--pout = hi = (digit)(rem / n);
1171		rem -= (twodigits)hi * n;
1172	}
1173	return (digit)rem;
1174}
1175
1176/* Divide a long integer by a digit, returning both the quotient
1177   (as function result) and the remainder (through *prem).
1178   The sign of a is ignored; n should not be zero. */
1179
1180static PyLongObject *
1181divrem1(PyLongObject *a, digit n, digit *prem)
1182{
1183	const Py_ssize_t size = ABS(Py_SIZE(a));
1184	PyLongObject *z;
1185
1186	assert(n > 0 && n <= PyLong_MASK);
1187	z = _PyLong_New(size);
1188	if (z == NULL)
1189		return NULL;
1190	*prem = inplace_divrem1(z->ob_digit, a->ob_digit, size, n);
1191	return long_normalize(z);
1192}
1193
1194/* Convert the long to a string object with given base,
1195   appending a base prefix of 0[box] if base is 2, 8 or 16.
1196   Add a trailing "L" if addL is non-zero.
1197   If newstyle is zero, then use the pre-2.6 behavior of octal having
1198   a leading "0", instead of the prefix "0o" */
1199PyAPI_FUNC(PyObject *)
1200_PyLong_Format(PyObject *aa, int base, int addL, int newstyle)
1201{
1202	register PyLongObject *a = (PyLongObject *)aa;
1203	PyStringObject *str;
1204	Py_ssize_t i, sz;
1205	Py_ssize_t size_a;
1206	char *p;
1207	int bits;
1208	char sign = '\0';
1209
1210	if (a == NULL || !PyLong_Check(a)) {
1211		PyErr_BadInternalCall();
1212		return NULL;
1213	}
1214	assert(base >= 2 && base <= 36);
1215	size_a = ABS(Py_SIZE(a));
1216
1217	/* Compute a rough upper bound for the length of the string */
1218	i = base;
1219	bits = 0;
1220	while (i > 1) {
1221		++bits;
1222		i >>= 1;
1223	}
1224	i = 5 + (addL ? 1 : 0);
1225	/* ensure we don't get signed overflow in sz calculation */
1226	if (size_a > (PY_SSIZE_T_MAX - i) / PyLong_SHIFT) {
1227		PyErr_SetString(PyExc_OverflowError,
1228				"long is too large to format");
1229		return NULL;
1230	}
1231	sz = i + 1 + (size_a * PyLong_SHIFT - 1) / bits;
1232	assert(sz >= 0);
1233	str = (PyStringObject *) PyString_FromStringAndSize((char *)0, sz);
1234	if (str == NULL)
1235		return NULL;
1236	p = PyString_AS_STRING(str) + sz;
1237	*p = '\0';
1238	if (addL)
1239		*--p = 'L';
1240	if (a->ob_size < 0)
1241		sign = '-';
1242
1243	if (a->ob_size == 0) {
1244		*--p = '0';
1245	}
1246	else if ((base & (base - 1)) == 0) {
1247		/* JRH: special case for power-of-2 bases */
1248		twodigits accum = 0;
1249		int accumbits = 0;	/* # of bits in accum */
1250		int basebits = 1;	/* # of bits in base-1 */
1251		i = base;
1252		while ((i >>= 1) > 1)
1253			++basebits;
1254
1255		for (i = 0; i < size_a; ++i) {
1256			accum |= (twodigits)a->ob_digit[i] << accumbits;
1257			accumbits += PyLong_SHIFT;
1258			assert(accumbits >= basebits);
1259			do {
1260				char cdigit = (char)(accum & (base - 1));
1261				cdigit += (cdigit < 10) ? '0' : 'a'-10;
1262				assert(p > PyString_AS_STRING(str));
1263				*--p = cdigit;
1264				accumbits -= basebits;
1265				accum >>= basebits;
1266			} while (i < size_a-1 ? accumbits >= basebits :
1267						accum > 0);
1268		}
1269	}
1270	else {
1271		/* Not 0, and base not a power of 2.  Divide repeatedly by
1272		   base, but for speed use the highest power of base that
1273		   fits in a digit. */
1274		Py_ssize_t size = size_a;
1275		digit *pin = a->ob_digit;
1276		PyLongObject *scratch;
1277		/* powbasw <- largest power of base that fits in a digit. */
1278		digit powbase = base;  /* powbase == base ** power */
1279		int power = 1;
1280		for (;;) {
1281			unsigned long newpow = powbase * (unsigned long)base;
1282			if (newpow >> PyLong_SHIFT)
1283				/* doesn't fit in a digit */
1284				break;
1285			powbase = (digit)newpow;
1286			++power;
1287		}
1288
1289		/* Get a scratch area for repeated division. */
1290		scratch = _PyLong_New(size);
1291		if (scratch == NULL) {
1292			Py_DECREF(str);
1293			return NULL;
1294		}
1295
1296		/* Repeatedly divide by powbase. */
1297		do {
1298			int ntostore = power;
1299			digit rem = inplace_divrem1(scratch->ob_digit,
1300						     pin, size, powbase);
1301			pin = scratch->ob_digit; /* no need to use a again */
1302			if (pin[size - 1] == 0)
1303				--size;
1304			SIGCHECK({
1305				Py_DECREF(scratch);
1306				Py_DECREF(str);
1307				return NULL;
1308			})
1309
1310			/* Break rem into digits. */
1311			assert(ntostore > 0);
1312			do {
1313				digit nextrem = (digit)(rem / base);
1314				char c = (char)(rem - nextrem * base);
1315				assert(p > PyString_AS_STRING(str));
1316				c += (c < 10) ? '0' : 'a'-10;
1317				*--p = c;
1318				rem = nextrem;
1319				--ntostore;
1320				/* Termination is a bit delicate:  must not
1321				   store leading zeroes, so must get out if
1322				   remaining quotient and rem are both 0. */
1323			} while (ntostore && (size || rem));
1324		} while (size != 0);
1325		Py_DECREF(scratch);
1326	}
1327
1328	if (base == 2) {
1329		*--p = 'b';
1330		*--p = '0';
1331	}
1332	else if (base == 8) {
1333		if (newstyle) {
1334			*--p = 'o';
1335			*--p = '0';
1336		}
1337		else
1338			if (size_a != 0)
1339				*--p = '0';
1340	}
1341	else if (base == 16) {
1342		*--p = 'x';
1343		*--p = '0';
1344	}
1345	else if (base != 10) {
1346		*--p = '#';
1347		*--p = '0' + base%10;
1348		if (base > 10)
1349			*--p = '0' + base/10;
1350	}
1351	if (sign)
1352		*--p = sign;
1353	if (p != PyString_AS_STRING(str)) {
1354		char *q = PyString_AS_STRING(str);
1355		assert(p > q);
1356		do {
1357		} while ((*q++ = *p++) != '\0');
1358		q--;
1359		_PyString_Resize((PyObject **)&str,
1360				 (Py_ssize_t) (q - PyString_AS_STRING(str)));
1361	}
1362	return (PyObject *)str;
1363}
1364
1365/* Table of digit values for 8-bit string -> integer conversion.
1366 * '0' maps to 0, ..., '9' maps to 9.
1367 * 'a' and 'A' map to 10, ..., 'z' and 'Z' map to 35.
1368 * All other indices map to 37.
1369 * Note that when converting a base B string, a char c is a legitimate
1370 * base B digit iff _PyLong_DigitValue[Py_CHARMASK(c)] < B.
1371 */
1372int _PyLong_DigitValue[256] = {
1373	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1374	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1375	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1376	0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  37, 37, 37, 37, 37, 37,
1377	37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1378	25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1379	37, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
1380	25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 37, 37, 37, 37, 37,
1381	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1382	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1383	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1384	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1385	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1386	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1387	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1388	37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37, 37,
1389};
1390
1391/* *str points to the first digit in a string of base `base` digits.  base
1392 * is a power of 2 (2, 4, 8, 16, or 32).  *str is set to point to the first
1393 * non-digit (which may be *str!).  A normalized long is returned.
1394 * The point to this routine is that it takes time linear in the number of
1395 * string characters.
1396 */
1397static PyLongObject *
1398long_from_binary_base(char **str, int base)
1399{
1400	char *p = *str;
1401	char *start = p;
1402	int bits_per_char;
1403	Py_ssize_t n;
1404	PyLongObject *z;
1405	twodigits accum;
1406	int bits_in_accum;
1407	digit *pdigit;
1408
1409	assert(base >= 2 && base <= 32 && (base & (base - 1)) == 0);
1410	n = base;
1411	for (bits_per_char = -1; n; ++bits_per_char)
1412		n >>= 1;
1413	/* n <- total # of bits needed, while setting p to end-of-string */
1414	while (_PyLong_DigitValue[Py_CHARMASK(*p)] < base)
1415		++p;
1416	*str = p;
1417	/* n <- # of Python digits needed, = ceiling(n/PyLong_SHIFT). */
1418	n = (p - start) * bits_per_char + PyLong_SHIFT - 1;
1419	if (n / bits_per_char < p - start) {
1420		PyErr_SetString(PyExc_ValueError,
1421				"long string too large to convert");
1422		return NULL;
1423	}
1424	n = n / PyLong_SHIFT;
1425	z = _PyLong_New(n);
1426	if (z == NULL)
1427		return NULL;
1428	/* Read string from right, and fill in long from left; i.e.,
1429	 * from least to most significant in both.
1430	 */
1431	accum = 0;
1432	bits_in_accum = 0;
1433	pdigit = z->ob_digit;
1434	while (--p >= start) {
1435		int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
1436		assert(k >= 0 && k < base);
1437		accum |= (twodigits)k << bits_in_accum;
1438		bits_in_accum += bits_per_char;
1439		if (bits_in_accum >= PyLong_SHIFT) {
1440			*pdigit++ = (digit)(accum & PyLong_MASK);
1441			assert(pdigit - z->ob_digit <= n);
1442			accum >>= PyLong_SHIFT;
1443			bits_in_accum -= PyLong_SHIFT;
1444			assert(bits_in_accum < PyLong_SHIFT);
1445		}
1446	}
1447	if (bits_in_accum) {
1448		assert(bits_in_accum <= PyLong_SHIFT);
1449		*pdigit++ = (digit)accum;
1450		assert(pdigit - z->ob_digit <= n);
1451	}
1452	while (pdigit - z->ob_digit < n)
1453		*pdigit++ = 0;
1454	return long_normalize(z);
1455}
1456
1457PyObject *
1458PyLong_FromString(char *str, char **pend, int base)
1459{
1460	int sign = 1;
1461	char *start, *orig_str = str;
1462	PyLongObject *z;
1463	PyObject *strobj, *strrepr;
1464	Py_ssize_t slen;
1465
1466	if ((base != 0 && base < 2) || base > 36) {
1467		PyErr_SetString(PyExc_ValueError,
1468				"long() arg 2 must be >= 2 and <= 36");
1469		return NULL;
1470	}
1471	while (*str != '\0' && isspace(Py_CHARMASK(*str)))
1472		str++;
1473	if (*str == '+')
1474		++str;
1475	else if (*str == '-') {
1476		++str;
1477		sign = -1;
1478	}
1479	while (*str != '\0' && isspace(Py_CHARMASK(*str)))
1480		str++;
1481	if (base == 0) {
1482		/* No base given.  Deduce the base from the contents
1483		   of the string */
1484		if (str[0] != '0')
1485			base = 10;
1486		else if (str[1] == 'x' || str[1] == 'X')
1487			base = 16;
1488		else if (str[1] == 'o' || str[1] == 'O')
1489			base = 8;
1490		else if (str[1] == 'b' || str[1] == 'B')
1491			base = 2;
1492		else
1493			/* "old" (C-style) octal literal, still valid in
1494			   2.x, although illegal in 3.x */
1495			base = 8;
1496	}
1497	/* Whether or not we were deducing the base, skip leading chars
1498	   as needed */
1499	if (str[0] == '0' &&
1500	    ((base == 16 && (str[1] == 'x' || str[1] == 'X')) ||
1501	     (base == 8  && (str[1] == 'o' || str[1] == 'O')) ||
1502	     (base == 2  && (str[1] == 'b' || str[1] == 'B'))))
1503		str += 2;
1504
1505	start = str;
1506	if ((base & (base - 1)) == 0)
1507		z = long_from_binary_base(&str, base);
1508	else {
1509/***
1510Binary bases can be converted in time linear in the number of digits, because
1511Python's representation base is binary.  Other bases (including decimal!) use
1512the simple quadratic-time algorithm below, complicated by some speed tricks.
1513
1514First some math:  the largest integer that can be expressed in N base-B digits
1515is B**N-1.  Consequently, if we have an N-digit input in base B, the worst-
1516case number of Python digits needed to hold it is the smallest integer n s.t.
1517
1518    PyLong_BASE**n-1 >= B**N-1  [or, adding 1 to both sides]
1519    PyLong_BASE**n >= B**N      [taking logs to base PyLong_BASE]
1520    n >= log(B**N)/log(PyLong_BASE) = N * log(B)/log(PyLong_BASE)
1521
1522The static array log_base_PyLong_BASE[base] == log(base)/log(PyLong_BASE) so we can compute
1523this quickly.  A Python long with that much space is reserved near the start,
1524and the result is computed into it.
1525
1526The input string is actually treated as being in base base**i (i.e., i digits
1527are processed at a time), where two more static arrays hold:
1528
1529    convwidth_base[base] = the largest integer i such that base**i <= PyLong_BASE
1530    convmultmax_base[base] = base ** convwidth_base[base]
1531
1532The first of these is the largest i such that i consecutive input digits
1533must fit in a single Python digit.  The second is effectively the input
1534base we're really using.
1535
1536Viewing the input as a sequence <c0, c1, ..., c_n-1> of digits in base
1537convmultmax_base[base], the result is "simply"
1538
1539   (((c0*B + c1)*B + c2)*B + c3)*B + ... ))) + c_n-1
1540
1541where B = convmultmax_base[base].
1542
1543Error analysis:  as above, the number of Python digits `n` needed is worst-
1544case
1545
1546    n >= N * log(B)/log(PyLong_BASE)
1547
1548where `N` is the number of input digits in base `B`.  This is computed via
1549
1550    size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
1551
1552below.  Two numeric concerns are how much space this can waste, and whether
1553the computed result can be too small.  To be concrete, assume PyLong_BASE = 2**15,
1554which is the default (and it's unlikely anyone changes that).
1555
1556Waste isn't a problem:  provided the first input digit isn't 0, the difference
1557between the worst-case input with N digits and the smallest input with N
1558digits is about a factor of B, but B is small compared to PyLong_BASE so at most
1559one allocated Python digit can remain unused on that count.  If
1560N*log(B)/log(PyLong_BASE) is mathematically an exact integer, then truncating that
1561and adding 1 returns a result 1 larger than necessary.  However, that can't
1562happen:  whenever B is a power of 2, long_from_binary_base() is called
1563instead, and it's impossible for B**i to be an integer power of 2**15 when
1564B is not a power of 2 (i.e., it's impossible for N*log(B)/log(PyLong_BASE) to be
1565an exact integer when B is not a power of 2, since B**i has a prime factor
1566other than 2 in that case, but (2**15)**j's only prime factor is 2).
1567
1568The computed result can be too small if the true value of N*log(B)/log(PyLong_BASE)
1569is a little bit larger than an exact integer, but due to roundoff errors (in
1570computing log(B), log(PyLong_BASE), their quotient, and/or multiplying that by N)
1571yields a numeric result a little less than that integer.  Unfortunately, "how
1572close can a transcendental function get to an integer over some range?"
1573questions are generally theoretically intractable.  Computer analysis via
1574continued fractions is practical:  expand log(B)/log(PyLong_BASE) via continued
1575fractions, giving a sequence i/j of "the best" rational approximations.  Then
1576j*log(B)/log(PyLong_BASE) is approximately equal to (the integer) i.  This shows that
1577we can get very close to being in trouble, but very rarely.  For example,
157876573 is a denominator in one of the continued-fraction approximations to
1579log(10)/log(2**15), and indeed:
1580
1581    >>> log(10)/log(2**15)*76573
1582    16958.000000654003
1583
1584is very close to an integer.  If we were working with IEEE single-precision,
1585rounding errors could kill us.  Finding worst cases in IEEE double-precision
1586requires better-than-double-precision log() functions, and Tim didn't bother.
1587Instead the code checks to see whether the allocated space is enough as each
1588new Python digit is added, and copies the whole thing to a larger long if not.
1589This should happen extremely rarely, and in fact I don't have a test case
1590that triggers it(!).  Instead the code was tested by artificially allocating
1591just 1 digit at the start, so that the copying code was exercised for every
1592digit beyond the first.
1593***/
1594		register twodigits c;	/* current input character */
1595		Py_ssize_t size_z;
1596		int i;
1597		int convwidth;
1598		twodigits convmultmax, convmult;
1599		digit *pz, *pzstop;
1600		char* scan;
1601
1602		static double log_base_PyLong_BASE[37] = {0.0e0,};
1603		static int convwidth_base[37] = {0,};
1604		static twodigits convmultmax_base[37] = {0,};
1605
1606		if (log_base_PyLong_BASE[base] == 0.0) {
1607			twodigits convmax = base;
1608			int i = 1;
1609
1610			log_base_PyLong_BASE[base] = log((double)base) /
1611						log((double)PyLong_BASE);
1612			for (;;) {
1613				twodigits next = convmax * base;
1614				if (next > PyLong_BASE)
1615					break;
1616				convmax = next;
1617				++i;
1618			}
1619			convmultmax_base[base] = convmax;
1620			assert(i > 0);
1621			convwidth_base[base] = i;
1622		}
1623
1624		/* Find length of the string of numeric characters. */
1625		scan = str;
1626		while (_PyLong_DigitValue[Py_CHARMASK(*scan)] < base)
1627			++scan;
1628
1629		/* Create a long object that can contain the largest possible
1630		 * integer with this base and length.  Note that there's no
1631		 * need to initialize z->ob_digit -- no slot is read up before
1632		 * being stored into.
1633		 */
1634		size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;
1635		/* Uncomment next line to test exceedingly rare copy code */
1636		/* size_z = 1; */
1637		assert(size_z > 0);
1638		z = _PyLong_New(size_z);
1639		if (z == NULL)
1640			return NULL;
1641		Py_SIZE(z) = 0;
1642
1643		/* `convwidth` consecutive input digits are treated as a single
1644		 * digit in base `convmultmax`.
1645		 */
1646		convwidth = convwidth_base[base];
1647		convmultmax = convmultmax_base[base];
1648
1649		/* Work ;-) */
1650		while (str < scan) {
1651			/* grab up to convwidth digits from the input string */
1652			c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)];
1653			for (i = 1; i < convwidth && str != scan; ++i, ++str) {
1654				c = (twodigits)(c *  base +
1655					_PyLong_DigitValue[Py_CHARMASK(*str)]);
1656				assert(c < PyLong_BASE);
1657			}
1658
1659			convmult = convmultmax;
1660			/* Calculate the shift only if we couldn't get
1661			 * convwidth digits.
1662			 */
1663			if (i != convwidth) {
1664				convmult = base;
1665				for ( ; i > 1; --i)
1666					convmult *= base;
1667			}
1668
1669			/* Multiply z by convmult, and add c. */
1670			pz = z->ob_digit;
1671			pzstop = pz + Py_SIZE(z);
1672			for (; pz < pzstop; ++pz) {
1673				c += (twodigits)*pz * convmult;
1674				*pz = (digit)(c & PyLong_MASK);
1675				c >>= PyLong_SHIFT;
1676			}
1677			/* carry off the current end? */
1678			if (c) {
1679				assert(c < PyLong_BASE);
1680				if (Py_SIZE(z) < size_z) {
1681					*pz = (digit)c;
1682					++Py_SIZE(z);
1683				}
1684				else {
1685					PyLongObject *tmp;
1686					/* Extremely rare.  Get more space. */
1687					assert(Py_SIZE(z) == size_z);
1688					tmp = _PyLong_New(size_z + 1);
1689					if (tmp == NULL) {
1690						Py_DECREF(z);
1691						return NULL;
1692					}
1693					memcpy(tmp->ob_digit,
1694					       z->ob_digit,
1695					       sizeof(digit) * size_z);
1696					Py_DECREF(z);
1697					z = tmp;
1698					z->ob_digit[size_z] = (digit)c;
1699					++size_z;
1700				}
1701			}
1702		}
1703	}
1704	if (z == NULL)
1705		return NULL;
1706	if (str == start)
1707		goto onError;
1708	if (sign < 0)
1709		Py_SIZE(z) = -(Py_SIZE(z));
1710	if (*str == 'L' || *str == 'l')
1711		str++;
1712	while (*str && isspace(Py_CHARMASK(*str)))
1713		str++;
1714	if (*str != '\0')
1715		goto onError;
1716	if (pend)
1717		*pend = str;
1718	return (PyObject *) z;
1719
1720 onError:
1721	Py_XDECREF(z);
1722	slen = strlen(orig_str) < 200 ? strlen(orig_str) : 200;
1723	strobj = PyString_FromStringAndSize(orig_str, slen);
1724	if (strobj == NULL)
1725		return NULL;
1726	strrepr = PyObject_Repr(strobj);
1727	Py_DECREF(strobj);
1728	if (strrepr == NULL)
1729		return NULL;
1730	PyErr_Format(PyExc_ValueError,
1731		     "invalid literal for long() with base %d: %s",
1732		     base, PyString_AS_STRING(strrepr));
1733	Py_DECREF(strrepr);
1734	return NULL;
1735}
1736
1737#ifdef Py_USING_UNICODE
1738PyObject *
1739PyLong_FromUnicode(Py_UNICODE *u, Py_ssize_t length, int base)
1740{
1741	PyObject *result;
1742	char *buffer = (char *)PyMem_MALLOC(length+1);
1743
1744	if (buffer == NULL)
1745		return NULL;
1746
1747	if (PyUnicode_EncodeDecimal(u, length, buffer, NULL)) {
1748		PyMem_FREE(buffer);
1749		return NULL;
1750	}
1751	result = PyLong_FromString(buffer, NULL, base);
1752	PyMem_FREE(buffer);
1753	return result;
1754}
1755#endif
1756
1757/* forward */
1758static PyLongObject *x_divrem
1759	(PyLongObject *, PyLongObject *, PyLongObject **);
1760static PyObject *long_long(PyObject *v);
1761static int long_divrem(PyLongObject *, PyLongObject *,
1762	PyLongObject **, PyLongObject **);
1763
1764/* Long division with remainder, top-level routine */
1765
1766static int
1767long_divrem(PyLongObject *a, PyLongObject *b,
1768	    PyLongObject **pdiv, PyLongObject **prem)
1769{
1770	Py_ssize_t size_a = ABS(Py_SIZE(a)), size_b = ABS(Py_SIZE(b));
1771	PyLongObject *z;
1772
1773	if (size_b == 0) {
1774		PyErr_SetString(PyExc_ZeroDivisionError,
1775				"long division or modulo by zero");
1776		return -1;
1777	}
1778	if (size_a < size_b ||
1779	    (size_a == size_b &&
1780	     a->ob_digit[size_a-1] < b->ob_digit[size_b-1])) {
1781		/* |a| < |b|. */
1782		*pdiv = _PyLong_New(0);
1783		if (*pdiv == NULL)
1784			return -1;
1785		Py_INCREF(a);
1786		*prem = (PyLongObject *) a;
1787		return 0;
1788	}
1789	if (size_b == 1) {
1790		digit rem = 0;
1791		z = divrem1(a, b->ob_digit[0], &rem);
1792		if (z == NULL)
1793			return -1;
1794		*prem = (PyLongObject *) PyLong_FromLong((long)rem);
1795		if (*prem == NULL) {
1796			Py_DECREF(z);
1797			return -1;
1798		}
1799	}
1800	else {
1801		z = x_divrem(a, b, prem);
1802		if (z == NULL)
1803			return -1;
1804	}
1805	/* Set the signs.
1806	   The quotient z has the sign of a*b;
1807	   the remainder r has the sign of a,
1808	   so a = b*z + r. */
1809	if ((a->ob_size < 0) != (b->ob_size < 0))
1810		z->ob_size = -(z->ob_size);
1811	if (a->ob_size < 0 && (*prem)->ob_size != 0)
1812		(*prem)->ob_size = -((*prem)->ob_size);
1813	*pdiv = z;
1814	return 0;
1815}
1816
1817/* Unsigned long division with remainder -- the algorithm */
1818
1819static PyLongObject *
1820x_divrem(PyLongObject *v1, PyLongObject *w1, PyLongObject **prem)
1821{
1822	Py_ssize_t size_v = ABS(Py_SIZE(v1)), size_w = ABS(Py_SIZE(w1));
1823	digit d = (digit) ((twodigits)PyLong_BASE / (w1->ob_digit[size_w-1] + 1));
1824	PyLongObject *v = mul1(v1, d);
1825	PyLongObject *w = mul1(w1, d);
1826	PyLongObject *a;
1827	Py_ssize_t j, k;
1828
1829	if (v == NULL || w == NULL) {
1830		Py_XDECREF(v);
1831		Py_XDECREF(w);
1832		return NULL;
1833	}
1834
1835	assert(size_v >= size_w && size_w > 1); /* Assert checks by div() */
1836	assert(Py_REFCNT(v) == 1); /* Since v will be used as accumulator! */
1837	assert(size_w == ABS(Py_SIZE(w))); /* That's how d was calculated */
1838
1839	size_v = ABS(Py_SIZE(v));
1840	k = size_v - size_w;
1841	a = _PyLong_New(k + 1);
1842
1843	for (j = size_v; a != NULL && k >= 0; --j, --k) {
1844		digit vj = (j >= size_v) ? 0 : v->ob_digit[j];
1845		twodigits q;
1846		stwodigits carry = 0;
1847		Py_ssize_t i;
1848
1849		SIGCHECK({
1850			Py_DECREF(a);
1851			a = NULL;
1852			break;
1853		})
1854		if (vj == w->ob_digit[size_w-1])
1855			q = PyLong_MASK;
1856		else
1857			q = (((twodigits)vj << PyLong_SHIFT) + v->ob_digit[j-1]) /
1858				w->ob_digit[size_w-1];
1859
1860		while (w->ob_digit[size_w-2]*q >
1861				((
1862					((twodigits)vj << PyLong_SHIFT)
1863					+ v->ob_digit[j-1]
1864					- q*w->ob_digit[size_w-1]
1865								) << PyLong_SHIFT)
1866				+ v->ob_digit[j-2])
1867			--q;
1868
1869		for (i = 0; i < size_w && i+k < size_v; ++i) {
1870			twodigits z = w->ob_digit[i] * q;
1871			digit zz = (digit) (z >> PyLong_SHIFT);
1872			carry += v->ob_digit[i+k] - z
1873				+ ((twodigits)zz << PyLong_SHIFT);
1874			v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
1875			carry = Py_ARITHMETIC_RIGHT_SHIFT(BASE_TWODIGITS_TYPE,
1876							  carry, PyLong_SHIFT);
1877			carry -= zz;
1878		}
1879
1880		if (i+k < size_v) {
1881			carry += v->ob_digit[i+k];
1882			v->ob_digit[i+k] = 0;
1883		}
1884
1885		if (carry == 0)
1886			a->ob_digit[k] = (digit) q;
1887		else {
1888			assert(carry == -1);
1889			a->ob_digit[k] = (digit) q-1;
1890			carry = 0;
1891			for (i = 0; i < size_w && i+k < size_v; ++i) {
1892				carry += v->ob_digit[i+k] + w->ob_digit[i];
1893				v->ob_digit[i+k] = (digit)(carry & PyLong_MASK);
1894				carry = Py_ARITHMETIC_RIGHT_SHIFT(
1895						BASE_TWODIGITS_TYPE,
1896						carry, PyLong_SHIFT);
1897			}
1898		}
1899	} /* for j, k */
1900
1901	if (a == NULL)
1902		*prem = NULL;
1903	e

Large files files are truncated, but you can click here to view the full file