From 61d228c0ea0d518de48a08577cd6d282e2f97759 Mon Sep 17 00:00:00 2001 From: Dmitry Konishchev Date: Tue, 17 May 2011 16:29:48 +0400 Subject: [PATCH] is_not_zero() optimization in qemu-img --- qemu-img.c | 24 +++++++++++++++++++++--- 1 files changed, 21 insertions(+), 3 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index e825123..41b4e32 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -498,12 +498,30 @@ static int img_commit(int argc, char **argv) static int is_not_zero(const uint8_t *sector, int len) { + /* + * Use long as the biggest available internal data type that fits into the + * CPU register and unroll the loop to smooth out the effect of memory + * latency. + */ + int i; - len >>= 2; - for(i = 0;i < len; i++) { - if (((uint32_t *)sector)[i] != 0) + len /= sizeof(long); + + long d0; + long d1; + long d2; + long d3; + + for(i = 0; i < len; i += 4) { + d0 = ((const long*) sector)[i + 0]; + d1 = ((const long*) sector)[i + 1]; + d2 = ((const long*) sector)[i + 2]; + d3 = ((const long*) sector)[i + 3]; + + if (d0 || d1 || d2 || d3) return 1; } + return 0; } -- 1.7.4.1