-
Notifications
You must be signed in to change notification settings - Fork 67
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
Conversation
' 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)}'); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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();
}
}
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Description & Motivation
LogicValue
s can be incorrect if their integer value is larger than the maximum positiveint
value in Dart #212Related Issue(s)
Fix #212
Testing
Added new tests covering changes
Backwards-compatibility
No
Documentation
No