-
Notifications
You must be signed in to change notification settings - Fork 463
Fix compile-time concatenation with negative right operand #5233
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
Conversation
The test P4 program and its expected output looks correct to me. |
if (rt->isSigned) { | ||
rt = IR::Type_Bits::get(rt->size); | ||
right = new IR::Constant(rt, right->value, 10, true /* no warning */); | ||
} |
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 no need to create a new IR::Constant here that you're going to throw away -- just take the big_int value and fix it if it is negative:
big_int rvalue = right->value;
if (rvalue < 0)
rvalue &= Util::mask(rt->size);
and then use rvalue
later instead of right->value
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.
I do not like the idea of spreading a conversion function from 2-complement signed to 2-complement unsigned over multiple places in the codebase. It is already in the Constant::handleOverflow
(invoked from the ctor) and it is not really possible to extract it from there because of the warnings which use the constant value in the logs.
I've changed the code to avoid the unnecessary dynamic allocation at least.
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.
IMO this is more obscure than just doing the mask with the big_int
value. boost::mulitprecision specifies that bitwise operations on negative value happen as-if the values are 2s complement (regardless of what the actual underlying representation is), so that does what we want for concatenation according to the P4 spec.
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
... because size is not correct for Extracted_Varbits Signed-off-by: Vladimír Štill <[email protected]>
Fixes #5231. The problem is most likely in the binary representation of the
big_int
for negative numbers. The solution is to always convert the right operand to a unsigned value with the same representation as does the signed value have.