From c704d190d6ca70e276877e56c22ecc7ed470830e Mon Sep 17 00:00:00 2001 From: Bobby Bingham Date: Tue, 11 Nov 2014 19:08:29 -0600 Subject: Shrink rotate function a bit Code size is about 40 bytes smaller on amd64, but performance is unchanged. --- rotate.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'rotate.c') 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; } } -- cgit v1.2.3