Skip to content

Commit

Permalink
Merge pull request #37 from graphcore-research/tidy-rounding-2
Browse files Browse the repository at this point in the history
Simplify rounding for precision=1
  • Loading branch information
awf authored Sep 17, 2024
2 parents cddcb4c + 33defcb commit ca391b8
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
13 changes: 4 additions & 9 deletions src/gfloat/round.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,10 @@ def round_float(
should_round_away = delta > (0.5 + srbits) * 2.0**-srnumbits

if should_round_away:
if fi.precision > 1:
isignificand += 1
else:
# Increment isignificand if zero, else increment exponent
if isignificand == 0:
isignificand = 1
else:
assert isignificand == 1
expval += 1
# This may increase isignificand to 2**p,
# which would require special casing in encode,
# but not here, where we reconstruct a rounded value.
isignificand += 1

# Reconstruct rounded result to float
result = isignificand * (2.0**expval)
Expand Down
11 changes: 1 addition & 10 deletions src/gfloat/round_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,7 @@ def round_ndarray(
else:
round_up = np.zeros_like(delta, dtype=bool)

if fi.precision > 1:
isignificand = np.where(round_up, isignificand + 1, isignificand)
else:
# if isignificand == 0:
# isignificand = 1
# else:
# assert isignificand == 1
# expval += 1
expval += round_up & (isignificand == 1)
isignificand = np.where(round_up, 1, isignificand)
isignificand = np.where(round_up, isignificand + 1, isignificand)

result = np.where(finite_nonzero, np.ldexp(isignificand, expval), absv)

Expand Down

0 comments on commit ca391b8

Please sign in to comment.