WIP: Fast 1-d in-place circshift!
I thought I needed this, but turns out I probably don't.
I still think it could be useful in the future, so I'm putting
it here, in case somebody (maybe me in the future) ever needs this.
There are three algorithms implemented here, as well as a selection
heuristic for switching between them. The three algorithms are:
1. Out-of-place with a stack buffer
2. Batched Cycle-chasing in place
3. The Knuth Triple-Reverse trick
Of these, the cycle chasing algorithm has optimal memory complexity
(by doing `N` loads and stores), but conventional wisdom is that it
is slower than triple-reverse (which does 2N memory operations)
because of cache inefficiencies. I found this to be true for non-batched
cycle chasing, but not for batched cycle chasing once the batching
factor was about a cache line or so.
As such, the polyalgorithm does the following:
a. For small arrays, use the on-stack copy
b. Then, compute the GCD. If the problem admits large batching factors,
do the batched cycle chasing algorithm.
c. If not, use the triple reverse trick, which is less memory efficent,
but has consistent performance (unlike cycle chasing whose performance
depends on the batching factor).
There are additional algorithms listed at https://github.com/scandum/rotate
that could be investigated to replace the reversal fallback.
There are variations that compute the GCD from the first cycle, but
I found that to be unnecessary. For arrays small enough for the GCD
computation cost to matter, we're using the on-stack copy anyway,
and for anything larger, the GCD is negligible and we use it to
make the polyalgorithm decision anyway.
I think to finish this, all we'd need is
- [] Some tests to make sure all the cases are covered (and fix cases that
may be broken, I did some refactoring, without retesting those)
- [] Ideally, a little more benchmarking to more carefully select the
algorithmic cutovers.