diff options
author | Bobby Bingham <koorogi@koorogi.info> | 2014-11-11 19:08:29 -0600 |
---|---|---|
committer | Bobby Bingham <koorogi@koorogi.info> | 2014-11-11 19:08:29 -0600 |
commit | c704d190d6ca70e276877e56c22ecc7ed470830e (patch) | |
tree | 015cd19042767570d9bfcab146044b458a5b13b9 | |
parent | f83c9c028a3ae8dd98ecee72154382d23620dc04 (diff) |
Code size is about 40 bytes smaller on amd64, but performance is unchanged.
-rw-r--r-- | rotate.c | 20 |
1 files changed, 9 insertions, 11 deletions
@@ -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; } } |