183
183
CHECK_SMALL_INT(ival);
186
/* if LONG_MIN == -LONG_MAX-1 (true on most platforms) then
187
ANSI C says that the result of -ival is undefined when ival
188
== LONG_MIN. Hence the following workaround. */
189
abs_ival = (unsigned long)(-1-ival) + 1;
186
/* negate: can't write this as abs_ival = -ival since that
187
invokes undefined behaviour when ival is LONG_MIN */
188
abs_ival = 0U-(unsigned long)ival;
193
192
abs_ival = (unsigned long)ival;
196
/* Fast path for single-digits ints */
197
if (!(ival>>PyLong_SHIFT)) {
195
/* Fast path for single-digit ints */
196
if (!(abs_ival >> PyLong_SHIFT)) {
198
197
v = _PyLong_New(1);
200
199
Py_SIZE(v) = sign;
201
v->ob_digit[0] = ival;
200
v->ob_digit[0] = Py_SAFE_DOWNCAST(
201
abs_ival, unsigned long, digit);
203
203
return (PyObject*)v;
207
if (!(ival >> 2*PyLong_SHIFT)) {
207
if (!(abs_ival >> 2*PyLong_SHIFT)) {
208
208
v = _PyLong_New(2);
210
210
Py_SIZE(v) = 2*sign;
211
v->ob_digit[0] = (digit)ival & PyLong_MASK;
212
v->ob_digit[1] = ival >> PyLong_SHIFT;
211
v->ob_digit[0] = Py_SAFE_DOWNCAST(
212
abs_ival & PyLong_MASK, unsigned long, digit);
213
v->ob_digit[1] = Py_SAFE_DOWNCAST(
214
abs_ival >> PyLong_SHIFT, unsigned long, digit);
214
216
return (PyObject*)v;
768
771
unsigned char* bytes, size_t n,
769
772
int little_endian, int is_signed)
771
int i; /* index into v->ob_digit */
774
Py_ssize_t i; /* index into v->ob_digit */
772
775
Py_ssize_t ndigits; /* |v->ob_size| */
773
776
twodigits accum; /* sliding register */
774
777
unsigned int accumbits; /* # bits in accum */
775
778
int do_twos_comp; /* store 2's-comp? is_signed and v < 0 */
776
twodigits carry; /* for computing 2's-comp */
779
digit carry; /* for computing 2's-comp */
777
780
size_t j; /* # bytes filled */
778
781
unsigned char* p; /* pointer to next byte in bytes */
779
782
int pincr; /* direction to move p */
822
825
/* Because we're going LSB to MSB, thisdigit is more
823
826
significant than what's already in accum, so needs to be
824
827
prepended to accum. */
825
accum |= thisdigit << accumbits;
826
accumbits += PyLong_SHIFT;
828
accum |= (twodigits)thisdigit << accumbits;
828
830
/* The most-significant digit may be (probably is) at least
830
832
if (i == ndigits - 1) {
831
833
/* Count # of sign bits -- they needn't be stored,
832
834
* although for signed conversion we need later to
833
* make sure at least one sign bit gets stored.
834
* First shift conceptual sign bit to real sign bit.
836
stwodigits s = (stwodigits)(thisdigit <<
837
(8*sizeof(stwodigits) - PyLong_SHIFT));
838
unsigned int nsignbits = 0;
839
while ((s < 0) == do_twos_comp && nsignbits < PyLong_SHIFT) {
835
* make sure at least one sign bit gets stored. */
836
digit s = do_twos_comp ? thisdigit ^ PyLong_MASK :
843
accumbits -= nsignbits;
844
accumbits += PyLong_SHIFT;
846
846
/* Store as many bytes as possible. */
847
847
while (accumbits >= 8) {
1712
1712
while (--p >= start) {
1713
1713
int k = _PyLong_DigitValue[Py_CHARMASK(*p)];
1714
1714
assert(k >= 0 && k < base);
1715
accum |= (twodigits)(k << bits_in_accum);
1715
accum |= (twodigits)k << bits_in_accum;
1716
1716
bits_in_accum += bits_per_char;
1717
1717
if (bits_in_accum >= PyLong_SHIFT) {
1718
1718
*pdigit++ = (digit)(accum & PyLong_MASK);
1719
assert(pdigit - z->ob_digit <= (int)n);
1719
assert(pdigit - z->ob_digit <= n);
1720
1720
accum >>= PyLong_SHIFT;
1721
1721
bits_in_accum -= PyLong_SHIFT;
1722
1722
assert(bits_in_accum < PyLong_SHIFT);
2232
2230
return sign < 0 ? -1 : sign > 0 ? 1 : 0;
2233
#define TEST_COND(cond) \
2234
((cond) ? Py_True : Py_False)
2235
2236
static PyObject *
2236
2237
long_richcompare(PyObject *self, PyObject *other, int op)
2239
2241
CHECK_BINOP(self, other);
2240
result = Py_CmpToRich(op, long_compare((PyLongObject*)self,
2241
(PyLongObject*)other));
2245
result = long_compare((PyLongObject*)self, (PyLongObject*)other);
2246
/* Convert the return value to a Boolean */
2249
v = TEST_COND(result == 0);
2252
v = TEST_COND(result != 0);
2255
v = TEST_COND(result <= 0);
2258
v = TEST_COND(result >= 0);
2261
v = TEST_COND(result == -1);
2264
v = TEST_COND(result == 1);
2267
PyErr_BadArgument();
2246
2275
long_hash(PyLongObject *v)
2267
2296
#define LONG_BIT_PyLong_SHIFT (8*sizeof(long) - PyLong_SHIFT)
2268
/* The following loop produces a C long x such that (unsigned long)x
2269
is congruent to the absolute value of v modulo ULONG_MAX. The
2297
/* The following loop produces a C unsigned long x such that x is
2298
congruent to the absolute value of v modulo ULONG_MAX. The
2270
2299
resulting x is nonzero if and only if v is. */
2271
2300
while (--i >= 0) {
2272
2301
/* Force a native long #-bits (32 or 64) circular shift */
2273
x = ((x << PyLong_SHIFT) & ~PyLong_MASK) | ((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
2302
x = ((x << PyLong_SHIFT) & ~PyLong_MASK) |
2303
((x >> LONG_BIT_PyLong_SHIFT) & PyLong_MASK);
2274
2304
x += v->ob_digit[i];
2275
/* If the addition above overflowed (thinking of x as
2276
unsigned), we compensate by incrementing. This preserves
2277
the value modulo ULONG_MAX. */
2278
if ((unsigned long)x < v->ob_digit[i])
2305
/* If the addition above overflowed we compensate by
2306
incrementing. This preserves the value modulo
2308
if (x < v->ob_digit[i])
2281
2311
#undef LONG_BIT_PyLong_SHIFT
3613
3643
PyUnicode_GET_SIZE(format_spec));
3617
3646
static PyObject *
3618
3647
long_round(PyObject *self, PyObject *args)
3620
#define UNDEF_NDIGITS (-0x7fffffff) /* Unlikely ndigits value */
3621
int ndigits = UNDEF_NDIGITS;
3625
if (!PyArg_ParseTuple(args, "|i", &ndigits))
3628
if (ndigits == UNDEF_NDIGITS)
3629
return long_long(self);
3631
/* If called with two args, defer to float.__round__(). */
3632
x = PyLong_AsDouble(self);
3633
if (x == -1.0 && PyErr_Occurred())
3635
self = PyFloat_FromDouble(x);
3649
PyObject *o_ndigits=NULL, *temp;
3650
PyLongObject *pow=NULL, *q=NULL, *r=NULL, *ndigits=NULL, *one;
3654
/* Notes on the algorithm: to round to the nearest 10**n (n positive),
3655
the straightforward method is:
3658
(2) round to nearest integer (round to even in case of tie)
3659
(3) multiply result by 10**n.
3661
But the rounding step involves examining the fractional part of the
3662
quotient to see whether it's greater than 0.5 or not. Since we
3663
want to do the whole calculation in integer arithmetic, it's
3666
(1) divide by (10**n)/2
3667
(2) round to nearest multiple of 2 (multiple of 4 in case of tie)
3668
(3) multiply result by (10**n)/2.
3670
Then all we need to know about the fractional part of the quotient
3671
arising in step (2) is whether it's zero or not.
3673
Doing both a multiplication and division is wasteful, and is easily
3674
avoided if we just figure out how much to adjust the original input
3675
by to do the rounding.
3677
Here's the whole algorithm expressed in Python.
3679
def round(self, ndigits = None):
3680
"""round(int, int) -> int"""
3681
if ndigits is None or ndigits >= 0:
3683
pow = 10**-ndigits >> 1
3684
q, r = divmod(self, pow)
3687
if (q & 2 == r == 0):
3694
if (!PyArg_ParseTuple(args, "|O", &o_ndigits))
3696
if (o_ndigits == NULL)
3697
return long_long(self);
3699
ndigits = (PyLongObject *)PyNumber_Index(o_ndigits);
3700
if (ndigits == NULL)
3703
if (Py_SIZE(ndigits) >= 0) {
3705
return long_long(self);
3708
Py_INCREF(self); /* to keep refcounting simple */
3709
/* we now own references to self, ndigits */
3711
/* pow = 10 ** -ndigits >> 1 */
3712
pow = (PyLongObject *)PyLong_FromLong(10L);
3715
temp = long_neg(ndigits);
3717
ndigits = (PyLongObject *)temp;
3718
if (ndigits == NULL)
3720
temp = long_pow((PyObject *)pow, (PyObject *)ndigits, Py_None);
3722
pow = (PyLongObject *)temp;
3725
assert(PyLong_Check(pow)); /* check long_pow returned a long */
3726
one = (PyLongObject *)PyLong_FromLong(1L);
3729
temp = long_rshift(pow, one);
3732
pow = (PyLongObject *)temp;
3736
/* q, r = divmod(self, pow) */
3737
errcode = l_divmod((PyLongObject *)self, pow, &q, &r);
3742
temp = long_sub((PyLongObject *)self, r);
3636
3745
if (self == NULL)
3638
res = PyObject_CallMethod(self, "__round__", "i", ndigits);
3641
#undef UNDEF_NDIGITS
3748
/* get value of quotient modulo 4 */
3749
if (Py_SIZE(q) == 0)
3751
else if (Py_SIZE(q) > 0)
3752
q_mod_4 = q->ob_digit[0] & 3;
3754
q_mod_4 = (PyLong_BASE-q->ob_digit[0]) & 3;
3756
if ((q_mod_4 & 1) == 1) {
3757
/* q is odd; round self up or down by adding or subtracting pow */
3758
if (q_mod_4 == 1 && Py_SIZE(r) == 0)
3759
temp = (PyObject *)long_sub((PyLongObject *)self, pow);
3761
temp = (PyObject *)long_add((PyLongObject *)self, pow);
3778
Py_XDECREF(ndigits);
3644
3782
static PyObject *
3672
3810
{"__ceil__", (PyCFunction)long_long, METH_NOARGS,
3673
3811
"Ceiling of an Integral returns itself."},
3674
3812
{"__round__", (PyCFunction)long_round, METH_VARARGS,
3675
"Rounding an Integral returns itself.\n"
3676
"Rounding with an ndigits arguments defers to float.__round__."},
3813
"Rounding an Integral returns itself.\n"
3814
"Rounding with an ndigits argument also returns an integer."},
3677
3815
{"__getnewargs__", (PyCFunction)long_getnewargs, METH_NOARGS},
3678
3816
{"__format__", (PyCFunction)long__format__, METH_VARARGS},
3679
3817
{"__sizeof__", (PyCFunction)long_sizeof, METH_NOARGS,