| 1 |
import math |
| 2 |
import py |
| 3 |
import sys |
| 4 |
import random |
| 5 |
import struct |
| 6 |
|
| 7 |
from rpython.rlib.mutbuffer import MutableStringBuffer |
| 8 |
from rpython.rlib.rstruct import ieee |
| 9 |
from rpython.rlib.rfloat import NAN, INFINITY |
| 10 |
from rpython.translator.c.test.test_genc import compile |
| 11 |
|
| 12 |
|
| 13 |
class TestFloatSpecific: |
| 14 |
def test_halffloat_exact(self): |
| 15 |
|
| 16 |
cases = [[0, 0], [10, 18688], [-10, 51456], [10e3, 28898], |
| 17 |
[float('inf'), 31744], [-float('inf'), 64512]] |
| 18 |
for c, h in cases: |
| 19 |
hbit = ieee.float_pack(c, 2) |
| 20 |
assert hbit == h |
| 21 |
assert c == ieee.float_unpack(h, 2) |
| 22 |
|
| 23 |
def test_halffloat_inexact(self): |
| 24 |
|
| 25 |
cases = [[10.001, 18688, 10.], [-10.001, 51456, -10], |
| 26 |
[0.027588, 10000, 0.027587890625], |
| 27 |
[22001, 30047, 22000]] |
| 28 |
for c, h, f in cases: |
| 29 |
hbit = ieee.float_pack(c, 2) |
| 30 |
assert hbit == h |
| 31 |
assert f == ieee.float_unpack(h, 2) |
| 32 |
|
| 33 |
def test_halffloat_overunderflow(self): |
| 34 |
cases = [[670000, float('inf')], [-67000, -float('inf')], |
| 35 |
[1e-08, 0], [-1e-8, -0.]] |
| 36 |
for f1, f2 in cases: |
| 37 |
try: |
| 38 |
f_out = ieee.float_unpack(ieee.float_pack(f1, 2), 2) |
| 39 |
except OverflowError: |
| 40 |
f_out = math.copysign(float('inf'), f1) |
| 41 |
assert f_out == f2 |
| 42 |
assert math.copysign(1., f_out) == math.copysign(1., f2) |
| 43 |
|
| 44 |
def test_float80_exact(self): |
| 45 |
s = [] |
| 46 |
ieee.pack_float80(s, -1., 16, False) |
| 47 |
assert repr(s[-1]) == repr('\x00\x00\x00\x00\x00\x00\x00\x80\xff\xbf\x00\x00\x00\x00\x00\x00') |
| 48 |
ieee.pack_float80(s, -1., 16, True) |
| 49 |
assert repr(s[-1]) == repr('\x00\x00\x00\x00\x00\x00\xbf\xff\x80\x00\x00\x00\x00\x00\x00\x00') |
| 50 |
ieee.pack_float80(s, -123.456, 16, False) |
| 51 |
assert repr(s[-1]) == repr('\x00\xb8\xf3\xfd\xd4x\xe9\xf6\x05\xc0\x00\x00\x00\x00\x00\x00') |
| 52 |
ieee.pack_float80(s, -123.456, 16, True) |
| 53 |
assert repr(s[-1]) == repr('\x00\x00\x00\x00\x00\x00\xc0\x05\xf6\xe9x\xd4\xfd\xf3\xb8\x00') |
| 54 |
|
| 55 |
x = ieee.unpack_float80('\x00\x00\x00\x00\x00\x00\x00\x80\xff?\xc8\x01\x00\x00\x00\x00', False) |
| 56 |
assert x == 1.0 |
| 57 |
x = ieee.unpack_float80('\x00\x00\x7f\x83\xe1\x91?\xff\x80\x00\x00\x00\x00\x00\x00\x00', True) |
| 58 |
assert x == 1.0 |
| 59 |
|
| 60 |
|
| 61 |
class TestFloatPacking: |
| 62 |
|
| 63 |
def check_float(self, x): |
| 64 |
|
| 65 |
for size in [10, 12, 16]: |
| 66 |
for be in [False, True]: |
| 67 |
Q = [] |
| 68 |
ieee.pack_float80(Q, x, size, be) |
| 69 |
Q = Q[0] |
| 70 |
y = ieee.unpack_float80(Q, be) |
| 71 |
assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q) |
| 72 |
|
| 73 |
for be in [False, True]: |
| 74 |
buf = MutableStringBuffer(8) |
| 75 |
ieee.pack_float(buf, 0, x, 8, be) |
| 76 |
Q = buf.finish() |
| 77 |
y = ieee.unpack_float(Q, be) |
| 78 |
assert repr(x) == repr(y), '%r != %r, Q=%r' % (x, y, Q) |
| 79 |
|
| 80 |
|
| 81 |
struct_pack8 = struct.unpack('<Q', struct.pack('<d', x))[0] |
| 82 |
float_pack8 = ieee.float_pack(x, 8) |
| 83 |
assert struct_pack8 == float_pack8 |
| 84 |
|
| 85 |
|
| 86 |
try: |
| 87 |
struct_pack4 = struct.unpack('<L', struct.pack('<f', x))[0] |
| 88 |
except OverflowError: |
| 89 |
struct_pack4 = "overflow" |
| 90 |
try: |
| 91 |
float_pack4 = ieee.float_pack(x, 4) |
| 92 |
except OverflowError: |
| 93 |
float_pack4 = "overflow" |
| 94 |
assert struct_pack4 == float_pack4 |
| 95 |
|
| 96 |
if float_pack4 == "overflow": |
| 97 |
return |
| 98 |
|
| 99 |
|
| 100 |
roundtrip = ieee.float_pack(ieee.float_unpack(float_pack4, 4), 4) |
| 101 |
assert float_pack4 == roundtrip |
| 102 |
|
| 103 |
try: |
| 104 |
float_pack2 = ieee.float_pack(x, 2) |
| 105 |
except OverflowError: |
| 106 |
return |
| 107 |
|
| 108 |
roundtrip = ieee.float_pack(ieee.float_unpack(float_pack2, 2), 2) |
| 109 |
assert (float_pack2, x) == (roundtrip, x) |
| 110 |
|
| 111 |
def test_infinities(self): |
| 112 |
self.check_float(float('inf')) |
| 113 |
self.check_float(float('-inf')) |
| 114 |
|
| 115 |
def test_zeros(self): |
| 116 |
self.check_float(0.0) |
| 117 |
self.check_float(-0.0) |
| 118 |
|
| 119 |
def test_nans(self): |
| 120 |
self.check_float(float('nan')) |
| 121 |
|
| 122 |
def test_simple(self): |
| 123 |
test_values = [1e-10, 0.00123, 0.5, 0.7, 1.0, 123.456, 1e10] |
| 124 |
for value in test_values: |
| 125 |
self.check_float(value) |
| 126 |
self.check_float(-value) |
| 127 |
|
| 128 |
def test_subnormal(self): |
| 129 |
|
| 130 |
self.check_float(2**-1074) |
| 131 |
self.check_float(2**-1022) |
| 132 |
self.check_float(2**-1021) |
| 133 |
self.check_float((2**53-1)*2**-1074) |
| 134 |
self.check_float((2**52-1)*2**-1074) |
| 135 |
self.check_float((2**52+1)*2**-1074) |
| 136 |
|
| 137 |
|
| 138 |
self.check_float(1e-309) |
| 139 |
self.check_float(1e-320) |
| 140 |
|
| 141 |
def test_powers_of_two(self): |
| 142 |
|
| 143 |
for k in range(-1074, 1024): |
| 144 |
self.check_float(2.**k) |
| 145 |
|
| 146 |
|
| 147 |
for k in range(-1074, 1024): |
| 148 |
self.check_float((2 - 2**-52) * 2.**k) |
| 149 |
|
| 150 |
def test_float4_boundaries(self): |
| 151 |
|
| 152 |
self.check_float(2**128.) |
| 153 |
|
| 154 |
self.check_float((1 - 2.**-24) * 2**128.) |
| 155 |
|
| 156 |
self.check_float((1 - 2.**-25) * 2**128.) |
| 157 |
self.check_float(2**-125) |
| 158 |
|
| 159 |
self.check_float(2**-126) |
| 160 |
|
| 161 |
self.check_float(2**-149) |
| 162 |
|
| 163 |
self.check_float(2**-150) |
| 164 |
|
| 165 |
self.check_float((1 + 2**-52) * 2**-150) |
| 166 |
|
| 167 |
def test_random(self): |
| 168 |
|
| 169 |
mantissa_mask = (1 << 53) - 1 |
| 170 |
for _ in xrange(10000): |
| 171 |
Q = random.randrange(2**64) |
| 172 |
x = struct.unpack('<d', struct.pack('<Q', Q))[0] |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
if math.isnan(x) and (Q & mantissa_mask) >= 1 << 11: |
| 177 |
continue |
| 178 |
self.check_float(x) |
| 179 |
|
| 180 |
def test_various_nans(self): |
| 181 |
|
| 182 |
maxmant64 = (1 << 52) - 1 |
| 183 |
maxmant16 = (1 << 10) - 1 |
| 184 |
assert maxmant64 >> 42 == maxmant16 |
| 185 |
exp = 0xfff << 52 |
| 186 |
for i in range(20): |
| 187 |
val_to_preserve = exp | ((maxmant16 - i) << 42) |
| 188 |
a = ieee.float_unpack(val_to_preserve, 8) |
| 189 |
assert math.isnan(a), 'i %d, maxmant %s' % (i, hex(val_to_preserve)) |
| 190 |
b = ieee.float_pack(a, 8) |
| 191 |
assert b == val_to_preserve, 'i %d, val %s b %s' % (i, hex(val_to_preserve), hex(b)) |
| 192 |
b = ieee.float_pack(a, 2) |
| 193 |
assert b == 0xffff - i, 'i %d, b%s' % (i, hex(b)) |
| 194 |
|
| 195 |
class TestCompiled: |
| 196 |
def test_pack_float(self): |
| 197 |
def pack(x, size): |
| 198 |
buf = MutableStringBuffer(size) |
| 199 |
ieee.pack_float(buf, 0, x, size, False) |
| 200 |
l = [] |
| 201 |
for c in buf.finish(): |
| 202 |
l.append(str(ord(c))) |
| 203 |
return ','.join(l) |
| 204 |
c_pack = compile(pack, [float, int]) |
| 205 |
|
| 206 |
def unpack(s): |
| 207 |
l = s.split(',') |
| 208 |
s = ''.join([chr(int(x)) for x in l]) |
| 209 |
return ieee.unpack_float(s, False) |
| 210 |
c_unpack = compile(unpack, [str]) |
| 211 |
|
| 212 |
def check_roundtrip(x, size): |
| 213 |
s = c_pack(x, size) |
| 214 |
if not math.isnan(x): |
| 215 |
|
| 216 |
assert s == pack(x, size) |
| 217 |
assert unpack(s) == x |
| 218 |
assert c_unpack(s) == x |
| 219 |
else: |
| 220 |
assert math.isnan(unpack(s)) |
| 221 |
assert math.isnan(c_unpack(s)) |
| 222 |
|
| 223 |
for size in [2, 4, 8]: |
| 224 |
check_roundtrip(123.4375, size) |
| 225 |
check_roundtrip(-123.4375, size) |
| 226 |
check_roundtrip(INFINITY, size) |
| 227 |
check_roundtrip(NAN, size) |