[pypy-commit] pypy default: (justinpeel) unary pos, neg, abs
fijal
noreply at buildbot.pypy.org
Mon Jul 4 22:03:42 CEST 2011
Author: Maciej Fijalkowski <fijall at gmail.com>
Branch:
Changeset: r45347:f5220ca4563e
Date: 2011-07-04 22:11 +0200
http://bitbucket.org/pypy/pypy/changeset/f5220ca4563e/
Log: (justinpeel) unary pos, neg, abs
diff --git a/pypy/module/micronumpy/compile.py b/pypy/module/micronumpy/compile.py
--- a/pypy/module/micronumpy/compile.py
+++ b/pypy/module/micronumpy/compile.py
@@ -43,6 +43,8 @@
elif b == '%':
right = stack.pop()
stack.append(stack.pop().descr_mod(space, right))
+ elif b == '|':
+ stack.append(stack.pop().descr_abs(space))
else:
print "Unknown opcode: %s" % b
raise BogusBytecode()
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
@@ -31,6 +31,12 @@
self.transitions[target] = new = Signature()
return new
+def pos(v):
+ return v
+def neg(v):
+ return -v
+def absolute(v):
+ return abs(v)
def add(v1, v2):
return v1 + v2
def sub(v1, v2):
@@ -57,6 +63,22 @@
arr.force_if_needed()
del self.invalidates[:]
+ def _unop_impl(function):
+ signature = Signature()
+ def impl(self, space):
+ new_sig = self.signature.transition(signature)
+ res = Call1(
+ function,
+ self,
+ self.signature.transition(signature))
+ self.invalidates.append(res)
+ return space.wrap(res)
+ return func_with_new_name(impl, "uniop_%s_impl" % function.__name__)
+
+ descr_pos = _unop_impl(pos)
+ descr_neg = _unop_impl(neg)
+ descr_abs = _unop_impl(absolute)
+
def _binop_impl(function):
signature = Signature()
def impl(self, space, w_other):
@@ -358,6 +380,9 @@
__getitem__ = interp2app(BaseArray.descr_getitem),
__setitem__ = interp2app(BaseArray.descr_setitem),
+ __pos__ = interp2app(BaseArray.descr_pos),
+ __neg__ = interp2app(BaseArray.descr_neg),
+ __abs__ = interp2app(BaseArray.descr_abs),
__add__ = interp2app(BaseArray.descr_add),
__sub__ = interp2app(BaseArray.descr_sub),
__mul__ = interp2app(BaseArray.descr_mul),
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -199,6 +199,27 @@
for i in range(5):
assert b[i] == i % 2
+ def test_pos(self):
+ from numpy import array
+ a = array([1.,-2.,3.,-4.,-5.])
+ b = +a
+ for i in range(5):
+ assert b[i] == a[i]
+
+ def test_neg(self):
+ from numpy import array
+ a = array([1.,-2.,3.,-4.,-5.])
+ b = -a
+ for i in range(5):
+ assert b[i] == -a[i]
+
+ def test_abs(self):
+ from numpy import array
+ a = array([1.,-2.,3.,-4.,-5.])
+ b = abs(a)
+ for i in range(5):
+ assert b[i] == abs(a[i])
+
def test_auto_force(self):
from numpy import array
a = array(range(5))
diff --git a/pypy/module/micronumpy/test/test_zjit.py b/pypy/module/micronumpy/test/test_zjit.py
--- a/pypy/module/micronumpy/test/test_zjit.py
+++ b/pypy/module/micronumpy/test/test_zjit.py
@@ -1,7 +1,7 @@
from pypy.jit.metainterp.test.support import LLJitMixin
from pypy.rpython.test.test_llinterp import interpret
from pypy.module.micronumpy.interp_numarray import (SingleDimArray, Signature,
- FloatWrapper, Call2, SingleDimSlice, add, mul)
+ FloatWrapper, Call2, SingleDimSlice, add, mul, neg, Call1)
from pypy.module.micronumpy.interp_ufuncs import negative
from pypy.module.micronumpy.compile import numpy_compile
@@ -36,6 +36,21 @@
"int_lt": 1, "guard_true": 1, "jump": 1})
assert result == f(5)
+ def test_neg(self):
+ space = self.space
+
+ def f(i):
+ ar = SingleDimArray(i)
+ v = Call1(neg, ar, Signature())
+ return v.get_concrete().storage[3]
+
+ result = self.meta_interp(f, [5], listops=True, backendopt=True)
+ self.check_loops({"getarrayitem_raw": 1, "float_neg": 1,
+ "setarrayitem_raw": 1, "int_add": 1,
+ "int_lt": 1, "guard_true": 1, "jump": 1})
+
+ assert result == f(5)
+
def test_already_forecd(self):
def f(i):
ar = SingleDimArray(i)
More information about the pypy-commit
mailing list