Skip to content

Add SimdInt::reverse_bits - #434

Merged
Shnatsel merged 4 commits into
linebender:mainfrom
RunDevelopment:reverse_bits
Oct 28, 2026
Merged

Shnatsel merged 4 commits into
linebender:mainfrom
RunDevelopment:reverse_bits

Conversation

@RunDevelopment

Copy link
Copy Markdown
Contributor

This adds another method from std::simd: reverse_bits.

It's implemented as first reversing the bits within the u8 bytes of the vector and then using swap_bytes to reverse the byte order for the given integer type. Platforms only differ in how they do the 8-bit reversal:

  • Arm Neon uses its vrbitq_u8 instruction.
  • AVX512 uses the gf2p8affine family of intrinsics to do the bit reversal in a single instruction (plus loading a constant). See this blog post.
  • All others use shifts and masking.

@Shnatsel

Shnatsel commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

A shuffle-based formulation models on llvm-mca as 2.5x faster than lots of shifts and bitwise ops, benchmarks with about the same improvements on my CPU. I've pushed it.

@Shnatsel Shnatsel 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.

Looks good to me but I'd appreciate if you could take a look at my changes

@Shnatsel

Copy link
Copy Markdown
Contributor

And in case you're wondering, the scalar fallback on SSE2 autovectorizes into the same slow-ish sequence of SIMD shifts.

@RunDevelopment

Copy link
Copy Markdown
Contributor Author

A shuffle-based formulation

Ohh, I read about exactly this use case before and completely forgot about it. Great catch!

Changes LGTM!
(I can't approve approve the PR, so take this as my approval :) )

And in case you're wondering, the scalar fallback on SSE2 autovectorizes into the same slow-ish sequence of SIMD shifts.

I was wondering that. Thanks!

@Shnatsel

Copy link
Copy Markdown
Contributor

(I can't approve approve the PR, so take this as my approval :)

Github does let you. It doesn't let anyone merge the code with only your review, but you can still leave a review.

@Shnatsel
Shnatsel added this pull request to the merge queue Oct 28, 2026
@RunDevelopment

Copy link
Copy Markdown
Contributor Author

I can't approve my own PR :)

image

Merged via the queue into linebender:main with commit 8b18b22 Oct 28, 2026
23 checks passed
@RunDevelopment
RunDevelopment deleted the reverse_bits branch October 28, 2026 22:50
@Shnatsel

Copy link
Copy Markdown
Contributor

Ah I see! I didn't think of that

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