def unpack_ibm_floats(data, num_items): """Unpack a series of binary-encoded big-endian single-precision IBM floats. Args: data: A sequence of bytes. num_items: The number of floats to be read. Returns: A sequence of floats. """ return [IBMFloat.from_bytes(data[i: i+4]) for i in range(0, num_items * 4, 4)]
def unpack_ibm_floats(data, count): """Unpack a series of binary-encoded big-endian single-precision IBM floats. Args: data: A sequence of bytes. (Python 2 - a str object, Python 3 - a bytes object) count: The number of floats to be read. Returns: A sequence of floats. """ return [IBMFloat.from_bytes(data[i: i+4]) for i in range(0, count * 4, 4)]
def test_zero_subnormal(self, b, c, d, shift): mantissa = (b << 16) | (c << 8) | d assume(mantissa != 0) mantissa >>= shift assert mantissa != 0 sa = EXPONENT_BIAS sb = (mantissa >> 16) & 0xff sc = (mantissa >> 8) & 0xff sd = mantissa & 0xff ibm = IBMFloat.from_bytes((sa, sb, sc, sd)) assert ibm.is_subnormal() z = ibm.zero_subnormal() self.assertTrue(z.is_zero())
def test_normalise_subnormal(self, b, c, d, shift): mantissa = (b << 16) | (c << 8) | d assume(mantissa != 0) mantissa >>= shift assert mantissa != 0 sa = EXPONENT_BIAS sb = (mantissa >> 16) & 0xff sc = (mantissa >> 8) & 0xff sd = mantissa & 0xff ibm = IBMFloat.from_bytes((sa, sb, sc, sd)) assert ibm.is_subnormal() normalized = ibm.normalize() self.assertFalse(normalized.is_subnormal())
def test_zero_subnormal(self, b, c, d, shift): mantissa = (b << 16) | (c << 8) | d assume(mantissa != 0) mantissa >>= shift assert mantissa != 0 sa = EXPONENT_BIAS sb = (mantissa >> 16) & 0xff sc = (mantissa >> 8) & 0xff sd = mantissa & 0xff ibm = IBMFloat.from_bytes((sa, sb, sc, sd)) assert ibm.is_subnormal() z = ibm.zero_subnormal() assert z.is_zero()
def test_normalise_subnormal2(self): ibm = IBMFloat.from_bytes((64, 1, 0, 0)) assert ibm.is_subnormal() normalized = ibm.normalize() self.assertFalse(normalized.is_subnormal())
def test_zero_from_bytes(self): zero = IBMFloat.from_bytes(b'\x00\x00\x00\x00') assert zero.is_zero()
def test_negate_non_zero(self, a, b, c, d): ibm = IBMFloat.from_bytes((a, b, c, d)) assume(not ibm.is_zero()) negated = -ibm assert ibm.signbit != negated.signbit
def test_normalise_subnormal1(self): ibm = IBMFloat.from_bytes( (0b01000000, 0b00000000, 0b11111111, 0b00000000)) assert ibm.is_subnormal() normalized = ibm.normalize() assert not normalized.is_subnormal()
def test_incorrect_number_of_bytes_raises_value_error(self): with raises(ValueError): IBMFloat.from_bytes(b'\x00\x00\x00')
def test_bytes_roundtrip(self, a, b, c, d): b = bytes((a, b, c, d)) ibm = IBMFloat.from_bytes(b) self.assertEqual(bytes(ibm), b)
def test_abs(self, a, b, c, d): ibm = IBMFloat.from_bytes((a, b, c, d)) abs_ibm = abs(ibm) self.assertGreaterEqual(abs_ibm.signbit, 0)
def test_zero_from_bytes(self): zero = IBMFloat.from_bytes(b'\x00\x00\x00\x00') self.assertTrue(zero.is_zero())
def unpack(self, data, num_items): return [IBMFloat.from_bytes(data[i: i+4]) for i in range(0, num_items * 4, 4)]
def test_bytes_roundtrip(self, a, b, c, d): b = bytes((a, b, c, d)) ibm = IBMFloat.from_bytes(b) assert bytes(ibm) == b
def test_abs(self, a, b, c, d): ibm = IBMFloat.from_bytes((a, b, c, d)) abs_ibm = abs(ibm) assert abs_ibm.signbit >= 0
def test_negate_non_zero(self, a, b, c, d): ibm = IBMFloat.from_bytes((a, b, c, d)) assume(not ibm.is_zero()) negated = -ibm self.assertNotEqual(ibm.signbit, negated.signbit)
def unpack_ibm_floats_py(data, num_items): return [ IBMFloat.from_bytes(data[i:i + 4]) for i in range(0, num_items * 4, 4) ]
def test_normalise_subnormal2(self): ibm = IBMFloat.from_bytes((64, 1, 0, 0)) assert ibm.is_subnormal() normalized = ibm.normalize() assert not normalized.is_subnormal()
def unpack(self, data, num_items): return [ IBMFloat.from_bytes(data[i:i + 4]) for i in range(0, num_items * 4, 4) ]
def test_normalise_subnormal1(self): ibm = IBMFloat.from_bytes((0b01000000, 0b00000000, 0b11111111, 0b00000000)) assert ibm.is_subnormal() normalized = ibm.normalize() self.assertFalse(normalized.is_subnormal())