Skip to content

Commit de588fe

Browse files
committed
Use python ints throughout, adding float64 to test
(Fixes #25, newly ailing test for float64 depending on later PR)
1 parent be5928f commit de588fe

File tree

5 files changed

+30
-7
lines changed

5 files changed

+30
-7
lines changed

src/gfloat/decode.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def decode_float(fi: FormatInfo, i: int) -> FloatValue:
2121
ValueError:
2222
If :paramref:`i` is outside the range of valid code points in :paramref:`fi`.
2323
"""
24+
assert isinstance(i, int)
25+
2426
k = fi.k
2527
p = fi.precision
2628
t = p - 1 # Trailing significand field width
@@ -56,13 +58,10 @@ def decode_float(fi: FormatInfo, i: int) -> FloatValue:
5658
expval = exp - expBias
5759
fsignificand = 1.0 + significand * 2**-t
5860

59-
# val: the raw value excluding specials
60-
val = sign * fsignificand * 2.0**expval
61-
62-
# Now overwrite the raw value with specials: Infs, NaN, -0, NaN_0
61+
# Handle specials: Infs, NaN, -0, NaN_0
6362
signed_infinity = -np.inf if signbit else np.inf
6463

65-
fval = val
64+
fval = None
6665
# All-bits-special exponent (ABSE)
6766
if w > 0 and exp == 2**w - 1:
6867
min_i_with_nan = 2 ** (p - 1) - fi.num_high_nans
@@ -78,6 +77,10 @@ def decode_float(fi: FormatInfo, i: int) -> FloatValue:
7877
else:
7978
fval = np.nan
8079

80+
# In range - compute value
81+
if fval is None:
82+
fval = sign * fsignificand * 2.0**expval
83+
8184
# Compute FloatClass
8285
fclass = None
8386
if fval == 0:

src/gfloat/formats.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
from .block import BlockFormatInfo
44
from .types import FormatInfo
55

6+
#: FormatInfo for IEEE-754 Binary64 format
7+
format_info_binary64 = FormatInfo(
8+
name="binary64",
9+
k=64,
10+
precision=53,
11+
emax=1023,
12+
has_nz=True,
13+
has_infs=True,
14+
num_high_nans=2**52 - 1,
15+
has_subnormals=True,
16+
is_signed=True,
17+
is_twos_complement=False,
18+
)
19+
620
#: FormatInfo for IEEE-754 Binary32 format
721
format_info_binary32 = FormatInfo(
822
name="binary32",
@@ -204,6 +218,7 @@ def format_info_p3109(precision: int) -> FormatInfo:
204218
*_fp8_formats,
205219
*_fp16_formats,
206220
format_info_binary32,
221+
format_info_binary64,
207222
]
208223

209224
# ------

src/gfloat/round.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ def encode_float(fi: FormatInfo, v: float) -> int:
191191
isig = 0
192192
biased_exp = 0
193193
else:
194-
assert fi.bits < 64 # TODO: check implementation if fi is binary64
195194
sig, exp = np.frexp(vpos)
195+
exp = int(exp) # All calculations in Python ints
196+
196197
# sig in range [0.5, 1)
197198
sig *= 2
198199
exp -= 1
@@ -226,6 +227,6 @@ def encode_float(fi: FormatInfo, v: float) -> int:
226227
isig = (1 << t) - isig
227228

228229
# Pack values into a single integer
229-
code = (sign << (k - 1)) | (biased_exp << t) | (isig << 0)
230+
code = (int(sign) << (k - 1)) | (biased_exp << t) | (isig << 0)
230231

231232
return code

test/test_encode.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def test_encode(fi: FormatInfo) -> None:
1818
step = 13
1919
elif fi.bits <= 32:
2020
step = 73013
21+
elif fi.bits <= 64:
22+
step = (73013 << 32) + 39
2123

2224
for i in range(0, 2**fi.bits, step):
2325
fv = decode_float(fi, i)

test/test_round.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ def test_round_roundtrip(fi: FormatInfo) -> None:
446446
step = 13
447447
elif fi.bits <= 32:
448448
step = 73013
449+
elif fi.bits <= 64:
450+
step = (73013 << 32) + 39
449451

450452
for i in range(0, 2**fi.bits, step):
451453
fv = decode_float(fi, i)

0 commit comments

Comments
 (0)