summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2003-01-31 15:52:05 (GMT)
committerTim Peters <tim.peters@gmail.com>2003-01-31 15:52:05 (GMT)
commit5b8132ffa34244cc24b603d8462e6e733e014e71 (patch)
tree9ed5a901229b5dfef77b2f88ddf961f57a211f1a /Objects
parent89fc4f3e5623d24632b1daf503a2f32ac2bd0d66 (diff)
downloadcpython-5b8132ffa34244cc24b603d8462e6e733e014e71.zip
cpython-5b8132ffa34244cc24b603d8462e6e733e014e71.tar.gz
cpython-5b8132ffa34244cc24b603d8462e6e733e014e71.tar.bz2
_PyLong_NumBits(): The definition of this was too specific to the quirky
needs of pickling longs. Backed off to a definition that's much easier to understand. The pickler will have to work a little harder, but other uses are more likely to be correct <0.5 wink>. _PyLong_Sign(): New teensy function to characterize a long, as to <0, ==0, or >0.
Diffstat (limited to 'Objects')
-rw-r--r--Objects/longobject.c25
1 files changed, 17 insertions, 8 deletions
diff --git a/Objects/longobject.c b/Objects/longobject.c
index 7a04f1e..2279fc3 100644
--- a/Objects/longobject.c
+++ b/Objects/longobject.c
@@ -260,25 +260,34 @@ PyLong_AsUnsignedLong(PyObject *vv)
return x;
}
+int
+_PyLong_Sign(PyObject *vv)
+{
+ PyLongObject *v = (PyLongObject *)vv;
+ const int ndigits = v->ob_size;
+
+ assert(v != NULL);
+ assert(PyLong_Check(v));
+ assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
+
+ return ndigits == 0 ? 0 : (ndigits < 0 ? -1 : 1);
+}
+
size_t
_PyLong_NumBits(PyObject *vv)
{
PyLongObject *v = (PyLongObject *)vv;
- size_t result = 1; /* for the sign bit */
- size_t ndigits = ABS(v->ob_size);
+ size_t result = 0;
+ int ndigits = ABS(v->ob_size);
assert(v != NULL);
assert(PyLong_Check(v));
assert(ndigits == 0 || v->ob_digit[ndigits - 1] != 0);
if (ndigits > 0) {
- size_t product;
digit msd = v->ob_digit[ndigits - 1];
- product = (ndigits - 1) * SHIFT;
- if (product / SHIFT != ndigits - 1)
- goto Overflow;
- result += product;
- if (result < product)
+ result = (ndigits - 1) * SHIFT;
+ if (result / SHIFT != ndigits - 1)
goto Overflow;
do {
++result;