Commits

Brian Kearns committed 7c03625

fix reciprocal of numpy int(0)

  • Participants
  • Parent commits 94270f9

Comments (0)

Files changed (2)

File pypy/module/micronumpy/test/test_ufuncs.py

             reference = [0, -1, 0, 1, 0]
             if dtype[0] == 'u':
                 reference[1] = 0
-            # XXX need to fix specialization issue in types.py first
-            #elif dtype == 'int32':
-            #        reference[2] = -2147483648
-            #elif dtype == 'int64':
-            #        reference[2] = -9223372036854775808
+            elif dtype == 'int32':
+                    reference[2] = -2147483648
+            elif dtype == 'int64':
+                    reference[2] = -9223372036854775808
             a = array([-2, -1, 0, 1, 2], dtype)
             b = reciprocal(a)
             assert (b == reference).all()

File pypy/module/micronumpy/types.py

 from rpython.rlib.rawstorage import (alloc_raw_storage, raw_storage_setitem,
                                   raw_storage_getitem)
 from rpython.rlib.objectmodel import specialize
-from rpython.rlib.rarithmetic import widen, byteswap, r_ulonglong
+from rpython.rlib.rarithmetic import widen, byteswap, r_ulonglong, most_neg_value_of
 from rpython.rtyper.lltypesystem import lltype, rffi
 from rpython.rlib.rstruct.runpack import runpack
 from rpython.rlib.rstruct.nativefmttable import native_is_bigendian
     def invert(self, v):
         return ~v
 
-    @simple_unary_op
+    @specialize.argtype(1)
     def reciprocal(self, v):
-        if v == 0:
+        raw = self.for_computation(self.unbox(v))
+        ans = 0
+        if raw == 0:
             # XXX good place to warn
-            # XXX can't do the following, func is specialized only on argtype(v)
-            # (which is the same for all int classes)
-            #if self.T in (rffi.INT, rffi.LONG):
-            #    return most_neg_value_of(self.T)
-            return 0
-        if abs(v) == 1:
-            return v
-        return 0
+            if self.T is rffi.INT or self.T is rffi.LONG:
+                ans = most_neg_value_of(self.T)
+        elif abs(raw) == 1:
+            ans = raw
+        return self.box(ans)
 
     @specialize.argtype(1)
     def round(self, v, decimals=0):