Hi,
I tried to write a comparison of 2 signed vectors with code like this:
if (vec4bit < -3’sd4)
vec4bit is signed [3:0] with a value of 1. I expected the result of this comparison to be false. But my simulator returns true. I used a display statement to test out what is going on:
$display(“%d”, 4’sd1 < -3’sd4);
1
The explanation I’ve gotten is this:
Verilog 2005 LRM section 5.1.7 Relational operators captures below:
When both operands are signed, the expression shall be interpreted as a comparison between signed values. If the operands are of unequal bit lengths, the smaller operand shall be sign-extended to the size of the larger operand.
And here were the steps they took:
4’sd1 < -3’sd4
4’sd1 < 4’(-3’sd4)
4’sd1 < -(4’(3’sd4))
4’sh1 < -(4’(3’sb100))
4’sh1 < -(4’sb1100)). // sign extension
4’sh1 < 4’sb0100 // 2’s complement
But I believe that step 3 is wrong. In step 3, the negation is moved outside until after the extension is done. I can’t find anywhere in the standard where it explicitly states that it should be done this way. In addition, if you compare this way:
$display(“%d”, 4’sd1 < $signed(-3’sd4));
0
The result is correct. Does it make sense that a needless cast should change the result?
Can someone please shed some light on this. I’ve tried this in VCS and Incisive, as well as the other simulators available on edaplayground. I don’t have ModelSim. All the simulators I’ve tested on produce the same unexpected result.
I realize that this behavior ONLY affects the max negative number. All other numbers work out the same both ways.
Thanks,
Nachum