Commits

Armin Rigo committed 4958d1d

Add most_pos_value_of(), similar to most_neg_value_of().

  • Participants
  • Parent commits 2242067

Comments (0)

Files changed (2)

File rpython/rlib/rarithmetic.py

         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:

File rpython/rlib/test/test_rarithmetic.py

     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)