Skip to content

Fixed sign parsing for ac_fixed and ac_int #727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions hls4ml/backends/fpga/fpga_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,20 @@ def _convert_ac_type(cls, precision):
integer = int(bits[1])
fields = 2
if len(bits) > 2:
signed = bool(bits[2])
# only if the third argument is false or 0, set signed to False
# (default is True)
if bits[2].strip().lower() in ['false', '0']:
signed = False
fields = 3
elif 'int' in precision:
width = int(bits[0])
integer = width
fields = 1
if len(bits) > 1:
signed = bool(bits[1])
# only if the second argument is false or 0, set signed to False
# (default is True)
if bits[1].strip().lower() in ['false', '0']:
signed = False
fields = 2
if len(bits) > fields:
round_mode = bits[fields]
Expand Down
29 changes: 29 additions & 0 deletions test/pytest/test_precision_parsing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import pytest

import hls4ml


@pytest.mark.parametrize(
'prec_pair',
[
('ap_fixed<3, 2>', True),
('ap_ufixed<3, 2>', False),
('ac_fixed<3, 2, true>', True),
('ac_fixed<3, 2, false>', False),
('ac_fixed<3, 2, 1>', True),
('ac_fixed<3, 2, 0>', False),
('ap_int<3, 2>', True),
('ap_uint<3>', False),
('ac_int<3, TRue>', True),
('ac_int<3, FALse>', False),
('ac_int<3, 1>', True),
('ac_int<3, 0>', False),
],
)
def test_sign_parsing(prec_pair):
'''Test that convert_precions_string determines the signedness correctly'''
strprec = prec_pair[0]
signed = prec_pair[1]

evalprec = hls4ml.backends.fpga.fpga_backend.FPGABackend.convert_precision_string(strprec)
assert evalprec.signed == signed