[pypy-svn] r14047 - in pypy/dist/pypy/objspace/std: . test

tismer at codespeak.net tismer at codespeak.net
Fri Jul 1 19:09:10 CEST 2005


Author: tismer
Date: Fri Jul  1 19:09:09 2005
New Revision: 14047

Modified:
   pypy/dist/pypy/objspace/std/longobject.py
   pypy/dist/pypy/objspace/std/test/test_longobject.py
Log:
(Jacob, chris)
long division half-way done; tests on helpers are working

Modified: pypy/dist/pypy/objspace/std/longobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/longobject.py	(original)
+++ pypy/dist/pypy/objspace/std/longobject.py	Fri Jul  1 19:09:09 2005
@@ -648,9 +648,32 @@
     z._normalize()
     return z
 
+
 def _inplace_divrem1(pout, pin, n):
-    rem = r_uint(0, space)
+    """
+    Divide long pin by non-zero digit n, storing quotient
+    in pout, and returning the remainder. It's OK for pin == pout on entry.
+    """
+    rem = r_uint(0)
     assert n > 0 and n <= SHORT_MASK
     size = len(pin.digits) * 2 - 1
     while size >= 0:
         rem = (rem << SHORT_BIT) + pin._getshort(size)
+        hi = rem // n
+        pout._setshort(size, hi)
+        rem -= hi * n
+        size -= 1
+    return rem
+
+def _divrem1(space, a, n):
+    """
+    Divide a long integer by a digit, returning both the quotient
+    and the remainder as a tuple.
+    The sign of a is ignored; n should not be zero.
+    """
+    assert n > 0 and n <= SHORT_MASK
+    size = len(a.digits)
+    z = W_LongObject(space, [r_uint(0)] * size, 1)
+    rem = _inplace_divrem1(z, a, n)
+    z._normalize()
+    return z, rem

Modified: pypy/dist/pypy/objspace/std/test/test_longobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/test/test_longobject.py	(original)
+++ pypy/dist/pypy/objspace/std/test/test_longobject.py	Fri Jul  1 19:09:09 2005
@@ -50,6 +50,24 @@
         result = lobj.mul__Long_Long(self.space, f1, f2)
         assert result.longval() == x * y
 
+    def test__inplace_divrem1(self):
+        # signs are not handled in the helpers!
+        x = 1238585838347L
+        y = 3
+        f1 = lobj.W_LongObject(self.space, *lobj.args_from_long(x))
+        f2 = r_uint(y)
+        remainder = lobj._inplace_divrem1(f1, f1, f2)
+        assert (f1.longval(), remainder) == divmod(x, y)
+
+    def test__divrem1(self):
+        # signs are not handled in the helpers!
+        x = 1238585838347L
+        y = 3
+        f1 = lobj.W_LongObject(self.space, *lobj.args_from_long(x))
+        f2 = r_uint(y)
+        div, rem = lobj._divrem1(self.space, f1, f2)
+        assert (div.longval(), rem) == divmod(x, y)
+
     def test_eq(self):
         x = 5858393919192332223L
         y = 585839391919233111223311112332L



More information about the Pypy-commit mailing list