| 1 |
import math |
| 2 |
from rpython.translator.c.test.test_genc import compile |
| 3 |
from rpython.rlib.longlong2float import longlong2float, float2longlong, r_rettype |
| 4 |
from rpython.rlib.longlong2float import uint2singlefloat, singlefloat2uint |
| 5 |
from rpython.rlib.rarithmetic import r_singlefloat, r_longlong |
| 6 |
from rpython.rtyper.test.test_llinterp import interpret |
| 7 |
|
| 8 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 9 |
|
| 10 |
DOUBLE_ARRAY_PTR = lltype.Ptr(lltype.Array(rffi.DOUBLE)) |
| 11 |
LONGLONG_ARRAY_PTR = lltype.Ptr(lltype.Array(rffi.LONGLONG)) |
| 12 |
UINT_ARRAY_PTR = lltype.Ptr(lltype.Array(rffi.UINT)) |
| 13 |
FLOAT_ARRAY_PTR = lltype.Ptr(lltype.Array(rffi.FLOAT)) |
| 14 |
|
| 15 |
|
| 16 |
from hypothesis import given, strategies |
| 17 |
|
| 18 |
|
| 19 |
def fn(f1): |
| 20 |
ll = float2longlong(f1) |
| 21 |
f2 = longlong2float(ll) |
| 22 |
return f2 |
| 23 |
|
| 24 |
def enum_floats(): |
| 25 |
inf = 1e200 * 1e200 |
| 26 |
yield 0.0 |
| 27 |
yield -0.0 |
| 28 |
yield 1.0 |
| 29 |
yield -2.34567 |
| 30 |
yield 2.134891117e22 |
| 31 |
yield inf |
| 32 |
yield -inf |
| 33 |
yield inf / inf |
| 34 |
|
| 35 |
def test_float2longlong(): |
| 36 |
assert float2longlong(0.0) == r_longlong(0) |
| 37 |
|
| 38 |
def test_longlong_as_float(): |
| 39 |
for x in enum_floats(): |
| 40 |
res = fn(x) |
| 41 |
assert repr(res) == repr(x) |
| 42 |
|
| 43 |
@given(strategies.floats()) |
| 44 |
def test_longlong_as_float_hypothesis_roundtrip(x): |
| 45 |
assert repr(x) == repr(fn(x)) |
| 46 |
|
| 47 |
@given(strategies.integers(-2**63, 2**63-1)) |
| 48 |
def test_consistency_longlong2float_ll2ctypes(llval): |
| 49 |
|
| 50 |
|
| 51 |
with lltype.scoped_alloc(DOUBLE_ARRAY_PTR.TO, 1) as d_array: |
| 52 |
ll_array = rffi.cast(LONGLONG_ARRAY_PTR, d_array) |
| 53 |
ll_array[0] = r_rettype(llval) |
| 54 |
floatval = d_array[0] |
| 55 |
assert repr(floatval) == repr(longlong2float(llval)) |
| 56 |
|
| 57 |
@given(strategies.floats()) |
| 58 |
def test_consistency_float2longlong_ll2ctypes(floatval): |
| 59 |
|
| 60 |
with lltype.scoped_alloc(DOUBLE_ARRAY_PTR.TO, 1) as d_array: |
| 61 |
ll_array = rffi.cast(LONGLONG_ARRAY_PTR, d_array) |
| 62 |
d_array[0] = floatval |
| 63 |
llval = ll_array[0] |
| 64 |
assert llval == float2longlong(floatval) |
| 65 |
|
| 66 |
|
| 67 |
def test_compiled(): |
| 68 |
fn2 = compile(fn, [float]) |
| 69 |
for x in enum_floats(): |
| 70 |
res = fn2(x) |
| 71 |
assert repr(res) == repr(x) |
| 72 |
|
| 73 |
def test_interpreted(): |
| 74 |
def f(f1): |
| 75 |
try: |
| 76 |
ll = float2longlong(f1) |
| 77 |
return longlong2float(ll) |
| 78 |
except Exception: |
| 79 |
return 500 |
| 80 |
|
| 81 |
for x in enum_floats(): |
| 82 |
res = interpret(f, [x]) |
| 83 |
assert repr(res) == repr(x) |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
def fnsingle(f1): |
| 88 |
sf1 = r_singlefloat(f1) |
| 89 |
ii = singlefloat2uint(sf1) |
| 90 |
sf2 = uint2singlefloat(ii) |
| 91 |
f2 = float(sf2) |
| 92 |
return f2 |
| 93 |
|
| 94 |
def test_int_as_singlefloat(): |
| 95 |
for x in enum_floats(): |
| 96 |
res = fnsingle(x) |
| 97 |
assert repr(res) == repr(float(r_singlefloat(x))) |
| 98 |
|
| 99 |
def test_compiled_single(): |
| 100 |
fn2 = compile(fnsingle, [float]) |
| 101 |
for x in enum_floats(): |
| 102 |
res = fn2(x) |
| 103 |
assert repr(res) == repr(float(r_singlefloat(x))) |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
def fn_encode_nan(f1, i2): |
| 108 |
from rpython.rlib.longlong2float import can_encode_float, can_encode_int32 |
| 109 |
from rpython.rlib.longlong2float import encode_int32_into_longlong_nan |
| 110 |
from rpython.rlib.longlong2float import decode_int32_from_longlong_nan |
| 111 |
from rpython.rlib.longlong2float import is_int32_from_longlong_nan |
| 112 |
assert can_encode_float(f1) |
| 113 |
assert can_encode_int32(i2) |
| 114 |
l1 = float2longlong(f1) |
| 115 |
l2 = encode_int32_into_longlong_nan(i2) |
| 116 |
assert not is_int32_from_longlong_nan(l1) |
| 117 |
assert is_int32_from_longlong_nan(l2) |
| 118 |
f1b = longlong2float(l1) |
| 119 |
assert f1b == f1 or (math.isnan(f1b) and math.isnan(f1)) |
| 120 |
assert decode_int32_from_longlong_nan(l2) == i2 |
| 121 |
return 42 |
| 122 |
|
| 123 |
def test_compiled_encode_nan(): |
| 124 |
fn2 = compile(fn_encode_nan, [float, int]) |
| 125 |
ints = [int(-2**31), int(2**31-1), 42] |
| 126 |
for x in enum_floats(): |
| 127 |
y = ints.pop() |
| 128 |
ints.insert(0, y) |
| 129 |
fn_encode_nan(x, y) |
| 130 |
res = fn2(x, y) |
| 131 |
assert res == 42 |