diff --git a/src/V3Const.cpp b/src/V3Const.cpp index 7a9abf574..dbe81ed4a 100644 --- a/src/V3Const.cpp +++ b/src/V3Const.cpp @@ -1162,11 +1162,10 @@ class ConstVisitor final : public VNVisitor { } else if (const AstShiftL* const shiftp = VN_CAST(nodep->rhsp(), ShiftL)) { if (const AstConst* const scp = VN_CAST(shiftp->rhsp(), Const)) { // Check if mask is full over the non-zero bits - V3Number maskLo{nodep, nodep->width()}; - V3Number maskHi{nodep, nodep->width()}; - maskLo.setMask(nodep->width() - scp->num().toUInt()); - maskHi.opShiftL(maskLo, scp->num()); - return checkMask(maskHi); + V3Number mask{nodep, nodep->width()}; + const uint32_t shiftAmount = scp->num().toUInt(); + mask.setMask(nodep->width() - shiftAmount, shiftAmount); + return checkMask(mask); } } return false; diff --git a/src/V3Delayed.cpp b/src/V3Delayed.cpp index 8798aa151..aebf30b62 100644 --- a/src/V3Delayed.cpp +++ b/src/V3Delayed.cpp @@ -531,10 +531,7 @@ class DelayedVisitor final : public VNVisitor { // Constant mask we can compute here if (AstConst* const cLsbp = VN_CAST(sLsbp, Const)) { AstConst* const cp = new AstConst{flp, AstConst::DTyped{}, eDTypep}; - cp->num().setAllBits0(); - const int lsb = cLsbp->toSInt(); - const int msb = lsb + sWidthp->toSInt() - 1; - for (int bit = lsb; bit <= msb; ++bit) cp->num().setBit(bit, '1'); + cp->num().setMask(sWidthp->toSInt(), cLsbp->toSInt()); return cp; } diff --git a/src/V3Expand.cpp b/src/V3Expand.cpp index 1ea5bf865..efe51bed3 100644 --- a/src/V3Expand.cpp +++ b/src/V3Expand.cpp @@ -551,7 +551,7 @@ class ExpandVisitor final : public VNVisitor { const int lsb = lhsp->lsbConst(); const int msb = lhsp->msbConst(); V3Number maskset{nodep, destp->widthMin()}; - for (int bit = lsb; bit < (msb + 1); bit++) maskset.setBit(bit, 1); + maskset.setMask(msb + 1 - lsb, lsb); V3Number maskold{nodep, destp->widthMin()}; maskold.opNot(maskset); if (destwide) { @@ -654,7 +654,7 @@ class ExpandVisitor final : public VNVisitor { fixCloneLvalue(oldvalp); V3Number maskwidth{nodep, destp->widthMin()}; - for (int bit = 0; bit < lhsp->widthConst(); bit++) maskwidth.setBit(bit, 1); + maskwidth.setMask(lhsp->widthConst()); if (destp->isQuad() && !rhsp->isQuad()) rhsp = new AstCCast{nfl, rhsp, nodep}; if (!ones) { diff --git a/src/V3Number.cpp b/src/V3Number.cpp index bb59c39bb..c446947aa 100644 --- a/src/V3Number.cpp +++ b/src/V3Number.cpp @@ -511,9 +511,9 @@ V3Number& V3Number::setValue1() { return *this; } -V3Number& V3Number::setMask(int nbits) { +V3Number& V3Number::setMask(int nbits, int lsb) { setZero(); - for (int bit = 0; bit < nbits; bit++) setBit(bit, 1); + for (int bit = lsb; bit < lsb + nbits; bit++) setBit(bit, 1); return *this; } diff --git a/src/V3Number.h b/src/V3Number.h index c62a631e7..19b449ca3 100644 --- a/src/V3Number.h +++ b/src/V3Number.h @@ -576,7 +576,8 @@ public: V3Number& setAllBits0(); V3Number& setAllBits1(); V3Number& setValue1(); - V3Number& setMask(int nbits); // IE if nbits=1, then 0b1, if 2->0b11, if 3->0b111 etc + // IE if nbits=1, then 0b1, if 2->0b11, if 3->0b111 etc + V3Number& setMask(int nbits, int lsb = 0); // ACCESSORS string ascii(bool prefixed = true, bool cleanVerilog = false) const VL_MT_STABLE;