I would like to code a function that iterates over a slice, and operates on two values for which the indices differ in one specific (but configurable) bit. It should be possible to parallelize this iteration, ideally using Rayon.
For example assume my slice has length 8. If this bit were bit 0 (the least significant bit), then the pairs of values would have the indices (0, 1), (2, 3), (4, 5) and (6, 7), so that this could be solved by using chunks_mut.
However if it's bit 1, these pairs would be (0, 2), (1, 3), (4, 6), (5, 7) and chunks_mut does not work.
I could use (and have used) nested chunks which I then split and combined with zip. This works, but is ugly and the performance depends on the index, and performance in this code is critical to me, so this is not a good solution.
Instead if I manually create a loop that iterates over the indices using some bitshifts, the code becomes much simpler and faster, but now I need to use unsafe code to write to borrow multiple locations mutably at the same time (note that this is fine, because no index is used more than once).
I would like to avoid unsafe code though, or if I use it, make sure that I actually need it.
So my question is:
a) Do you know of a simple and performant way to implement this in safe Rust, or a crate that implements this type of functionality?
b) If I do implement this in unsafe Rust, can I simply create mutable pointers, as long as I make sure that no index is used more than once?