Compare commits
8
Commits
e1395f20ef
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b665b2dc6f | ||
|
|
bf149c26b5 | ||
|
|
f23d8a28d3 | ||
|
|
2c8221be54 | ||
|
|
6e8f5175e2 | ||
|
|
fad9095062 | ||
|
|
023ed53579 | ||
|
|
e351a0cf8d |
No files matched your search
@@ -75,7 +75,8 @@ proof.
|
|||||||
differential fuzzing, and by a deterministic finite sweep of small
|
differential fuzzing, and by a deterministic finite sweep of small
|
||||||
readable instances; `cpp/README.md` describes the model, the tests, the
|
readable instances; `cpp/README.md` describes the model, the tests, the
|
||||||
coverage check, and the benchmark. The summarised implementation has 100%
|
coverage check, and the benchmark. The summarised implementation has 100%
|
||||||
line and branch coverage via `cpp/run-coverage.sh`.
|
coverage on every metric `llvm-cov` reports (region, function, instantiation,
|
||||||
|
line, branch, and MC/DC) via `cpp/run-coverage.sh`.
|
||||||
|
|
||||||
## Continuous integration
|
## Continuous integration
|
||||||
|
|
||||||
|
|||||||
+20
-73
@@ -25,7 +25,7 @@ The Lean build is untouched.
|
|||||||
| `tests/fuzz_raw.cpp` | libFuzzer safety test on arbitrary instances under ASan/UBSan. |
|
| `tests/fuzz_raw.cpp` | libFuzzer safety test on arbitrary instances under ASan/UBSan. |
|
||||||
| `tests/bench.cpp` | nanobench literal-vs-fast benchmark. |
|
| `tests/bench.cpp` | nanobench literal-vs-fast benchmark. |
|
||||||
| `run-fuzz.sh` | Rebuilds and runs both fuzzers for a fixed time. |
|
| `run-fuzz.sh` | Rebuilds and runs both fuzzers for a fixed time. |
|
||||||
| `run-coverage.sh` | Builds with Clang source-based coverage and requires 100% line and branch coverage of `src/fast.cpp`. |
|
| `run-coverage.sh` | Builds with Clang source-based coverage and requires 100% region, function, instantiation, line, branch, and MC/DC coverage of `src/fast.cpp`. |
|
||||||
| `third_party/nanobench.h` | Vendored nanobench (commit `7252da9d96bd0d71f508e9b2813ab1f57fdfaec1`). |
|
| `third_party/nanobench.h` | Vendored nanobench (commit `7252da9d96bd0d71f508e9b2813ab1f57fdfaec1`). |
|
||||||
|
|
||||||
## Data model
|
## Data model
|
||||||
@@ -117,8 +117,8 @@ It runs in about a second and is also the coverage driver. It is exhaustive
|
|||||||
only within those bounds; the class is not all readable inputs, and the
|
only within those bounds; the class is not all readable inputs, and the
|
||||||
guarantee is that every instance in this family agrees. In
|
guarantee is that every instance in this family agrees. In
|
||||||
particular, branch coverage cannot exercise a predicate operand that the
|
particular, branch coverage cannot exercise a predicate operand that the
|
||||||
implementation no longer contains: the coverage result below describes the code
|
implementation no longer contains: the coverage gate describes the code as
|
||||||
as written, it is not a proof of equivalence with `literal.cpp`.
|
written, it is not a proof of equivalence with `literal.cpp`.
|
||||||
|
|
||||||
## Coverage
|
## Coverage
|
||||||
|
|
||||||
@@ -126,23 +126,19 @@ as written, it is not a proof of equivalence with `literal.cpp`.
|
|||||||
./cpp/run-coverage.sh
|
./cpp/run-coverage.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
The script configures a Clang source-based-coverage build, runs
|
The script configures a Clang source-based-coverage build with MC/DC
|
||||||
`sweep_test`, and fails unless `src/fast.cpp` has 100% line and branch
|
instrumentation, runs `sweep_test`, and fails unless `src/fast.cpp` has 100%
|
||||||
coverage. `sweep_test` alone covers every region, function, line, and
|
region, function, instantiation, line, branch, and MC/DC coverage. `sweep_test`
|
||||||
branch side of the summarised implementation:
|
alone covers every region, function, instantiation, line, branch side, and
|
||||||
|
MC/DC condition of the summarised implementation. MC/DC instrumentation requires
|
||||||
```text
|
Clang 18 or newer. The totals are not recorded here: they change with the
|
||||||
src/fast.cpp 560 regions (0 missed) 121 functions (0 missed) 675 lines (0 missed) 374 branches (0 missed)
|
implementation, and the coverage run reports the current ones.
|
||||||
```
|
|
||||||
|
|
||||||
That is 100.00% line, function, region, and branch coverage. Offset 5 is part of
|
|
||||||
the sweep specifically to exercise the last three short-circuit branch sides,
|
|
||||||
which the previous `[-3, 4]` window missed.
|
|
||||||
|
|
||||||
Because equivalence is only promised on readable inputs, `src/fast.cpp` deletes
|
Because equivalence is only promised on readable inputs, `src/fast.cpp` deletes
|
||||||
both unreachable control-flow branches and predicate operands that an enclosing
|
both unreachable control-flow branches and predicate operands that an enclosing
|
||||||
dispatch invariant fixes. Examples include the complete-closed family, the
|
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
|
`crossedCompleteEndLeOne` guard, and the repeated
|
||||||
`!c.r->reverse`/`!o.more`/offset tests inside subtrees that are only entered
|
`!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
|
after those tests succeeded. `literal.cpp` keeps the full Lean conditions so the
|
||||||
@@ -172,17 +168,6 @@ requires exact endpoints.
|
|||||||
`fuzz_raw` only checks memory safety and undefined behaviour for arbitrary
|
`fuzz_raw` only checks memory safety and undefined behaviour for arbitrary
|
||||||
(possibly unreadable) inputs.
|
(possibly unreadable) inputs.
|
||||||
|
|
||||||
One representative validation run on this machine:
|
|
||||||
|
|
||||||
| check | cases / runs | result |
|
|
||||||
| --- | ---: | --- |
|
|
||||||
| literal vs. Lean golden vectors | 40,000 | 0 mismatches (exact endpoints) |
|
|
||||||
| fast vs. literal on sorted corpus | 18,971 | 0 mismatches |
|
|
||||||
| extreme-offset regression (`extreme_test`) | 7,574 | 0 mismatches |
|
|
||||||
| finite readable sweep (`sweep_test`) | 13,565,772 | 0 mismatches |
|
|
||||||
| readable differential fuzzing (60 s) | 1,182,424 | 0 mismatches |
|
|
||||||
| raw safety fuzzing (60 s) | 1,905,270 | no ASan/UBSan findings |
|
|
||||||
|
|
||||||
`extreme_test` covers offsets the fuzzers deliberately avoid (they draw offsets
|
`extreme_test` covers offsets the fuzzers deliberately avoid (they draw offsets
|
||||||
from `[-256, 255]`). Both implementations decide the closed form's
|
from `[-256, 255]`). Both implementations decide the closed form's
|
||||||
`keyAfterN n k < upper` test from the key lengths (`keyAfterNLt`) rather than
|
`keyAfterN n k < upper` test from the key lengths (`keyAfterNLt`) rather than
|
||||||
@@ -191,54 +176,17 @@ constant-time instead of attempting a 2 GiB allocation.
|
|||||||
|
|
||||||
## Benchmark
|
## Benchmark
|
||||||
|
|
||||||
|
`cr_bench` compares the literal translation with the summarised implementation
|
||||||
|
across small and long complete, truncated, and crossed reads in both traversal
|
||||||
|
directions:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
./cpp/build/cr_bench
|
./cpp/build/cr_bench
|
||||||
```
|
```
|
||||||
|
|
||||||
Representative results (Clang 21, default Release build: `-O3 -DNDEBUG`; medians
|
It reports timings and instruction and branch counts per case. Absolute numbers
|
||||||
of nine runs pinned to one core; relative to the literal translation, lower is
|
vary with the machine, compiler, and frequency scaling, so run it for current
|
||||||
better for `fast`).
|
figures.
|
||||||
CPU frequency scaling makes the absolute numbers noisy, but the shape is stable:
|
|
||||||
|
|
||||||
| case | literal | fast | fast/literal |
|
|
||||||
| --- | ---: | ---: | ---: |
|
|
||||||
| small complete (8 keys) | 41.6 ns | 16.2 ns | 0.39x |
|
|
||||||
| small truncated forward | 16.9 ns | 22.9 ns | 1.35x |
|
|
||||||
| crossed truncated forward | 56.0 ns | 46.6 ns | 0.83x |
|
|
||||||
| crossed truncated reverse | 130.1 ns | 82.7 ns | 0.64x |
|
|
||||||
| long complete forward (512 keys) | 2189 ns | 32.6 ns | 0.01x |
|
|
||||||
| long truncated forward | 15.0 ns | 19.6 ns | 1.31x |
|
|
||||||
| long truncated reverse | 9.5 ns | 12.8 ns | 1.35x |
|
|
||||||
|
|
||||||
The large win is on complete reads: the literal translation rescans the reported
|
|
||||||
keys many times (`residualT`, `reportedBelowBegin`, `allBelowEnd`, ...), while
|
|
||||||
the summary answers those questions in `O(log n)` or from a cached count.
|
|
||||||
Plain truncated cases are dominated by the shared decision tree, so the two
|
|
||||||
implementations are close; the literal path is slightly cheaper there because it
|
|
||||||
performs no summary setup. Crossed cases report 16 of a 33-key range, so the
|
|
||||||
summary wins there too.
|
|
||||||
|
|
||||||
Against the same implementation with owned input keys (the previous model), the
|
|
||||||
borrowed-key model changes the costs as follows (nine interleaved runs each,
|
|
||||||
median). The crossed cases are absent from this table: their benchmark inputs
|
|
||||||
were corrected to nonempty ranges after the owned-key measurements.
|
|
||||||
|
|
||||||
| case | literal (borrow vs own) | fast (borrow vs own) |
|
|
||||||
| --- | ---: | ---: |
|
|
||||||
| small complete | -64% | -17% |
|
|
||||||
| small truncated forward | -33% | -19% |
|
|
||||||
| long complete forward | -69% | -15% |
|
|
||||||
| long truncated forward | -29% | -6% |
|
|
||||||
| long truncated reverse | -16% | +16% |
|
|
||||||
|
|
||||||
The complete-read gains come from not copying the reported keys just to
|
|
||||||
normalise the result: a forward result is now the caller's span, and a reverse
|
|
||||||
result is a reversed view rather than an owned copy. The fast path also benefits
|
|
||||||
because it previously normalised complete results the same way. Truncated
|
|
||||||
reverse (`fast`) is the one small regression: it is a few nanoseconds and a few
|
|
||||||
dozen instructions, and the other paths move in the opposite direction, so it
|
|
||||||
reads as code-layout/code-generation noise rather than extra algorithmic work.
|
|
||||||
|
|
||||||
|
|
||||||
## Why the fast implementation is equivalent
|
## Why the fast implementation is equivalent
|
||||||
|
|
||||||
@@ -251,8 +199,7 @@ only changes are:
|
|||||||
reported keys, so they survive the reversed-view normalisation.
|
reported keys, so they survive the reversed-view normalisation.
|
||||||
* Interval cardinalities (`intervalCard`) are cached lazily.
|
* Interval cardinalities (`intervalCard`) are cached lazily.
|
||||||
* Complete observations are not copied: a forward result borrows the caller's
|
* Complete observations are not copied: a forward result borrows the caller's
|
||||||
span, and a reversed result is viewed backwards in place. (The previous
|
span, and a reversed result is viewed backwards in place.
|
||||||
owned-key model copied and reversed the reported keys here.)
|
|
||||||
* `allBelowEnd` is `residualT == 0` and `anyAtOrAboveEnd` is `residualT != 0`.
|
* `allBelowEnd` is `residualT == 0` and `anyAtOrAboveEnd` is `residualT != 0`.
|
||||||
|
|
||||||
No branch condition or returned endpoint is otherwise changed on readable
|
No branch condition or returned endpoint is otherwise changed on readable
|
||||||
|
|||||||
+30
-13
@@ -1,8 +1,8 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# Build with Clang source-based coverage and require 100% line and branch
|
# Build with Clang source-based coverage and require 100% region, function,
|
||||||
# coverage of the summarised implementation (`src/fast.cpp`). `sweep_test`
|
# instantiation, line, branch, and MC/DC coverage of the summarised
|
||||||
# deterministically enumerates a finite class of small readable instances and is
|
# implementation (`src/fast.cpp`). `sweep_test` deterministically enumerates a
|
||||||
# sufficient on its own.
|
# finite class of small readable instances and is sufficient on its own.
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||||
@@ -72,7 +72,7 @@ cmake -S "$here" -B "$build" -G Ninja \
|
|||||||
-DCMAKE_BUILD_TYPE=Debug \
|
-DCMAKE_BUILD_TYPE=Debug \
|
||||||
-DCR_BUILD_FUZZERS=OFF \
|
-DCR_BUILD_FUZZERS=OFF \
|
||||||
-DCR_BUILD_BENCH=OFF \
|
-DCR_BUILD_BENCH=OFF \
|
||||||
-DCMAKE_CXX_FLAGS="-fprofile-instr-generate -fcoverage-mapping" \
|
-DCMAKE_CXX_FLAGS="-fprofile-instr-generate -fcoverage-mapping -fcoverage-mcdc" \
|
||||||
-DCMAKE_EXE_LINKER_FLAGS="-fprofile-instr-generate"
|
-DCMAKE_EXE_LINKER_FLAGS="-fprofile-instr-generate"
|
||||||
cmake --build "$build" --target sweep_test
|
cmake --build "$build" --target sweep_test
|
||||||
|
|
||||||
@@ -86,18 +86,35 @@ LLVM_PROFILE_FILE="$profdir/sweep-%p.profraw" "$build/sweep_test"
|
|||||||
|
|
||||||
report() {
|
report() {
|
||||||
"$cov" report "$build/sweep_test" \
|
"$cov" report "$build/sweep_test" \
|
||||||
-instr-profile="$profdir/cov.profdata" src/fast.cpp
|
-instr-profile="$profdir/cov.profdata" \
|
||||||
|
--show-branch-summary \
|
||||||
|
--show-instantiation-summary \
|
||||||
|
--show-mcdc-summary \
|
||||||
|
--show-region-summary \
|
||||||
|
src/fast.cpp
|
||||||
}
|
}
|
||||||
output="$(report)"
|
output="$(report)"
|
||||||
printf '%s\n' "$output"
|
printf '%s\n' "$output"
|
||||||
line_cover="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print $10 }')"
|
|
||||||
branch_cover="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print $13 }')"
|
# The row fields follow the summary order enabled above:
|
||||||
if [[ "$line_cover" != "100.00%" ]]; then
|
# region, function, instantiation, line, branch, and MC/DC coverage.
|
||||||
echo "fast.cpp line coverage is $line_cover, expected 100.00%" >&2
|
row="$(printf '%s\n' "$output" | awk '/fast\.cpp/ { print; exit }')"
|
||||||
|
if [[ -z "$row" ]]; then
|
||||||
|
echo "coverage report did not contain src/fast.cpp" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
if [[ "$branch_cover" != "100.00%" ]]; then
|
read -r -a fields <<< "$row"
|
||||||
echo "fast.cpp branch coverage is $branch_cover, expected 100.00%" >&2
|
metrics=(region function instantiation line branch MC/DC)
|
||||||
|
columns=(3 6 9 12 15 18)
|
||||||
|
failed=0
|
||||||
|
for i in "${!metrics[@]}"; do
|
||||||
|
cover="${fields[${columns[$i]}]}"
|
||||||
|
if [[ "$cover" != "100.00%" ]]; then
|
||||||
|
echo "fast.cpp ${metrics[$i]} coverage is ${cover:-missing}, expected 100.00%" >&2
|
||||||
|
failed=1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [[ "$failed" -ne 0 ]]; then
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
echo "fast.cpp: 100.00% line and branch coverage"
|
echo "fast.cpp: 100.00% region, function, instantiation, line, branch, and MC/DC coverage"
|
||||||
+47
-87
@@ -250,30 +250,6 @@ struct Impl {
|
|||||||
return c.r->end_.offset == 1;
|
return c.r->end_.offset == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool negCrossHeadBottom(const Ctx& c, const Obs& o) const {
|
|
||||||
return c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key) &&
|
|
||||||
o.firstK().empty();
|
|
||||||
}
|
|
||||||
|
|
||||||
bool negCrossClampNonposBottom(const Ctx& c, const Obs& o) const {
|
|
||||||
return c.r->end_.offset <= 0 && optLe(c.r->begin.key, c.r->end_.key);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool negCrossClampLongNonpos(const Ctx& c, const Obs& o) const {
|
|
||||||
return c.r->end_.offset <= 0 && optLt(c.r->end_.key, c.r->begin.key);
|
|
||||||
}
|
|
||||||
|
|
||||||
ConflictRange closedFormNegCrossClampUnified(const Ctx& c, const Obs& o) const {
|
|
||||||
OptKey end;
|
|
||||||
if (negCrossClampEndOne(c, o))
|
|
||||||
end = clampEndOneUpper(c, o);
|
|
||||||
else if (negCrossClampLongNonpos(c, o))
|
|
||||||
end = clampLongUpper(c, o);
|
|
||||||
else
|
|
||||||
end = c.r->end_.key;
|
|
||||||
return mk(bottomKey(), std::move(end));
|
|
||||||
}
|
|
||||||
|
|
||||||
OptKey negCrossCompleteHighM(const Ctx& c, const Obs& o) const {
|
OptKey negCrossCompleteHighM(const Ctx& c, const Obs& o) const {
|
||||||
return optMax(c.r->begin.key, after(o.lastK()));
|
return optMax(c.r->begin.key, after(o.lastK()));
|
||||||
}
|
}
|
||||||
@@ -412,15 +388,11 @@ struct Impl {
|
|||||||
return niLe(lhs, rhs);
|
return niLe(lhs, rhs);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool negCrossTruncRevResidualTopLargeSentinel(const Ctx& c, const Obs& o) const {
|
|
||||||
return negCrossTruncRevResidualTopLarge(c, o) && isTop(c.r->begin.key);
|
|
||||||
}
|
|
||||||
|
|
||||||
ConflictRange closedFormNegCrossTruncRevResidualTopLargeUnified(const Ctx& c,
|
ConflictRange closedFormNegCrossTruncRevResidualTopLargeUnified(const Ctx& c,
|
||||||
const Obs& o) const {
|
const Obs& o) const {
|
||||||
OptKey L = OptKey{o.lastK()};
|
OptKey L = OptKey{o.lastK()};
|
||||||
if (negCrossTruncRevResidualTopLargeSentinel(c, o) &&
|
// The caller has already established `negCrossTruncRevResidualTopLarge`.
|
||||||
sentinelLargeTight(c, o))
|
if (isTop(c.r->begin.key) && sentinelLargeTight(c, o))
|
||||||
return mk(std::move(L), topKey());
|
return mk(std::move(L), topKey());
|
||||||
return mk(c.r->end_.key, topKey());
|
return mk(c.r->end_.key, topKey());
|
||||||
}
|
}
|
||||||
@@ -613,7 +585,8 @@ struct Impl {
|
|||||||
OptKeyView begin = closedFormBegin(c, o);
|
OptKeyView begin = closedFormBegin(c, o);
|
||||||
if (c.r->end_.offset <= 1)
|
if (c.r->end_.offset <= 1)
|
||||||
return mk(std::move(begin), c.r->end_.key);
|
return mk(std::move(begin), c.r->end_.key);
|
||||||
if (clampsToBottom(c) || optLe(c.r->begin.key, c.r->end_.key)) {
|
// `begin` is `⊥` exactly when `clampsToBottom`, and `⊥ <= end` always.
|
||||||
|
if (optLe(begin, c.r->end_.key)) {
|
||||||
if (c.t == rE(c)) return mk(std::move(begin), after(o.lastK()));
|
if (c.t == rE(c)) return mk(std::move(begin), after(o.lastK()));
|
||||||
if (allBelowEnd(c)) return mk(std::move(begin), topKey());
|
if (allBelowEnd(c)) return mk(std::move(begin), topKey());
|
||||||
if (closedFormFinite(c)) return mk(std::move(begin), after(o.lastK()));
|
if (closedFormFinite(c)) return mk(std::move(begin), after(o.lastK()));
|
||||||
@@ -679,10 +652,9 @@ struct Impl {
|
|||||||
: c.r->begin.key;
|
: c.r->begin.key;
|
||||||
return mk(std::move(begin), after(o.firstK()));
|
return mk(std::move(begin), after(o.firstK()));
|
||||||
}
|
}
|
||||||
OptKeyView begin =
|
bool beginAtEnd = optLt(c.r->end_.key, c.r->begin.key) &&
|
||||||
(optLt(c.r->end_.key, c.r->begin.key) && closedFormCrossedRoom(c, o))
|
closedFormCrossedRoom(c, o);
|
||||||
? c.r->end_.key
|
OptKeyView begin = beginAtEnd ? c.r->end_.key : c.r->begin.key;
|
||||||
: c.r->begin.key;
|
|
||||||
return mk(std::move(begin), topKey());
|
return mk(std::move(begin), topKey());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -712,8 +684,9 @@ struct Impl {
|
|||||||
bool crossedCompleteEndLeOne(const Ctx& c) const {
|
bool crossedCompleteEndLeOne(const Ctx& c) const {
|
||||||
if (c.r->end_.offset == 1 && bSlack(c) < c.below)
|
if (c.r->end_.offset == 1 && bSlack(c) < c.below)
|
||||||
return true;
|
return true;
|
||||||
return c.r->end_.offset <= 0 &&
|
if (c.r->end_.offset > 0) return false;
|
||||||
!(c.below <= bSlack(c) && clampNonposBottom(c));
|
bool clampedBottom = c.below <= bSlack(c) && clampNonposBottom(c);
|
||||||
|
return !clampedBottom;
|
||||||
}
|
}
|
||||||
|
|
||||||
// The overshoot branch above already covered the offset-zero overshoot
|
// The overshoot branch above already covered the offset-zero overshoot
|
||||||
@@ -722,12 +695,6 @@ struct Impl {
|
|||||||
return c.r->begin.offset == 0;
|
return c.r->begin.offset == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool crossedCompleteClamp(const Ctx& c, const Obs& o) const {
|
|
||||||
return negCrossClampEndOne(c, o) || negCrossHeadBottom(c, o) ||
|
|
||||||
negCrossClampNonposBottom(c, o) ||
|
|
||||||
negCrossClampLongNonpos(c, o);
|
|
||||||
}
|
|
||||||
|
|
||||||
ConflictRange closedFormCrossedComplete(const Ctx& c, const Obs& o) const {
|
ConflictRange closedFormCrossedComplete(const Ctx& c, const Obs& o) const {
|
||||||
if (crossedCompleteOvershoot(c))
|
if (crossedCompleteOvershoot(c))
|
||||||
return closedFormNegCrossTopUnified(c, o);
|
return closedFormNegCrossTopUnified(c, o);
|
||||||
@@ -735,8 +702,15 @@ struct Impl {
|
|||||||
return closedFormNegCrossNonclampUnified(c, o);
|
return closedFormNegCrossNonclampUnified(c, o);
|
||||||
if (crossedCompleteBeginZero(c, o))
|
if (crossedCompleteBeginZero(c, o))
|
||||||
return closedFormZeroCrossResidual(c, o);
|
return closedFormZeroCrossResidual(c, o);
|
||||||
if (crossedCompleteClamp(c, o))
|
if (negCrossClampEndOne(c, o))
|
||||||
return closedFormNegCrossClampUnified(c, o);
|
return mk(bottomKey(), clampEndOneUpper(c, o));
|
||||||
|
if (c.r->end_.offset <= 0) {
|
||||||
|
// The two clamp cases below are `end.key < begin.key` and its negation;
|
||||||
|
// the non-strict ordering is implied by the failed strict test.
|
||||||
|
if (optLt(c.r->end_.key, c.r->begin.key))
|
||||||
|
return mk(bottomKey(), clampLongUpper(c, o));
|
||||||
|
return mk(bottomKey(), c.r->end_.key);
|
||||||
|
}
|
||||||
return closedFormNegCrossCompleteHighResidual(c, o);
|
return closedFormNegCrossCompleteHighResidual(c, o);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -749,26 +723,22 @@ struct Impl {
|
|||||||
return c.r->begin.offset == 0;
|
return c.r->begin.offset == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool negCrossTruncRevUnder(const Ctx& c, const Obs& o) const {
|
// `revTruncLeOneLow` is reached only under `end.offset <= 1` (the dispatch
|
||||||
return optLe(c.r->begin.key, c.r->end_.key) && c.r->end_.offset <= 1;
|
// 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 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));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool revTruncLeOneLow(const Ctx& c, const Obs& o) const {
|
bool revTruncLeOneLow(const Ctx& c, const Obs& o) const {
|
||||||
return negCrossTruncRevUnder(c, o) ||
|
if (optLe(c.r->begin.key, c.r->end_.key)) return true;
|
||||||
negCrossTruncRevCrossedLow(c, o);
|
// 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 {
|
bool negCrossTruncRevBelowEnd(const Ctx& c, const Obs& o) const {
|
||||||
return overshoot(c);
|
// The dispatch has already failed `end.offset <= 1`, so `end.offset >= 2`.
|
||||||
|
return allBelowEnd(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool negCrossTruncRevResidualRoom(const Ctx& c, const Obs& o) const {
|
bool negCrossTruncRevResidualRoom(const Ctx& c, const Obs& o) const {
|
||||||
@@ -776,33 +746,20 @@ struct Impl {
|
|||||||
NatInf::of(rE(c)));
|
NatInf::of(rE(c)));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool negCrossTruncRevResidualTopCrossedSmallGap(const Ctx& c,
|
|
||||||
const Obs& o) const {
|
|
||||||
if (!oLtK(c.r->end_.key, o.lastK())) return false;
|
|
||||||
NatInf lhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
|
|
||||||
NatInf::of(static_cast<std::uint64_t>(c.n)));
|
|
||||||
return niLt(lhs, NatInf::of(rE(c)));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool negCrossTruncRevResidualTopCrossed(const Ctx& c, const Obs& o) const {
|
|
||||||
return !oLtK(c.r->end_.key, o.lastK()) && c.t < rE(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool revTruncGeTwoTop(const Ctx& c, const Obs& o) const {
|
bool revTruncGeTwoTop(const Ctx& c, const Obs& o) const {
|
||||||
return negCrossTruncRevBelowEnd(c, o) ||
|
if (negCrossTruncRevBelowEnd(c, o)) return true;
|
||||||
negCrossTruncRevResidualRoom(c, o) ||
|
if (negCrossTruncRevResidualRoom(c, o)) return true;
|
||||||
negCrossTruncRevResidualTopCrossedSmallGap(c, o) ||
|
// The two `TopCrossed` cases split on `end.key < last`, so test it once.
|
||||||
negCrossTruncRevResidualTopCrossed(c, o);
|
if (oLtK(c.r->end_.key, o.lastK())) {
|
||||||
}
|
NatInf lhs = niAdd(cardOK(c.r->end_.key, o.lastK()),
|
||||||
|
NatInf::of(static_cast<std::uint64_t>(c.n)));
|
||||||
bool negCrossTruncRevResidualTopLargeCrossed(const Ctx& c, const Obs& o) const {
|
return niLt(lhs, NatInf::of(rE(c)));
|
||||||
return negCrossTruncRevResidualTopLarge(c, o) &&
|
}
|
||||||
!isTop(c.r->begin.key);
|
return c.t < rE(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool revTruncGeTwoLarge(const Ctx& c, const Obs& o) const {
|
bool revTruncGeTwoLarge(const Ctx& c, const Obs& o) const {
|
||||||
return negCrossTruncRevResidualTopLargeCrossed(c, o) ||
|
return negCrossTruncRevResidualTopLarge(c, o);
|
||||||
negCrossTruncRevResidualTopLargeSentinel(c, o);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool negCrossTruncTopTop(const Ctx& c, const Obs& o) const {
|
bool negCrossTruncTopTop(const Ctx& c, const Obs& o) const {
|
||||||
@@ -896,10 +853,13 @@ struct Impl {
|
|||||||
if (c.r->reverse) {
|
if (c.r->reverse) {
|
||||||
if (zeroCrossTruncRevCrossed(c, o))
|
if (zeroCrossTruncRevCrossed(c, o))
|
||||||
return closedFormZeroCrossTruncRevCrossed(c, o);
|
return closedFormZeroCrossTruncRevCrossed(c, o);
|
||||||
if (revTruncLeOneLow(c, o))
|
// `revTruncLeOneLow` implies `end.offset <= 1`, so test the cheap
|
||||||
return mk(OptKey{o.lastK()}, c.r->end_.key);
|
// comparison first and keep the disjunction on that branch only.
|
||||||
if (c.r->end_.offset <= 1)
|
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);
|
return mk(OptKey{o.lastK()}, c.r->begin.key);
|
||||||
|
}
|
||||||
if (revTruncGeTwoTop(c, o))
|
if (revTruncGeTwoTop(c, o))
|
||||||
return mk(OptKey{o.lastK()}, topKey());
|
return mk(OptKey{o.lastK()}, topKey());
|
||||||
if (revTruncGeTwoLarge(c, o))
|
if (revTruncGeTwoLarge(c, o))
|
||||||
|
|||||||
+3
-5
@@ -169,11 +169,9 @@ inline OptKey optMax(const std::optional<A>& a, const std::optional<B>& b) {
|
|||||||
|
|
||||||
inline OptKey topKey() { return std::nullopt; }
|
inline OptKey topKey() { return std::nullopt; }
|
||||||
inline OptKey bottomKey() { return OptKey{Key{}}; }
|
inline OptKey bottomKey() { return OptKey{Key{}}; }
|
||||||
// A view of a static empty string, so empty views always have a valid pointer.
|
// A view of the empty string literal, so empty views always have a valid
|
||||||
inline KeyView emptyKeyView() {
|
// pointer. Unlike a function-local static, this needs no guard variable.
|
||||||
static const Key empty;
|
inline KeyView emptyKeyView() { return KeyView("", 0); }
|
||||||
return KeyView(empty);
|
|
||||||
}
|
|
||||||
inline OptKeyView bottomKeyView() { return OptKeyView{emptyKeyView()}; }
|
inline OptKeyView bottomKeyView() { return OptKeyView{emptyKeyView()}; }
|
||||||
|
|
||||||
inline bool isTop(OptKeyView k) { return !k.has_value(); }
|
inline bool isTop(OptKeyView k) { return !k.has_value(); }
|
||||||
|
|||||||
@@ -28,8 +28,6 @@ namespace {
|
|||||||
|
|
||||||
using namespace cr;
|
using namespace cr;
|
||||||
|
|
||||||
// Offset 5 is what lets the default sweep exercise the last three
|
|
||||||
// short-circuit branch sides in `fast.cpp` (all in crossed subtrees).
|
|
||||||
const std::int32_t kOffsets[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
|
const std::int32_t kOffsets[] = {-3, -2, -1, 0, 1, 2, 3, 4, 5};
|
||||||
|
|
||||||
void check(const RangeRead& r, const Result& res, long long& cases,
|
void check(const RangeRead& r, const Result& res, long long& cases,
|
||||||
|
|||||||
Reference in new issue
Block a user