← Back to Pull requests

Add EqualSpan and ReverseSpan to hwy/contrib/algo #3376

Closed
unknown closed this pull request without merging on Sep 11, 2026 17:36 UTC
View on GitHub
unknown unknown commented on Sep 11, 2026 16:13 UTC
Author

Implements EqualSpan and ReverseSpan from the "Remaining STL functions for
hwy/contrib/algo" list in op_wishlist.md, following #3370. That leaves
ShuffleSpan and Reduce, which I have asked about in #3374 rather than
guessing at the API.

EqualSpan (find-inl.h)

Returns whether a[0, count) and b[0, count) are equal element-wise, like
std::equal, stopping at the first mismatch.

The tail takes no FirstN mask, unlike AnyOf. LoadN zero-fills the lanes past
remaining on both sides, so those lanes hold the same value and compare
equal. A mask there would be dead work.

Argument order is (d, a, b, count) rather than the (from, count, to) used
by Copy/CopyIf. Copy has a direction, so count attaches to the source; here
the two spans are symmetric and share one count. Dot::Compute(d, pa, pb, num)
in contrib/dot is the closest precedent and does the same. Happy to change it
if you prefer the Copy shape.

It unrolls 4x like AnyOf rather than 2x like Find, since the position of the
mismatch is never needed.

NaN follows std::equal and operator==, so a NaN is not equal to itself and
spans holding a NaN at the same index are reported unequal. That is documented
on the function.

ReverseSpan (copy-inl.h)

Reverses a span in place, like std::reverse. Each iteration takes one vector
from each end, reverses it and stores it at the opposite end. Three cases:

  • the main loop, while lo + 2 N <= hi

  • N <= remaining < 2 N, where the two vectors overlap. Both are loaded

  • before either is stored, and the overlapping lanes are written twice with
    the second write being the correct one
  • fewer than N elements left, handled with LoadN, Reverse and

  • SlideDownLanes(N - remaining) so that no memory past the end is read

    I kept the overlapping pair as its own block rather than folding it into the
    loop with a second exit, because the three cases read more clearly separated.
    Glad to restructure if you would rather see one copy of the body.

    Verification

    find_test 25 of 25 and copy_test 20 of 20 on AVX2, SSE4, SSSE3, SSE2 and
    EMU128. Compiled clean for AVX3, AVX3_DL, AVX3_ZEN4, AVX3_SPR and AVX10_2, and
    separately for HWY_SCALAR. The two -inl.h headers also compile under C++11 with
    HWY_COMPILE_ALL_ATTAINABLE. Zero warnings from both GCC 16 and Clang 22 under
    the warning set in CMakeLists, including -Wconversion and -Wsign-conversion.

    The tests sweep every count up to 4 * Lanes(d) with three misalignments and
    all types. ReverseSpan additionally checks a sentinel one past the end, so an
    over-store is caught, and checks that reversing twice restores the input.

    I broke each implementation on purpose to confirm the tests would notice:

    | Mutation | Caught at |
    | --- | --- |
    | EqualSpan tail check removed | count 324, differing element at 320 |
    | EqualSpan 4th vector dropped from the Or | count 450, differing element at 225 |
    | ReverseSpan made a no-op | count 2 |
    | ReverseSpan short-tail branch skipped | count 2 |
    | ReverseSpan overlap branch skipped | count 33 |

    I have not run NEON, SVE, RVV, WASM or the big-endian targets, only compiled
    what this x86 box can reach.

    One note on the test for ReverseSpan: it builds its reference by hand instead
    of calling std::reverse on a std::vector. GCC 16 cannot bound count through
    that inlining and emits a spurious -Wstringop-overflow for every instantiation,
    418 of them in my first version.

    Diff: 426 lines (+319 / -107) across 5 files Reviewer: jan-wassenberg Review duration: 82m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 426 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.362
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #3376"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.1% (MEDIUM) · Higher = larger blast radius
    45.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.8% · Safe to merge
    17.8%
    Repository