Skip to content

Commit

Permalink
Smallfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akashlevy committed Dec 20, 2024
1 parent 39ff442 commit 832c877
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions passes/pmgen/peepopt_muldiv_c.pmg
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,40 @@ code
int offset = GetSize(div_a) - GetSize(mul_y);

// Get properties and values of b_const and c_const
// b_const may be coming from the A port but it's an RTLIL invariant that A_SIGNED equals B_SIGNED
// b_const may be coming from the A port
// But it is an RTLIL invariant that A_SIGNED equals B_SIGNED
bool b_const_signed = mul->getParam(ID::B_SIGNED).as_bool();
bool c_const_signed = div->getParam(ID::B_SIGNED).as_bool();
int b_const_int = b_const.as_int(b_const_signed);
int c_const_int = c_const.as_int(c_const_signed);
int b_const_int_shifted = b_const_int << offset;

// Helper Lambdas for 2's complement math
// Helper lambdas for two's complement math
auto sign2sComplement = [](auto value, int numBits) {
if (value & (1 << (numBits - 1))) {
return -1;
} else {
return 1;
}
};

auto twosComplement = [](auto value, int numBits) {
if (value & (1 << (numBits - 1))) {
return (~value) + 1; // Invert bits before adding 1
return (~value) + 1; // invert bits before adding 1
} else {
return value;
}
};
// 2's complement convertion

// Two's complement conversion
if (b_const_signed)
b_const_int = sign2sComplement(b_const_int, b_const.size()) * twosComplement(b_const_int, b_const.size());
if (c_const_signed)
c_const_int = sign2sComplement(c_const_int, c_const.size()) * twosComplement(c_const_int, c_const.size());
// Calculate the constant and compress the width to fit the value
Const const_ratio;
Const b_const_actual;
// Avoid division by zero
if (c_const_int == 0)
// Avoid division by zero
reject;
b_const_actual = b_const_int_shifted;
b_const_actual.compress(b_const_signed);
Expand All @@ -88,17 +89,17 @@ code
const_ratio.compress(b_const_signed | c_const_signed);

// Integer values should be lesser than 64 bits
// This is because we are using C++ types, and int is 64 bits
if (mul->getParam(ID::B_WIDTH).size() > 64)
reject;
if (b_const.size() > 64)
reject;
if (c_const.size() + offset > 64)
reject;

// Check for potential mult overflow
if (b_const_actual.size() + a.size() > mul_y.size()) {
// Check for potential multiplier overflow
if (b_const_actual.size() + a.size() > mul_y.size())
reject;
}

// Check that there are only zeros before offset
if (offset < 0 || !div_a.extract(0, offset).is_fully_zero())
Expand Down

0 comments on commit 832c877

Please sign in to comment.