[pypy-commit] pypy numpypy.float16: translation fixes

mattip noreply at buildbot.pypy.org
Sat Nov 3 23:55:00 CET 2012


Author: mattip <matti.picus at gmail.com>
Branch: numpypy.float16
Changeset: r58705:fd7f852397ea
Date: 2012-11-04 00:29 +0200
http://bitbucket.org/pypy/pypy/changeset/fd7f852397ea/

Log:	translation fixes

diff --git a/pypy/module/micronumpy/halffloat.py b/pypy/module/micronumpy/halffloat.py
--- a/pypy/module/micronumpy/halffloat.py
+++ b/pypy/module/micronumpy/halffloat.py
@@ -4,6 +4,7 @@
 
 
 def halfbits_to_floatbits(x):
+    x = rffi.cast(rffi.UINT, x)
     h_exp = x & 0x7c00
     f_sgn = (x & 0x8000) << 16
     if h_exp == 0: #0 or subnormal
@@ -25,7 +26,7 @@
 
 
 def floatbits_to_halfbits(f):
-    h_sgn = (f & 0x80000000) >> 16
+    h_sgn = (f >>16) & 0x8000
     f_exp = f & 0x7f800000
     if f_exp >= 0x47800000:
         # Exponent overflow, convert to signed inf/nan
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
@@ -930,13 +930,16 @@
     BoxType = interp_boxes.W_Float16Box
 
     def pack_str(self, box):
-        xxx
+        fbits = float_pack(self.unbox(box))
+        hbits = halffloat.floatbits_to_halfbits(fbits)
+        return struct.pack('H', hbits)
 
     def get_element_size(self):
         return rffi.sizeof(self._STORAGE_T)
 
     def runpack_str(self, s):
-        hbits = runpack('H', s)
+        hbits = rffi.cast(rffi.UINT, runpack('H', s))
+        assert hbits >=0
         fbits = halffloat.halfbits_to_floatbits(hbits)
         return self.box(float_unpack(fbits, 4))
 
@@ -947,19 +950,34 @@
         return self.box(-1.0)
 
     def _read(self, storage, i, offset):
-        byte_rep = raw_storage_getitem(self._STORAGE_T, storage, i + offset)
+        byte_rep = rffi.cast(rffi.UINT, 
+                raw_storage_getitem(self._STORAGE_T, storage, i + offset))
+        assert byte_rep >=0
         fbits = halffloat.halfbits_to_floatbits(byte_rep)
         return float_unpack(fbits, 4)
 
     def _write(self, storage, i, offset, value):
         fbits = float_pack(value,4)
         hbits = halffloat.floatbits_to_halfbits(fbits)
-        raw_storage_setitem(storage, i + offset, rffi.cast(self._STORAGE_T, hbits))
+        raw_storage_setitem(storage, i + offset,
+                rffi.cast(self._STORAGE_T, hbits))
 
-class NonNativeFloat16(BaseType, NonNativeFloat):
+class NonNativeFloat16(Float16):
     _attrs_ = ()
+    BoxType = interp_boxes.W_Float16Box
 
-    BoxType = interp_boxes.W_Float16Box
+    def _read(self, storage, i, offset):
+        res = Float16._read(self, storage, i, offset)
+        #return byteswap(res) XXX
+        return res
+
+    def _write(self, storage, i, offset, value):
+        #value = byteswap(value) XXX
+        Float16._write(self, storage, i, offset, value)
+
+    def pack_str(self, box):
+        # XXX byteswap
+        return Float16.pack_str(self, box)
 
 class Float32(BaseType, Float):
     _attrs_ = ()


More information about the pypy-commit mailing list