[pypy-svn] pypy default: Rewrite in the same way int_floordiv.
arigo
commits-noreply at bitbucket.org
Fri Mar 25 17:39:57 CET 2011
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r42940:8e5db514fc1f
Date: 2011-03-25 17:34 +0100
http://bitbucket.org/pypy/pypy/changeset/8e5db514fc1f/
Log: Rewrite in the same way int_floordiv.
diff --git a/pypy/rpython/rint.py b/pypy/rpython/rint.py
--- a/pypy/rpython/rint.py
+++ b/pypy/rpython/rint.py
@@ -212,35 +212,18 @@
# 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
@@ -248,6 +231,18 @@
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
More information about the Pypy-commit
mailing list