Author: Brian Kearns <bdkea...@gmail.com> Branch: Changeset: r61254:2c747f18f5fd Date: 2013-02-15 05:52 -0500 http://bitbucket.org/pypy/pypy/changeset/2c747f18f5fd/
Log: make sure pack/unpack_float80 are doing what they claim, add tests 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 @@ -14,8 +14,8 @@ from rpython.rtyper.lltypesystem import lltype, rffi from rpython.rlib.rstruct.runpack import runpack from rpython.rlib.rstruct.nativefmttable import native_is_bigendian -from rpython.rlib.rstruct.ieee import (float_pack, float_unpack, pack_float80, - unpack_float, unpack_float128) +from rpython.rlib.rstruct.ieee import (float_pack, float_unpack, unpack_float, + pack_float80, unpack_float80) from rpython.tool.sourcetools import func_with_new_name from rpython.rlib import jit from rpython.rlib.rstring import StringBuilder @@ -1525,14 +1525,14 @@ def runpack_str(self, s): assert len(s) == 12 - fval = unpack_float128(s, native_is_bigendian) + fval = unpack_float80(s, native_is_bigendian) return self.box(fval) def byteswap(self, w_v): value = self.unbox(w_v) result = StringBuilder(12) - pack_float80(result, value, 12, not native_is_bigendian) - return self.box(unpack_float128(result.build(), native_is_bigendian)) + pack_float80(result, value, not native_is_bigendian) + return self.box(unpack_float80(result.build(), native_is_bigendian)) NonNativeFloat96 = Float96 @@ -1555,14 +1555,14 @@ def runpack_str(self, s): assert len(s) == 16 - fval = unpack_float128(s, native_is_bigendian) + fval = unpack_float80(s, native_is_bigendian) return self.box(fval) def byteswap(self, w_v): value = self.unbox(w_v) result = StringBuilder(16) - pack_float80(result, value, 16, not native_is_bigendian) - return self.box(unpack_float128(result.build(), native_is_bigendian)) + pack_float80(result, value, not native_is_bigendian) + return self.box(unpack_float80(result.build(), native_is_bigendian)) NonNativeFloat128 = Float128 diff --git a/rpython/rlib/rstruct/ieee.py b/rpython/rlib/rstruct/ieee.py --- a/rpython/rlib/rstruct/ieee.py +++ b/rpython/rlib/rstruct/ieee.py @@ -235,12 +235,12 @@ result.append("".join(l)) @jit.unroll_safe -def pack_float80(result, x, size, be): +def pack_float80(result, x, be): l = [] unsigned = float_pack80(x) for i in range(8): l.append(chr((unsigned[0] >> (i * 8)) & 0xFF)) - for i in range(size - 8): + for i in range(2): l.append(chr((unsigned[1] >> (i * 8)) & 0xFF)) if be: l.reverse() @@ -253,12 +253,14 @@ unsigned |= r_ulonglong(c) << (i * 8) return float_unpack(unsigned, len(s)) -def unpack_float128(s, be): +def unpack_float80(s, be): + if len(s) != 10: + raise ValueError QQ = [r_ulonglong(0), r_ulonglong(0)] for i in range(8): - c = ord(s[len(s) - 1 - i if be else i]) + c = ord(s[9 - i if be else i]) QQ[0] |= r_ulonglong(c) << (i * 8) - for i in range(8, len(s)): - c = ord(s[len(s) - 1 - i if be else i]) + for i in range(8, 10): + c = ord(s[9 - i if be else i]) QQ[1] |= r_ulonglong(c) << ((i - 8) * 8) return float_unpack80(QQ) diff --git a/rpython/rlib/rstruct/test/test_ieee.py b/rpython/rlib/rstruct/test/test_ieee.py --- a/rpython/rlib/rstruct/test/test_ieee.py +++ b/rpython/rlib/rstruct/test/test_ieee.py @@ -23,6 +23,18 @@ y = ieee.float_unpack80(Q) assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q) + Q = [] + ieee.pack_float(Q, x, 8, False) + Q = Q[0] + y = ieee.unpack_float(Q, False) + assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q) + + Q = [] + ieee.pack_float80(Q, x, False) + Q = Q[0] + y = ieee.unpack_float80(Q, False) + assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q) + # check that packing agrees with the struct module struct_pack8 = struct.unpack('<Q', struct.pack('<d', x))[0] float_pack8 = ieee.float_pack(x, 8) _______________________________________________ pypy-commit mailing list pypy-commit@python.org http://mail.python.org/mailman/listinfo/pypy-commit