[Python-checkins] r81036 - python/trunk/Objects/longobject.c (original) (raw)

mark.dickinson python-checkins at python.org
Sun May 9 22:30:29 CEST 2010


Author: mark.dickinson Date: Sun May 9 22:30:29 2010 New Revision: 81036

Log: Post-detabification cleanup: whitespace fixes and long line rewraps only.

Modified: python/trunk/Objects/longobject.c

Modified: python/trunk/Objects/longobject.c

--- python/trunk/Objects/longobject.c (original) +++ python/trunk/Objects/longobject.c Sun May 9 22:30:29 2010 @@ -1,5 +1,3 @@

@@ -177,12 +175,12 @@ neg = 0; if (Py_IS_INFINITY(dval)) { PyErr_SetString(PyExc_OverflowError,

@@ -412,7 +411,7 @@ x = 0; if (i < 0) { PyErr_SetString(PyExc_OverflowError,

-Overflow:

{

@@ -619,9 +617,9 @@ int little_endian, int is_signed) { Py_ssize_t i; /* index into v->ob_digit */

@@ -680,8 +678,7 @@ /* 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. */

@@ -741,7 +738,7 @@

 return 0;

-Overflow:

@@ -821,7 +818,7 @@ */

#define IS_LITTLE_ENDIAN (int)(unsigned char)&one -#define PY_ABS_LLONG_MIN (0-(unsigned PY_LONG_LONG)PY_LLONG_MIN) +#define PY_ABS_LLONG_MIN (0-(unsigned PY_LONG_LONG)PY_LLONG_MIN)

/* Create a new long int object from a C PY_LONG_LONG int. */

@@ -900,9 +897,8 @@ { Py_ssize_t bytes = ival; int one = 1;

}

/* Create a new long int object from a C size_t. */ @@ -912,9 +908,8 @@ { size_t bytes = ival; int one = 1;

}

/* Get a C PY_LONG_LONG int from a long int object. @@ -959,9 +954,8 @@ return -1; }

@@ -985,9 +979,8 @@ return (unsigned PY_LONG_LONG)-1; }

@@ -1120,7 +1113,7 @@ /* res is already set to -1 */ } }

@@ -1506,8 +1499,7 @@ *--p = cdigit; accumbits -= basebits; accum >>= basebits;

@@ -1762,14 +1754,15 @@ PyLong_BASEn >= BN [taking logs to base PyLong_BASE] n >= log(B**N)/log(PyLong_BASE) = N * log(B)/log(PyLong_BASE)

-The static array log_base_PyLong_BASE[base] == log(base)/log(PyLong_BASE) so we can compute -this quickly. A Python long with that much space is reserved near the start, -and the result is computed into it. +The static array log_base_PyLong_BASE[base] == log(base)/log(PyLong_BASE) so +we can compute this quickly. A Python long with that much space is reserved +near the start, and the result is computed into it.

The input string is actually treated as being in base base**i (i.e., i digits are processed at a time), where two more static arrays hold:

The first of these is the largest i such that i consecutive input digits @@ -1793,33 +1786,34 @@ size_z = (Py_ssize_t)((scan - str) * log_base_PyLong_BASE[base]) + 1;

below. Two numeric concerns are how much space this can waste, and whether -the computed result can be too small. To be concrete, assume PyLong_BASE = 215, -which is the default (and it's unlikely anyone changes that). +the computed result can be too small. To be concrete, assume PyLong_BASE = +215, which is the default (and it's unlikely anyone changes that).

-Waste isn't a problem: provided the first input digit isn't 0, the difference +Waste isn't a problem: provided the first input digit isn't 0, the difference between the worst-case input with N digits and the smallest input with N -digits is about a factor of B, but B is small compared to PyLong_BASE so at most -one allocated Python digit can remain unused on that count. If -Nlog(B)/log(PyLong_BASE) is mathematically an exact integer, then truncating that -and adding 1 returns a result 1 larger than necessary. However, that can't -happen: whenever B is a power of 2, long_from_binary_base() is called -instead, and it's impossible for Bi to be an integer power of 215 when -B is not a power of 2 (i.e., it's impossible for Nlog(B)/log(PyLong_BASE) to be +digits is about a factor of B, but B is small compared to PyLong_BASE so at +most one allocated Python digit can remain unused on that count. If +Nlog(B)/log(PyLong_BASE) is mathematically an exact integer, then truncating +that and adding 1 returns a result 1 larger than necessary. However, that +can't happen: whenever B is a power of 2, long_from_binary_base() is called +instead, and it's impossible for Bi to be an integer power of 215 when B +is not a power of 2 (i.e., it's impossible for Nlog(B)/log(PyLong_BASE) to be an exact integer when B is not a power of 2, since Bi has a prime factor other than 2 in that case, but (215)**j's only prime factor is 2).

-The computed result can be too small if the true value of Nlog(B)/log(PyLong_BASE) -is a little bit larger than an exact integer, but due to roundoff errors (in -computing log(B), log(PyLong_BASE), their quotient, and/or multiplying that by N) -yields a numeric result a little less than that integer. Unfortunately, "how -close can a transcendental function get to an integer over some range?" -questions are generally theoretically intractable. Computer analysis via -continued fractions is practical: expand log(B)/log(PyLong_BASE) via continued -fractions, giving a sequence i/j of "the best" rational approximations. Then -jlog(B)/log(PyLong_BASE) is approximately equal to (the integer) i. This shows that -we can get very close to being in trouble, but very rarely. For example, -76573 is a denominator in one of the continued-fraction approximations to -log(10)/log(215), and indeed: +The computed result can be too small if the true value of +Nlog(B)/log(PyLong_BASE) is a little bit larger than an exact integer, but +due to roundoff errors (in computing log(B), log(PyLong_BASE), their quotient, +and/or multiplying that by N) yields a numeric result a little less than that +integer. Unfortunately, "how close can a transcendental function get to an +integer over some range?" questions are generally theoretically intractable. +Computer analysis via continued fractions is practical: expand +log(B)/log(PyLong_BASE) via continued fractions, giving a sequence i/j of "the +best" rational approximations. Then jlog(B)/log(PyLong_BASE) is +approximately equal to (the integer) i. This shows that we can get very close +to being in trouble, but very rarely. For example, 76573 is a denominator in +one of the continued-fraction approximations to log(10)/log(215), and +indeed:

 >>> log(10)/log(2**15)*76573
 16958.000000654003

@@ -1850,8 +1844,8 @@ twodigits convmax = base; int i = 1;

@@ -1895,7 +1889,7 @@ c = (digit)_PyLong_DigitValue[Py_CHARMASK(*str++)]; for (i = 1; i < convwidth && str != scan; ++i, ++str) { c = (twodigits)(c * base +

@@ -1960,7 +1954,7 @@ *pend = str; return (PyObject *) z;

@@ -2153,7 +2147,7 @@ (stwodigits)q * (stwodigits)w0[i]; vk[i] = (digit)z & PyLong_MASK; zhi = (sdigit)Py_ARITHMETIC_RIGHT_SHIFT(stwodigits,

@@ -2220,7 +2214,7 @@ if (a_size >= (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 && (a_size > (PY_SSIZE_T_MAX - 1) / PyLong_SHIFT + 1 || a_bits > (PY_SSIZE_T_MAX - 1) % PyLong_SHIFT + 1))

@@ -2278,7 +2272,8 @@ break; } }

@@ -2422,8 +2417,8 @@ if (size_a < size_b) { { PyLongObject *temp = a; a = b; b = temp; } { Py_ssize_t size_temp = size_a;

@@ -2581,9 +2576,9 @@ digit *paend = a->ob_digit + size_a;

         SIGCHECK({

@@ -2619,9 +2614,9 @@ digit *pbend = b->ob_digit + size_b;

         SIGCHECK({

@@ -2645,7 +2640,10 @@ Returns 0 on success, -1 on failure. */ static int -kmul_split(PyLongObject *n, Py_ssize_t size, PyLongObject **high, PyLongObject **low) +kmul_split(PyLongObject *n,

{ PyLongObject *hi, *lo; Py_ssize_t size_lo, size_hi; @@ -2834,7 +2832,7 @@

 return long_normalize(ret);

@@ -3411,7 +3409,7 @@ if (Py_SIZE(b) < 0) { /* if exponent is negative */ if (c) { PyErr_SetString(PyExc_TypeError, "pow() 2nd argument "

@@ -3541,13 +3539,13 @@ } goto Done;

@@ -3658,12 +3656,11 @@ for (i = 0, j = wordshift; i < newsize; i++, j++) { z->ob_digit[i] = (a->ob_digit[j] >> loshift) & lomask; if (i+1 < newsize)

{ int nega, negb, negz; Py_ssize_t size_a, size_b, size_z, i; @@ -3933,13 +3930,13 @@ x = PyLong_AsLong(v); if (PyErr_Occurred()) { if (PyErr_ExceptionMatches(PyExc_OverflowError)) {

@@ -4000,8 +3997,8 @@ if (srepr == NULL) return NULL; PyErr_Format(PyExc_ValueError,

@@ -4015,7 +4012,7 @@ #endif else { PyErr_SetString(PyExc_TypeError,

-error:

static PyNumberMethods long_as_number = {

};

PyTypeObject PyLong_Type = { @@ -4290,7 +4287,7 @@ 0, /* tp_setattro / 0, / tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |

};

static PyTypeObject Long_InfoType; @@ -4322,8 +4319,7 @@

static PyStructSequence_Field long_info_fields[] = { {"bits_per_digit", "size of a digit in bits"},

@@ -4331,7 +4327,7 @@ "sys.long_info", /* name / long_info__doc__, / doc / long_info_fields, / fields */

};

PyObject *



More information about the Python-checkins mailing list