cinap_lenrekadd hg and python4bb9e2215 years agoHistory
3458 lines · 84.5 KB · C
RawDownload
			   so needs to be prepended to accum. */			accum |= thisbyte << accumbits;			accumbits += 8;			if (accumbits >= SHIFT) {				/* There's enough to fill a Python digit. */				assert(idigit < (int)ndigits);				v->ob_digit[idigit] = (digit)(accum & MASK);				++idigit;				accum >>= SHIFT;				accumbits -= SHIFT;				assert(accumbits < SHIFT);			}		}		assert(accumbits < SHIFT);		if (accumbits) {			assert(idigit < (int)ndigits);			v->ob_digit[idigit] = (digit)accum;			++idigit;		}	} 	v->ob_size = is_signed ? -idigit : idigit;	return (PyObject *)long_normalize(v);} int_PyLong_AsByteArray(PyLongObject* v,		    unsigned char* bytes, size_t n,		    int little_endian, int is_signed){	int i;			/* index into v->ob_digit */	Py_ssize_t ndigits;		/* |v->ob_size| */	twodigits accum;	/* sliding register */	unsigned int accumbits; /* # bits in accum */	int do_twos_comp;	/* store 2's-comp?  is_signed and v < 0 */	twodigits carry;	/* for computing 2's-comp */	size_t j;		/* # bytes filled */	unsigned char* p;	/* pointer to next byte in bytes */	int pincr;		/* direction to move p */ 	assert(v != NULL && PyLong_Check(v)); 	if (v->ob_size < 0) {		ndigits = -(v->ob_size);		if (!is_signed) {			PyErr_SetString(PyExc_TypeError,				"can't convert negative long to unsigned");			return -1;		}		do_twos_comp = 1;	}	else {		ndigits = v->ob_size;		do_twos_comp = 0;	} 	if (little_endian) {		p = bytes;		pincr = 1;	}	else {		p = bytes + n - 1;		pincr = -1;	} 	/* Copy over all the Python digits.	   It's crucial that every Python digit except for the MSD contribute	   exactly SHIFT bits to the total, so first assert that the long is	   normalized. */	assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);	j = 0;	accum = 0;	accumbits = 0;	carry = do_twos_comp ? 1 : 0;	for (i = 0; i < ndigits; ++i) {		twodigits thisdigit = v->ob_digit[i];		if (do_twos_comp) {			thisdigit = (thisdigit ^ MASK) + carry;			carry = thisdigit >> SHIFT;			thisdigit &= MASK;		}		/* Because we're going LSB to MSB, thisdigit is more		   significant than what's already in accum, so needs to be		   prepended to accum. */		accum |= thisdigit << accumbits;		accumbits += SHIFT; 		/* The most-significant digit may be (probably is) at least		   partly empty. */		if (i == ndigits - 1) {			/* Count # of sign bits -- they needn't be stored,			 * although for signed conversion we need later to			 * make sure at least one sign bit gets stored.			 * First shift conceptual sign bit to real sign bit.			 */			stwodigits s = (stwodigits)(thisdigit <<				(8*sizeof(stwodigits) - SHIFT));			unsigned int nsignbits = 0;			while ((s < 0) == do_twos_comp && nsignbits < SHIFT) {				++nsignbits;				s <<= 1;			}			accumbits -= nsignbits;		} 		/* Store as many bytes as possible. */		while (accumbits >= 8) {			if (j >= n)				goto Overflow;			++j;			*p = (unsigned char)(accum & 0xff);			p += pincr;			accumbits -= 8;			accum >>= 8;		}	} 	/* Store the straggler (if any). */	assert(accumbits < 8);	assert(carry == 0);  /* else do_twos_comp and *every* digit was 0 */	if (accumbits > 0) {		if (j >= n)			goto Overflow;		++j;		if (do_twos_comp) {			/* Fill leading bits of the byte with sign bits			   (appropriately pretending that the long had an			   infinite supply of sign bits). */			accum |= (~(twodigits)0) << accumbits;		}		*p = (unsigned char)(accum & 0xff);		p += pincr;	}	else if (j == n && n > 0 && is_signed) {		/* The main loop filled the byte array exactly, so the code		   just above didn't get to ensure there's a sign bit, and the		   loop below wouldn't add one either.  Make sure a sign bit		   exists. */		unsigned char msb = *(p - pincr);		int sign_bit_set = msb >= 0x80;		assert(accumbits == 0);		if (sign_bit_set == do_twos_comp)			return 0;		else			goto Overflow;	} 	/* Fill remaining bytes with copies of the sign bit. */	{		unsigned char signbyte = do_twos_comp ? 0xffU : 0U;		for ( ; j < n; ++j, p += pincr)			*p = signbyte;	} 	return 0; Overflow:	PyErr_SetString(PyExc_OverflowError, "long too big to convert");	return -1; } double_PyLong_AsScaledDouble(PyObject *vv, int *exponent){/* NBITS_WANTED should be > the number of bits in a double's precision,   but small enough so that 2**NBITS_WANTED is within the normal double   range.  nbitsneeded is set to 1 less than that because the most-significant   Python digit contains at least 1 significant bit, but we don't want to   bother counting them (catering to the worst case cheaply).    57 is one more than VAX-D double precision; I (Tim) don't know of a double   format with more precision than that; it's 1 larger so that we add in at   least one round bit to stand in for the ignored least-significant bits.*/#define NBITS_WANTED 57	PyLongObject *v;	double x;	const double multiplier = (double)(1L << SHIFT);	Py_ssize_t i;	int sign;	int nbitsneeded; 	if (vv == NULL || !PyLong_Check(vv)) {		PyErr_BadInternalCall();		return -1;	}	v = (PyLongObject *)vv;	i = v->ob_size;	sign = 1;	if (i < 0) {		sign = -1;		i = -(i);	}	else if (i == 0) {		*exponent = 0;		return 0.0;	}	--i;	x = (double)v->ob_digit[i];	nbitsneeded = NBITS_WANTED - 1;	/* Invariant:  i Python digits remain unaccounted for. */	while (i > 0 && nbitsneeded > 0) {		--i;		x = x * multiplier + (double)v->ob_digit[i];		nbitsneeded -= SHIFT;	}	/* There are i digits we didn't shift in.  Pretending they're all	   zeroes, the true value is x * 2**(i*SHIFT). */	*exponent = i;	assert(x > 0.0);	return x * sign;#undef NBITS_WANTED} /* Get a C double from a long int object. */ doublePyLong_AsDouble(PyObject *vv){	int e = -1;	double x; 	if (vv == NULL || !PyLong_Check(vv)) {		PyErr_BadInternalCall();		return -1;	}	x = _PyLong_AsScaledDouble(vv, &e);	if (x == -1.0 && PyErr_Occurred())		return -1.0;	/* 'e' initialized to -1 to silence gcc-4.0.x, but it should be	   set correctly after a successful _PyLong_AsScaledDouble() call */	assert(e >= 0);	if (e > INT_MAX / SHIFT)		goto overflow;	errno = 0;	x = ldexp(x, e * SHIFT);	if (Py_OVERFLOWED(x))		goto overflow;	return x; overflow:	PyErr_SetString(PyExc_OverflowError,		"long int too large to convert to float");	return -1.0;} /* Create a new long (or int) object from a C pointer */ PyObject *PyLong_FromVoidPtr(void *p){#if SIZEOF_VOID_P <= SIZEOF_LONG	if ((long)p < 0)		return PyLong_FromUnsignedLong((unsigned long)p);	return PyInt_FromLong((long)p);#else #ifndef HAVE_LONG_LONG#   error "PyLong_FromVoidPtr: sizeof(void*) > sizeof(long), but no long long"#endif#if SIZEOF_LONG_LONG < SIZEOF_VOID_P#   error "PyLong_FromVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"#endif	/* optimize null pointers */	if (p == NULL)		return PyInt_FromLong(0);	return PyLong_FromUnsignedLongLong((unsigned PY_LONG_LONG)p); #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */} /* Get a C pointer from a long object (or an int object in some cases) */ void *PyLong_AsVoidPtr(PyObject *vv){	/* This function will allow int or long objects. If vv is neither,	   then the PyLong_AsLong*() functions will raise the exception:	   PyExc_SystemError, "bad argument to internal function"	*/#if SIZEOF_VOID_P <= SIZEOF_LONG	long x; 	if (PyInt_Check(vv))		x = PyInt_AS_LONG(vv);	else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)		x = PyLong_AsLong(vv);	else		x = PyLong_AsUnsignedLong(vv);#else #ifndef HAVE_LONG_LONG#   error "PyLong_AsVoidPtr: sizeof(void*) > sizeof(long), but no long long"#endif#if SIZEOF_LONG_LONG < SIZEOF_VOID_P#   error "PyLong_AsVoidPtr: sizeof(PY_LONG_LONG) < sizeof(void*)"#endif	PY_LONG_LONG x; 	if (PyInt_Check(vv))		x = PyInt_AS_LONG(vv);	else if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)		x = PyLong_AsLongLong(vv);	else		x = PyLong_AsUnsignedLongLong(vv); #endif /* SIZEOF_VOID_P <= SIZEOF_LONG */ 	if (x == -1 && PyErr_Occurred())		return NULL;	return (void *)x;} #ifdef HAVE_LONG_LONG /* Initial PY_LONG_LONG support by Chris Herborth (chrish@qnx.com), later * rewritten to use the newer PyLong_{As,From}ByteArray API. */ #define IS_LITTLE_ENDIAN (int)*(unsigned char*)&one /* Create a new long int object from a C PY_LONG_LONG int. */ PyObject *PyLong_FromLongLong(PY_LONG_LONG ival){	PyLongObject *v;	unsigned PY_LONG_LONG t;  /* unsigned so >> doesn't propagate sign bit */	int ndigits = 0;	int negative = 0; 	if (ival < 0) {		ival = -ival;		negative = 1;	} 	/* Count the number of Python digits.	   We used to pick 5 ("big enough for anything"), but that's a	   waste of time and space given that 5*15 = 75 bits are rarely	   needed. */	t = (unsigned PY_LONG_LONG)ival;	while (t) {		++ndigits;		t >>= SHIFT;	}	v = _PyLong_New(ndigits);	if (v != NULL) {		digit *p = v->ob_digit;		v->ob_size = negative ? -ndigits : ndigits;		t = (unsigned PY_LONG_LONG)ival;		while (t) {			*p++ = (digit)(t & MASK);			t >>= SHIFT;		}	}	return (PyObject *)v;} /* Create a new long int object from a C unsigned PY_LONG_LONG int. */ PyObject *PyLong_FromUnsignedLongLong(unsigned PY_LONG_LONG ival){	PyLongObject *v;	unsigned PY_LONG_LONG t;	int ndigits = 0; 	/* Count the number of Python digits. */	t = (unsigned PY_LONG_LONG)ival;	while (t) {		++ndigits;		t >>= SHIFT;	}	v = _PyLong_New(ndigits);	if (v != NULL) {		digit *p = v->ob_digit;		v->ob_size = ndigits;		while (ival) {			*p++ = (digit)(ival & MASK);			ival >>= SHIFT;		}	}	return (PyObject *)v;} /* Create a new long int object from a C Py_ssize_t. */ PyObject *_PyLong_FromSsize_t(Py_ssize_t ival){	Py_ssize_t bytes = ival;	int one = 1;	return _PyLong_FromByteArray(			(unsigned char *)&bytes,			SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);} /* Create a new long int object from a C size_t. */ PyObject *_PyLong_FromSize_t(size_t ival){	size_t bytes = ival;	int one = 1;	return _PyLong_FromByteArray(			(unsigned char *)&bytes,			SIZEOF_SIZE_T, IS_LITTLE_ENDIAN, 0);} /* Get a C PY_LONG_LONG int from a long int object.   Return -1 and set an error if overflow occurs. */ PY_LONG_LONGPyLong_AsLongLong(PyObject *vv){	PY_LONG_LONG bytes;	int one = 1;	int res; 	if (vv == NULL) {		PyErr_BadInternalCall();		return -1;	}	if (!PyLong_Check(vv)) {		PyNumberMethods *nb;		PyObject *io;		if (PyInt_Check(vv))			return (PY_LONG_LONG)PyInt_AsLong(vv);		if ((nb = vv->ob_type->tp_as_number) == NULL ||		    nb->nb_int == NULL) {			PyErr_SetString(PyExc_TypeError, "an integer is required");			return -1;		}		io = (*nb->nb_int) (vv);		if (io == NULL)			return -1;		if (PyInt_Check(io)) {			bytes = PyInt_AsLong(io);			Py_DECREF(io);			return bytes;		}		if (PyLong_Check(io)) {			bytes = PyLong_AsLongLong(io);			Py_DECREF(io);			return bytes;		}		Py_DECREF(io);		PyErr_SetString(PyExc_TypeError, "integer conversion failed");		return -1;	} 	res = _PyLong_AsByteArray(			(PyLongObject *)vv, (unsigned char *)&bytes,			SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 1); 	/* Plan 9 can't handle PY_LONG_LONG in ? : expressions */	if (res < 0)		return (PY_LONG_LONG)-1;	else		return bytes;} /* Get a C unsigned PY_LONG_LONG int from a long int object.   Return -1 and set an error if overflow occurs. */ unsigned PY_LONG_LONGPyLong_AsUnsignedLongLong(PyObject *vv){	unsigned PY_LONG_LONG bytes;	int one = 1;	int res; 	if (vv == NULL || !PyLong_Check(vv)) {		PyErr_BadInternalCall();		return (unsigned PY_LONG_LONG)-1;	} 	res = _PyLong_AsByteArray(			(PyLongObject *)vv, (unsigned char *)&bytes,			SIZEOF_LONG_LONG, IS_LITTLE_ENDIAN, 0); 	/* Plan 9 can't handle PY_LONG_LONG in ? : expressions */	if (res < 0)		return (unsigned PY_LONG_LONG)res;	else		return bytes;} /* Get a C unsigned long int from a long int object, ignoring the high bits.   Returns -1 and sets an error condition if an error occurs. */ unsigned PY_LONG_LONGPyLong_AsUnsignedLongLongMask(PyObject *vv){	register PyLongObject *v;	unsigned PY_LONG_LONG x;	Py_ssize_t i;	int sign; 

Lines 501–1000 of 3458. Previous lines