Commits

Armin Rigo committed 8e5db51

Rewrite in the same way int_floordiv.

  • Participants
  • Parent commits e43b8c3

Comments (0)

Files changed (1)

File pypy/rpython/rint.py

         # cpython, and rpython, assumed that integer division truncates
         # towards -infinity.  however, in C99 and most (all?) other
         # backends, integer division truncates towards 0.  so assuming
-        # that, we can generate scary code that applies the necessary
+        # that, we call a helper function that applies the necessary
         # correction in the right cases.
-        # paper and pencil are encouraged for this :)
-
-        from pypy.rpython.rbool import bool_repr
-        assert isinstance(repr.lowleveltype, Number)
-        c_zero = inputconst(repr.lowleveltype, repr.lowleveltype._default)
 
         op = func.split('_', 1)[0]
 
         if op == 'floordiv':
-            # return (x/y) - (((x^y)<0)&((x%y)!=0));
-            v_xor = hop.genop(prefix + 'xor', vlist,
-                            resulttype=repr)
-            v_xor_le = hop.genop(prefix + 'lt', [v_xor, c_zero],
-                                 resulttype=Bool)
-            v_xor_le = hop.llops.convertvar(v_xor_le, bool_repr, repr)
-            v_mod = hop.genop(prefix + 'mod', vlist,
-                            resulttype=repr)
-            v_mod_ne = hop.genop(prefix + 'ne', [v_mod, c_zero],
-                               resulttype=Bool)
-            v_mod_ne = hop.llops.convertvar(v_mod_ne, bool_repr, repr)
-            v_corr = hop.genop(prefix + 'and', [v_xor_le, v_mod_ne],
-                             resulttype=repr)
-            v_res = hop.genop(prefix + 'sub', [v_res, v_corr],
-                              resulttype=repr)
+            llfunc = globals()['ll_correct_' + prefix + 'floordiv']
+            v_res = hop.gendirectcall(llfunc, vlist[0], vlist[1], v_res)
         elif op == 'mod':
             llfunc = globals()['ll_correct_' + prefix + 'mod']
             v_res = hop.gendirectcall(llfunc, vlist[1], v_res)
+
     v_res = hop.llops.convertvar(v_res, repr, r_result)
     return v_res
 
 INT_BITS_1 = r_int.BITS - 1
 LLONG_BITS_1 = r_longlong.BITS - 1
 
+def ll_correct_int_floordiv(x, y, r):
+    p = r * y
+    if y < 0: u = p - x
+    else:     u = x - p
+    return r + (u >> INT_BITS_1)
+
+def ll_correct_llong_floordiv(x, y, r):
+    p = r * y
+    if y < 0: u = p - x
+    else:     u = x - p
+    return r + (u >> LLONG_BITS_1)
+
 def ll_correct_int_mod(y, r):
     if y < 0: u = -r
     else:     u = r