Re: [PATCH 1/8] lib/sort: Heapsort implementation of sort()

From: Paulo Marques
Date: Mon Jan 31 2005 - 12:35:10 EST


Andreas Gruenbacher wrote:
[...]

static inline void swap(void *a, void *b, int size)
{
if (size % sizeof(long)) {
char t;
do {
t = *(char *)a;
*(char *)a++ = *(char *)b;
*(char *)b++ = t;
} while (--size > 0);
} else {
long t;
do {
t = *(long *)a;
*(long *)a = *(long *)b;
*(long *)b = t;
size -= sizeof(long);
} while (size > sizeof(long));

You forgot to increment a and b, and this should be "while (size);", no?

}
}

Or better yet,

static inline void swap(void *a, void *b, int size)
{
long tl;
char t;

while (size >= sizeof(long)) {
tl = *(long *)a;
*(long *)a = *(long *)b;
*(long *)b = tl;
a += sizeof(long);
b += sizeof(long);
size -= sizeof(long);
}
while (size) {
t = *(char *)a;
*(char *)a++ = *(char *)b;
*(char *)b++ = t;
size--;
}
}

This works better if the size is not a multiple of sizeof(long), but is bigger than a long.

However it seems that this should be put in a generic library function...

--
Paulo Marques - www.grupopie.com

All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke (1729 - 1797)
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/