[pypy-commit] pypy numpy-dtype-refactor: start re-adding many ops

alex_gaynor noreply at buildbot.pypy.org
Fri Nov 11 17:02:16 CET 2011


Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: numpy-dtype-refactor
Changeset: r49323:156f37cb96f3
Date: 2011-11-10 14:45 -0500
http://bitbucket.org/pypy/pypy/changeset/156f37cb96f3/

Log:	start re-adding many ops

diff --git a/pypy/module/micronumpy/interp_boxes.py b/pypy/module/micronumpy/interp_boxes.py
--- a/pypy/module/micronumpy/interp_boxes.py
+++ b/pypy/module/micronumpy/interp_boxes.py
@@ -31,15 +31,18 @@
             return getattr(interp_ufuncs.get(space), ufunc_name).call(space, [self, w_other])
         return func_with_new_name(impl, "binop_%s_impl" % ufunc_name)
 
+    descr_add = _binop_impl("add")
     descr_div = _binop_impl("divide")
-
     descr_eq = _binop_impl("equal")
 
 
-class W_BoolBox(Wrappable):
+class W_BoolBox(W_GenericBox):
     def __init__(self, value):
         self.value = value
 
+    def convert_to(self, dtype):
+        return dtype.box(self.value)
+
 class W_NumberBox(W_GenericBox):
     def __init__(self, value):
         self.value = value
@@ -107,6 +110,7 @@
     __int__ = interp2app(W_GenericBox.descr_int),
     __float__ = interp2app(W_GenericBox.descr_float),
 
+    __add__ = interp2app(W_GenericBox.descr_add),
     __div__ = interp2app(W_GenericBox.descr_div),
     __eq__ = interp2app(W_GenericBox.descr_eq),
 
diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -141,7 +141,7 @@
         i = 0
         while i < size:
             all_driver.jit_merge_point(signature=self.signature, self=self, dtype=dtype, size=size, i=i)
-            if not dtype.bool(self.eval(i)):
+            if not dtype.itemtype.bool(self.eval(i)):
                 return False
             i += 1
         return True
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
@@ -1,3 +1,5 @@
+import math
+
 from pypy.module.micronumpy import interp_boxes
 from pypy.objspace.std.floatobject import float2string
 from pypy.rlib import rfloat
@@ -5,6 +7,11 @@
 from pypy.rpython.lltypesystem import lltype, rffi
 
 
+def simple_op(func):
+    def dispatcher(self, v1, v2):
+        return self.box(func(self, self.unbox(v1), self.unbox(v2)))
+    return dispatcher
+
 class BaseType(object):
     def _unimplemented_ufunc(self, *args):
         raise NotImplementedError
@@ -52,12 +59,18 @@
     def add(self, v1, v2):
         return self.box(self.unbox(v1) + self.unbox(v2))
 
-    def div(self, v1, v2):
-        return self.box(self.unbox(v1) / self.unbox(v2))
+    def sub(self, v1, v2):
+        return self.box(self.unbox(v1) - self.unbox(v2))
+
+    def mul(self, v1, v2):
+        return self.box(self.unbox(v1) * self.unbox(v2))
 
     def eq(self, v1, v2):
         return self.unbox(v1) == self.unbox(v2)
 
+    def bool(self, v):
+        return bool(v)
+
     def max(self, v1, v2):
         return self.box(max(self.unbox(v1), self.unbox(v2)))
 
@@ -93,6 +106,12 @@
         value = self.unbox(box)
         return str(value)
 
+    @simple_op
+    def div(self, v1, v2):
+        if v2 == 0:
+            return 0
+        return v1 / v2
+
 class Int8(Integer):
     T = rffi.SIGNEDCHAR
     BoxType = interp_boxes.W_Int8Box
@@ -141,6 +160,19 @@
         value = self.unbox(box)
         return float2string(value, "g", rfloat.DTSF_STR_PRECISION)
 
+    @simple_op
+    def div(self, v1, v2):
+        try:
+            return v1 / v2
+        except ZeroDivisionError:
+            if v1 == v2 == 0.0:
+                return rfloat.NAN
+            return rfloat.copysign(rfloat.INFINITY, v1 * v2)
+
+    @simple_op
+    def pow(self, v1, v2):
+        return math.pow(v1, v2)
+
 class Float32(Float):
     T = rffi.FLOAT
     BoxType = interp_boxes.W_Float32Box


More information about the pypy-commit mailing list