Skip to content
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

Fix bugs related to 64-bit int conversion, fix #212 #213

Merged
merged 3 commits into from
Dec 1, 2022

Conversation

mkorbel1
Copy link
Contributor

@mkorbel1 mkorbel1 commented Dec 1, 2022

Description & Motivation

Related Issue(s)

Fix #212

Testing

Added new tests covering changes

Backwards-compatibility

Is this a breaking change that will not be backwards-compatible? If yes, how so?

No

Documentation

Does the change require any updates to documentation? If so, where? Are they included?

No

@mkorbel1 mkorbel1 merged commit 7db8cde into intel:main Dec 1, 2022
@mkorbel1 mkorbel1 deleted the bigint64unsigned branch December 1, 2022 18:06
' is greater than ${LogicValue._INT_BITS}');
} else if (width == LogicValue._INT_BITS) {
// With `0x` in front of a hex literal, will be interpreted as unsigned.
return int.parse('0x${toRadixString(16)}');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's got to be a better way to get at this; maybe 32 bits at a time can be safely extracted from the bigint through bit shifting?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, looks like I just barely merged before your comment!

You're right, this appears to work well also, and is probably better? What do you think?

  int toIntUnsigned(int width) {
    if (width > LogicValue._INT_BITS) {
      throw Exception('Cannot convert to BigInt when width $width'
          ' is greater than ${LogicValue._INT_BITS}');
    } else if (width == LogicValue._INT_BITS) {
      const maskWidth = 32;
      final mask = _BigLogicValue._maskOfWidth(maskWidth);
      return (this & mask).toInt() |
          (((this >> maskWidth) & mask).toInt() << maskWidth);
    } else {
      return toInt();
    }
  }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing my late comment. I see no correctness issues in this code. Unfortunately it's a bit more complex than I expected, but I don't see a nice way to clean that up. Despite version control history recording why these changes were made, I still suggest adding a comment explaining that this intentionally complex code is done this way to avoid signed conversion.
Finally, I worry a bit about what happens when this all is run in a javascript environment where _INT_BITS isn't 64. If _INT_BITS isn't even, even adjusting maskWidth won't be able to fix this code. But maybe that's a problem for the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll add a comment.

There's already a lot of problems with javascript that I'm noticing. For example, in this PR I disabled the lint checks for large constants already (like 0xffffffffffffffff) that won't work with javascript. I think there's going to be a number of surprises when compiling to js, unfortunately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants