Author: Matti Picus <[email protected]>
Branch: missing-ndarray-attributes
Changeset: r60840:1c8ab37095aa
Date: 2013-02-03 16:28 +0200
http://bitbucket.org/pypy/pypy/changeset/1c8ab37095aa/
Log: back out changeset 849864c9f43a
diff --git a/pypy/module/micronumpy/arrayimpl/sort.py
b/pypy/module/micronumpy/arrayimpl/sort.py
--- a/pypy/module/micronumpy/arrayimpl/sort.py
+++ b/pypy/module/micronumpy/arrayimpl/sort.py
@@ -31,20 +31,16 @@
self.indexes = indexes
def getitem(self, item):
- #v = raw_storage_getitem(TP, self.values, item * self.stride_size
- # + self.start)
- v = itemtype.read_from_storage(TP, self.values, self.start,
- item*self.stride_size)
+ v = raw_storage_getitem(TP, self.values, item * self.stride_size
+ + self.start)
v = itemtype.for_computation(v)
return (v, raw_storage_getitem(lltype.Signed, self.indexes,
item * self.index_stride_size +
self.index_start))
def setitem(self, idx, item):
- #raw_storage_setitem(self.values, idx * self.stride_size +
- # self.start, rffi.cast(TP, item[0]))
- itemtype.write_to_storage(TP, self.values, self.start,
- idx * self.stride_size, item[0])
+ raw_storage_setitem(self.values, idx * self.stride_size +
+ self.start, rffi.cast(TP, item[0]))
raw_storage_setitem(self.indexes, idx * self.index_stride_size +
self.index_start, item[1])
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
@@ -2371,17 +2371,13 @@
def test_argsort_dtypes(self):
from _numpypy import array, arange
- nnp = self.non_native_prefix
assert array(2.0).argsort() == 0
- for dtype in ['int', 'float', 'int8', 'int16', 'float32',
- nnp + 'i2']:
- print dtype
- a = array([6, 4, 1, 3, 8, 4, 20, 100, 101], dtype=dtype)
- c = a.copy()
+ for dtype in ['int', 'float', 'int8', 'int16', 'float32']:
+ a = array([6, 4, 1, 3, 8, 3], dtype=dtype)
res = a.argsort()
- assert (res == [2, 3, 1, 5, 0, 4, 6, 7, 8]).all()
- assert (a == c).all() # not modified
- a = arange(100, dtype=dtype)
+ assert (res == [2, 3, 5, 1, 0, 4]).all()
+ assert (a == [6, 4, 1, 3, 8, 3]).all() # not modified
+ a = arange(100)
assert (a.argsort() == a).all()
def test_argsort_nd(self):
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
@@ -135,7 +135,6 @@
@specialize.argtype(1)
def box(self, value):
- # assumes T is both the storage type and the value type
return self.BoxType(rffi.cast(self.T, value))
@specialize.argtype(1, 2)
@@ -168,31 +167,25 @@
def default_fromstring(self, space):
raise NotImplementedError
- @staticmethod
- def read_from_storage(ST, storage, i, offset):
- ret = raw_storage_getitem(ST, storage, i + offset)
- return ret
+ def _read(self, storage, i, offset):
+ return raw_storage_getitem(self.T, storage, i + offset)
def read(self, arr, i, offset, dtype=None):
- return self.box(self.read_from_storage(self.T,
- arr.storage, i, offset))
+ return self.box(self._read(arr.storage, i, offset))
def read_bool(self, arr, i, offset):
- return bool(self.for_computation(self.read_from_storage(self.T,
- arr.storage, i, offset)))
+ return bool(self.for_computation(self._read(arr.storage, i, offset)))
- @staticmethod
- def write_to_storage(ST, storage, i, offset, value):
- raw_storage_setitem(storage, i + offset, rffi.cast(ST,value))
+ def _write(self, storage, i, offset, value):
+ raw_storage_setitem(storage, i + offset, value)
def store(self, arr, i, offset, box):
- self.write_to_storage(self.T, arr.storage, i, offset,
- self.unbox(box))
+ self._write(arr.storage, i, offset, self.unbox(box))
def fill(self, storage, width, box, start, stop, offset):
value = self.unbox(box)
for i in xrange(start, stop, width):
- self.write_to_storage(self.T, storage, i, offset, value)
+ self._write(storage, i, offset, value)
def runpack_str(self, s):
v = runpack(self.format_code, s)
@@ -308,16 +301,13 @@
class NonNativePrimitive(Primitive):
_mixin_ = True
- @staticmethod
- def read_from_storage(ST, storage, i, offset):
- res = raw_storage_getitem(ST, storage, i + offset)
- res = rffi.cast(ST, res) # create a copy for inplace byteswap
- return rffi.cast(ST, byteswap(res))
+ def _read(self, storage, i, offset):
+ res = raw_storage_getitem(self.T, storage, i + offset)
+ return byteswap(res)
- @staticmethod
- def write_to_storage(ST, storage, i, offset, value):
- h = byteswap(rffi.cast(ST, value))
- raw_storage_setitem(storage, i + offset, h)
+ def _write(self, storage, i, offset, value):
+ value = byteswap(value)
+ raw_storage_setitem(storage, i + offset, value)
class Bool(BaseType, Primitive):
_attrs_ = ()
@@ -956,27 +946,26 @@
class NonNativeFloat(NonNativePrimitive, Float):
_mixin_ = True
- @staticmethod
- def read_from_storage(ST, storage, i, offset):
- res = raw_storage_getitem(ST, storage, i + offset)
- return byteswap(res)
+ def _read(self, storage, i, offset):
+ res = raw_storage_getitem(self.T, storage, i + offset)
+ return rffi.cast(lltype.Float, byteswap(res))
- @staticmethod
- def write_to_storage(ST, storage, i, offset, value):
- swapped_value = byteswap(value)
- raw_storage_setitem(storage, i + offset, rffi.cast(ST, swapped_value))
+ def _write(self, storage, i, offset, value):
+ swapped_value = byteswap(rffi.cast(self.T, value))
+ raw_storage_setitem(storage, i + offset, swapped_value)
class BaseFloat16(Float):
- '''This is the only class where the storage type is different from
- the underlying ideal type, since C has no 16 bit float
- '''
_mixin_ = True
_attrs_ = ()
- T = rffi.USHORT
+ _STORAGE_T = rffi.USHORT
+ T = rffi.DOUBLE
BoxType = interp_boxes.W_Float16Box
+ def get_element_size(self):
+ return rffi.sizeof(self._STORAGE_T)
+
def runpack_str(self, s):
assert len(s) == 2
fval = unpack_float(s, native_is_bigendian)
@@ -987,50 +976,29 @@
def byteswap(self, w_v):
value = self.unbox(w_v)
- hbits = float_pack(float(value),2)
- swapped = byteswap(rffi.cast(self.T, hbits))
+ hbits = float_pack(value,2)
+ swapped = byteswap(rffi.cast(self._STORAGE_T, hbits))
return self.box(float_unpack(r_ulonglong(swapped), 2))
- @staticmethod
- def for_computation(v):
- return rffi.cast(rffi.DOUBLE, v)
-
- @specialize.argtype(1)
- def box(self, value):
- # stored as a USHORT, boxed into a DOUBLE
- return self.BoxType(rffi.cast(rffi.DOUBLE, value))
-
- @specialize.argtype(1, 2)
- def box_complex(self, real, imag):
- #XXX this is the place to display a warning
- return self.BoxType(rffi.cast(rffi.DOUBLE, real))
-
-
class Float16(BaseType, BaseFloat16):
- @staticmethod
- def read_from_storage(ST, storage, i, offset):
- hbits = raw_storage_getitem(ST, storage, i + offset)
+ def _read(self, storage, i, offset):
+ hbits = raw_storage_getitem(self._STORAGE_T, storage, i + offset)
return float_unpack(r_ulonglong(hbits), 2)
- @staticmethod
- def write_to_storage(ST, storage, i, offset, value):
- # value can be a r_singlefloat
- hbits = float_pack(float(value),2)
+ def _write(self, storage, i, offset, value):
+ hbits = float_pack(value,2)
raw_storage_setitem(storage, i + offset,
- rffi.cast(ST, hbits))
+ rffi.cast(self._STORAGE_T, hbits))
class NonNativeFloat16(BaseType, BaseFloat16):
- @staticmethod
- def read_from_storage(ST, storage, i, offset):
- hbits = raw_storage_getitem(ST, storage, i + offset)
- r = float_unpack(r_ulonglong(byteswap(hbits)), 2)
- return r
+ def _read(self, storage, i, offset):
+ hbits = raw_storage_getitem(self._STORAGE_T, storage, i + offset)
+ return float_unpack(r_ulonglong(byteswap(hbits)), 2)
- @staticmethod
- def write_to_storage(ST, storage, i, offset, value):
- hbits = float_pack(float(value),2)
+ def _write(self, storage, i, offset, value):
+ hbits = float_pack(value,2)
raw_storage_setitem(storage, i + offset,
- byteswap(rffi.cast(ST, hbits)))
+ byteswap(rffi.cast(self._STORAGE_T, hbits)))
class Float32(BaseType, Float):
@@ -1047,10 +1015,13 @@
BoxType = interp_boxes.W_Float32Box
format_code = "f"
- #def read_bool(self, arr, i, offset):
- # v = self.for_computation(self.read_from_storage(self.STORAGE_T,
- # arr.storage, i, offset))
- # return bool(v)
+ def read_bool(self, arr, i, offset):
+ # it's not clear to me why this is needed
+ # but a hint might be that calling for_computation(v)
+ # causes translation to fail, and the assert is necessary
+ v = self._read(arr.storage, i, offset)
+ assert isinstance(v, float)
+ return bool(v)
class Float64(BaseType, Float):
_attrs_ = ()
@@ -1104,11 +1075,11 @@
return float(v[0]), float(v[1])
def read_bool(self, arr, i, offset):
- v = self.for_computation(self.read_from_storage(self.T, arr.storage,
i, offset))
+ v = self.for_computation(self._read(arr.storage, i, offset))
return bool(v[0]) or bool(v[1])
def get_element_size(self):
- return 2 * rffi.sizeof(self.T)
+ return 2 * rffi.sizeof(self._COMPONENTS_T)
def byteswap(self, w_v):
real, imag = self.unbox(w_v)
@@ -1117,19 +1088,19 @@
@specialize.argtype(1)
def box(self, value):
return self.BoxType(
- rffi.cast(self.T, value),
- rffi.cast(self.T, 0.0))
+ rffi.cast(self._COMPONENTS_T, value),
+ rffi.cast(self._COMPONENTS_T, 0.0))
@specialize.argtype(1)
def box_component(self, value):
return self.ComponentBoxType(
- rffi.cast(self.T, value))
+ rffi.cast(self._COMPONENTS_T, value))
@specialize.argtype(1, 2)
def box_complex(self, real, imag):
return self.BoxType(
- rffi.cast(self.T, real),
- rffi.cast(self.T, imag))
+ rffi.cast(self._COMPONENTS_T, real),
+ rffi.cast(self._COMPONENTS_T, imag))
def unbox(self, box):
assert isinstance(box, self.BoxType)
@@ -1141,18 +1112,16 @@
real, imag = self.unbox(box)
raw_storage_setitem(arr.storage, i+offset, real)
raw_storage_setitem(arr.storage,
- i+offset+rffi.sizeof(self.T), imag)
+ i+offset+rffi.sizeof(self._COMPONENTS_T), imag)
- @staticmethod
- def read_from_storage(ST, storage, i, offset):
- real = raw_storage_getitem(ST, storage, i + offset)
- imag = raw_storage_getitem(ST, storage,
- i + offset + rffi.sizeof(ST))
+ def _read(self, storage, i, offset):
+ real = raw_storage_getitem(self._COMPONENTS_T, storage, i + offset)
+ imag = raw_storage_getitem(self._COMPONENTS_T, storage,
+ i + offset + rffi.sizeof(self._COMPONENTS_T))
return real, imag
def read(self, arr, i, offset, dtype=None):
- real, imag = self.read_from_storage(self.T, arr.storage, i,
- offset)
+ real, imag = self._read(arr.storage, i, offset)
return self.box_complex(real, imag)
@complex_binary_op
@@ -1566,7 +1535,8 @@
class Complex64(ComplexFloating, BaseType):
_attrs_ = ()
- T = rffi.FLOAT
+ T = rffi.CHAR
+ _COMPONENTS_T = rffi.FLOAT
BoxType = interp_boxes.W_Complex64Box
ComponentBoxType = interp_boxes.W_Float32Box
@@ -1576,7 +1546,8 @@
class Complex128(ComplexFloating, BaseType):
_attrs_ = ()
- T = rffi.DOUBLE
+ T = rffi.CHAR
+ _COMPONENTS_T = rffi.DOUBLE
BoxType = interp_boxes.W_Complex128Box
ComponentBoxType = interp_boxes.W_Float64Box
@@ -1608,7 +1579,8 @@
class Complex192(ComplexFloating, BaseType):
_attrs_ = ()
- T = rffi.LONGDOUBLE
+ T = rffi.CHAR
+ _COMPONENTS_T = rffi.LONGDOUBLE
BoxType = interp_boxes.W_Complex192Box
ComponentBoxType = interp_boxes.W_Float96Box
@@ -1639,7 +1611,8 @@
class Complex256(ComplexFloating, BaseType):
_attrs_ = ()
- T = rffi.LONGDOUBLE
+ T = rffi.CHAR
+ _COMPONENTS_T = rffi.LONGDOUBLE
BoxType = interp_boxes.W_Complex256Box
ComponentBoxType = interp_boxes.W_Float128Box
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit