Skip to content

fix(HwSort): update sorting logic to prioritize valid elements#148

Merged
ywlcode merged 1 commit into
masterfrom
fix-HwSort
May 18, 2026
Merged

fix(HwSort): update sorting logic to prioritize valid elements#148
ywlcode merged 1 commit into
masterfrom
fix-HwSort

Conversation

@ywlcode

@ywlcode ywlcode commented May 15, 2026

Copy link
Copy Markdown
Contributor

1

HwSort only swapped two entries when both were valid. An invalid entry could block valid entries behind it, so valid entries were not guaranteed to be packed together or sorted.

Example:
in0: valid, ptr = 10
in1: invalid, ptr = X
in2: valid, ptr = 5

The old logic could keep this order:

valid ptr=10, invalid, valid ptr=5

Solution: change the base 2-entry compare-swap to valid-first ordering:

val swap = xVec(1).valid && (!xVec(0).valid || cmp(xVec(1).ptr, xVec(0).ptr))

New behavior: valid entries are moved to the front and sorted by cmp. Equal keys and invalid entries are not guaranteed to be stable.

2

The old 4-way final-stage wiring was incorrect: it compared (0,1) but wrote the result to (1,2), and compared (2,3) but wrote the result to (0,3).

This could corrupt even already sorted inputs:

input = [0, 1, 2, 3]
output = [2, 0, 1, 3]

The fix writes each compare-swap result back to its matching lanes:

tmp3_1 -> row3(0), row3(1)
tmp3_2 -> row3(2), row3(3)

@ywlcode ywlcode requested a review from Maxpicca-Li May 15, 2026 08:24
@ywlcode ywlcode requested a review from linjuanZ May 18, 2026 07:55
@ywlcode ywlcode changed the title fix(HwSort): update sorting logic to prioritize valid elements and delete stability guarantees fix(HwSort): update sorting logic to prioritize valid elements May 18, 2026
@ywlcode ywlcode merged commit bae2aef into master May 18, 2026
1 check passed
@ywlcode ywlcode deleted the fix-HwSort branch May 18, 2026 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants