Skip to content

Commit de30dee

Browse files
authored
Add RoundMode.ToOdd (#56)
1 parent 23b27ad commit de30dee

4 files changed

Lines changed: 105 additions & 10 deletions

File tree

‎src/gfloat/round.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ def round_float(
128128
should_round_away = delta + 0.5 >= 1.0
129129
case RoundMode.TiesToEven:
130130
should_round_away = delta > 0.5 or (delta == 0.5 and code_is_odd)
131+
case RoundMode.ToOdd:
132+
should_round_away = delta > 0 and not code_is_odd
131133

132134
case RoundMode.StochasticFastest:
133135
assert srbits is not None

‎src/gfloat/round_ndarray.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ def round_ndarray(
161161
case RoundMode.TiesToEven:
162162
should_round_away = (delta > 0.5) | ((delta == 0.5) & code_is_odd)
163163

164+
case RoundMode.ToOdd:
165+
should_round_away = (delta > 0) & ~code_is_odd
166+
164167
case RoundMode.StochasticFastest:
165168
assert srbits is not None
166169
exp2r = 2**srnumbits

‎src/gfloat/types.py‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) 2024 Graphcore Ltd. All rights reserved.
22

33
from dataclasses import dataclass
4-
from enum import Enum
4+
from enum import Enum, auto
55
import math
66

77

@@ -25,15 +25,16 @@ class RoundMode(Enum):
2525
2626
"""
2727

28-
TowardZero = 1 #: Return the largest :math:`r` such that :math:`|r| \le |v|`
29-
TowardNegative = 2 #: Return the largest :math:`r` such that :math:`r \le v`
30-
TowardPositive = 3 #: Return the smallest :math:`r` such that :math:`r \ge v`
31-
TiesToEven = 4 #: Round to nearest, ties to even
32-
TiesToAway = 5 #: Round to nearest, ties away from zero
33-
Stochastic = 6 #: Stochastic rounding, RTNE before comparison
34-
StochasticOdd = 7 #: Stochastic rounding, RTNO before comparison
35-
StochasticFast = 8 #: Stochastic rounding - faster, but biased
36-
StochasticFastest = 9 #: Stochastic rounding - even faster, but more biased
28+
TowardZero = auto() #: Return the largest :math:`r` such that :math:`|r| \le |v|`
29+
TowardNegative = auto() #: Return the largest :math:`r` such that :math:`r \le v`
30+
TowardPositive = auto() #: Return the smallest :math:`r` such that :math:`r \ge v`
31+
TiesToEven = auto() #: Round to nearest, ties to even
32+
TiesToAway = auto() #: Round to nearest, ties away from zero
33+
ToOdd = auto() #: Round to odd
34+
Stochastic = auto() #: Stochastic rounding, RTNE before comparison
35+
StochasticOdd = auto() #: Stochastic rounding, RTNO before comparison
36+
StochasticFast = auto() #: Stochastic rounding - faster, but biased
37+
StochasticFastest = auto() #: Stochastic rounding - even faster, but more biased
3738

3839

3940
class Domain(Enum):

‎test/test_round.py‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def rnd_array(
5050
@pytest.mark.parametrize("round_float", (rnd_scalar, rnd_array))
5151
def test_round_p3109(round_float: Callable) -> None:
5252
fi = format_info_p3109(8, 4)
53+
54+
assert fi.max == 224.0
5355
assert round_float(fi, 0.0068359375) == 0.0068359375
5456
assert round_float(fi, 0.0029296875) == 0.0029296875
5557
assert round_float(fi, 0.0078125) == 0.0078125
@@ -73,6 +75,47 @@ def test_round_p3109(round_float: Callable) -> None:
7375

7476
assert round_float(fi, 232.1) == np.inf
7577

78+
def _isodd(v: int) -> bool:
79+
return v & 0x1 == 1
80+
81+
assert round_float(fi, 224.1, RoundMode.ToOdd) == np.inf
82+
83+
fi_binary4p2se = format_info_p3109(4, 2, Signedness.Signed, Domain.Extended)
84+
# Top three values are 1.0, 1.5, 2.0; even, odd, even
85+
assert fi_binary4p2se.max == 2.0
86+
assert not _isodd(fi_binary4p2se.code_of_max)
87+
88+
assert round_float(fi_binary4p2se, 1.0, RoundMode.ToOdd) == 1.0
89+
assert round_float(fi_binary4p2se, 1.01, RoundMode.ToOdd) == 1.5
90+
assert round_float(fi_binary4p2se, 1.5, RoundMode.ToOdd) == 1.5
91+
assert round_float(fi_binary4p2se, 1.99, RoundMode.ToOdd) == 1.5
92+
assert round_float(fi_binary4p2se, 2.0, RoundMode.ToOdd) == 2.0 # max
93+
assert round_float(fi_binary4p2se, 2.01, RoundMode.ToOdd) == np.inf
94+
95+
fi_binary4p2ue = format_info_p3109(4, 2, Signedness.Unsigned, Domain.Extended)
96+
# Top two values are 4.0, 6.0, then would be 8.0; even, odd, even
97+
assert fi_binary4p2ue.max == 6.0
98+
assert _isodd(fi_binary4p2ue.code_of_max)
99+
100+
assert round_float(fi_binary4p2ue, 4.00, RoundMode.ToOdd) == 4.0
101+
assert round_float(fi_binary4p2ue, 4.01, RoundMode.ToOdd) == 6.0
102+
assert round_float(fi_binary4p2ue, 6.0, RoundMode.ToOdd) == 6.0 # max
103+
assert round_float(fi_binary4p2ue, 6.01, RoundMode.ToOdd) == 6.0
104+
assert round_float(fi_binary4p2ue, 7.99, RoundMode.ToOdd) == 6.0
105+
assert round_float(fi_binary4p2ue, 8.0, RoundMode.ToOdd) == np.inf # max + 1 ulp
106+
107+
fi_binary4p2uf = format_info_p3109(4, 2, Signedness.Unsigned, Domain.Finite)
108+
# top 3 are 4.0, 6.0, 8.0; even, odd, even; sat must be true
109+
assert fi_binary4p2uf.max == 8.0
110+
assert not _isodd(fi_binary4p2uf.code_of_max)
111+
112+
assert round_float(fi_binary4p2uf, 3.99, RoundMode.ToOdd, True) == 3.0
113+
assert round_float(fi_binary4p2uf, 4.01, RoundMode.ToOdd, True) == 6.0
114+
assert round_float(fi_binary4p2uf, 6.01, RoundMode.ToOdd, True) == 6.0
115+
assert round_float(fi_binary4p2uf, 7.99, RoundMode.ToOdd, True) == 6.0
116+
assert round_float(fi_binary4p2uf, 8.0, RoundMode.ToOdd, True) == 8.0
117+
assert round_float(fi_binary4p2uf, 8.01, RoundMode.ToOdd, True) == 8.0
118+
76119

77120
p4min = 2**-10 # smallest subnormal in 8p4
78121

@@ -171,6 +214,26 @@ def test_round_p3109(round_float: Callable) -> None:
171214
(-58.0, -60.0),
172215
),
173216
),
217+
(
218+
RoundMode.ToOdd,
219+
(
220+
(p4min, p4min),
221+
(p4min / 4, p4min),
222+
(p4min / 2, p4min),
223+
(-p4min, -p4min),
224+
(-p4min / 4, -p4min),
225+
(-p4min / 2, -p4min),
226+
(64.0, 64.0),
227+
(63.0, 60.0),
228+
(62.0, 60.0),
229+
(61.0, 60.0),
230+
(-64.0, -64.0),
231+
(-63.0, -60.0),
232+
(-62.0, -60.0),
233+
(-61.0, -60.0),
234+
(-58.0, -60.0),
235+
),
236+
),
174237
),
175238
)
176239
@pytest.mark.parametrize("round_float", (rnd_scalar, rnd_array))
@@ -320,6 +383,32 @@ def test_round_p3109b(round_float: Callable, mode: RoundMode, vals: list) -> Non
320383
(-np.inf, -np.inf),
321384
),
322385
),
386+
(
387+
(RoundMode.ToOdd, True),
388+
(
389+
(p4max, p4max),
390+
(p4maxhalfup, p4max),
391+
(p4maxup, p4max),
392+
(np.inf, p4max),
393+
(-p4max, -p4max),
394+
(-p4maxhalfup, -p4max),
395+
(-p4maxup, -p4max),
396+
(-np.inf, -p4max),
397+
),
398+
),
399+
(
400+
(RoundMode.ToOdd, False),
401+
(
402+
(p4max, p4max),
403+
(p4maxhalfup, np.inf),
404+
(p4maxup, np.inf),
405+
(np.inf, np.inf),
406+
(-p4max, -p4max),
407+
(-p4maxhalfup, -np.inf),
408+
(-p4maxup, -np.inf),
409+
(-np.inf, -np.inf),
410+
),
411+
),
323412
),
324413
ids=lambda x: f"{str(x[0])}-{'Sat' if x[1] else 'Inf'}" if len(x) == 2 else None,
325414
)

0 commit comments

Comments
 (0)