Internals: Favor std::swap. No functional change.

This commit is contained in:
Wilson Snyder 2026-03-29 20:33:06 -04:00
parent 5036af3d37
commit 600eb7ec86
7 changed files with 12 additions and 34 deletions

View File

@ -1482,11 +1482,7 @@ public:
~VNumRange() = default; ~VNumRange() = default;
// MEMBERS // MEMBERS
void init(int hi, int lo, bool ascending) { void init(int hi, int lo, bool ascending) {
if (lo > hi) { if (lo > hi) std::swap(hi, lo);
const int t = hi;
hi = lo;
lo = t;
}
m_left = ascending ? lo : hi; m_left = ascending ? lo : hi;
m_right = ascending ? hi : lo; m_right = ascending ? hi : lo;
m_ranged = true; m_ranged = true;

View File

@ -1185,11 +1185,7 @@ class WidthVisitor final : public VNVisitor {
if (const AstUnpackArrayDType* const adtypep = VN_CAST(fromDtp, UnpackArrayDType)) { if (const AstUnpackArrayDType* const adtypep = VN_CAST(fromDtp, UnpackArrayDType)) {
frommsb = adtypep->hi(); frommsb = adtypep->hi();
fromlsb = adtypep->lo(); fromlsb = adtypep->lo();
if (fromlsb > frommsb) { if (fromlsb > frommsb) std::swap(frommsb, fromlsb);
const int t = frommsb;
frommsb = fromlsb;
fromlsb = t;
}
// However, if the lsb<0 we may go negative, so need more bits! // However, if the lsb<0 we may go negative, so need more bits!
if (fromlsb < 0) frommsb += -fromlsb; if (fromlsb < 0) frommsb += -fromlsb;
nodep->dtypeFrom(adtypep->subDTypep()); // Need to strip off array reference nodep->dtypeFrom(adtypep->subDTypep()); // Need to strip off array reference

View File

@ -448,21 +448,15 @@ class WidthSelVisitor final : public VNVisitor {
adtypep, adtypep,
"Array extraction with width miscomputed " << adtypep->width() << "/" "Array extraction with width miscomputed " << adtypep->width() << "/"
<< fromRange.elements()); << fromRange.elements());
if (fromRange.ascending()) { // Below code assumes descending bit range; just works out if we swap
// Below code assumes descending bit range; just works out if we swap if (fromRange.ascending()) std::swap(msb, lsb);
const int x = msb;
msb = lsb;
lsb = x;
}
if (lsb > msb) { if (lsb > msb) {
nodep->v3warn( nodep->v3warn(
SELRANGE, SELRANGE,
"[" << msb << ":" << lsb "[" << msb << ":" << lsb
<< "] Slice range has ascending bit ordering, perhaps you wanted [" << lsb << "] Slice range has ascending bit ordering, perhaps you wanted [" << lsb
<< ":" << msb << "]"); << ":" << msb << "]");
const int x = msb; std::swap(msb, lsb);
msb = lsb;
lsb = x;
} }
const int elwidth = adtypep->width() / fromRange.elements(); const int elwidth = adtypep->width() / fromRange.elements();
AstSel* const newp AstSel* const newp
@ -477,21 +471,15 @@ class WidthSelVisitor final : public VNVisitor {
nodep->replaceWith(newp); nodep->replaceWith(newp);
VL_DO_DANGLING(pushDeletep(nodep), nodep); VL_DO_DANGLING(pushDeletep(nodep), nodep);
} else if (VN_IS(ddtypep, BasicDType)) { } else if (VN_IS(ddtypep, BasicDType)) {
if (fromRange.ascending()) { // Below code assumes descending bit range; just works out if we swap
// Below code assumes descending bit range; just works out if we swap if (fromRange.ascending()) std::swap(msb, lsb);
const int x = msb;
msb = lsb;
lsb = x;
}
if (lsb > msb) { if (lsb > msb) {
nodep->v3warn( nodep->v3warn(
SELRANGE, SELRANGE,
"[" << msb << ":" << lsb "[" << msb << ":" << lsb
<< "] Slice range has ascending bit ordering, perhaps you wanted [" << lsb << "] Slice range has ascending bit ordering, perhaps you wanted [" << lsb
<< ":" << msb << "]"); << ":" << msb << "]");
const int x = msb; std::swap(msb, lsb);
msb = lsb;
lsb = x;
} }
AstSel* const newp = new AstSel{nodep->fileline(), fromp, newSubLsbOf(lsbp, fromRange), AstSel* const newp = new AstSel{nodep->fileline(), fromp, newSubLsbOf(lsbp, fromRange),
msb + 1 - lsb}; msb + 1 - lsb};
@ -508,9 +496,7 @@ class WidthSelVisitor final : public VNVisitor {
"[" << msb << ":" << lsb "[" << msb << ":" << lsb
<< "] Slice range has ascending bit ordering, perhaps you wanted [" << lsb << "] Slice range has ascending bit ordering, perhaps you wanted [" << lsb
<< ":" << msb << "]"); << ":" << msb << "]");
const int x = msb; std::swap(msb, lsb);
msb = lsb;
lsb = x;
} }
AstSel* const newp = new AstSel{nodep->fileline(), fromp, newSubLsbOf(lsbp, fromRange), AstSel* const newp = new AstSel{nodep->fileline(), fromp, newSubLsbOf(lsbp, fromRange),
msb + 1 - lsb}; msb + 1 - lsb};

View File

@ -1,5 +1,5 @@
%Warning-SELRANGE: t/t_param_sel_range.v:41:33: Selection index out of range: 7:7 outside 4:0 %Warning-SELRANGE: t/t_param_sel_range1.v:41:33: Selection index out of range: 7:7 outside 4:0
: ... note: In instance 't.u2' : ... note: In instance 't.u2'
41 | else r_rst[i] <= r_rst[i-1]; 41 | else r_rst[i] <= r_rst[i-1];
| ^ | ^
... For warning description see https://verilator.org/warn/SELRANGE?v=latest ... For warning description see https://verilator.org/warn/SELRANGE?v=latest

View File

@ -10,7 +10,7 @@
import vltest_bootstrap import vltest_bootstrap
test.scenarios('linter') test.scenarios('linter')
test.top_filename = "t/t_param_sel_range.v" test.top_filename = "t/t_param_sel_range1.v"
test.lint(fails=True, expect_filename=test.golden_filename) test.lint(fails=True, expect_filename=test.golden_filename)