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

[Bug] signed_mul_overflow bug #98

Open
JaewonHur opened this issue Dec 4, 2019 · 1 comment
Open

[Bug] signed_mul_overflow bug #98

JaewonHur opened this issue Dec 4, 2019 · 1 comment

Comments

@JaewonHur
Copy link
Contributor

JaewonHur commented Dec 4, 2019

in the code mor1kx_excute_alu.v

    // One signed overflow detection for all multiplication implmentations
    assign mul_signed_overflow = (FEATURE_MULTIPLIER=="NONE") || (FEATURE_MULTIPLIER=="PIPELINED") ? 1'b0 :
    // Same signs, check for negative result
    // (should be positive)
    ((a[OPTION_OPERAND_WIDTH-1] == b[OPTION_OPERAND_WIDTH-1]) && mul_result[OPTION_OPERAND_WIDTH-1]) ||
    // Differring signs, check for positive result
    // (should be negative)
    ((a[OPTION_OPERAND_WIDTH-1] ^ b[OPTION_OPERAND_WIDTH-1]) && !mul_result[OPTION_OPERAND_WIDTH-1]);

multiplying negative value with 0 will mistakenly assert the overflow.
Also, other cases such as '0x4 * 0x5fffffff' will not detect the overflow.

In the case of SERIAL multiplier, we can fix it by changing

assign mul_signed_overflow = (mul_prod_r[(OPTION_OPERAND_WIDTH*2)-1:OPTION_OPERAND_WIDTH] == 32'h0 || mul_prod_r[(OPTION_OPERAND_WIDTH*2)-1:OPTION_OPERAND_WIDTH] == 32'hffffffff) ? 0:1;
@JaewonHur
Copy link
Contributor Author

Modifying the solution, I misunderstood multiply signed overflow.
The bug was that the original mul_signed_overflow code does not consider when one of the operands is 0 and the other is a negative value.
In this case, the original code will always set mul_signed_overflow.

My solution is

// One signed overflow detection for all multiplication implmentations
    assign mul_signed_overflow = (FEATURE_MULTIPLIER=="NONE") ||
 				(FEATURE_MULTIPLIER=="PIPELINED") ? 1'b0 :
                                // When either a or b is 0, result should not
                                // be negative
                                ((a == 0 || b == 0) && mul_result[OPTION_OPERAND_WIDTH-1]) ||
 				// Same signs, check for negative result
 				// (should be positive)
 				((a[OPTION_OPERAND_WIDTH-1] ==
 				  b[OPTION_OPERAND_WIDTH-1]) &&
 				 mul_result[OPTION_OPERAND_WIDTH-1]) ||
 				// Differring signs, check for positive result
 				// (should be negative)
 				((a[OPTION_OPERAND_WIDTH-1] ^
 				  b[OPTION_OPERAND_WIDTH-1]) &&
 				 !mul_result[OPTION_OPERAND_WIDTH-1]);

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

No branches or pull requests

1 participant