Skip to content

Fix AVX-512 reduce_max/reduce_min missing bytes 32-39 for 8-bit types - #1431

Merged
DiamonDinoia merged 1 commit into
xtensor-stack:masterfrom
emrys53:bug/reduce_min/max
Oct 23, 2026
Merged

DiamonDinoia merged 1 commit into
xtensor-stack:masterfrom
emrys53:bug/reduce_min/max

Conversation

@emrys53

@emrys53 emrys53 commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

The avx512f kernels folded the upper 256 bits into the lower half with _mm512_permutexvar_epi64 using indices {5, 6, 7, 8, ...}. The intrinsic only uses the low 3 bits of each index, so 8 wraps to 0 and qword 4 (bytes 32-39) was never compared. An extremum located there was missed.

Take the max/min of the two 256-bit halves directly (lower_half / upper_half) and continue with the AVX2 reduction. This also drops the 512-bit max whose upper half was discarded, and it extends the kernel to 16-bit types, which previously used the generic swizzle reduction (up to 4x slower with GCC).

Add a test that places the extremum at each position of every batch type.

Assisted-By: Claude Opus 5.5 noreply@anthropic.com

I found this issue via AI-review of my PR that uses xsimd. Instead of fixing the batch_constant indices I decided to use lower_half/upper_half functions since I thought it made the code more readable and understandable and not bug prone.

The avx512f kernels folded the upper 256 bits into the lower half with
_mm512_permutexvar_epi64 using indices {5, 6, 7, 8, ...}. The intrinsic
only uses the low 3 bits of each index, so 8 wraps to 0 and qword 4
(bytes 32-39) was never compared. An extremum located there was missed.

Take the max/min of the two 256-bit halves directly (lower_half /
upper_half) and continue with the AVX2 reduction. This also drops the
512-bit max whose upper half was discarded, and it extends the kernel to
16-bit types, which previously used the generic swizzle reduction (up to
4x slower with GCC).

Add a test that places the extremum at each position of every batch type.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>

@DiamonDinoia DiamonDinoia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, the code is also simpler.

@DiamonDinoia
DiamonDinoia merged commit ebe9991 into xtensor-stack:master Oct 23, 2026
94 of 96 checks passed
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