summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby Bingham <koorogi@koorogi.info>2014-11-11 19:08:29 -0600
committerBobby Bingham <koorogi@koorogi.info>2014-11-11 19:08:29 -0600
commitc704d190d6ca70e276877e56c22ecc7ed470830e (patch)
tree015cd19042767570d9bfcab146044b458a5b13b9
parentf83c9c028a3ae8dd98ecee72154382d23620dc04 (diff)
Shrink rotate function a bitHEADmaster
Code size is about 40 bytes smaller on amd64, but performance is unchanged.
-rw-r--r--rotate.c20
1 files changed, 9 insertions, 11 deletions
diff --git a/rotate.c b/rotate.c
index 1815812..6aebf7c 100644
--- a/rotate.c
+++ b/rotate.c
@@ -6,17 +6,15 @@
/* rotates left */
void rotate(char *base, size_t size, size_t shift)
{
- int dir = 1;
-
- counts[CURRENT].rotate++;
- while (shift) {
- while (2*shift <= size) {
- swap(base, base + dir*shift, shift);
- size -= shift;
- base += shift*dir;
+ size_t a = shift, b = size - shift;
+ while (a && b) {
+ if (a <= b) {
+ swap(base, base + b, a);
+ b -= a;
+ } else {
+ swap(base, base + a, b);
+ base += b;
+ a -= b;
}
- shift = size - shift;
- base = dir > 0 ? base + size - shift : base - shift;
- dir *= -1;
}
}