Add EqualSpan and ReverseSpan to hwy/contrib/algo #3376
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
jan-wassenberg
Implements
EqualSpanandReverseSpanfrom the "Remaining STL functions forhwy/contrib/algo" list in op_wishlist.md, following #3370. That leaves
ShuffleSpanandReduce, which I have asked about in #3374 rather thanguessing at the API.
EqualSpan (find-inl.h)
Returns whether
a[0, count)andb[0, count)are equal element-wise, likestd::equal, stopping at the first mismatch.
The tail takes no FirstN mask, unlike AnyOf. LoadN zero-fills the lanes past
remainingon both sides, so those lanes hold the same value and compareequal. A mask there would be dead work.
Argument order is
(d, a, b, count)rather than the(from, count, to)usedby Copy/CopyIf. Copy has a direction, so
countattaches to the source; herethe 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 andspans 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:
lo + 2 N <= hiN <= remaining < 2 N, where the two vectors overlap. Both are loadedbefore either is stored, and the overlapping lanes are written twice with
the second write being the correct one
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_test25 of 25 andcopy_test20 of 20 on AVX2, SSE4, SSSE3, SSE2 andEMU128. 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 andall 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
countthroughthat inlining and emits a spurious -Wstringop-overflow for every instantiation,
418 of them in my first version.