Skip to content

Rotation by a runtime value? #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
aldanor opened this issue Dec 18, 2021 · 6 comments
Open

Rotation by a runtime value? #216

aldanor opened this issue Dec 18, 2021 · 6 comments

Comments

@aldanor
Copy link

aldanor commented Dec 18, 2021

This is such a common operation, I was wondering how does one do it with the current API, and/or is anything planned for it?

(With rotate_lanes_right/left the rotation parameter must be const. Same, with swizzling API it must be all const.)

@Lokathor
Copy link
Contributor

Question:
Do you mean rotating the bits within a lane by a runtime value?
or rotating the lanes themselves by a runtime value?

rotating lanes themselves does not seem to be common in what I've done, but maybe i work on very different problems.

@aldanor
Copy link
Author

aldanor commented Dec 18, 2021

Rotating lanes themselves, yea.

@workingjubilee
Copy link
Member

It's a reasonable request, LLVM just doesn't expose anything you can't write via to_array and copy_from_slice.

@ghost
Copy link

ghost commented Jul 14, 2022

How might this even be vectorised?
I imagine that the fastest implementation would simply be the scalar vector.as_mut_array().rotate(k)?

@thomcc
Copy link
Member

thomcc commented Jul 14, 2022

This should be doable with dynamic shuffles once we have them.

@ghost
Copy link

ghost commented Jul 14, 2022

Well, even with dynamic shuffles, we would still need to build the indices array, which I cannot think how to quickly generate that.
I'm thinking that it could be generated via either building an ROTATE_SHUFFLE_ARRAYS: [[u32; N]; N], or via

fn rotate<T, const N: usize>(vector: Simd<T, N>, k: usize) -> Simd<T, N> {
 vector.shuffle(
  (Simd::from_array([0, 1, .., N]) + Simd::splat(k)) % Simd::splat(N)
 )
}

neither of which should be better than [T]::rotate?

I encountered https://stackoverflow.com/questions/19205026/simd-rotate-by-variable, but these don't scale past the native width, or begin to take unreasonable amounts of memory per-table as with the initial idea I suggest.

May I have your input on the performance aspects of this?

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

No branches or pull requests

4 participants