diff --git a/passes/pmgen/peepopt_muldiv_c.pmg b/passes/pmgen/peepopt_muldiv_c.pmg index fc46afcef97..5488dd7a1a6 100644 --- a/passes/pmgen/peepopt_muldiv_c.pmg +++ b/passes/pmgen/peepopt_muldiv_c.pmg @@ -47,14 +47,15 @@ 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; @@ -62,15 +63,15 @@ code 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) @@ -78,8 +79,8 @@ code // 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); @@ -88,6 +89,7 @@ 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) @@ -95,10 +97,9 @@ code 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())