[pypy-commit] lang-smalltalk default: (cfbolz, lwassermann): implemented unwrap_uint using polymorphism to get rid of loop and extensive branches
lwassermann
noreply at buildbot.pypy.org
Fri Apr 12 00:12:33 CEST 2013
Author: Lars Wassermann <lars.wassermann at gmail.com>
Branch:
Changeset: r255:8f0af90c962e
Date: 2013-04-11 15:04 +0200
http://bitbucket.org/pypy/lang-smalltalk/changeset/8f0af90c962e/
Log: (cfbolz, lwassermann): implemented unwrap_uint using polymorphism to
get rid of loop and extensive branches
diff --git a/spyvm/model.py b/spyvm/model.py
--- a/spyvm/model.py
+++ b/spyvm/model.py
@@ -127,6 +127,9 @@
def rshift(self, space, shift):
raise error.PrimitiveFailedError()
+ def unwrap_uint(self, space):
+ raise error.UnwrappingError("Got unexpected class in unwrap_uint")
+
class W_SmallInteger(W_Object):
"""Boxed integer value"""
# TODO can we tell pypy that its never larger then 31-bit?
@@ -165,6 +168,14 @@
def rshift(self, space, shift):
return space.wrap_int(self.value >> shift)
+ def unwrap_uint(self, space):
+ from rpython.rlib.rarithmetic import r_uint
+ val = self.value
+ if val < 0:
+ raise error.UnwrappingError("got negative integer")
+ return r_uint(val)
+
+
@jit.elidable
def as_repr_string(self):
return "W_SmallInteger(%d)" % self.value
@@ -262,6 +273,10 @@
# and only in this case we do need such a mask
return space.wrap_int((self.value >> shift) & mask)
+ def unwrap_uint(self, space):
+ from rpython.rlib.rarithmetic import r_uint
+ return r_uint(self.value)
+
def clone(self, space):
return W_LargePositiveInteger1Word(self.value)
@@ -651,6 +666,16 @@
w_result.bytes = list(self.bytes)
return w_result
+ def unwrap_uint(self, space):
+ # TODO: Completely untested! This failed translation bigtime...
+ # XXX Probably we want to allow all subclasses
+ if not self.getclass(space).is_same_object(space.w_LargePositiveInteger):
+ raise error.UnwrappingError("Failed to convert bytes to word")
+ word = 0
+ for i in range(self.size()):
+ word += r_uint(ord(self.getchar(i))) << 8*i
+ return word
+
class W_WordsObject(W_AbstractObjectWithClassReference):
_attrs_ = ['words']
diff --git a/spyvm/objspace.py b/spyvm/objspace.py
--- a/spyvm/objspace.py
+++ b/spyvm/objspace.py
@@ -251,24 +251,7 @@
raise UnwrappingError("expected a W_SmallInteger or W_LargePositiveInteger1Word, got %s" % (w_value,))
def unwrap_uint(self, w_value):
- if isinstance(w_value, model.W_SmallInteger):
- val = w_value.value
- if val < 0:
- raise UnwrappingError("got negative integer")
- return r_uint(w_value.value)
- elif isinstance(w_value, model.W_LargePositiveInteger1Word):
- return r_uint(w_value.value)
- elif isinstance(w_value, model.W_BytesObject):
- # TODO: Completely untested! This failed translation bigtime...
- # XXX Probably we want to allow all subclasses
- if not w_value.getclass(self).is_same_object(self.w_LargePositiveInteger):
- raise UnwrappingError("Failed to convert bytes to word")
- word = 0
- for i in range(w_value.size()):
- word += r_uint(ord(w_value.getchar(i))) << 8*i
- return word
- else:
- raise UnwrappingError("Got unexpected class in unwrap_uint")
+ return w_value.unwrap_uint(self)
def unwrap_positive_32bit_int(self, w_value):
if isinstance(w_value, model.W_SmallInteger):
More information about the pypy-commit
mailing list