[pypy-commit] pypy default: Add most_pos_value_of(), similar to most_neg_value_of().
arigo
noreply at buildbot.pypy.org
Wed Feb 13 11:27:02 CET 2013
Author: Armin Rigo <arigo at tunes.org>
Branch:
Changeset: r61166:4958d1d8d8d9
Date: 2013-02-13 10:50 +0100
http://bitbucket.org/pypy/pypy/changeset/4958d1d8d8d9/
Log: Add most_pos_value_of(), similar to most_neg_value_of().
diff --git a/rpython/rlib/rarithmetic.py b/rpython/rlib/rarithmetic.py
--- a/rpython/rlib/rarithmetic.py
+++ b/rpython/rlib/rarithmetic.py
@@ -246,6 +246,23 @@
return r_class(0)
most_neg_value_of._annspecialcase_ = 'specialize:memo'
+def most_pos_value_of_same_type(x):
+ from rpython.rtyper.lltypesystem import lltype
+ return most_pos_value_of(lltype.typeOf(x))
+most_pos_value_of_same_type._annspecialcase_ = 'specialize:argtype(0)'
+
+def most_pos_value_of(tp):
+ from rpython.rtyper.lltypesystem import lltype, rffi
+ if tp is lltype.Signed:
+ return sys.maxint
+ r_class = rffi.platform.numbertype_to_rclass[tp]
+ assert issubclass(r_class, base_int)
+ if r_class.SIGNED:
+ return r_class(r_class.MASK >> 1)
+ else:
+ return r_class(r_class.MASK)
+most_pos_value_of._annspecialcase_ = 'specialize:memo'
+
def is_signed_integer_type(tp):
from rpython.rtyper.lltypesystem import lltype, rffi
if tp is lltype.Signed:
diff --git a/rpython/rlib/test/test_rarithmetic.py b/rpython/rlib/test/test_rarithmetic.py
--- a/rpython/rlib/test/test_rarithmetic.py
+++ b/rpython/rlib/test/test_rarithmetic.py
@@ -357,6 +357,14 @@
assert most_neg_value_of_same_type(r_longlong(123)) == llmin
assert most_neg_value_of_same_type(r_ulonglong(123)) == 0
+def test_most_pos_value_of():
+ assert most_pos_value_of_same_type(123) == sys.maxint
+ assert most_pos_value_of_same_type(r_uint(123)) == 2 * sys.maxint + 1
+ llmax_sign = (2**(r_longlong.BITS-1))-1
+ llmax_unsign = (2**r_longlong.BITS)-1
+ assert most_pos_value_of_same_type(r_longlong(123)) == llmax_sign
+ assert most_pos_value_of_same_type(r_ulonglong(123)) == llmax_unsign
+
def test_is_signed_integer_type():
from rpython.rtyper.lltypesystem import lltype, rffi
assert is_signed_integer_type(lltype.Signed)
More information about the pypy-commit
mailing list