[pypy-commit] pypy numpy-dtype-refactor: don't try to do arithmatic on small types

alex_gaynor noreply at buildbot.pypy.org
Sat Nov 12 01:07:37 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: numpy-dtype-refactor
Changeset: r49351:bb6c5b0c09a7
Date: 2011-11-11 19:03 -0500
http://bitbucket.org/pypy/pypy/changeset/bb6c5b0c09a7/

Log:	don't try to do arithmatic on small types

diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -4,18 +4,29 @@
 from pypy.objspace.std.floatobject import float2string
 from pypy.rlib import rfloat
 from pypy.rlib.objectmodel import specialize
-from pypy.rlib.rarithmetic import LONG_BIT
+from pypy.rlib.rarithmetic import LONG_BIT, widen
 from pypy.rpython.lltypesystem import lltype, rffi
 
 
 def simple_unary_op(func):
     def dispatcher(self, v):
-        return self.box(func(self, self.unbox(v)))
+        return self.box(
+            func(
+                self,
+                self.for_computation(self.unbox(v))
+            )
+        )
     return dispatcher
 
 def simple_binary_op(func):
     def dispatcher(self, v1, v2):
-        return self.box(func(self, self.unbox(v1), self.unbox(v2)))
+        return self.box(
+            func(
+                self,
+                self.for_computation(self.unbox(v1)),
+                self.for_computation(self.unbox(v2)),
+            )
+        )
     return dispatcher
 
 class BaseType(object):
@@ -64,20 +75,25 @@
             rffi.cast(rffi.CArrayPtr(self.T), ptr)[0] = value
             ptr = rffi.ptradd(ptr, self.get_element_size())
 
+    @simple_binary_op
     def add(self, v1, v2):
-        return self.box(self.unbox(v1) + self.unbox(v2))
+        return v1 + v2
 
+    @simple_binary_op
     def sub(self, v1, v2):
-        return self.box(self.unbox(v1) - self.unbox(v2))
+        return v1 - v2
 
+    @simple_binary_op
     def mul(self, v1, v2):
-        return self.box(self.unbox(v1) * self.unbox(v2))
+        return v1 * v2
 
+    @simple_unary_op
     def pos(self, v):
-        return self.box(+self.unbox(v))
+        return +v
 
+    @simple_unary_op
     def neg(self, v):
-        return self.box(-self.unbox(v))
+        return -v
 
     @simple_unary_op
     def abs(self, v):
@@ -104,11 +120,13 @@
     def bool(self, v):
         return bool(self.unbox(v))
 
+    @simple_binary_op
     def max(self, v1, v2):
-        return self.box(max(self.unbox(v1), self.unbox(v2)))
+        return max(v1, v2)
 
+    @simple_binary_op
     def min(self, v1, v2):
-        return self.box(min(self.unbox(v1), self.unbox(v2)))
+        return min(v1, v2)
 
 class Bool(BaseType, Primitive):
     T = lltype.Bool
@@ -132,6 +150,9 @@
         value = self.unbox(box)
         return "True" if value else "False"
 
+    def for_computation(self, v):
+        return int(v)
+
 class Integer(Primitive):
     _mixin_ = True
 
@@ -142,6 +163,9 @@
         value = self.unbox(box)
         return str(value)
 
+    def for_computation(self, v):
+        return widen(v)
+
     @simple_binary_op
     def div(self, v1, v2):
         if v2 == 0:
@@ -212,6 +236,9 @@
         value = self.unbox(box)
         return float2string(value, "g", rfloat.DTSF_STR_PRECISION)
 
+    def for_computation(self, v):
+        return float(v)
+
     @simple_binary_op
     def div(self, v1, v2):
         try:


More information about the pypy-commit mailing list