Hoist the crossed offset test in fast.cpp

Test end.offset <= 1 before revTruncLeOneLow in the reversed crossed
dispatch, and specialize revTruncLeOneLow under that path fact.  The
helper drops the now-tautological end.offset conjuncts and the key
comparison implied by a failed optLe.

The crossed truncated reverse benchmark drops from about 67 ns to
about 61 ns.  Sweep, corpus, SMT cases, and 100% line and branch
coverage still pass.
This commit is contained in:
andrew committed 2026-11-18 14:14:44 -05:00
1 parent e1395f20ef
commit e351a0cf8d
2 files changed
+19 -20

No files matched your search

+3 -2
View File
@@ -132,7 +132,7 @@ coverage. `sweep_test` alone covers every region, function, line, and
branch side of the summarised implementation:
```text
src/fast.cpp 560 regions (0 missed) 121 functions (0 missed) 675 lines (0 missed) 374 branches (0 missed)
src/fast.cpp 553 regions (0 missed) 119 functions (0 missed) 668 lines (0 missed) 362 branches (0 missed)
```
That is 100.00% line, function, region, and branch coverage. Offset 5 is part of
@@ -142,7 +142,8 @@ which the previous `[-3, 4]` window missed.
Because equivalence is only promised on readable inputs, `src/fast.cpp` deletes
both unreachable control-flow branches and predicate operands that an enclosing
dispatch invariant fixes. Examples include the complete-closed family, the
offset-one total/any wrappers, the `end.offset <= 1` shortcuts, the
offset-one total/any wrappers, the hoisted and specialised
`end.offset <= 1` / `revTruncLeOneLow` path split, the
`crossedCompleteEndLeOne` guard, and the repeated
`!c.r->reverse`/`!o.more`/offset tests inside subtrees that are only entered
after those tests succeeded. `literal.cpp` keeps the full Lean conditions so the
+16 -18
View File
@@ -749,22 +749,17 @@ struct Impl {
return c.r->begin.offset == 0;
}
bool negCrossTruncRevUnder(const Ctx& c, const Obs& o) const {
return optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1;
}
bool negCrossTruncRevCrossedLow(const Ctx& c, const Obs& o) const {
return optLt(c.r->end_.key, c.r->begin.key) &&
c.r->end_.offset <= 1 &&
niLe(niAdd(niAdd(cardEB(c),
NatInf::of(static_cast<std::uint64_t>(c.n))),
NatInf::of(toNat(1 - c.r->end_.offset))),
NatInf::of(bSlack(c) + 1));
}
// `revTruncLeOneLow` is reached only under `end.offset <= 1` (the dispatch
// tests that comparison first), so the `end.offset <= 1` conjunct in each
// original disjunct is discharged by the caller and has been dropped here.
bool revTruncLeOneLow(const Ctx& c, const Obs& o) const {
return negCrossTruncRevUnder(c, o) ||
negCrossTruncRevCrossedLow(c, o);
if (optLe(c.r->begin.key, c.r->end_.key)) return true;
// The failed `optLe` test already gives `end.key < begin.key`, so the
// original `optLt` conjunct is implied here.
return niLe(niAdd(niAdd(cardEB(c),
NatInf::of(static_cast<std::uint64_t>(c.n))),
NatInf::of(toNat(1 - c.r->end_.offset))),
NatInf::of(bSlack(c) + 1));
}
bool negCrossTruncRevBelowEnd(const Ctx& c, const Obs& o) const {
@@ -896,10 +891,13 @@ struct Impl {
if (c.r->reverse) {
if (zeroCrossTruncRevCrossed(c, o))
return closedFormZeroCrossTruncRevCrossed(c, o);
if (revTruncLeOneLow(c, o))
return mk(OptKey{o.lastK()}, c.r->end_.key);
if (c.r->end_.offset <= 1)
// `revTruncLeOneLow` implies `end.offset <= 1`, so test the cheap
// comparison first and keep the disjunction on that branch only.
if (c.r->end_.offset <= 1) {
if (revTruncLeOneLow(c, o))
return mk(OptKey{o.lastK()}, c.r->end_.key);
return mk(OptKey{o.lastK()}, c.r->begin.key);
}
if (revTruncGeTwoTop(c, o))
return mk(OptKey{o.lastK()}, topKey());
if (revTruncGeTwoLarge(c, o))