diff --git a/cpp/README.md b/cpp/README.md index bb74f88..7a8420d 100644 --- a/cpp/README.md +++ b/cpp/README.md @@ -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 diff --git a/cpp/src/fast.cpp b/cpp/src/fast.cpp index 61aecef..275c036 100644 --- a/cpp/src/fast.cpp +++ b/cpp/src/fast.cpp @@ -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(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(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))